12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- const app = getApp()
- Page({
- data: {
- },
- onLoad: function () {
- if (app.globalData.userInfo) {
- return;
- } else if (this.data.canIUse){
- // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
- // 所以此处加入 callback 以防止这种情况
- app.userInfoReadyCallback = res => {
- this.setData({
- userInfo: res.userInfo,
- hasUserInfo: true
- })
- }
- } else {
- // 在没有 open-type=getUserInfo 版本的兼容处理
- wx.getUserInfo({
- success: res => {
- wx.setStorageSync('userInfo', res.userInfo);
- app.globalData.userInfo = res.userInfo
- }
- })
- }
- },
- getUserInfo: function(e) {
- const errno = e.detail.errMsg;
- const that = this, userInfo = {};
- if (errno === "getUserInfo:fail auth deny") {
- return;
- }else{
- userInfo.encryptedData = e.detail.encryptedData;
- userInfo.iv = e.detail.iv;
- userInfo.avatar_url = e.detail.userInfo.avatarUrl;
- userInfo.nick_name = e.detail.userInfo.nickName;
- app.globalData.userInfo = userInfo;
- wx.setStorageSync('userInfo', userInfo);
- app.login(that);
- }
- },
- })
|