var http = require('./utils/request') var i18n = require('./i18n/index') App({ globalData: { userInfo: null, userId: null, connectedDevice: null, currentTreatment: null, statusBarHeight: 44 }, onLaunch: function () { var sysInfo = wx.getSystemInfoSync() this.globalData.statusBarHeight = sysInfo.statusBarHeight || 44 i18n.getLocale() i18n.applyTabBar() this.checkLogin() }, // Safety net: if the framework ever tries to open an unknown/empty route // (e.g. a stale page stack after the pages list changed), fall back to home // instead of surfacing a hard "page not found" crash. onPageNotFound: function (res) { if (res && res.isEntryPage) { wx.reLaunch({ url: '/pages/index/index', fail: function () {} }) } }, checkLogin: function () { var token = wx.getStorageSync('token') if (!token) { this.globalData.userInfo = null return } this.loadProfile() }, loadProfile: function () { var self = this return http.get('/api/v1/user/profile').then(function (profile) { self.globalData.userInfo = profile self.globalData.userId = String(profile.user_id) return profile }).catch(function () { wx.removeStorageSync('token') }) }, doLogin: function () { return new Promise(function (resolve, reject) { wx.login({ success: function (res) { http.post('/api/v1/auth/login', { code: res.code }).then(function (data) { wx.setStorageSync('token', data.token) wx.setStorageSync('token_expiry', String(Math.floor(Date.now() / 1000) + (data.expires_in || 604800))) var app = getApp() app.globalData.userInfo = data.user_info app.globalData.userId = data.user_id resolve(data) }).catch(reject) }, fail: function (err) { reject({ code: 2002, message: err.errMsg || '微信登录失败' }) } }) }) } })