Procházet zdrojové kódy

dynamic mx-route permission is ok

hare8999@163.com před 1 rokem
rodič
revize
1a8c464bca

+ 1 - 1
package.json

@@ -67,7 +67,7 @@
     "vue-count-to": "1.0.13",
     "vue-cropper": "0.5.5",
     "vue-esign": "^1.1.0",
-    "vue-router": "3.4.9",
+    "vue-router": "3.0.6",
     "vue-scrollto": "^2.20.0",
     "vue-video-player": "^5.0.2",
     "vuedraggable": "2.24.3",

+ 10 - 11
src/router/index.js

@@ -4,11 +4,10 @@ import Router from 'vue-router'
 import Layout from '@/layout'
 
 // 解决冗余导航报错
-const originalPush = Router.prototype.push
-
-Router.prototype.push = function push(location) {
-  return originalPush.call(this, location).catch(err => err)
-}
+// const originalPush = Router.prototype.push
+// Router.prototype.push = function push(location) {
+//   return originalPush.call(this, location).catch(err => err)
+// }
 
 Vue.use(Router)
 
@@ -693,21 +692,21 @@ export const constantRoutes = [{
         }
       },
       {
-        path: '/sygh/multiple-way/form',
+        path: '/career/multiway/form',
         component: (resolve) => require(['@/views/career/MultipleWay/form.vue'], resolve),
-        name: 'MultipleWayForm',
+        name: 'MultiwayForm',
         meta: {
           title: '多元升学路径规划测评',
-          parentPath: '/new-gaokao/multiway/index'
+          parentRoute: 'MultiwayIndex'
         }
       },
       {
-        path: '/sygh/multiple-way/report',
+        path: '/career/multiway/report',
         component: (resolve) => require(['@/views/career/MultipleWay/report.vue'], resolve),
-        name: 'MultipleWayReport',
+        name: 'MultiwayReport',
         meta: {
           title: '多元升学路径规划报告',
-          parentPath: '/new-gaokao/multiway/index'
+          parentRoute: 'MultiwayIndex'
         }
       }
     ]

+ 13 - 1
src/store/modules/mx-permission.js

@@ -103,11 +103,23 @@ const permission = {
       const ancestors = { fullMatched: false, matches: [] }
       let targetPath = to.path
       traceAncestorRoutesInMenus(topRoutes, targetPath, ancestors)
+      // try to fix active menus by parentPath config
       if (!ancestors.fullMatched && to.meta?.parentPath) {
         targetPath = to.meta.parentPath
         ancestors.matches.length = 0 // clear for re-trace
         traceAncestorRoutesInMenus(topRoutes, targetPath, ancestors)
       }
+      // try to fix active menus by parentRoute config.
+      // only match exactly routeName, so active menus will not be changed when config is wrong.
+      if (!ancestors.fullMatched && to.meta?.parentRoute) {
+        const parentTo = { name: to.meta.parentRoute }
+        const matched = router.getMatchedComponents(parentTo)
+        if (matched.length) {
+          targetPath = router.resolve(parentTo).href
+          ancestors.matches.length = 0 // clear for re-trace
+          traceAncestorRoutesInMenus(topRoutes, targetPath, ancestors)
+        }
+      }
 
       // 完全匹配,开始操作菜单
       if (ancestors.fullMatched) {
@@ -223,7 +235,7 @@ function handleAsyncRouterComponent(route) {
 
 function handleAsyncRouterLocalReference(route, parent, level) {
   // 属性挂在meta上可以不受匹配方法等因素的影响,最大限度保留设置
-  route.meta = route.meta || {}
+  route.meta = route.meta || { routeName: null }
   route.name = route.meta.routeName || route.name // overwrite route name by server meta config
   // 挂parent可逆向追溯, 不能挂反向引用vuex内部会深拷贝!
   // route.parent = parent

+ 1 - 1
src/views/career/MultipleWay/form.vue

@@ -410,7 +410,7 @@ export default {
       this.loading = true
       try {
         const res = await submitMultipleWayForm(postForm)
-        this.$router.push({ path: '/sygh/multiple-way/report', query: { formId: res.data }})
+        this.$router.push({ name: 'MultiwayReport', query: { formId: res.data }})
       } finally {
         this.loading = false
       }

+ 2 - 2
src/views/career/MultipleWay/index.vue

@@ -81,10 +81,10 @@ export default {
   },
   methods: {
     goForm() {
-      this.$router.push('/sygh/multiple-way/form')
+      this.$router.push({name: 'MultiwayForm'})
     },
     goReport(row) {
-      const path = this.$router.resolve({ path: '/sygh/multiple-way/report', query: { formId: row.formId }})
+      const path = this.$router.resolve({ name: 'MultiwayReport', query: { formId: row.formId }})
       window.open(path.href, '_blank')
     },
     async getList() {