Admin
2022-11-15 5aa2cc1ebbdd538fbdf4b21862c7f50a4aa09317
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
import { permissionList } from '@/api/permission'
import lazyLoading from './lazyLoading.js'
import Cookies from 'js-cookie'
import { parseError } from '@/utils/messageDialog'
 
export function initRouter(vm) {
 
  const constRoutes = []
 
  // 判断用户是否登录
  const userId = Cookies.get('userId')
  if (userId === null || userId === '' || userId === undefined) {
    // 未登录
    vm.$store.commit('SET_ROUTES', [])
    return
  }
  const params = {}
  params['userId'] = userId
  // 加载菜单
  permissionList(params).then(response => {
    const res = response.data
    if (res.code == '200') {
      const menuData = res.result
 
      if (menuData === null || menuData === '' || menuData == undefined) {
        return
      }
      initRouterNode(constRoutes, menuData)
 
      vm.$store.commit('SET_ROUTES', constRoutes.filter(item => item.children.length > 0))
    } else {
      parseError({ error: res.message, vm: vm })
    }
  })
}
 
// 生成路由节点
export function initRouterNode(routers, data) {
  for (var item of data) {
    if (item.type !== 1) {
      continue
    }
    const menu = Object.assign({}, item)
 
    //   menu.component = import(`@/views/${menu.component}.vue`)
 
    menu.component = lazyLoading(menu.component)
 
    if (item.children && item.children.length > 0) {
      menu.children = []
      initRouterNode(menu.children, item.children)
    }
 
    const meta = {}
    // 给页面添加权限、标题、第三方网页链接
    meta.permTypes = menu.permTypes ? menu.permTypes : null
    meta.title = menu.title ? menu.title : null
    meta.url = menu.url ? menu.url : null
    if (menu.icon) { meta.icon = menu.icon }
    menu.meta = meta
 
    routers.push(menu)
  }
}