You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
5.0 KiB

2 weeks ago
/* eslint-disable perfectionist/sort-imports */
import dayjs from 'dayjs'
2 months ago
import page from '@/utils/page'
import { request } from './api/request'
import { parseScene } from './utils/util'
2 weeks ago
2 months ago
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<IAppOption>({
globalData: {
2 weeks ago
url: 'https://app.gohighedu.cn',
upFileUrl: 'https://app.gohighedu.cn/upload/index',
imageUrl: 'https://app.gohighedu.cn/images/',
2 months ago
Timestamp: new Date().getTime(),
waitBindDoctorId: '',
scene: {},
initLoginInfo: {},
userInfo: {},
accessToken: '',
needBind: true,
loginPromise: Promise.resolve(),
2 months ago
},
onLaunch() {
this.autoUpdate()
Page = page as WechatMiniprogram.Page.Constructor
wx.ajax = licia.curry(request)({ gUrl: this.globalData.url })
},
onShow(options: Record<string, any> & { query?: Record<string, string> }) {
if (options.query?.scene) {
this.globalData.scene = parseScene(options.query!.scene) as any
}
this.globalData.loginPromise = this.startLogin()
2 months ago
},
startLogin(callback?: () => void): Promise<void> {
return new Promise<void>((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)
},
})
2 months ago
})
},
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()
2 months ago
}
})
},
2 weeks ago
/**
*
*/
redirectToLogin() {
2 weeks ago
// 获取当前页面路径
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',
})
},
2 months ago
getUserInfo(type: 0 | 1 | 2 = 0) {
const url: Record<number, string> = {
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: '检测到新版本,是否下载新版本并重启小程序?',
2 weeks ago
success: (res) => {
2 months ago
if (res.confirm) {
2 weeks ago
this.downLoadAndUpdate(updateManager)
2 months ago
} else if (res.cancel) {
wx.showModal({
title: '温馨提示~',
content: '本次版本更新涉及到新的功能添加,旧版本无法正常访问的哦~',
showCancel: false,
confirmText: '确定更新',
2 weeks ago
success: (res) => {
2 months ago
if (res.confirm) {
2 weeks ago
this.downLoadAndUpdate(updateManager)
2 months ago
}
},
})
}
},
})
}
})
} else {
wx.showModal({
2 weeks ago
title: '温馨提示',
content: '当前微信版本过低,无法使用版本更新功能,请升级到最新微信版本后重试。',
2 months ago
})
}
},
downLoadAndUpdate(updateManager: WechatMiniprogram.UpdateManager) {
updateManager.onUpdateReady(() => {
wx.hideLoading()
updateManager.applyUpdate()
})
updateManager.onUpdateFailed(() => {
wx.showModal({
title: '新版本更新失败',
content: '您可删除当前小程序,重新打开尝试',
})
})
},
})