import { AppGlobalData } from './types/index' import { getCachedUser } from './utils/auth' import { CLOUD_ENV, HEARTBEAT_INTERVAL_MS } from './utils/constants' import { api } from './utils/api' App<{ globalData: AppGlobalData }>({ globalData: { userInfo: null, isLoggedIn: false, currentLocation: null, _locationTimer: null, navBarInfo: { statusBarHeight: 0, navbarHeight: 44, navbarPaddingRight: 0 }, }, onLaunch() { wx.cloud.init({ env: CLOUD_ENV, traceUser: true, }) // 测量系统胶囊按钮位置,供所有页面共用 const sysInfo = wx.getSystemInfoSync() const capsule = wx.getMenuButtonBoundingClientRect() this.globalData.navBarInfo = { statusBarHeight: sysInfo.statusBarHeight, navbarHeight: (capsule.top - sysInfo.statusBarHeight) * 2 + capsule.height, navbarPaddingRight: sysInfo.windowWidth - capsule.left, } const cached = getCachedUser() if (cached) { this.globalData.userInfo = cached this.globalData.isLoggedIn = true } this._initLocation() }, onShow() { this._startLocationHeartbeat() }, onHide() { this._stopLocationHeartbeat() }, _initLocation() { wx.getLocation({ type: 'gcj02', success: res => { this.globalData.currentLocation = { latitude: res.latitude, longitude: res.longitude, } if (this.globalData.isLoggedIn) { api.updateLocation(res.latitude, res.longitude).catch(() => {}) } }, fail: () => {}, }) }, _startLocationHeartbeat() { if (this.globalData._locationTimer) return this.globalData._locationTimer = setInterval(() => { if (!this.globalData.isLoggedIn) return wx.getLocation({ type: 'gcj02', success: res => { this.globalData.currentLocation = { latitude: res.latitude, longitude: res.longitude, } api.updateLocation(res.latitude, res.longitude).catch(() => {}) }, fail: () => {}, }) }, HEARTBEAT_INTERVAL_MS) }, _stopLocationHeartbeat() { if (this.globalData._locationTimer) { clearInterval(this.globalData._locationTimer) this.globalData._locationTimer = null } }, })