首先,小程序手机号快捷登录普遍适用于各种小程序直接,之前上网搜过有的能直接拿来用,但是会有地狱回调问题,我这里写的代码可以直接拿来用
创建一个button按钮,用户点击之后可以调取手机号api
<button v-show="z"class="btn" open-type="getPhoneNumber" type="primary"
@getphonenumber="getUserInfo">微信用户登录</button>
getUserInfo(e) {
//检查token是否过期
uni.checkSession({
success:res=> {
console.log(res)
},
fail:res=> {
uni.login({
success: res => {
console.log(res)
this.code=res.code
}
})
},
complete:res=>{
myRequest({
url: "/api/login/login",
method:"post",
data:{
code:this.code,
encryptedData:e.detail.encryptedData,
iv:e.detail.iv
}
}).then(res=>{
if(res.data.code==200){
//设置登录状态
uni.setStorageSync("is_login",1)
uni.setStorageSync("uid",res.data.data.id)
uni.setStorageSync("if_real",res.data.data.if_real)
uni.setStorageSync("name",res.data.data.real_name)
uni.setStorageSync("phonenum",res.data.data.mobile)
uni.setStorageSync("idcard",res.data.data.number)
uni.showToast({
title:"登录成功",
duration:2000
})
if(res.data.data.if_real==0){
uni.navigateTo({
url:"/pages/personal/personal"
})
}else{
setTimeout(function (){
uni.switchTab({
url: '/pages/index/index'
});
},2000)
}
}
})
}
})
//触发获取手机号加密信息
console.log(e)
// let that = this
// encryptedData = e.detail.encryptedData
// iv = e.detail.iv
//将code,encryptedData,iv传给后台进行解密
}