微信小程序页面迁移校验之前 P5任务处理之前
This commit is contained in:
70
apps/miniprogram - 副本/miniprogram/app.ts
Normal file
70
apps/miniprogram - 副本/miniprogram/app.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
// 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 页
|
||||
}
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user