uniapp 微信小程序授權(quán)登錄總結(jié)

一:微信小程序中的登錄

主要思路:

(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~

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容