第一步 uni.login(object)
详细说明可以看uniapp 的官网uni.login(OBJECT) | uni-app官网
uni.login({
provider: "weixin", //登录服务提供商
scopes: ' auth_base', //授权类型,默认 auth_base。
onlyAuthorize: true, //微信登录仅请求授权认证
success: (res) => {
console.log(res); //主要是取code的值传给后端拿token
//这里是后端的请求的接口
this.$api.user.wxLogin({
js_code: res.code
}, res => {
console.log(res.data);
if (res.data.code == 1) {
uni.setStorageSync('wxlogin', res.data.data.token);
} else {
uni.showToast({
title: res.data.msg,
icon: "none"
})
}
})
}
})
<button open-type="getPhoneNumber" class="btn" @getphonenumber="getPhoneNumber">手机号快捷登录(推荐)</button>
getPhoneNumber(res) { // 获取手机号
console.log(res)
if(res.detail.errMsg && res.detail.errMsg == 'getPhoneNumber:fail user deny'){
console.log('用户拒绝获取手机号');
uni.showToast({
title: '您点击了拒绝',
icon:"none"
});
return false
}
this.phone_code = res.detail.code // 获得的手机code
this.phone_encryptedData = res.detail.encryptedData //用于解密
this.phone_iv = res.detail.iv // 用于解密
//请求后端接口 返回用户信息
this.$api.user.bingPhoneFromWx({
code:this.phone_code,
token:uni.getStorageSync('wxlogin')
},res=>{
console.log(res);
if(res.data.code==1){
uni.switchTab({
url:'../../pages/tabbar/mine/mine'
})
}else{
uni.showToast({
title:res.data.msg,
icon:"none"
})
}
})
},