loulijun2021
2022-06-26 d597f9a4d8c83b149694354a7369057f80e5faac
src/store/modules/permission.js
@@ -1,4 +1,4 @@
import { commonRoutes } from '@/router'
import { asyncRoutes, commonRoutes } from '@/router'
import Layout from '@/layout'
// 判断是否有权限
@@ -13,52 +13,49 @@
/**
 * 把后台返回菜单组装成routes要求的格式
 * @param {*} routes
 * @param {*} menu
 */
export function getAsyncRoutes(routes) {
  const res = []
export function getAsyncRoutes(routes, menu) {
  const menuCode = []// 将后端code取出存成数组形式
  menu.forEach(item => {
    menuCode.push(item.code)
    if (item.children && item.children.length > 0) {
      item.children.forEach(it => {
        menuCode.push(it.code)
      })
    }
  })
  const newRoutes = [] // 新路由
  routes.forEach(item => {
    const newItem = {}
    if (item.component) {
      if (item.component === 'Layout') {
        newItem.component = Layout
        newItem.path = item.path
        if (item.redirect) {
          newItem.redirect = item.redirect
    if (menuCode.includes(item.code) && item.children && item.children.length > 0) {
      const children = []
      let flag = false// 判断是否有children存进去
      item.children.forEach(it => {
        if (menuCode.includes(it.code)) {
          children.push(it)
          flag = true
        }
        if (item.hidden === '0') {
          newItem.hidden = false
        }
        if (item.hidden === '1') {
          newItem.hidden = true
        }
        if (item.meta) {
          newItem.meta = { title: item.meta.title, icon: item.meta.icon, affix: item.meta.affix === '1' }
        }
      } else {
        newItem.path = item.path
        if (item.name) {
          newItem.name = item.name
        }
        if (item.hidden === '0') {
          newItem.hidden = false
        }
        if (item.hidden === '1') {
          newItem.hidden = true
        }
        newItem.component = (resolve) => require(['@/views' + `${item.component}`], resolve)
        if (item.meta) {
          newItem.meta = { title: item.meta.title, icon: item.meta.icon, affix: item.meta.affix === '1' }
        }
      })
      if (flag) {
        newRoutes.push({
          alwaysShow: item.alwaysShow,
          code: item.code,
          name: item.name,
          path: item.path,
          redirect: item.redirect,
          component: item.component,
          children: children,
          meta: item.meta
        })
      }
    }
    if (item.children && item.children.length) {
      newItem.children = getAsyncRoutes(item.children)
    }
    res.push(newItem)
  })
  return res
  newRoutes.push({ path: '*', redirect: '/404', hidden: true })
  return newRoutes
}
// 过滤出有权限的路由
@@ -89,106 +86,11 @@
}
const actions = {
  generateRoutes({ commit }, info) {
  generateRoutes({ commit }, menu) {
    return new Promise(resolve => {
      // const accessedRoutes
      // getMenuList().then(routes => {
      //   const asyncRoutes = getAsyncRoutes(routes.data) // 对路由格式进行处理
      //   commit('SET_ROUTES', asyncRoutes)
      //   resolve(asyncRoutes)
      // })
      // '0'代表false,'1'代表true
      const routes = [
        {
          path: '/redirect',
          component: 'Layout',
          hidden: '1',
          children: [
            {
              path: '/redirect/:path(.*)',
              component: '/redirect/index'
            }
          ]
        },
        {
          path: '/',
          component: 'Layout',
          redirect: '/index',
          children: [{
            path: 'index',
            name: 'index',
            component: '/xtsy/index',
            meta: { title: '系统首页', icon: 'el-icon-house', affix: '1' }
          }]
        },
        {
          path: '/jcsz',
          component: 'Layout',
          redirect: '/jcsz/zzjg',
          name: 'jcsz',
          meta: { title: '基础设置', icon: 'el-icon-setting' },
          children: [
            {
              path: 'zzjg',
              name: 'Table',
              component: '/jcsz/zzjg',
              meta: { title: '组织架构', icon: '' }
            },
            {
              path: 'yhqd',
              name: 'yhqd',
              component: '/jcsz/yhqd',
              meta: { title: '用户清单', icon: '' }
            },
            {
              path: 'jsqd',
              name: 'jsqd',
              component: '/jcsz/jsqd',
              meta: { title: '角色清单', icon: '' }
            },
            {
              path: 'wldw',
              name: 'wldw',
              component: '/jcsz/wldw',
              meta: { title: '往来单位', icon: '' }
            }
          ]
        },
        {
          path: '/zzmx',
          component: 'Layout',
          redirect: '/zzmx/gylx',
          name: 'zzmx',
          meta: { title: '制造模型', icon: 'el-icon-s-help' },
          children: [
            {
              path: 'gylx',
              name: 'gylx',
              component: '/zzmx/gylx',
              meta: { title: '公益路线', icon: '' }
            },
            {
              path: 'jpgj',
              name: 'jpgj',
              component: '/zzmx/jpgj',
              meta: { title: '节拍工价', icon: '' }
            },
            {
              path: 'test',
              name: 'test',
              component: '/zzmx/test',
              meta: { title: '测试页面', icon: '' }
            }
          ]
        }
      ]
      const asyncRoutes = getAsyncRoutes(routes)
      asyncRoutes.push({ path: '*', redirect: '/404', hidden: true })
      commit('SET_ROUTES', asyncRoutes)
      resolve(asyncRoutes)
      console.log(asyncRoutes, 1)
      const newRoutes = getAsyncRoutes(asyncRoutes, menu)
      commit('SET_ROUTES', newRoutes)
      resolve(newRoutes)
    })
  }
}