Files
2026-01-03 15:47:49 +08:00

162 lines
4.8 KiB
TypeScript
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// app.ts
import { IAppOption } from './types/app'
import apiManager from './utils/api'
import { cloudConfig } from './utils/cloud.config'
App<IAppOption>({
globalData: {
isLoggedIn: false,
userInfo: undefined,
token: undefined,
dictLevel: undefined, // 新增字段,用户词典等级配置
pendingReferrerId: undefined
},
onLaunch() {
console.log('小程序启动')
wx.cloud.init()
// 初始化登录状态
this.initLoginStatus()
// 设置存储日志
const logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
},
async call(obj: any, number: number = 0) {
const that: any = this
if (!that.cloud) {
const cloud = new wx.cloud.Cloud({
resourceAppid: cloudConfig.resourceAppid || wx.getAccountInfoSync()?.miniProgram?.appId || '',
resourceEnv: cloudConfig.env
})
that.cloud = cloud
await that.cloud.init()
}
try {
const result = await that.cloud.callContainer({
path: obj.path,
method: obj.method || 'GET',
header: {
'X-WX-SERVICE': cloudConfig.service
},
data: obj.data || {}
})
return result.data
} catch (e) {
const error = String(e)
if (error.indexOf("Cloud API isn't enabled") !== -1 && number < 3) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(this.call(obj, number + 1))
}, 300)
})
} else {
throw new Error(error)
}
}
},
onShow() {
console.log('小程序显示')
},
onHide() {
console.log('小程序隐藏')
},
// 初始化登录状态
initLoginStatus() {
try {
const authInfo = apiManager.getStoredAuthInfo()
if (authInfo && authInfo.token) {
// 使用API管理器的方法检查token是否过期
if (!apiManager.isTokenExpired()) {
this.globalData.isLoggedIn = true
this.globalData.token = authInfo.token
this.globalData.userInfo = authInfo.userInfo
// 初始化词典等级
this.globalData.dictLevel = authInfo.dictLevel || 'level1'
console.log('登录状态有效,自动登录')
} else {
console.log('Token 已过期,清理本地数据')
this.clearLoginData()
// 尝试重新登录(携带 referrerId
if (this.globalData.pendingReferrerId) {
apiManager.login(0, this.globalData.pendingReferrerId)
}
}
} else {
console.log('未找到登录信息')
this.globalData.isLoggedIn = false
// 尝试登录(携带 referrerId
if (this.globalData.pendingReferrerId) {
apiManager.login(0, this.globalData.pendingReferrerId)
}
}
} catch (error) {
console.error('初始化登录状态失败:', error)
this.globalData.isLoggedIn = false
}
},
// 更新登录状态
updateLoginStatus(loginData: { access_token: string; access_token_expire_time: string; session_uuid: string; userInfo?: IUserInfo, dict_level?: string }) {
try {
this.globalData.isLoggedIn = true
this.globalData.token = loginData.access_token
this.globalData.userInfo = loginData.userInfo
// 更新词典等级
this.globalData.dictLevel = loginData.dict_level || 'level1'
// 存储到本地
wx.setStorageSync('token', loginData.access_token)
if (loginData.userInfo) {
wx.setStorageSync('userInfo', loginData.userInfo)
}
// 存储词典等级
if (loginData.dict_level) {
wx.setStorageSync('dictLevel', loginData.dict_level)
}
// 处理过期时间,将字符串时间转换为时间戳
const expiryTime = new Date(loginData.access_token_expire_time).getTime()
wx.setStorageSync('tokenExpiry', expiryTime)
// 存储 session_uuid
wx.setStorageSync('sessionUuid', loginData.session_uuid)
console.log('登录状态更新成功', {
tokenExpiry: loginData.access_token_expire_time,
sessionUuid: loginData.session_uuid,
dictLevel: loginData.dict_level
})
} catch (error) {
console.error('更新登录状态失败:', error)
}
},
// 清理登录数据
clearLoginData() {
try {
this.globalData.isLoggedIn = false
this.globalData.token = undefined
this.globalData.userInfo = undefined
this.globalData.dictLevel = undefined
wx.removeStorageSync('token')
wx.removeStorageSync('userInfo')
wx.removeStorageSync('tokenExpiry')
wx.removeStorageSync('sessionUuid')
wx.removeStorageSync('dictLevel')
console.log('登录数据清理完成')
} catch (error) {
console.error('清理登录数据失败:', error)
}
}
})