2026-03-12 13:05:20 +08:00
|
|
|
/**
|
|
|
|
|
* 路由配置
|
|
|
|
|
*
|
|
|
|
|
* 使用 unplugin-vue-router 自动生成文件路由
|
|
|
|
|
* 同时添加路由守卫和元信息处理
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { createRouter, createWebHistory } from 'vue-router';
|
2026-02-26 15:22:26 +08:00
|
|
|
import { message } from 'ant-design-vue';
|
|
|
|
|
|
2026-03-12 14:09:56 +08:00
|
|
|
// 导入手动配置的路由(从 routes.ts 中的传统路由配置复制)
|
|
|
|
|
const routes = [
|
|
|
|
|
{
|
|
|
|
|
path: '/login',
|
|
|
|
|
name: 'Login',
|
|
|
|
|
component: () => import('../views/auth/LoginView.vue'),
|
|
|
|
|
meta: { requiresAuth: false, title: '登录' }
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: '/admin',
|
|
|
|
|
name: 'Admin',
|
|
|
|
|
redirect: '/admin/dashboard',
|
|
|
|
|
meta: { requiresAuth: true, role: 'admin', title: '超管端' }
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: '/admin/dashboard',
|
|
|
|
|
name: 'AdminDashboard',
|
|
|
|
|
component: () => import('../views/admin/DashboardView.vue'),
|
|
|
|
|
meta: { requiresAuth: true, role: 'admin', title: '控制台' }
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: '/admin/tenants',
|
|
|
|
|
name: 'AdminTenants',
|
|
|
|
|
component: () => import('../views/admin/tenants/TenantListView.vue'),
|
|
|
|
|
meta: { requiresAuth: true, role: 'admin', title: '租户管理' }
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: '/admin/courses',
|
|
|
|
|
name: 'AdminCourses',
|
|
|
|
|
component: () => import('../views/admin/courses/CourseListView.vue'),
|
|
|
|
|
meta: { requiresAuth: true, role: 'admin', title: '课程管理' }
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: '/teacher',
|
|
|
|
|
name: 'Teacher',
|
|
|
|
|
redirect: '/teacher/dashboard',
|
|
|
|
|
meta: { requiresAuth: true, role: 'teacher', title: '教师端' }
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: '/teacher/dashboard',
|
|
|
|
|
name: 'TeacherDashboard',
|
|
|
|
|
component: () => import('../views/teacher/DashboardView.vue'),
|
|
|
|
|
meta: { requiresAuth: true, role: 'teacher', title: '控制台' }
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: '/teacher/courses',
|
|
|
|
|
name: 'TeacherCourses',
|
|
|
|
|
component: () => import('../views/teacher/courses/CourseListView.vue'),
|
|
|
|
|
meta: { requiresAuth: true, role: 'teacher', title: '我的课程' }
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: '/teacher/school-courses',
|
|
|
|
|
name: 'TeacherSchoolCourses',
|
|
|
|
|
component: () => import('../views/teacher/school-courses/SchoolCourseListView.vue'),
|
|
|
|
|
meta: { requiresAuth: true, role: 'teacher', title: '校本课程' }
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: '/teacher/lessons',
|
|
|
|
|
name: 'TeacherLessons',
|
|
|
|
|
component: () => import('../views/teacher/lessons/LessonView.vue'),
|
|
|
|
|
meta: { requiresAuth: true, role: 'teacher', title: '授课记录' }
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: '/school',
|
|
|
|
|
name: 'School',
|
|
|
|
|
redirect: '/school/dashboard',
|
|
|
|
|
meta: { requiresAuth: true, role: 'school', title: '学校端' }
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: '/parent',
|
|
|
|
|
name: 'Parent',
|
|
|
|
|
redirect: '/parent/dashboard',
|
|
|
|
|
meta: { requiresAuth: true, role: 'parent', title: '家长端' }
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: '/:pathMatch(.*)*',
|
|
|
|
|
redirect: '/login'
|
|
|
|
|
}
|
|
|
|
|
];
|
2026-02-26 15:22:26 +08:00
|
|
|
|
|
|
|
|
const router = createRouter({
|
|
|
|
|
history: createWebHistory(import.meta.env.BASE_URL),
|
|
|
|
|
routes,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 路由守卫
|
2026-03-12 13:05:20 +08:00
|
|
|
router.beforeEach((to, _from, next) => {
|
2026-02-26 15:22:26 +08:00
|
|
|
const token = localStorage.getItem('token');
|
|
|
|
|
const userRole = localStorage.getItem('role');
|
|
|
|
|
|
|
|
|
|
// 设置页面标题
|
|
|
|
|
if (to.meta.title) {
|
|
|
|
|
document.title = `${to.meta.title} - 幼儿阅读教学服务平台`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 需要认证但未登录
|
2026-03-12 13:05:20 +08:00
|
|
|
if (to.meta.requiresAuth !== false && !token && to.path !== '/login') {
|
2026-02-26 15:22:26 +08:00
|
|
|
message.warning('请先登录');
|
|
|
|
|
next('/login');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 已登录用户访问登录页,跳转到对应首页
|
|
|
|
|
if (to.path === '/login' && token) {
|
|
|
|
|
const defaultRoute = userRole ? `/${userRole}/dashboard` : '/admin/dashboard';
|
|
|
|
|
next(defaultRoute);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 角色权限检查
|
|
|
|
|
if (to.meta.role && userRole !== to.meta.role) {
|
|
|
|
|
message.error('没有权限访问该页面');
|
|
|
|
|
next(`/${userRole}/dashboard`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export { router };
|
|
|
|
|
export default router;
|