Browse Source

fix(server): 换一种方案

小黑兔 2 years ago
parent
commit
bfdf6ee21d
4 changed files with 24 additions and 20 deletions
  1. 4 4
      commitlint.config.js
  2. 6 3
      src/permission.js
  3. 12 12
      src/store/modules/async-router.js
  4. 2 1
      src/utils/request.js

+ 4 - 4
commitlint.config.js

@@ -18,9 +18,9 @@ module.exports = {
     'type-enum': [
       2,
       'always',
-      ['feat', 'fix', 'docs', 'style', 'refactor', 'test', 'chore', 'revert'],
+      ['feat', 'fix', 'docs', 'style', 'refactor', 'test', 'chore', 'revert']
     ],
     'subject-full-stop': [0, 'never'],
-    'subject-case': [0, 'never'],
-  },
-};
+    'subject-case': [0, 'never']
+  }
+}

+ 6 - 3
src/permission.js

@@ -7,6 +7,7 @@ import notification from 'ant-design-vue/es/notification'
 import { setDocumentTitle, domTitle } from '@/utils/domUtil'
 import { ACCESS_TOKEN } from '@/store/mutation-types'
 import { i18nRender } from '@/locales'
+import { generatorDynamicRouter } from '@/router/generator-routers'
 
 NProgress.configure({ showSpinner: false }) // NProgress Configuration
 
@@ -18,7 +19,8 @@ router.beforeEach((to, from, next) => {
   NProgress.start() // start progress bar
   to.meta && typeof to.meta.title !== 'undefined' && setDocumentTitle(`${i18nRender(to.meta.title)} - ${domTitle}`)
   /* has token */
-  if (storage.get(ACCESS_TOKEN)) {
+  const token = storage.get(ACCESS_TOKEN)
+  if (token) {
     if (to.path === loginRoutePath) {
       next({ path: defaultRoutePath })
       NProgress.done()
@@ -29,9 +31,10 @@ router.beforeEach((to, from, next) => {
         store
           .dispatch('GetInfo')
           .then(res => {
-            const roles = res.result && res.result.role
             // generate dynamic router
-            store.dispatch('GenerateRoutes', { roles }).then(() => {
+            generatorDynamicRouter(token).then(routers => {
+              store.commit('SET_ROUTERS', routers)
+
               // 根据roles权限生成可访问的路由表
               // 动态添加可访问路由表
               // VueRouter@3.5.0+ New API

+ 12 - 12
src/store/modules/async-router.js

@@ -2,7 +2,6 @@
  * 向后端请求用户的菜单,动态生成路由
  */
 import { constantRouterMap } from '@/config/router.config'
-import { generatorDynamicRouter } from '@/router/generator-routers'
 
 const permission = {
   state: {
@@ -14,18 +13,19 @@ const permission = {
       state.addRouters = routers
       state.routers = constantRouterMap.concat(routers)
     }
-  },
-  actions: {
-    GenerateRoutes ({ commit }, data) {
-      return new Promise(resolve => {
-        const { token } = data
-        generatorDynamicRouter(token).then(routers => {
-          commit('SET_ROUTERS', routers)
-          resolve()
-        })
-      })
-    }
   }
+  // ,
+  // actions: {
+  //   GenerateRoutes({ commit }, data) {
+  //     return new Promise(resolve => {
+  //       const { token } = data
+  //       generatorDynamicRouter(token).then(routers => {
+  //         commit('SET_ROUTERS', routers)
+  //         resolve()
+  //       })
+  //     })
+  //   }
+  // }
 }
 
 export default permission

+ 2 - 1
src/utils/request.js

@@ -1,4 +1,5 @@
 import axios from 'axios'
+import store from '@/store'
 import storage from 'store'
 import notification from 'ant-design-vue/es/notification'
 import { VueAxios } from './axios'
@@ -29,7 +30,7 @@ const errorHandler = (error) => {
         description: 'Authorization verification failed'
       })
       if (token) {
-        this.$store.dispatch('Logout').then(() => {
+        store.dispatch('Logout').then(() => {
           setTimeout(() => {
             window.location.reload()
           }, 1500)