You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.5 KiB

2 years ago
import { constantRoutes } from '@/router'
import { permissions } from '@/api/me'
import Layout from '@/layout'
import Nested from '@/layout/nested.vue'
import Wujie from '@/views/wujie'
2 years ago
import SiderbarLayout from '@/layout/SiderbarLayout.vue'
2 years ago
const loadView = (view) => {
2 years ago
return (resolve) => require([`@/views${view}`], resolve)
2 years ago
}
2 years ago
const componentHandle = (path, route) => {
2 years ago
if (/^#+/.test(path) && route.pid === 0 && route.children.length <= 1) {
2 years ago
return Layout
2 years ago
} else if (/^#+/.test(path) && route.pid === 0 && route.children.length > 1) {
return SiderbarLayout
2 years ago
} else if (/^#+/.test(path) && route.pid !== 0) {
return Nested
} else if (/^\/./.test(path)) {
return loadView(path)
} else {
return Wujie
}
}
2 years ago
export function filterAsyncRoutes(routes) {
const res = []
routes.forEach(route => {
if (!route.visible) return
2 years ago
const tmp = {
2 years ago
key: `key-${route.id}`,
path: route.path === '#' ? '' : route.path,
2 years ago
component: componentHandle(route.path, route),
2 years ago
name: route.name,
hidden: !route.visible,
meta: {
2 years ago
id: route.id,
2 years ago
title: route.title,
icon: route.icon,
guard: route.guard_name,
2 years ago
folder: route.folder,
isModule: !/^\/./.test(route.path),
2 years ago
// TODO:修改地址
2 years ago
// moduleUri: /^\/./.test(route.path) ? '' : `https://cz-hjjc.115.langye.net/${route.path}`,
2 years ago
moduleUri: /^\/./.test(route.path) ? '' : `/${route.path}`,
1 year ago
moduleName: /^\/./.test(route.path) ? '' : route.path
2 years ago
}
}
2 years ago
if (route.children && route.children instanceof Array && route.children.length > 0) {
2 years ago
tmp.children = filterAsyncRoutes(route.children)
}
res.push(tmp)
})
return res
}
const state = {
routes: [],
addRoutes: []
}
const mutations = {
SET_ROUTES: (state, routes) => {
state.addRoutes = routes
state.routes = constantRoutes.concat(routes)
1 year ago
},
RESET_ADD_ROUTES: (state) => {
state.addRoutes = []
2 years ago
}
}
const actions = {
generateRoutes({ commit }, roles) {
2 years ago
return new Promise(async(resolve, reject) => {
2 years ago
try {
const routes = await permissions()
2 years ago
const accessedRoutes = filterAsyncRoutes(routes)
// 把404拦截放在最后匹配
2 years ago
accessedRoutes.push({ path: '*', redirect: '/404', hidden: true })
2 years ago
commit('SET_ROUTES', accessedRoutes)
resolve(accessedRoutes)
} catch (err) {
reject(err)
}
})
}
}
export default {
namespaced: true,
state,
mutations,
actions
}