小程序 初始化 登录 流程
生活随笔
收集整理的這篇文章主要介紹了
小程序 初始化 登录 流程
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
小程序初始化由于app.js 中的周期函數(shù)是異步方法,所以暫且想到的同步處理就是做一個加載頁、app.json 中添加個loading路由
在這個路由中做登錄的流程如下
wx.checkSession({success: function () {// session_key 未過期,并且在本生命周期一直有效wx.getStorage({key: 'openid',success: function (res) {app.globalData.openid = res.data;wx.getStorage({key: 'extAppid',success: function (idres) {app.globalData.extAppid = idres.data;self.getabilityinfo()},})},fail: function () {self.login()}})},fail: function () {// session_key 已經(jīng)失效,需要重新執(zhí)行登錄流程self.login()}})login(){let self = thiswx.login({success: function (suss) {if (wx.getExtConfig) {wx.getExtConfig({success: function (conf) {app.globalData.extAppid = conf.extConfig.extAppid;wx.setStorage({key: 'extAppid',data: conf.extConfig.extAppid,})wx.request({url: config.jscode2session,method: "GET",header: {FR_APPID: conf.extConfig.extAppid,FR_CODE: suss.code},data: {FR_APPID: conf.extConfig.extAppid,FR_CODE: suss.code},success(sres) {app.globalData.openid = sres.data.data;wx.setStorage({key: 'openid',data: sres.data.data,})self.getabilityinfo()},fail(loginData) {}})},fail: function (err) {}})}},fail: function (err) {}});}getabilityinfo(){let self = thisconfig.$api.get(config.abilityinfo).then(res=>{app.globalData.appInfo = res.data;self.jump()}) },針對性的對上面3個方法有響應(yīng)說明 和目的性的解釋、
由于我是第三方小程序模板開發(fā)、會有ext.json 配置文件。大家可以忽略、加入自己的思維即可
因為小程序的入口有聊天頁下拉
和
這兩個入口,
先給大家解釋下這兩個地方的不同,
第一個是本地常用的小程序快捷入口,第二個是自己所授權(quán)使用過的所有,根據(jù)使用頻率來進(jìn)行排序。
大家都知道,小程序有g(shù)lobalData 這個參數(shù)。在小程序響應(yīng)之后就會存在。
> 關(guān)鍵聲明
圖1中可以刪除熱門使用小程序,
當(dāng)然 這里刪除,不會刪除你跟wx后臺的sessionKey的關(guān)聯(lián)。因為小程序還在你的小程序列表中,但是這種行為會讓你和后臺服務(wù)器斷開登錄狀態(tài)。同時,會將app.js 的globalData 中的數(shù)據(jù)清空!!!!如果此時清空了,又進(jìn)入小程序,他再調(diào)用一遍wx.login 和getExtConfig 會覆蓋服務(wù)器端的sessionKey 會導(dǎo)致登錄不上去。因為一個微信號與一個小程序 有兩個唯一標(biāo)識,openid 和appid 所以在checksession中直接存至globalData 本地,就避免了這樣的問題。且目前完美兼容登錄。
總結(jié)
以上是生活随笔為你收集整理的小程序 初始化 登录 流程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 尼采语录
- 下一篇: TweenMax.js大熊猫吃面条动画