- 当前 Bug 的表现(可附上截图)
- 预期表现
- 复现路径
- 提供一个最简复现 Demo
bindGetUserInfo: function (e) {
let userInfo = wx.getStorageSync(‘userInfo’);
let token = wx.getStorageSync(‘token’);
let userMobile = wx.getStorageSync(‘userMobile’);
if (userInfo && token && userMobile != “” && userMobile!=null) {
// wx.redirectTo({
// url: “/pages/index/index”
// });
}
if (e.detail.userInfo) {
//用户按了允许授权按钮
user.loginByWeixin(e.detail).then(res => {
this.setData({
userInfo: res.data.userInfo
});
app.globalData.userInfo = res.data.userInfo;
app.globalData.token = res.data.token;
app.globalData.userMobile = res.data.userMobile;
if (res.data.userMobile){
wx.redirectTo({
url: “/pages/index/index”
});
}else{
wx.redirectTo({
url: “/pages/accounts/accounts”
});
}
}).catch((err) => {
console.log(err)
});
} else {
//用户按了拒绝按钮
wx.showModal({
title: ‘警告通知’,
content: ‘您点击了拒绝授权,将无法正常显示个人信息,点击确定重新获取授权。’,
success: function (res) {
if (res.confirm) {
wx.openSetting({
success: (res) => {
if (res.authSetting[“scope.userInfo”]) {////如果用户重新同意了授权登录
user.loginByWeixin(e.detail).then(res => {
this.setData({
userInfo: res.data.userInfo
});
app.globalData.userInfo = res.data.userInfo;
app.globalData.token = res.data.token;
}).catch((err) => {
console.log(err)
});
}
}
})
}
}
});
}
}
loginByWeixin方法实现
/**
* 调用微信登录
*/
function loginByWeixin(userInfo) {
let code = null;
return new Promise(function (resolve, reject) {
return util.login().then((res) => {
code = res.code;
return userInfo;
}).then((userInfo) => {
//登录远程服务器
util.request(api.AuthLoginByWeixin, { code: code, userInfo: userInfo }, ‘POST’, ‘application/json’).then(res => {
if (res.errno === 0) {
//存储用户信息
wx.setStorageSync(‘userInfo’, res.data.userInfo);
wx.setStorageSync(‘token’, res.data.token);
wx.setStorageSync(‘userMobile’, res.data.userMobile);
resolve(res);
} else {
util.showErrorToast(res.errmsg)
reject(res);
}
}).catch((err) => {
reject(err);
});
}).catch((err) => {
reject(err);
})
});
}