Преглед изворни кода

判断企业路由和个人路由,防止互串

lifanagju_citu пре 11 месеци
родитељ
комит
6fce4d3f5f
5 измењених фајлова са 44 додато и 20 уклоњено
  1. 9 3
      src/permission.js
  2. 4 4
      src/router/modules/recruit.js
  3. 18 13
      src/router/modules/remaining.js
  4. 2 0
      src/store/user.js
  5. 11 0
      src/utils/loginType.js

+ 9 - 3
src/permission.js

@@ -1,7 +1,7 @@
 import router from './router'
 import { useNProgress } from '@/hooks/web/useNProgress'
 import { useTitle } from '@/hooks/web/useTitle'
-import { getToken } from '@/utils/auth'
+import { getToken, removeToken } from '@/utils/auth'
 
 const { start, done } = useNProgress()
 // 路由不重定向白名单
@@ -27,10 +27,16 @@ router.beforeEach(async (to, from, next) => {
     if (to.path === '/login') {
       next({ path: '/' })
     } else {
-      next()
+      const type = localStorage.getItem('loginType')
+      // 判断企业路由和个人路由,防止互串
+      if (!type) { removeToken(); next(`/login?redirect=${to.fullPath}`) }
+      else if (type === 'personal' && to.meta?.loginType === 'noLogin') next()
+      else if (to.meta?.loginType === type) next()
+      else next({ path: `/${type}` })
+      // next()
     }
   } else {
-    if (to.meta?.noLogin) { // noLogin:页面不需要登录
+    if (to.meta?.loginType === 'noLogin') { // 页面不需要登录
       next()
     } else {
       next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页

+ 4 - 4
src/router/modules/recruit.js

@@ -13,7 +13,7 @@ const recruit = [
         name: 'recruitPosition',
         meta: {
           title: '职位',
-          noLogin: true
+          loginType: 'noLogin'
         }
       },
       {
@@ -22,7 +22,7 @@ const recruit = [
         name: 'recruitCompany',
         meta: {
           title: '公司',
-          noLogin: true
+          loginType: 'noLogin'
         }
       },
       {
@@ -31,7 +31,7 @@ const recruit = [
         name: 'recruitPositionDetails',
         meta: {
           title: '职位详情',
-          noLogin: true
+          loginType: 'noLogin'
         }
       }
     ]
@@ -49,7 +49,7 @@ const recruit = [
         component: () => import('@/views/enterprise/components/enterpriseDetails.vue'),
         meta: {
           title: '企业详情',
-          noLogin: true
+          loginType: 'noLogin'
         },
       }
     ]

+ 18 - 13
src/router/modules/remaining.js

@@ -2,8 +2,12 @@ import personal from './personal'
 import enterprise from './enterprise'
 import recruit from './recruit'
 import Layout from '@/layout'
+import { setLoginType } from '@/utils/loginType'
+
+setLoginType(recruit, 'noLogin')
+setLoginType(personal, 'personal')
+setLoginType(enterprise, 'enterprise')
 
-// const type  = 0
 const routeArray = [
   ...recruit,
   ...personal,
@@ -18,7 +22,7 @@ const remainingRouter = [
     meta: {
       hidden: true,
       title: '登录/注册',
-      noLogin: true
+      loginType: 'noLogin'
     }
   },
   {
@@ -31,7 +35,7 @@ const remainingRouter = [
         component: () => import('@/views/login/components/userAgreement.vue'),
         meta: {
           title: '用户协议',
-          noLogin: true
+          loginType: 'noLogin'
         }
       }
     ]
@@ -46,7 +50,7 @@ const remainingRouter = [
         component: () => import('@/views/login/components/privacyPolicy.vue'),
         meta: {
           title: '隐私政策',
-          noLogin: true
+          loginType: 'noLogin'
         }
       }
     ]
@@ -56,19 +60,20 @@ const remainingRouter = [
     component: Layout,
     redirect: '/home',
     children: [
-      // {
-      //   path: '',
-      //   component: () => import('@/views/Home/index'),
-      //   meta: {
-      //     title: '首页'
-      //   }
-      // },
+      {
+        path: '/personal',
+        redirect: '/home',
+        meta: {
+          title: '首页',
+          loginType: 'noLogin'
+        }
+      },
       {
         path: '/home',
         component: () => import('@/views/Home/index'),
         meta: {
           title: '首页',
-          noLogin: true
+          loginType: 'noLogin'
         }
       }
     ]
@@ -80,7 +85,7 @@ const remainingRouter = [
     meta: {
       hidden: true,
       title: '注册企业',
-      noLogin: true
+      loginType: 'noLogin'
     }
   },
   ...routeArray

+ 2 - 0
src/store/user.js

@@ -28,6 +28,7 @@ export const useUserStore = defineStore('user',
             this.accountInfo = res
             localStorage.setItem('accountInfo', JSON.stringify(res))
             localStorage.setItem('expiresTime', res.expiresTime) // token过期时间
+            localStorage.setItem('loginType', 'personal')
             this.getUserInfos()
             if (!this.loginType) this.getUserBaseInfos()
             resolve()
@@ -45,6 +46,7 @@ export const useUserStore = defineStore('user',
             this.accountInfo = res
             localStorage.setItem('accountInfo', JSON.stringify(res))
             localStorage.setItem('expiresTime', res.expiresTime) // token过期时间
+            localStorage.setItem('loginType', 'personal')
             this.getUserInfos()
             if (!this.loginType) this.getUserBaseInfos()
             resolve()

+ 11 - 0
src/utils/loginType.js

@@ -0,0 +1,11 @@
+export const setLoginType = (arr, type) => {
+  traverse(arr)
+
+  function traverse(list) {
+    list.forEach(e => {
+      if (!e.meta) e.meta = {}
+      if (!e.meta.loginType) e.meta.loginType = type // 存在的话取e.meta.loginType,meta.loginType优先级高于这里默认的type
+      if (e.children?.length) traverse(e.children)
+    })
+  }
+}