import updateManager from './common/updateManager'; import { QUERY_KEY as PROMOTER_KEY, savePendingPromoter, getPendingPromoter, clearPendingPromoter, bindPendingPromoter, } from './utils/promoter'; var util = require('utils/util.js'); var api = require('config/api.js'); App({ onLaunch: function (options) { wx.$api = api wx.$util = util // 展示本地存储能力 var logs = wx.getStorageSync('logs') || [] logs.unshift(Date.now()) wx.setStorageSync('logs', logs) //通过分享链接/小程序码冷启动时缓存推广人,随登录请求一起绑定 savePendingPromoter(options && options.query); this.loggingIn = true; // 登录。loginReady 给需要鉴权的请求等待,避免首屏带着空 token 发出去 this.loginReady = new Promise((resolve) => { const finish = () => { this.loggingIn = false; resolve(); }; wx.login({ success: (loginRes) => { util.request(api.AuthLoginByWeixin, { code: loginRes.code, [PROMOTER_KEY]: getPendingPromoter(), }, 'POST', { auth: false }).then((res) => { if (res.code === 1) { let userInfo = res.data.userinfo; util.setToken(res.data.token); wx.setStorageSync('userInfo', userInfo); //推广关系为永久绑定,登录成功后无论是否绑定都无需再次提交 clearPendingPromoter(); } }).catch((err) => { console.error('wxLogin failed:', err); }).then(finish); }, fail: () => { finish(); }, }); }); }, onShow: function (options) { //热启动从分享进入:已登录时直接绑定,冷启动由 onLaunch 的登录请求处理 savePendingPromoter(options && options.query); if (!this.loggingIn) { bindPendingPromoter(); } updateManager(); // 切回前台的时间。Tab 页用它区分「从后台回来」和「切换 Tab」,避免每次 onShow 都打接口 this.globalData.appShowAt = Date.now(); }, onHide: function () {}, globalData: { unread: 0, //未读消息数(tabbar红点共享缓存) unreadSeq: 0, //未读数写入序号,用来丢弃过期响应 appShowAt: 0, //最近一次 App.onShow 的时间 refreshDemandId: 0, //操作后需要刷新状态的需求ID(跨页面通知列表页) }, });