微信小程序的登錄邏輯:
1、調用wx.login獲取微信code
2、將code、授權登錄參數(標識不同的小程序)傳給后端獲取openid,unionid,customerTel。存儲openid支付時使用。
包括用戶在當前小程序的唯一標識(openid)、微信開放平台帳號下的唯一標識(unionid,若當前小程序已綁定到微信開放平台帳號)及本次登錄的會話密鑰(session_key)等。
3、選擇微信授權登錄,不同用戶情況分類:
1)、返回了customerTel:
后端根據unionid查詢到有綁定關系的customerTel返回給前端(可判斷為注冊過的老用戶),前端再調用登錄接口獲取token,存儲token,實現靜默登錄。
2)、 未返回customerTel:
后端根據unionid查詢到沒有綁定的customerTel(無法判斷是新用戶還是老用戶)
點擊微信一鍵登錄按鈕,拉起手機號授權彈窗,如果用戶允許授權,獲取信息發給后端解密出手機號
前端再調用登錄接口獲取token,存儲token,實現授權登錄
4、
選擇驗證碼登錄,跳轉新頁面
輸入手機號、驗證碼。首次登錄執行注冊+登錄
wx.login({
success: (res) => {
api
.getUnionId({
code: res.code,
wxType: env.wxType,
})
.then((res) => {
app.globalData.openId = res.responseData.openId; //微信支付需要使用openid
app.storage.setData("openID", res.responseData.openId);
this.data.openId = res.responseData.openId;
this.data.unionId = res.responseData.unionId;
this.setData({
hideLoading: true,
});
if (res.responseData.customerTel) {
if (!app.globalData.isNewCustomer) {
//標識新用戶,'10'新用戶,'20'老用戶
app.globalData.isNewCustomer = "20";
}
this.data.customerTel = res.responseData.customerTel;
} else {
if (!app.globalData.isNewCustomer) {
//標識新用戶,'10'新用戶,'20'老用戶
app.globalData.isNewCustomer = "10";
}
}
})
.catch((error) => {
this.setData({
hideLoading: true,
});
});
},
});
