var http = require('./utils/request') App({ globalData: { userInfo: null, userId: null, connectedDevice: null, currentTreatment: null }, onLaunch: function () { this.checkLogin() }, checkLogin: function () { var token = wx.getStorageSync('token') if (!token) { this.globalData.userInfo = null return } this.loadProfile() }, loadProfile: function () { var self = this http.get('/api/v1/user/profile').then(function (profile) { self.globalData.userInfo = profile self.globalData.userId = String(profile.user_id) }).catch(function () { wx.removeStorageSync('token') wx.reLaunch({ url: '/pages/login/login' }) }) }, doLogin: function () { return new Promise(function (resolve, reject) { wx.login({ success: function (res) { if (!res.code) { reject({ message: 'wx.login failed' }) return } http.post('/api/v1/auth/login', { code: res.code }).then(function (data) { wx.setStorageSync('token', data.token) var app = getApp() app.globalData.userInfo = data.user_info app.globalData.userId = data.user_id resolve(data) }).catch(reject) }, fail: reject }) }) } })