//app.js import config from './utils/config.js' import api from './utils/api.js' App({ onLaunch: function (options) { console.log('onLaunch-----', options) this.globalData.shareQuery = options.query if (options.scene === 1044 || options.scene === 1007 || options.scene === 1008) { // wx.getShareInfo({ // shareTicket: options.shareTicket, // success: function(res) { // var encryptedData = res.encryptedData; // var iv = res.iv; // } // }) } const _self = this wx.checkSession({ success: function() { console.log('session_key 未过期,并且在本生命周期一直有效') console.log('token', wx.getStorageSync('userInfo').user_token || '空') if (wx.getStorageSync('userInfo').user_token) { wx.getSetting({ success: res => { if (res.authSetting['scope.userInfo']) { // 已经授权,可以直接调用 getUserInfo 获取用户信息,不会弹框 wx.getUserInfo({ success: res => { api.updateUserInfo({ raw_data: res.rawData, signature: res.signature, encrypted_data: res.encryptedData, iv: res.iv, HideLoading: true, HideToast: true }).then(res => { _self.getMeInfo() }).catch(res => { _self.getMeInfo() }) }, fail: res => { _self.getMeInfo() } }) } else { _self.getMeInfo() } }, fail: res => { _self.getMeInfo() } }) } else { console.log('未获取到token,重新登录') _self.againLogin() } }, fail: function() { console.log('session_key 已经失效,需要重新执行登录流程') _self.againLogin() } }) const originPage = Page let app = this Page = function (config) { let { onShareAppMessage } = config if (!onShareAppMessage) { config.onShareAppMessage = function (options) { let { shareinfo, state, mem_id } = app.globalData.userInfo if (shareinfo) { shareinfo = shareinfo[Math.floor(Math.random() * shareinfo.length)] } else { shareinfo = { title: '', image: '' } } return { title: shareinfo.title, path: `/pages/share/share?state=${state}&mem_id=${mem_id}`, imageUrl: shareinfo.image } } } return originPage(config) } }, onShow: function (options) { console.log('app onshow', options) // 安卓系统桌面图标 let sysInfo = wx.getSystemInfoSync() // if ((sysInfo.platform === 'ios' && options.scene === 1089) || options.scene === 1023) { if (options.scene === 1023) { this.globalData.windowOpen = true } }, getMeInfo() { const _self = this console.log('开始获取用户信息...') api.getUserInfo().then(res => { if (res.code === 200) { _self.globalData.auth = res.data.auth } if (res.data.code === 1002) { console.log('登陆过期正在重新登陆...') _self.againLogin() } }) }, // 重新登陆 againLogin: function () { if (this.globalData.againLogin) return false this.memLogin().then(() => { this.globalData.againLogin = false console.log('登录成功') this.getMeInfo() }) }, memLogin: function() { const _self = this console.log('登录中...') return new Promise((resolve, reject) => { wx.login({ success: function (res) { console.log('wx login success', res) if (res.code) { let params = { code: res.code } if (_self.globalData.shareQuery) { params.state = _self.globalData.shareQuery.state || _self.globalData.shareQuery.scene || '' } api.menLogin(params).then(res => { console.log('login success', res) if (res.code === 200) { wx.setStorageSync('userInfo', res.data) resolve() } else { console.log('登陆失败...') } }, err => { console.log('login fail', err) }) } else { console.log('登录失败!' + res.errMsg) reject() } }, fail: function (res) { console.log('wx login fail', res) } }); }) }, globalData: { windowOpen: false, openGZH: false, shareQuery: null, userInfo: {}, // scene: 1001, // is_session: false, PageCallback: false, auth: { favorite: 1, gain: 1, guide: 1, memcenter: 1, red: 1, share: 1, active: 1 }, mpType: config.mpType, mpName: '多鱼游戏盒子', mpGzh: '多鱼游戏' } })