Procházet zdrojové kódy

-动态路由添加

ming před 3 roky
rodič
revize
590a56778a

+ 3 - 3
public/static/config.js

@@ -1,9 +1,9 @@
 // api 请求路径
 var PLATFROM_CONFIG = {};
-// PLATFROM_CONFIG.baseUrl = "http://172.16.120.104:8010/" //杨强本地
-// PLATFROM_CONFIG.baseUrl = "http://172.16.1.196:8010/" //超博本地
+PLATFROM_CONFIG.baseUrl = "http://172.16.120.104:8010/" //杨强本地
+    // PLATFROM_CONFIG.baseUrl = "http://172.16.1.196:8010/" //超博本地
 
-PLATFROM_CONFIG.baseUrl = "https://qhome.usky.cn/fivapi/" //线上
+// PLATFROM_CONFIG.baseUrl = "https://qhome.usky.cn/fivapi/" //线上
 PLATFROM_CONFIG.images = "https://qhome.usky.cn/file/" //线上图片
 PLATFROM_CONFIG.fileUrl = "https://qhome.usky.cn/file/" //xlsx文件访问路径
 

+ 1 - 1
src/config/default/setting.config.js

@@ -56,7 +56,7 @@ const setting = {
     //是否开启登录RSA加密
     loginRSA: false,
     //intelligence(前端导出路由)和all(后端导出路由)两种方式
-    authentication: 'intelligence',
+    authentication: 'all',
     //是否开启roles字段进行角色权限控制(如果是all模式后端完全处理角色并进行json组装,可设置false不处理路由中的roles字段)
     rolesControl: true,
     //vertical gallery comprehensive common布局时是否只保持一个子菜单的展开

+ 47 - 46
src/store/modules/routes.js

@@ -7,56 +7,57 @@ import { getRouterList } from '@/api/router'
 import { convertRouter, filterRoutes } from '@/utils/routes'
 
 const state = () => ({
-  routes: [],
-  partialRoutes: [],
+    routes: [],
+    partialRoutes: [],
 })
 const getters = {
-  routes: (state) => state.routes,
-  partialRoutes: (state) => state.partialRoutes,
+    routes: (state) => state.routes,
+    partialRoutes: (state) => state.partialRoutes,
 }
 const mutations = {
-  setRoutes(state, routes) {
-    state.routes = routes
-  },
-  setPartialRoutes(state, routes) {
-    state.partialRoutes = routes
-  },
+    setRoutes(state, routes) {
+        state.routes = routes
+    },
+    setPartialRoutes(state, routes) {
+        state.partialRoutes = routes
+    },
 }
 const actions = {
-  /**
-   * @author chuzhixin 1204505056@qq.com
-   * @description intelligence模式设置路由
-   * @param {*} { commit }
-   * @returns
-   */
-  async setRoutes({ commit }) {
-    const finallyRoutes = filterRoutes([...constantRoutes, ...asyncRoutes])
-    commit('setRoutes', finallyRoutes)
-    return [...asyncRoutes]
-  },
-  /**
-   * @author chuzhixin 1204505056@qq.com
-   * @description all模式设置路由
-   * @param {*} { commit }
-   * @returns
-   */
-  async setAllRoutes({ commit }) {
-    let { data } = await getRouterList()
-    if (data[data.length - 1].path !== '*')
-      data.push({ path: '*', redirect: '/404', hidden: true })
-    const asyncRoutes = convertRouter(data)
-    const finallyRoutes = filterRoutes([...constantRoutes, ...asyncRoutes])
-    commit('setRoutes', finallyRoutes)
-    return [...asyncRoutes]
-  },
-  /**
-   * @author chuzhixin 1204505056@qq.com
-   * @description 画廊布局、综合布局设置路由
-   * @param {*} { commit }
-   * @param accessedRoutes 画廊布局、综合布局设置路由
-   */
-  setPartialRoutes({ commit }, accessedRoutes) {
-    commit('setPartialRoutes', accessedRoutes)
-  },
+    /**
+     * @author chuzhixin 1204505056@qq.com
+     * @description intelligence模式设置路由
+     * @param {*} { commit }
+     * @returns
+     */
+    async setRoutes({ commit }) {
+        const finallyRoutes = filterRoutes([...constantRoutes, ...asyncRoutes])
+        commit('setRoutes', finallyRoutes)
+        return [...asyncRoutes]
+    },
+    /**
+     * @author chuzhixin 1204505056@qq.com
+     * @description all模式设置路由
+     * @param {*} { commit }
+     * @returns
+     */
+    async setAllRoutes({ commit }) {
+        let { data } = await getRouterList()
+        if (data[data.length - 1].path !== '*')
+            data.push({ path: '*', redirect: '/404', hidden: true })
+        const asyncRoutes = convertRouter(data)
+        const finallyRoutes = filterRoutes([...constantRoutes, ...asyncRoutes])
+        console.log(finallyRoutes)
+        commit('setRoutes', finallyRoutes)
+        return [...asyncRoutes]
+    },
+    /**
+     * @author chuzhixin 1204505056@qq.com
+     * @description 画廊布局、综合布局设置路由
+     * @param {*} { commit }
+     * @param accessedRoutes 画廊布局、综合布局设置路由
+     */
+    setPartialRoutes({ commit }, accessedRoutes) {
+        commit('setPartialRoutes', accessedRoutes)
+    },
 }
-export default { state, getters, mutations, actions }
+export default { state, getters, mutations, actions }

+ 46 - 44
src/utils/routes.js

@@ -3,6 +3,7 @@ import path from 'path'
 import { rolesControl } from '@/config'
 import { isExternal } from '@/utils/validate'
 import { hasRole } from '@/utils/hasRole'
+import { Layout } from 'ant-design-vue'
 
 /**
  * @author chuzhixin 1204505056@qq.com
@@ -11,35 +12,36 @@ import { hasRole } from '@/utils/hasRole'
  * @returns {*}
  */
 export function convertRouter(constantRoutes) {
-  return constantRoutes.map((route) => {
-    if (route.component) {
-      if (route.component === 'Layout') {
-        const path = 'layouts'
-        route.component = (resolve) => require([`@/${path}`], resolve)
-      } else {
-        let path = 'views/' + route.component
-        if (
-          new RegExp('^/views/.*$').test(route.component) ||
-          new RegExp('^views/.*$').test(route.component)
-        ) {
-          path = route.component
-        } else if (new RegExp('^/.*$').test(route.component)) {
-          path = 'views' + route.component
-        } else if (new RegExp('^@views/.*$').test(route.component)) {
-          path = route.component.slice(1)
-        } else {
-          path = 'views/' + route.component
+    return constantRoutes.map((route) => {
+        if (route.component) {
+            if (route.component === 'Layout') {
+                // const path = 'layouts'
+                // route.component = (resolve) => require([`@/${path}`], resolve)
+                route.component = Layout
+            } else {
+                let path = route.component
+                if (
+                    new RegExp('^/views/.*$').test(route.component) ||
+                    new RegExp('^views/.*$').test(route.component)
+                ) {
+                    path = route.component
+                } else if (new RegExp('^/.*$').test(route.component)) {
+                    path = 'views' + route.component
+                } else if (new RegExp('^@views/.*$').test(route.component)) {
+                    path = route.component.slice(1)
+                } else {
+                    path = route.component
+                }
+                route.component = (resolve) => require([`@/${path}`], resolve)
+            }
         }
-        route.component = (resolve) => require([`@/${path}`], resolve)
-      }
-    }
-    if (route.children && route.children.length)
-      route.children = convertRouter(route.children)
+        if (route.children && route.children.length)
+            route.children = convertRouter(route.children)
 
-    if (route.children && route.children.length === 0) delete route.children
+        if (route.children && route.children.length === 0) delete route.children
 
-    return route
-  })
+        return route
+    })
 }
 
 /**
@@ -50,20 +52,20 @@ export function convertRouter(constantRoutes) {
  * @returns {[]}
  */
 export function filterRoutes(routes, baseUrl = '/') {
-  return routes
-    .filter((route) => {
-      if (route.meta && route.meta.roles)
-        return !rolesControl || hasRole(route.meta.roles)
-      else return true
-    })
-    .map((route) => {
-      if (route.path !== '*' && !isExternal(route.path))
-        route.path = path.resolve(baseUrl, route.path)
-      route.fullPath = route.path
-      if (route.children)
-        route.children = filterRoutes(route.children, route.fullPath)
-      return route
-    })
+    return routes
+        .filter((route) => {
+            if (route.meta && route.meta.roles)
+                return !rolesControl || hasRole(route.meta.roles)
+            else return true
+        })
+        .map((route) => {
+            if (route.path !== '*' && !isExternal(route.path))
+                route.path = path.resolve(baseUrl, route.path)
+            route.fullPath = route.path
+            if (route.children)
+                route.children = filterRoutes(route.children, route.fullPath)
+            return route
+        })
 }
 
 /**
@@ -71,7 +73,7 @@ export function filterRoutes(routes, baseUrl = '/') {
  * @returns {string}
  */
 export function handleFirstMenu() {
-  const firstMenu = router.currentRoute.matched[0].path
-  if (firstMenu === '') return '/'
-  return firstMenu
-}
+    const firstMenu = router.currentRoute.matched[0].path
+    if (firstMenu === '') return '/'
+    return firstMenu
+}