原创

小程序-js-微信重新授权-拒绝授权-获取用户信息

wx.getUserInfo({
withCredentials: false,//暂时给false
success: function (res) {
var userInfo = res.userInfo
var nickName = userInfo.nickName
var avatarUrl = userInfo.avatarUrl
var gender = userInfo.gender //性别 0:未知、1:男、2:女
var province = userInfo.province
var city = userInfo.city
var country = userInfo.country
// console.log('getUserInfo=' + JSON.stringify(res));
console.log('开始保存本地获取微信信息' + JSON.stringify(userInfo));
SaveUserInfo(userInfo);
},
fail: function (e) {
wx.showModal({
title: '警告',
content: '您点击了拒绝授权,将无法正常显示个人信息,点击确定重新获取授权。',
success: function (res) {
if (res.confirm) {
wx.openSetting({
success: (res) => {
if (res.authSetting["scope.userInfo"]) {////如果用户重新同意了授权登录
wx.getUserInfo({
success: function (res) {
var userInfo = res.userInfo;
SaveUserInfo(userInfo);
}
})
}
}, fail: function (res) {
}
})
}
}
})
}
});



var SaveUserInfo = function (newData) {
var userinfo = wx.getStorageSync("userinfo") || {};
for (var k in newData) {
userinfo[k] = newData[k];
}
console.log('更新存储新数据0:' + JSON.stringify(userinfo));
var gd = getApp().globalData || {};
if (gd && gd.userinfo) {
gd.userinfo = userinfo;
} else {
var newd = {
"userinfo": userinfo
}
getApp().globalData = newd;//设置到globalData全局数据中 会不会丢失?
}
console.log('更新存储新数据1:' + JSON.stringify(getApp().globalData.userinfo));
wx.setStorageSync("userinfo", userinfo);//这个后期废弃
console.log('更新存储新数据:' + JSON.stringify(wx.getStorageSync("userinfo")));

}

正文到此结束
本文目录