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)
|
}
|
}
|