/* eslint-disable perfectionist/sort-imports */ import dayjs from 'dayjs' import page from '@/utils/page' import { request } from './api/request' import { parseScene } from './utils/util' 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: 'https://app.gohighedu.cn', upFileUrl: 'https://app.gohighedu.cn/upload/index', imageUrl: 'https://app.gohighedu.cn/images/', Timestamp: new Date().getTime(), waitBindDoctorId: '', scene: {}, initLoginInfo: {}, userInfo: {}, accessToken: '', needBind: true, loginPromise: Promise.resolve(), }, 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.globalData.loginPromise = this.startLogin() }, startLogin(callback?: () => void): Promise { return new Promise((resolve, reject) => { wx.login({ success: (res) => { // 调用静默登录接口 wx.ajax({ method: 'POST', url: '/auth/silent-login', showMsg: false, // 隐藏错误提示 data: { code: res.code, }, }) .then((response: any) => { const { accessToken, user, needBind } = response // 存储 accessToken this.globalData.accessToken = accessToken this.globalData.needBind = needBind // 存储用户信息 if (user) { this.globalData.userInfo = user } if (callback) { callback() } resolve() }) .catch((err: any) => { // 静默失败,不提示用户 console.error('静默登录请求失败:', err) reject(err) }) }, fail: (err) => { // 静默失败,不提示用户 console.error('wx.login 失败:', err) reject(err) }, }) }) }, waitLogin({ type }: { type?: 0 | 1 } = { type: 0 }) { return this.globalData.loginPromise.then(() => { // type = 0:不需要登录即可访问 if (type === 0) { return } if (type === 1 && this.globalData.needBind) { this.redirectToLogin() } }) }, /** * 重定向到登录页,并记录当前页面路径 */ redirectToLogin() { // 获取当前页面路径 const pages = getCurrentPages() const currentPage = pages[pages.length - 1] const currentUrl = currentPage ? currentPage.route : '' // 记录来源页面,登录后返回 if (currentUrl && currentUrl !== 'pages/login/index') { this.globalData.loginRedirectUrl = currentUrl } if (currentUrl.includes('login/index')) return wx.reLaunch({ url: '/pages/login/index', }) }, 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() { if (wx.canIUse('getUpdateManager')) { const updateManager = wx.getUpdateManager() updateManager.onCheckForUpdate((res) => { if (res.hasUpdate) { wx.showModal({ title: '更新提示', content: '检测到新版本,是否下载新版本并重启小程序?', success: (res) => { if (res.confirm) { this.downLoadAndUpdate(updateManager) } else if (res.cancel) { wx.showModal({ title: '温馨提示~', content: '本次版本更新涉及到新的功能添加,旧版本无法正常访问的哦~', showCancel: false, confirmText: '确定更新', success: (res) => { if (res.confirm) { this.downLoadAndUpdate(updateManager) } }, }) } }, }) } }) } else { wx.showModal({ title: '温馨提示', content: '当前微信版本过低,无法使用版本更新功能,请升级到最新微信版本后重试。', }) } }, downLoadAndUpdate(updateManager: WechatMiniprogram.UpdateManager) { updateManager.onUpdateReady(() => { wx.hideLoading() updateManager.applyUpdate() }) updateManager.onUpdateFailed(() => { wx.showModal({ title: '新版本更新失败', content: '您可删除当前小程序,重新打开尝试', }) }) }, })