<button type="primary" plain="true" class="wx-login" open-type="getUserInfo" lang="zh_CN" bindgetuserinfo="bindGetUserInfo">微信登录</button>
//授权登录
bindGetUserInfo: function (e) {
if (e.detail.userInfo) {
//用户按了允许授权按钮
var that = this;
//存储用户信息
wx.setStorageSync('userInfo', e.detail.userInfo);
} else {
wx.showModal({
title: '提示',
content: '您点击了拒绝授权,将无法正常使用小程序,请授权之后再进入!',
showCancel: false,
confirmText: '返回授权',
success: function (res) {
// 用户没有授权成功,不需要改变 isHide 的值
if (res.confirm) {
console.log('“返回授权”');
}
}
});
}
//小程序的登录关键在于使用wx.login()方法后,要到取到临时登录凭证code值传到后台,再用小程序平台本帐号生成的appid+addsecret+code去微信接口服务取得用户唯一标识后即可登录【注意:此步骤必须要在本项目后台服务中才能成功,在小程序端是无效的】
wx.login({
success: res => {
wx.request({
// 自行补上自己的 APPID 和 SECRET
// url: 'http://opentest.dududahuo.com:94/api/WxOAuth/GetSession',
url: fetch.baseUrl +'/getsession',
method:'get',
data: {
"jsCode": res.code
},
success: res => {
// 获取到用户的 openid openid 是用户的唯一标识符是微信官方提供的登录能力
var data = res.data.data;
wx.setStorageSync('sessionKey', data.session_key);
wx.setStorageSync('openId', data.openid);
wx.setStorageSync('unionId', data.unionid);
}
});
}
});
},