<button class="wechat-login-button" open-type="getUserInfo" bindgetuserinfo="getUserInfo">微信登录</button>
必有:open-type="getUserInfo" bindgetuserinfo="getUserInfo"
getUserInfo: function (e) {
主要是获取iv和encryptedData,与此同时,未授权状态下会弹出授权弹框
this.data.iv = e.detail.iv
this.data.edata = e.detail.encryptedData
console.log(this.data.iv)
console.log(this.data.edata)
this.setData({
iv: this.data.iv,
edata: this.data.edata
})
this.wxLogin(() => {
this.findMyUserInfo();
})
},
wxLogin () {
wx.login({
success: res => {
if (res.code) {
var code = res.code;
wx.request({
url: config.apiUrl + '/wechat/login',
method: 'POST',
data: {
code: code,
iv: this.data.iv,
encryptedData: this.data.edata
},
header: {
'Accept': config.accept,
},
complete: function (res) {
wx.hideLoading()
switch (res.statusCode) {
case 200:
config.setAuthorization(res.data)
callback()
break
case 400:
wx.showToast({
title: '请清理缓存',
icon: 'none'
})
break
case 442:
config.setWechatToken(res.data.data.token)
wx.showToast({
title: '未绑定手机号',
icon: 'none',
success: response => {
setTimeout(function () {
wx.navigateTo({
url: '../regist/regist',
})
}, 1000)
}
})
break;
default:
wx.showToast({
title: '服务器错误',
icon: 'none'
})
break
}
}
})
}
}
})
},
<!-- <open-data type="userNickName"></open-data>
<open-data type="userAvatarUrl"></open-data>
<open-data type="userGender" lang="zh_CN"></open-data>
<open-data type="userCity"></open-data>
<open-data type="userProvince"></open-data>
<open-data type="userCountry"></open-data> -->
也可以这样直接显示在页面上,未登录状态下就可以