1、添加按钮
<button class="login-btn" type="primary" @click="login">微信授权自动登录</button>
2、添加方法
login(e) {
wx.getUserProfile({
desc: '用于完善会员资料',
success: (uRes) => {
if(uRes.errMsg !== 'getUserProfile:ok') {
alert("授权失败!")
return
}
uni.showLoading({
mask:true,
title: '正在登录...'
})
uni.login({
provider: 'weixin',
success: function (loRes) {
console.log("loRes", loRes)
if(!loRes.code) {
return
}
uni.request({
url: '后台验证请求路径',
data: {code: loRes.code,
signature: uRes.signature,
rawData: uRes.rawData,
encryptedData: uRes.encryptedData,
iv: uRes.iv
},
method: 'POST',
withCredentials: true,
xhrFields: {withCredentials: true },
header: {
'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
},
success: function(authRes) {
},
fail: (authRes) => {
}
})
},
fail: function(res) {
},
complete: () => {
uni.hideLoading()
}
})
}
})
}