loulijun2021
2023-05-06 24a3cbbc31b8fbff1c6a3c9b9e4360dc82a05f4e
src/store/modules/permission.js
@@ -1,5 +1,6 @@
import { commonRoutes } from '@/router'
import { asyncRoutes, asyncRoutesCloud, commonRoutes } from '@/router'
import Layout from '@/layout'
import { getCookie } from '@/utils/auth'
// 判断是否有权限
function hasPermission(roles, route) {
@@ -13,52 +14,59 @@
/**
 * 把后台返回菜单组装成routes要求的格式
 * @param {*} routes
 * @param {*} menu
 */
export function getAsyncRoutes(routes) {
  const res = []
  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 (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' }
        }
export function getAsyncRoutes(routes, menu) {
  // menu  中flag  "0" 表示 true  "1"表示false
  const menuCode = []// 将后端code取出存成数组形式
  const menuNameAndCode = []// 将后端code与name取出存成数组形式
  menu.forEach(item => {
    if (item.flag === '0') {
      menuCode.push(item.code)
      menuNameAndCode.push({ code: item.code, name: item.name })
      if (item.children && item.children.length > 0) {
        item.children.forEach(it => {
          if (it.flag === '0') {
            menuCode.push(it.code)
            menuNameAndCode.push({ code: it.code, name: it.name })
          }
        })
      }
    }
    if (item.children && item.children.length) {
      newItem.children = getAsyncRoutes(item.children)
    }
    res.push(newItem)
  })
  return res
  const newRoutes = [] // 新路由
  routes.forEach(item => {
    if (menuCode.includes(item.code) && item.children && item.children.length > 0) {
      item.meta.title = menuNameAndCode.find(item2 => item2.code === item.code).name
      const children = []
      let flag = false// 判断是否有children存进去
      item.children.forEach(it => {
        if (menuCode.includes(it.code)) {
          it.meta.title = menuNameAndCode.find(item2 => item2.code === it.code).name
          children.push(it)
          flag = true
        }
      })
      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
        })
      }
    }
  })
  newRoutes.push({ path: '*', redirect: '/404', hidden: true })
  return newRoutes
}
// 过滤出有权限的路由
@@ -89,106 +97,23 @@
}
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)
      let newRoutes
      if (getCookie('cloud')) {
        const arr = menu.map(i => i.code)
        newRoutes = asyncRoutesCloud.filter(i => {
          if (arr.includes(i.code)) {
            return i
          }
        })
        newRoutes.push({ path: '*', redirect: '/404', hidden: true })
      } else {
        newRoutes = getAsyncRoutes(asyncRoutes, menu)
      }
      commit('SET_ROUTES', newRoutes)
      resolve(newRoutes)
    })
  }
}