loulijun2021
2022-06-14 76b2da3d2511d252298aaf1d37db200c48a8d092
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { commonRoutes } from '@/router'
import Layout from '@/layout'
 
// 判断是否有权限
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
 */
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' }
        }
      }
    }
 
    if (item.children && item.children.length) {
      newItem.children = getAsyncRoutes(item.children)
    }
    res.push(newItem)
  })
  return res
}
 
// 过滤出有权限的路由
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 }, info) {
    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)
    })
  }
}
 
export default {
  namespaced: true,
  state,
  mutations,
  actions
}