一:微信小程序中的登錄
主要思路:
(1)用button組件的open-type="getUserInfo"屬性獲取用戶信息;
(2)用uni.login 方法獲取唯一的標(biāo)識碼code,將獲取到的所需參數(shù)通過接口傳給后端;
(3)后端返回成功回調(diào)數(shù)據(jù),將所需要的信息存入緩存。
具體實(shí)現(xiàn):
1:在頁面中使用 button 組件的 open-type屬性獲取用戶信息
<!-- 微信登錄 -->
? <!-- #ifdef MP-WEIXIN -->
<button @getuserinfo='getUserInfo' open-type="getUserInfo"? type="primary" class='login'>微信授權(quán)登錄</button>
? <!-- #endif -->
2:授權(quán)登錄
getUserInfo(e) {
let _this = this;
let ret=e.detail;
if(ret.errMsg == "getUserInfo:fail auth deny") {
_this.$api.toast("為了更好的服務(wù),請同意授權(quán)"); //自己封裝第一個提示方法
}else if(ret.errMsg == "getUserInfo:ok") {
_this.userLogin(e.detail);
}else {
_this.$api.toast("授權(quán)失敗")
}
},
3:登錄接口
userLogin(e) {
let _this = this ;
uni.login({
success: res => {
let code = res.code;
if(code){
//登錄參數(shù)
let data={
code: code,
signature:e.signature,
iv:e.iv,
encryptedData:e.encryptedData,
rawData:e.rawData,
shop_id:_this.$store.state.shopId || 0
}
//調(diào)登錄接口
_this.$api.ajax('smdc/index/login',data,function(ret){
console.log("登錄成功回調(diào)",ret.token);
uni.navigateBack();
},'POST',true);
}
},
fail: () => {
_this.$api.toast("沒有獲取到登陸信息,請重試")
}
});
}
以上就是小程序微信登錄的所有大概流程。over~