loulijun2021
2023-04-28 55e1ca6bccbee868b10fa9096f2f56704928461c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { asyncRoutes, commonRoutes } from '@/router'
import Layout from '@/layout'
import { getCookie } from '@/utils/auth'
 
// 判断是否有权限
function hasPermission(roles, route) {
  if (route.meta && route.meta.role) {
    return roles.some(role => route.meta.role.includes(role))
  } else {
    return true
  }
}
 
/**
 * 把后台返回菜单组装成routes要求的格式
 * @param {*} routes
 * @param {*} menu
 */
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 })
          }
        })
      }
    }
  })
 
  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
}
 
// 过滤出有权限的路由
export function filterAsyncRoutes(routes, roles) {
  const res = []
  routes.forEach(route => {
    const tmp = { ...route }
    if (hasPermission(roles, tmp)) {
      if (tmp.children) {
        tmp.children = filterAsyncRoutes(tmp.children, roles)
      }
      res.push(tmp)
    }
  })
  return res
}
 
const state = {
  routes: commonRoutes,
  addRouters: []
}
 
const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRouters = routes
    state.routes = commonRoutes.concat(routes)
  }
}
 
const actions = {
  generateRoutes({ commit }, menu) {
    return new Promise(resolve => {
      // const newRoutes = getAsyncRoutes(asyncRoutes, menu)
      // const newRoutes = asyncRoutes
 
      let newRoutes
      if (getCookie('cloud')) {
        const arr = menu.map(i => i.code)
        newRoutes = asyncRoutes.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)
    })
  }
}
 
export default {
  namespaced: true,
  state,
  mutations,
  actions
}