225 lines
6.3 KiB
TypeScript
225 lines
6.3 KiB
TypeScript
import { createRouter, createWebHistory } from 'vue-router'
|
|
import type { RouteRecordRaw, RouteLocationNormalized } from 'vue-router'
|
|
|
|
declare module 'vue-router' {
|
|
interface RouteMeta {
|
|
requiresAuth?: boolean
|
|
roles?: string[]
|
|
title?: string
|
|
}
|
|
}
|
|
|
|
const routes: RouteRecordRaw[] = [
|
|
{
|
|
path: '/login',
|
|
name: 'Login',
|
|
component: () => import('@/views/system/Login.vue'),
|
|
meta: { title: '登录' }
|
|
},
|
|
{
|
|
path: '/403',
|
|
name: 'Forbidden',
|
|
component: () => import('@/views/system/Forbidden.vue'),
|
|
meta: { title: '无权限' }
|
|
},
|
|
{
|
|
path: '/',
|
|
component: () => import('@/layouts/DefaultLayout.vue'),
|
|
redirect: '/dashboard',
|
|
meta: { requiresAuth: true },
|
|
children: [
|
|
{
|
|
path: 'dashboard',
|
|
name: 'Dashboard',
|
|
component: () => import('@/views/system/Dashboard.vue'),
|
|
meta: { title: '仪表盘' }
|
|
},
|
|
{
|
|
path: 'users',
|
|
name: 'UserManagement',
|
|
component: () => import('@/views/system/UserManagement.vue'),
|
|
meta: { title: '用户管理' }
|
|
},
|
|
{
|
|
path: 'coach',
|
|
name: 'CoachManagement',
|
|
component: () => import('@/views/system/CoachManagement.vue'),
|
|
meta: { title: '教练管理' }
|
|
},
|
|
{
|
|
path: 'coach/time-rules',
|
|
name: 'CoachTimeRuleManagement',
|
|
component: () => import('@/views/coach/CoachTimeRuleManagement.vue'),
|
|
meta: { title: '时间规则配置' }
|
|
},
|
|
{
|
|
path: 'roles',
|
|
name: 'RoleManagement',
|
|
component: () => import('@/views/system/RoleManagement.vue'),
|
|
meta: { title: '角色管理' }
|
|
},
|
|
{
|
|
path: 'menus',
|
|
name: 'MenuManagement',
|
|
component: () => import('@/views/system/MenuManagement.vue'),
|
|
meta: { title: '菜单管理' }
|
|
},
|
|
{
|
|
path: 'sys/config',
|
|
name: 'ConfigManagement',
|
|
component: () => import('@/views/config/ConfigManagement.vue'),
|
|
meta: { title: '参数配置' }
|
|
},
|
|
{
|
|
path: 'dict',
|
|
name: 'DictManagement',
|
|
component: () => import('@/views/config/DictManagement.vue'),
|
|
meta: { title: '字典管理' }
|
|
},
|
|
{
|
|
path: 'files',
|
|
name: 'FileManagement',
|
|
component: () => import('@/views/file/FileManagement.vue'),
|
|
meta: { title: '文件管理' }
|
|
},
|
|
{
|
|
path: 'notice',
|
|
name: 'NoticeManagement',
|
|
component: () => import('@/views/notify/NoticeManagement.vue'),
|
|
meta: { title: '通知公告' }
|
|
},
|
|
{
|
|
path: 'banners',
|
|
name: 'BannerManagement',
|
|
component: () => import('@/views/banner/BannerManagement.vue'),
|
|
meta: { title: '轮播图管理' }
|
|
},
|
|
{
|
|
path: 'brand',
|
|
name: 'BrandManagement',
|
|
component: () => import('@/views/brand/BrandManagement.vue'),
|
|
meta: { title: '品牌定制' }
|
|
},
|
|
{
|
|
path: 'loginlog',
|
|
name: 'LoginLog',
|
|
component: () => import('@/views/audit/LoginLog.vue'),
|
|
meta: { title: '登录日志' }
|
|
},
|
|
{
|
|
path: 'oplog',
|
|
name: 'OperationLog',
|
|
component: () => import('@/views/audit/OperationLog.vue'),
|
|
meta: { title: '操作日志' }
|
|
},
|
|
{
|
|
path: 'exceptionlog',
|
|
name: 'ExceptionLog',
|
|
component: () => import('@/views/audit/ExceptionLog.vue'),
|
|
meta: { title: '异常日志' }
|
|
},
|
|
{
|
|
path: 'courses',
|
|
name: 'GroupCourseManagement',
|
|
component: () => import('@/views/groupcourse/GroupCourseManagement.vue'),
|
|
meta: { title: '团课管理' }
|
|
},
|
|
{
|
|
path: 'courses/types',
|
|
name: 'CourseTypeManagement',
|
|
component: () => import('@/views/groupcourse/CourseTypeManagement.vue'),
|
|
meta: { title: '团课类型' }
|
|
},
|
|
{
|
|
path: 'courses/labels',
|
|
name: 'CourseLabelManagement',
|
|
component: () => import('@/views/groupcourse/CourseLabelManagement.vue'),
|
|
meta: { title: '团课标签' }
|
|
},
|
|
{
|
|
path: 'courses/recommend',
|
|
name: 'CourseRecommendManagement',
|
|
component: () => import('@/views/groupcourse/CourseRecommendManagement.vue'),
|
|
meta: { title: '团课推荐' }
|
|
},
|
|
{
|
|
path: 'members',
|
|
name: 'MemberManagement',
|
|
component: () => import('@/views/member/MemberManagement.vue'),
|
|
meta: { title: '会员管理' }
|
|
},
|
|
{
|
|
path: 'member-cards',
|
|
name: 'MemberCardManagement',
|
|
component: () => import('@/views/member/MemberCardManagement.vue'),
|
|
meta: { title: '会员卡管理' }
|
|
},
|
|
{
|
|
path: 'statistics',
|
|
name: 'StatisticsDashboard',
|
|
component: () => import('@/views/statistics/StatisticsDashboard.vue'),
|
|
meta: { title: '数据统计看板' }
|
|
}
|
|
]
|
|
}
|
|
]
|
|
|
|
const router = createRouter({
|
|
history: createWebHistory(),
|
|
routes
|
|
})
|
|
|
|
function checkRoutePermission(route: RouteLocationNormalized, userRoles: string[]): boolean {
|
|
if (!route.meta.roles || !Array.isArray(route.meta.roles) || route.meta.roles.length === 0) {
|
|
return true
|
|
}
|
|
return route.meta.roles.some((role: string) => userRoles.includes(role))
|
|
}
|
|
|
|
router.beforeEach((to, _from, next) => {
|
|
try {
|
|
const token = localStorage.getItem('token')
|
|
const rolesStr = localStorage.getItem('roles')
|
|
let userRoles: string[] = []
|
|
|
|
try {
|
|
userRoles = rolesStr ? JSON.parse(rolesStr) : []
|
|
} catch (e) {
|
|
console.warn('解析用户角色失败,将使用空数组:', e)
|
|
userRoles = []
|
|
}
|
|
|
|
if (to.meta.title) {
|
|
document.title = `${to.meta.title} - Novalon 管理系统`
|
|
}
|
|
|
|
if (to.path === '/login') {
|
|
if (token) {
|
|
next('/')
|
|
} else {
|
|
next()
|
|
}
|
|
} else if (to.path === '/403') {
|
|
next()
|
|
} else {
|
|
if (to.meta.requiresAuth !== false && !token) {
|
|
next('/login')
|
|
return
|
|
}
|
|
|
|
if (!checkRoutePermission(to, userRoles)) {
|
|
console.warn(`用户角色 ${userRoles} 无权访问路由 ${to.path},需要角色: ${to.meta.roles}`)
|
|
next('/403')
|
|
return
|
|
}
|
|
|
|
next()
|
|
}
|
|
} catch (error) {
|
|
console.error('路由守卫错误:', error)
|
|
next('/login')
|
|
}
|
|
})
|
|
|
|
export default router
|