Ver código fonte

菜单栏图标显示问题,router守卫优化

zwq 2 anos atrás
pai
commit
ed3ae666b9

+ 1 - 3
nngkxxdp/src/main/resources/static/naqwzsjtj/naqwzsjtj/src/layout/components/Sidebar/index.vue

@@ -50,8 +50,6 @@ export default {
       return !this.sidebar.opened
     }
   },
-  created() {
-    console.log('routes', this.permission_routes)
-  }
+  created() {}
 }
 </script>

+ 13 - 8
nngkxxdp/src/main/resources/static/naqwzsjtj/naqwzsjtj/src/permission.js

@@ -15,14 +15,19 @@ router.beforeEach(async(to, from, next) => {
   document.title = getPageTitle(to.meta.title)
   
   if (sessionStorage.getItem('utoken')) {
-    // 如果已登录,加载routes,直接跳转
-    // routes是根据角色动态的菜单列表,这里没有角色,默认admin
-    const role = ['admin']
-    // 这里要生成一次用户访问路由的权限,不然vuex拿不到
-    const accessRoutes = await store.dispatch('permission/generateRoutes', role)
-    // 没有动态路由,不添加
-    // router.addRoutes(accessRoutes)
-    next()
+    // 如果已登录,已存在路由,直接跳转
+    if (store.state.permission.routes.length && store.state.permission.routes.length > 0) {
+      next()
+    } else {
+      // 如果不存在路由,加载路由,然后跳转
+      // routes是根据角色动态的菜单列表,这里没有角色,默认admin
+      const role = ['admin']
+      // 这里要生成一次用户访问路由的权限,不然vuex拿不到
+      const accessRoutes = await store.dispatch('permission/generateRoutes', role)
+      // 没有动态路由,不添加
+      router.addRoutes(accessRoutes)
+      next({ ...to, replace: true })
+    }
   } else {
     // 没有登录
     if (whiteList.indexOf(to.path) !== -1) {

+ 15 - 12
nngkxxdp/src/main/resources/static/naqwzsjtj/naqwzsjtj/src/router/index.js

@@ -41,7 +41,7 @@ export const constantRoutes = [
     path: '/',
     component: Layout,
     redirect: '/ImpressionNanan/index',
-    meta: { title: '印象南岸', icon: 'dashboard', affix: true },
+    meta: { title: '印象南岸', icon: 'el-icon-star-off', affix: true },
     children: [
       {
         path: '/ImpressionNanan/index',
@@ -62,26 +62,27 @@ export const constantRoutes = [
     component: Layout,
     redirect: '/history/index',
     name: 'history',
+    meta: { title: '历史记录', icon: 'el-icon-s-order' },
     children: [
       {
         path: 'index',
         component: History,
         name: 'History',
-        meta: { title: '历史记录', icon: 'dashboard' }
+        meta: { title: '历史记录' }
       },
       {
         path: 'informationEntry',
         component: InformationEntry,
         name: 'InformationEntry',
         hidden: true,
-        meta: { title: '信息录入', icon: 'dashboard', activeMenu: '/history/index' }
+        meta: { title: '信息录入', activeMenu: '/history/index' }
       },
       {
         path: 'historyDetail',
         component: HistoryDetail,
         name: 'HistoryDetail',
         hidden: true,
-        meta: { title: '历史记录详情', icon: 'dashboard', activeMenu: '/history/index' }
+        meta: { title: '历史记录详情', activeMenu: '/history/index' }
       }
     ]
   },
@@ -90,12 +91,13 @@ export const constantRoutes = [
     component: Layout,
     redirect: '/huaLongData/index',
     name: 'HuaLongData',
+    meta: { title: '华龙网数据', icon: 'el-icon-s-data' },
     children: [
       {
         path: 'index',
         component: HuaLongData,
-        name: 'HuaLongData',
-        meta: { title: '华龙网数据', icon: 'dashboard' }
+        name: 'HuaLongDataIndex',
+        meta: { title: '华龙网数据' }
       }
     ]
   },
@@ -104,12 +106,13 @@ export const constantRoutes = [
     component: Layout,
     redirect: '/decisionMatters/index',
     name: 'DecisionMatters',
+    meta: { title: '决策事项管理', icon: 'el-icon-s-cooperation' },
     children: [
       {
         path: 'index',
         component: DecisionMatters,
-        name: 'DecisionMatters',
-        meta: { title: '决策事项管理', icon: 'dashboard' }
+        name: 'DecisionMattersIndex',
+        meta: { title: '决策事项管理' }
       }
     ]
   },
@@ -118,19 +121,19 @@ export const constantRoutes = [
     component: Layout,
     redirect: '/mapEdit/index',
     name: 'MapEdit',
-    meta: { title: '地图纠错', icon: 'dashboard' },
+    meta: { title: '地图纠错', icon: 'el-icon-map-location' },
     children: [
       {
         path: 'index',
         component: MapEdit,
-        name: 'MapEdit',
-        meta: { title: '地图纠错', icon: 'dashboard' }
+        name: 'MapEditIndex',
+        meta: { title: '地图纠错' }
       },
       {
         path: 'mapDetails',
         component: MapDetails,
         name: 'MapDetails',
-        meta: { title: '地图详情', icon: 'dashboard', activeMenu: '/mapEdit/index' },
+        meta: { title: '地图详情', activeMenu: '/mapEdit/index' },
         hidden: true
       }
     ]

+ 23 - 2
nngkxxdp/src/main/resources/static/naqwzsjtj/naqwzsjtj/src/views/LoginPage.vue

@@ -70,12 +70,26 @@
                     ],
                 },
                 flag: false,
+                redirect: undefined,
+                otherQuery: {},
                 loginForm: {
                     userName: '',
                     password: '',
                 },
             }
         },
+		watch: {
+		  $route: {
+		    handler: function(route) {
+		      const query = route.query
+		      if (query) {
+		        this.redirect = query.redirect
+		        this.otherQuery = this.getOtherQuery(query)
+		      }
+		    },
+		    immediate: true
+		  }
+		},
         mounted() {
             document.onkeydown = (ev => {
                 if (ev.keyCode === 13) {
@@ -102,8 +116,7 @@
                                 sessionStorage['utoken'] = JSON.stringify(r.data.data.utoken)
                                 sessionStorage['pid'] = JSON.stringify(r.data.data.pid)
                                 sessionStorage['username'] = JSON.stringify(r.data.data.username)
-                                console.log('登录成功')
-                                this.$router.push('/')
+                                this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
                             } else {
                                 this.$message.error('登录失败,请确认密码')
                             }
@@ -114,6 +127,14 @@
                     loading.close()
                 })
             },
+			getOtherQuery(query) {
+			  return Object.keys(query).reduce((acc, cur) => {
+			    if (cur !== 'redirect') {
+			      acc[cur] = query[cur]
+			    }
+			    return acc
+			  }, {})
+			}
         }
     }
 </script>

+ 23 - 0
nngkxxdp/src/main/resources/static/naqwzsjtj/naqwzsjtj/vue.config.js

@@ -1,3 +1,10 @@
+
+const path = require('path')
+
+function resolve(dir) {
+    return path.join(__dirname, dir)
+}
+
 module.exports = {
     outputDir:'yxnaht',
     publicPath: './',
@@ -19,4 +26,20 @@ module.exports = {
         },
         disableHostCheck: true,
     },
+    chainWebpack(config) {
+        // set svg-sprite-loader
+        config.module
+          .rule('svg')
+          .exclude.add(resolve('src/icons'))
+          .end()
+        config.module
+          .rule('icons')
+          .test(/\.svg$/)
+          .include.add(resolve('src/icons'))
+          .end()
+          .use('svg-sprite-loader')
+          .loader('svg-sprite-loader')
+          .options({ symbolId: 'icon-[name]' })
+          .end()
+    }
 }