2026-05-17 15:55:06 +08:00
|
|
|
import { createRouter, createWebHistory } from 'vue-router'
|
|
|
|
|
|
|
|
|
|
const routes = [
|
|
|
|
|
{
|
|
|
|
|
path: '/login',
|
|
|
|
|
name: 'Login',
|
|
|
|
|
component: () => import('@/views/Login.vue'),
|
|
|
|
|
meta: { title: '登录' },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: '/register',
|
|
|
|
|
name: 'Register',
|
|
|
|
|
component: () => import('@/views/Register.vue'),
|
|
|
|
|
meta: { title: '注册' },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: '/',
|
|
|
|
|
component: () => import('@/layouts/BlogLayout.vue'),
|
|
|
|
|
children: [
|
|
|
|
|
{
|
|
|
|
|
path: '',
|
|
|
|
|
name: 'Home',
|
|
|
|
|
component: () => import('@/views/Home.vue'),
|
|
|
|
|
meta: { title: '首页' },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: 'article/:id',
|
|
|
|
|
name: 'ArticleDetail',
|
|
|
|
|
component: () => import('@/views/ArticleDetail.vue'),
|
|
|
|
|
meta: { title: '文章详情' },
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: '/app',
|
|
|
|
|
component: () => import('@/layouts/AppLayout.vue'),
|
|
|
|
|
meta: { requiresAuth: true },
|
|
|
|
|
children: [
|
|
|
|
|
{
|
|
|
|
|
path: 'dashboard',
|
|
|
|
|
name: 'Dashboard',
|
|
|
|
|
component: () => import('@/views/app/Dashboard.vue'),
|
|
|
|
|
meta: { title: '控制台', requiresAuth: true },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: 'articles',
|
|
|
|
|
name: 'MyArticles',
|
|
|
|
|
component: () => import('@/views/app/MyArticles.vue'),
|
|
|
|
|
meta: { title: '我的文章', requiresAuth: true },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: 'editor',
|
|
|
|
|
name: 'NewArticle',
|
|
|
|
|
component: () => import('@/views/app/ArticleEditor.vue'),
|
|
|
|
|
meta: { title: '写文章', requiresAuth: true },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: 'editor/:id',
|
|
|
|
|
name: 'EditArticle',
|
|
|
|
|
component: () => import('@/views/app/ArticleEditor.vue'),
|
|
|
|
|
meta: { title: '编辑文章', requiresAuth: true },
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: '/admin',
|
|
|
|
|
component: () => import('@/layouts/AdminLayout.vue'),
|
|
|
|
|
meta: { requiresAuth: true, requiresAdmin: true },
|
|
|
|
|
children: [
|
|
|
|
|
{
|
|
|
|
|
path: '',
|
|
|
|
|
name: 'AdminDashboard',
|
|
|
|
|
component: () => import('@/views/admin/Dashboard.vue'),
|
|
|
|
|
meta: { title: '管理后台', requiresAuth: true, requiresAdmin: true },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: 'users',
|
|
|
|
|
name: 'AdminUsers',
|
|
|
|
|
component: () => import('@/views/admin/Users.vue'),
|
|
|
|
|
meta: { title: '用户管理', requiresAuth: true, requiresAdmin: true },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: 'articles',
|
|
|
|
|
name: 'AdminArticles',
|
|
|
|
|
component: () => import('@/views/admin/Articles.vue'),
|
|
|
|
|
meta: { title: '文章管理', requiresAuth: true, requiresAdmin: true },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: 'comments',
|
|
|
|
|
name: 'AdminComments',
|
|
|
|
|
component: () => import('@/views/admin/Comments.vue'),
|
|
|
|
|
meta: { title: '评论管理', requiresAuth: true, requiresAdmin: true },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: 'categories',
|
|
|
|
|
name: 'AdminCategories',
|
|
|
|
|
component: () => import('@/views/admin/Categories.vue'),
|
|
|
|
|
meta: { title: '分类管理', requiresAuth: true, requiresAdmin: true },
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
const router = createRouter({
|
|
|
|
|
history: createWebHistory(),
|
|
|
|
|
routes,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
router.beforeEach((to, from, next) => {
|
|
|
|
|
document.title = to.meta.title ? `${to.meta.title} - 博客系统` : '博客系统'
|
|
|
|
|
const token = localStorage.getItem('access_token')
|
2026-05-18 03:16:37 +08:00
|
|
|
const userInfo = JSON.parse(localStorage.getItem('user_info') || 'null')
|
2026-05-17 15:55:06 +08:00
|
|
|
if (to.meta.requiresAuth && !token) {
|
|
|
|
|
next({ name: 'Login', query: { redirect: to.fullPath } })
|
2026-05-18 03:16:37 +08:00
|
|
|
} else if (to.meta.requiresAdmin && userInfo?.role !== 'ADMIN') {
|
|
|
|
|
next({ name: 'Home' })
|
2026-05-17 15:55:06 +08:00
|
|
|
} else {
|
|
|
|
|
next()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export default router
|