新会员页面

This commit is contained in:
2026-05-15 19:02:11 +08:00
parent f341408254
commit 14c7660770
31 changed files with 3496 additions and 396 deletions
+24 -1
View File
@@ -27,6 +27,12 @@ const router = createRouter({
routes: staticRoutes,
})
/**
* 上次静默校验登录状态的时间戳,用于节流(5 分钟内不重复请求)
*/
let lastCheckTime = 0
const CHECK_INTERVAL = 5 * 60 * 1000 // 5 分钟
/**
* 全局前置守卫 — 核心逻辑:
*
@@ -34,7 +40,7 @@ const router = createRouter({
* 2. 需要鉴权的路由 → 调 checkLogin 接口验证 Cookie 是否有效
* - 有效:同步 isAuthenticated = true,放行
* - 无效:同步 isAuthenticated = false,弹登录框,阻止导航
* 3. 不需要鉴权的路由 → 直接放行
* 3. 不需要鉴权的路由 → 静默同步登录状态(不阻止导航、不弹登录框),5 分钟内节流
*/
router.beforeEach(async (to, _from, next) => {
// 动态路由只需加载一次,与登录状态无关
@@ -53,6 +59,7 @@ router.beforeEach(async (to, _from, next) => {
if (to.meta?.requiresAuth) {
try {
const res = await checkLogin()
lastCheckTime = Date.now()
if (res.code === '0' && res.data === true) {
// Cookie 有效,同步前端状态并放行
store.commit('SET_AUTHENTICATED', true)
@@ -72,6 +79,22 @@ router.beforeEach(async (to, _from, next) => {
return
}
// 非鉴权路由 — 静默同步登录状态(节流:5 分钟内不重复请求)
if (Date.now() - lastCheckTime > CHECK_INTERVAL) {
checkLogin()
.then((res) => {
lastCheckTime = Date.now()
if (res.code === '0' && res.data === true) {
store.commit('SET_AUTHENTICATED', true)
} else {
store.commit('SET_AUTHENTICATED', false)
}
})
.catch(() => {
store.commit('SET_AUTHENTICATED', false)
})
}
next()
})