Files
Neo-ZQYY/apps/miniprogram - 副本/miniprogram/app.ts

71 lines
1.9 KiB
TypeScript
Raw 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 { request } from "./utils/request"
App<IAppOption>({
globalData: {},
onLaunch() {
// 从 Storage 恢复 token 和用户信息
const token = wx.getStorageSync("token")
const refreshToken = wx.getStorageSync("refreshToken")
const userId = wx.getStorageSync("userId")
if (token) {
this.globalData.token = token
this.globalData.refreshToken = refreshToken
if (userId) {
this.globalData.authUser = {
userId,
status: wx.getStorageSync("userStatus") || "new",
}
}
// 有 token → 查询最新用户状态并路由
this.checkAuthStatus()
}
// 无 token → 停留在 login 页(首页已设为 login
},
async checkAuthStatus() {
try {
const data = await request({
url: "/api/xcx/me",
method: "GET",
needAuth: true,
})
// 持久化用户信息
this.globalData.authUser = {
userId: data.user_id,
status: data.status,
nickname: data.nickname,
}
wx.setStorageSync("userId", data.user_id)
wx.setStorageSync("userStatus", data.status)
// 根据状态路由
switch (data.status) {
case "approved":
wx.reLaunch({ url: "/pages/task-list/task-list" })
break
case "pending":
wx.reLaunch({ url: "/pages/reviewing/reviewing" })
break
case "new":
wx.reLaunch({ url: "/pages/apply/apply" })
break
case "rejected":
wx.reLaunch({ url: "/pages/no-permission/no-permission" })
break
case "disabled":
wx.reLaunch({ url: "/pages/no-permission/no-permission" })
break
default:
wx.reLaunch({ url: "/pages/apply/apply" })
break
}
} catch {
// token 无效或网络错误 → 停留在 login 页
}
},
})