日常記錄學(xué)習(xí)~~
官方文檔:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html
官方文檔中,如上圖是這樣定義登錄流程的敬拓,我的理解是:
1钱慢、獲取登錄信息的情況
通過wx.login()獲取到code值佑刷,然后拿著code值通過wx.request()請(qǐng)求我方服務(wù)器殿较,我方服務(wù)器拿著appid滑臊,appSecret和code去調(diào)用code2Session
(登錄憑證校驗(yàn))api蚪拦,請(qǐng)求微信接口服務(wù)則返回session_key和openid牍疏;
對(duì)應(yīng)的地址:
[wx:request:https://developers.weixin.qq.com/miniprogram/dev/api/network/request/wx.request.html
code2Session:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html
]
wx.login({//獲取code
success(res) {
wx.request({// 獲却浪瘛(openid、session_key鳞陨、unionid)
url: '',
data: {},
header: {'content-type': 'application/json' // 默認(rèn)值},
success (res) {console.log(res.data)}})
}
})
2昨寞、需要授權(quán)才可以登錄的情況
以下以需授權(quán)手機(jī)號(hào)登錄來舉例:
文檔:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/getPhoneNumber.html
彈出授權(quán):
<button open-type="getPhoneNumber" catchtap="tapLogin" bindgetphonenumber="tabUserPhone">立即授權(quán)</button>
登錄成功!
做登錄功能一定要重點(diǎn)關(guān)注這個(gè)“會(huì)話密鑰 session_key 的有效性”
官方文檔說明:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/signature.html
這邊的做法就是:
有一個(gè)token來表示全局唯一接口調(diào)用憑據(jù)掏导,并且每一個(gè)接口都需要使用這個(gè)token享怀,且效期為1個(gè)小時(shí),需定時(shí)刷新趟咆,只要一調(diào)用接口添瓷,就檢測(cè)這個(gè)token是否過期,如果過期就通過wx.login()將其刷新值纱,如果失效就重新登錄鳞贷。