const api = require('./utils/api') const storage = require('./utils/storage') const sync = require('./utils/sync') const migrate = require('./utils/migrate') App({ onLaunch() { // 先修一遍本地数据,再走网络流程 migrate.run() this.getUserOpenId() }, // 获取 openid:调自建后端 /api/login async getUserOpenId() { let openid = wx.getStorageSync('openid') if (!openid) { try { const res = await api.login() openid = res.openid wx.setStorageSync('openid', openid) console.log('获取openid成功:', openid) } catch (err) { console.error('获取openid失败:', err) return } } this.globalData.openid = openid // 拿到 openid 后,本地无数据时从云端拉一次(新设备/重装恢复场景) const pulled = await storage.pullFromCloudIfEmpty() if (pulled) { console.log('已从云端恢复数据') // 云端可能存着老版本写上去的坏数据,拉回来后再修一遍(run 是幂等的) migrate.run() } // flush 之前同步失败的待重试队列 sync.flush() }, globalData: { userInfo: null, openid: '' } })