import page from '@/utils/page' import { request } from './api/request' import { parseScene } from './utils/util' const dayjs = require('dayjs') const licia = require('miniprogram-licia') require('/utils/dayjs/day-zh-cn.js') const relativeTime = require('/utils/dayjs/relativeTime.js') dayjs.locale('zh-cn') dayjs.extend(relativeTime) App({ globalData: { url: '', upFileUrl: '', imageUrl: '', Timestamp: new Date().getTime(), waitBindDoctorId: '', scene: {}, loginState: '', initLoginInfo: {}, userInfo: {}, }, onLaunch() { this.autoUpdate() Page = page as WechatMiniprogram.Page.Constructor wx.ajax = licia.curry(request)({ gUrl: this.globalData.url }) }, onShow(options: Record & { query?: Record }) { if (options.query?.scene) { this.globalData.scene = parseScene(options.query!.scene) as any } this.startLogin() }, startLogin(callback?: () => void) { wx.login({ success: (res) => { wx.ajax({ method: 'GET', url: '?r=wtx/user/init-login', data: { code: res.code, }, }).then((res: any) => { this.globalData.loginState = res.loginState this.globalData.initLoginInfo = res if (callback) { callback() } }) }, }) }, updateLoginInfo(callback?: () => void) { wx.ajax({ method: 'GET', url: '?r=wtx/user/init-info', data: {}, }).then((res: any) => { this.globalData.initLoginInfo = res if (callback) { callback() } }) }, waitLogin({ type }: { type?: 0 | 1 | 2 | 'any' } = { type: 'any' }) { return new Promise((resolve) => { const checkLogin = () => { if (this.globalData.loginState) { if (this.checkLoginType(type ?? 'any')) { resolve() } return } setTimeout(() => { checkLogin() }, 500) } checkLogin() }) }, checkLoginType(type: 0 | 1 | 2 | 'any') { const { loginType, isLogin, isReg } = this.globalData.initLoginInfo if (type === 'any') { return true } if (isLogin !== 1) { if (type === 0) { return true } wx.reLaunch({ url: '/pages/index/index', }) return false } if (isReg !== 1) { const typePageUrl: Record = { 1: '/pages/login/index', 2: '/pages/login/index', } wx.reLaunch({ url: typePageUrl[loginType as 1 | 2], }) return false } if (loginType !== type) { wx.reLaunch({ url: '/pages/index/index', }) return false } return true }, getUserInfo(type: 0 | 1 | 2 = 0) { const url: Record = { 0: '?r=wtx/user/userinfo', 1: '?r=wtx/account/info', 2: '?r=wtx/doctor/account/info', } return wx.ajax({ method: 'GET', url: url[type], }) }, autoUpdate() { const self = this if (wx.canIUse('getUpdateManager')) { const updateManager = wx.getUpdateManager() updateManager.onCheckForUpdate((res) => { if (res.hasUpdate) { wx.showModal({ title: '更新提示', content: '检测到新版本,是否下载新版本并重启小程序?', success(res) { if (res.confirm) { self.downLoadAndUpdate(updateManager) } else if (res.cancel) { wx.showModal({ title: '温馨提示~', content: '本次版本更新涉及到新的功能添加,旧版本无法正常访问的哦~', showCancel: false, confirmText: '确定更新', success(res) { if (res.confirm) { self.downLoadAndUpdate(updateManager) } }, }) } }, }) } }) } else { wx.showModal({ title: '提示', content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。', }) } }, downLoadAndUpdate(updateManager: WechatMiniprogram.UpdateManager) { updateManager.onUpdateReady(() => { wx.hideLoading() updateManager.applyUpdate() }) updateManager.onUpdateFailed(() => { wx.showModal({ title: '新版本更新失败', content: '您可删除当前小程序,重新打开尝试', }) }) }, })