-
首先布局登錄按鈕(觸發(fā)登錄的一定要是button,button才有獲取用戶的方法)
文章來源:http://www.zghlxwxcb.cn/news/detail-548873.html
<button
open-type="getPhoneNumber"
@click.stop="loginfn"
@getphonenumber.stop="onGetPhoneNumber"
>點(diǎn)擊微信一鍵登錄</button>
2.寫點(diǎn)擊button之后的邏輯,,獲取iv,code,ncryptedData,調(diào)用后端接口進(jìn)行登錄文章來源地址http://www.zghlxwxcb.cn/news/detail-548873.html
// 用戶授權(quán)登錄
//首先點(diǎn)擊登錄按鈕的時(shí)候獲取一下code,保存到data里
loginfn(){
wx.login({
success(res) {
that.code = res.code;
return;
},
});
}
//調(diào)用button自帶的彈窗獲取用戶信息方法
async onGetPhoneNumber(val) {
//此時(shí)的val是用戶點(diǎn)擊了允許還是拒絕
let that = this;
if (val.detail.errMsg === "getPhoneNumber:ok") {
//保存需要的** iv, encryptedData **
const { iv, encryptedData } = val.detail;
//調(diào)用后臺(tái)登錄的接口,傳遞參數(shù)
const result = await that.api.getLoginApi({
loginType: "1",
iv,
code: that.code,
encryptedData,
});
// 登陸成功
if (result.data.token) {
//保存token
uni.setStorageSync("token", result.data.token);
that.token = uni.getStorageSync("token");
uni.showToast({
title: 登錄成功",
duration: 800,
icon: "success",
});
// 刷新回到用戶頁頁面
uni.reLaunch({
url: "/pages/myself/myself",
});
return;
}
}
} else {
uni.showToast({
title: "已取消登錄",
duration: 500,
icon: "success",
});
}
}
到了這里,關(guān)于uniapp實(shí)現(xiàn)小程序登錄,微信一鍵登錄,獲取token,iv,code,ncryptedData的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!