Răsfoiți Sursa

优化各登录页跳转主页,各主页跳转权限限制

liyanbo 3 luni în urmă
părinte
comite
a9a78890b1
1 a modificat fișierele cu 98 adăugiri și 5 ștergeri
  1. 98 5
      src/router/index.js

+ 98 - 5
src/router/index.js

@@ -2,7 +2,6 @@ import { createRouter, createWebHashHistory } from 'vue-router'
 import App from '../App.vue'
 
 const routes = [
-
   { path: '/', component: () => import('../views/Login.vue') },
   { path: '/login', component: () => import('../views/Login.vue') },
   // 免登录
@@ -13,7 +12,6 @@ const routes = [
   //【blockly编程课】免租户登录
   { path: '/blockly-login', meta: {TENANT: '内部测试租户'}, component: () => import('../views/BlocklyLogin.vue') },
 
-
   // 【通识课】首页
   {
     path: '/home',
@@ -90,7 +88,6 @@ const routes = [
     component: () => import('../views/virtuallaboratory/index.vue')
   },
 
-
   // 【blockly编程课】首页
   {
     path: '/blocklyHome',
@@ -119,7 +116,6 @@ const routes = [
     component: () => import('../views/programming/Interface.vue')
   },
 
-
   // 【AI实验课】首页
   // 实验室主题
   {
@@ -142,8 +138,105 @@ const routes = [
     component: () => import('../views/laboratory/ExperimentalInterface.vue')
   }
 ]
+
+// 定义主页及其子路由映射
+const homeRoutes = {
+  main: '/home',
+  children: [
+    '/ai-general-course',
+    '/ai-laboratory',
+    '/ai-ennumerals',
+    '/evaluation',
+    '/testTopic',
+    '/personalized',
+    '/testSubmit',
+    '/ai-painting',
+    '/ai-image',
+    '/ai-avatar',
+    '/ai-video',
+    '/ai-questions',
+    '/ai-develop',
+    '/virtual-laboratory'
+  ]
+}
+
+const blocklyRoutes = {
+  main: '/blocklyHome',
+  children: [
+    '/blockly',
+    '/blockly2',
+    '/mapGame',
+    '/programming02',
+    '/programminglist',
+    '/programmingcourset',
+    '/interface'
+  ]
+}
+
+const aiCourseRoutes = {
+  main: '/aiCourseHome',
+  children: [
+    '/experiment-type',
+    '/experimental-course',
+    '/experimental-interface'
+  ]
+}
+
+// 定义登录页面与主页的映射关系
+const loginToHomeMap = {
+  '/login': homeRoutes.main,
+  '/quick-login': homeRoutes.main,
+  '/promotion-login': homeRoutes.main,
+  '/blockly-login': blocklyRoutes.main,
+  '/ai-login': aiCourseRoutes.main
+}
+
 const router = createRouter({
   history: createWebHashHistory(),
   routes
 })
-export default router
+
+// 导航守卫
+router.beforeEach((to, from, next) => {
+  // 如果是登录页面,直接放行并存储登录信息
+  if (Object.keys(loginToHomeMap).includes(to.path)) {
+    localStorage.setItem('loginPath', to.path)
+    next()
+    return
+  }
+
+  // 获取当前登录类型
+  const loginPath = localStorage.getItem('loginPath')
+
+  // 获取当前登录类型对应的主页
+  const allowedHome = loginToHomeMap[loginPath]
+
+  // 如果没有登录信息,允许访问所有页面(或根据实际需求处理)
+  if (!loginPath || !allowedHome) {
+    next()
+    return
+  }
+
+  // 检查目标路由是否在允许的路由范围内
+  let isAllowed = false
+
+  if (allowedHome === homeRoutes.main) {
+    // home主页允许访问所有页面
+    isAllowed = true
+  } else if (allowedHome === blocklyRoutes.main) {
+    // blocklyHome主页只允许访问自己和子路由
+    isAllowed = to.path === blocklyRoutes.main || blocklyRoutes.children.includes(to.path)
+  } else if (allowedHome === aiCourseRoutes.main) {
+    // aiCourseHome主页只允许访问自己和子路由
+    isAllowed = to.path === aiCourseRoutes.main || aiCourseRoutes.children.includes(to.path)
+  }
+
+  if (isAllowed) {
+    next()
+  } else {
+    // 不允许访问时,重定向到对应主页
+    next(allowedHome)
+  }
+})
+
+export default router