/* 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', imageUrl: 'https://app.gohighedu.cn/images', Timestamp: new Date().getTime(), waitBindDoctorId: '', scene: {}, 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) => { console.log("DEBUGPRINT[244]: app.ts:42: res=", res) // // 调用静默登录接口 // wx.ajax({ // method: 'POST', // url: '/auth/silent-login', // showMsg: false, // 隐藏错误提示 // data: { // code: res.code, // }, // }) // .then((response: any) => { // const { accessToken, user } = response // // // 存储 accessToken // this.globalData.accessToken = accessToken // // // 存储用户信息 // if (user) { // this.globalData.userInfo = user // } // // // 更新 initLoginInfo // this.globalData.initLoginInfo = { // user, // } // // if (callback) { // callback() // } // }) // .catch((err: any) => { // // 静默失败,不提示用户 // console.error('静默登录请求失败:', err) // }) }, fail: (err) => { // 静默失败,不提示用户 console.error('wx.login 失败:', err) }, }) }, 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, reject) => { const checkLogin = () => { // type = 0:不需要登录即可访问 if (type === 0) { resolve() return } // type = 'any':不检查登录状态 if (type === 'any') { resolve() return } // type = 1 或 2:需要登录 if (type === 1 || type === 2) { // 检查是否有 accessToken if (this.globalData.accessToken) { // 已登录,检查是否需要绑定 if (this.globalData.initLoginInfo?.needBind) { // 需要绑定,跳转到登录页 this.redirectToLogin(type) reject(new Error('need_bind')) return } resolve() return } // 未登录,跳转到登录页 this.redirectToLogin(type) reject(new Error('not_logged_in')) return } resolve() } checkLogin() }) }, /** * 重定向到登录页,并记录当前页面路径 */ redirectToLogin(_type: 1 | 2) { // 获取当前页面路径 const pages = getCurrentPages() const currentPage = pages[pages.length - 1] const currentUrl = currentPage ? currentPage.route : '' // 记录来源页面,登录后返回 if (currentUrl && currentUrl !== 'pages/login/index') { this.globalData.loginRedirectUrl = currentUrl } wx.reLaunch({ url: '/pages/login/index', }) }, checkLoginType(type: 0 | 1 | 2 | 'any') { // type = 0:不需要登录 if (type === 0) { return true } // type = 'any':不检查 if (type === 'any') { return true } // type = 1 或 2:需要登录 if (type === 1 || type === 2) { // 检查是否有 accessToken if (!this.globalData.accessToken) { this.redirectToLogin(type as 1 | 2) return false } // 检查是否需要绑定 if (this.globalData.initLoginInfo?.needBind) { this.redirectToLogin(type as 1 | 2) return false } return true } 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() { 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: '您可删除当前小程序,重新打开尝试', }) }) }, })