//index.js //获取应用实例 const app = getApp() Page({ data: { userInfo: {}, hasUserInfo: false, canIUse: wx.canIUse('button.open-type.getUserInfo'), avatar_url: 'http://m.henhaojie.com/images/KX0004.png', nick_name: '点击授权登录' }, onLoad: function () { if (app.globalData.userInfo) { this.setData({ userInfo: app.globalData.userInfo, hasUserInfo: true, avatar_url: app.globalData.userInfo.avatar_url, nick_name: app.globalData.userInfo.nick_name }) } 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 this.setData({ userInfo: res.userInfo, hasUserInfo: true }) } }) } }, getUserInfo: function(e) { const that = this, userInfo = {}; console.log(); userInfo.encryptedData = e.detail.encryptedData; userInfo.iv = e.detail.iv; // console.log(e.detail.userInfo); 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); this.setData({ userInfo: e.detail.userInfo, hasUserInfo: true, avatar_url: e.detail.userInfo.avatarUrl, nick_name: e.detail.userInfo.nickName }) }, initInfo() { var token = wx.getStorageSync('user').token, data = {}, that = this; console.log(token); if (token) { that.getInfo(token) } else { app.login(that, that.getInfo); } }, getInfo(token) { var that = this, token = token || wx.getStorageSync('user').token, data = null; wx.request({ url: app.globalData.HOST + '/user/wx/personalCenter', method: 'POST', data: { token: token }, success: function (res) { if (res.data.errno == '0') { data = res.data.rst that.setData({ userInfo: data, avatar_url: data.user.avatar_url, nick_name: data.user.nick_name}) } else if (res.data.errno == '90001') { } } }); }, goApplication() { var tel = wx.getStorageSync('tel') || '0', user_id = wx.getStorageSync('user').user_id || '0'; console.log(user_id); console.log(tel); if (tel != '0' || user_id != '0') { wx.navigateTo({ url: "/pages/application/application" }) } else { wx.navigateTo({ url: "/pages/getPhone/getPhone?from=mine" }) } }, goFeedback: function (e) { var tel = wx.getStorageSync('tel') || '0', user_id = wx.getStorageSync('user').user_id || '0'; if (tel != '0' || user_id != '0') { wx.navigateTo({ url: "/pages/feedback/feedback" }) } else { wx.navigateTo({ url: "/pages/getPhone/getPhone?from=mineFeedback" }) } }, onShareAppMessage: function () { return { title: app.globalData.title, imageUrl: app.globalData.img, path: '/pages/index/index', success: function(res) { app.noticeModal('分享成功') }, fail: function(res) { app.noticeModal('您取消了分享') } } } })