mirror of
https://git.fightbot.fun/hxuanyu/BingPaper.git
synced 2026-02-15 16:59:32 +08:00
新增后端管理页面
This commit is contained in:
@@ -2,6 +2,8 @@ import { createRouter, createWebHistory } from 'vue-router'
|
||||
import Home from '@/views/Home.vue'
|
||||
import ImageView from '@/views/ImageView.vue'
|
||||
import ApiDocs from '@/views/ApiDocs.vue'
|
||||
import AdminLogin from '@/views/AdminLogin.vue'
|
||||
import Admin from '@/views/Admin.vue'
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(),
|
||||
@@ -29,13 +31,54 @@ const router = createRouter({
|
||||
meta: {
|
||||
title: 'API 文档'
|
||||
}
|
||||
},
|
||||
{
|
||||
path: '/admin/login',
|
||||
name: 'AdminLogin',
|
||||
component: AdminLogin,
|
||||
meta: {
|
||||
title: '管理员登录'
|
||||
}
|
||||
},
|
||||
{
|
||||
path: '/admin',
|
||||
name: 'Admin',
|
||||
component: Admin,
|
||||
meta: {
|
||||
title: '管理后台',
|
||||
requiresAuth: true
|
||||
}
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
// 路由守卫 - 更新页面标题
|
||||
// 路由守卫 - 更新页面标题和认证检查
|
||||
router.beforeEach((to, _from, next) => {
|
||||
document.title = (to.meta.title as string) || '必应每日一图'
|
||||
|
||||
// 检查是否需要认证
|
||||
if (to.meta.requiresAuth) {
|
||||
const token = localStorage.getItem('admin_token')
|
||||
if (!token) {
|
||||
// 未登录,重定向到登录页
|
||||
next('/admin/login')
|
||||
return
|
||||
}
|
||||
|
||||
// 检查 token 是否过期
|
||||
const expiresAt = localStorage.getItem('admin_token_expires')
|
||||
if (expiresAt) {
|
||||
const expireDate = new Date(expiresAt)
|
||||
if (expireDate < new Date()) {
|
||||
// token 已过期,清除并重定向到登录页
|
||||
localStorage.removeItem('admin_token')
|
||||
localStorage.removeItem('admin_token_expires')
|
||||
next('/admin/login')
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
next()
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user