|
|
|
@ -8,10 +8,12 @@
|
|
|
|
// | Author: CRMEB Team <admin@crmeb.com>
|
|
|
|
// | Author: CRMEB Team <admin@crmeb.com>
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
import { asyncRoutes, constantRoutes } from '@/router'
|
|
|
|
|
|
|
|
|
|
|
|
import router, { asyncRoutes, tempRoutes, constantRoutes } from '@/router'
|
|
|
|
import * as roleApi from '@/api/roleApi.js'
|
|
|
|
import * as roleApi from '@/api/roleApi.js'
|
|
|
|
import * as Auth from '@/libs/wechat';
|
|
|
|
import * as Auth from '@/libs/wechat';
|
|
|
|
import {formatRoutes} from '@/utils/parsing'
|
|
|
|
import {formatRoutes} from '@/utils/parsing'
|
|
|
|
|
|
|
|
import Layout from '@/layout'
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Filter asynchronous routing tables by recursion
|
|
|
|
* Filter asynchronous routing tables by recursion
|
|
|
|
@ -57,25 +59,34 @@ const actions = {
|
|
|
|
generateRoutes({ commit }, roleid) {
|
|
|
|
generateRoutes({ commit }, roleid) {
|
|
|
|
return new Promise(async resolve => {
|
|
|
|
return new Promise(async resolve => {
|
|
|
|
let accessedRoutes = []
|
|
|
|
let accessedRoutes = []
|
|
|
|
let menus= []
|
|
|
|
// let menus= []
|
|
|
|
// const { rules } = await roleApi.getRoleById(roleid)
|
|
|
|
// const { rules } = await roleApi.getRoleById(roleid)
|
|
|
|
let menusAll = await roleApi.menuListApi()
|
|
|
|
let menusAll = await roleApi.menuListApi();
|
|
|
|
|
|
|
|
!Auth.isPhone() ? menusAll = menusAll.filter(item => item.component !== '/javaMobile') : menusAll = menusAll.filter(item => item.component === '/javaMobile')
|
|
|
|
|
|
|
|
// const routes = menusToRoutes(menusAll);
|
|
|
|
|
|
|
|
const routes = copyRoutes(tempRoutes);
|
|
|
|
|
|
|
|
const newRoutes = findRoutes(menusAll);
|
|
|
|
|
|
|
|
// console.log('newRoutes',newRoutes);
|
|
|
|
|
|
|
|
combineRoutes(routes, newRoutes);
|
|
|
|
|
|
|
|
router.addRoutes(routes); // 动态添加可访问路由表
|
|
|
|
|
|
|
|
// console.log('routes-end',routes);
|
|
|
|
menusAll = formatRoutes(menusAll)
|
|
|
|
menusAll = formatRoutes(menusAll)
|
|
|
|
|
|
|
|
|
|
|
|
!Auth.isPhone() ? menus = menusAll.filter(item => item.url !== '/javaMobile') : menus = menusAll.filter(item => item.url === '/javaMobile')
|
|
|
|
const _routerResult = comRouter(menusAll, asyncRoutes)
|
|
|
|
const _routerResult = comRouter(menus, asyncRoutes)
|
|
|
|
console.log('_routerResult',_routerResult);
|
|
|
|
accessedRoutes = filterAsyncRoutes(_routerResult)
|
|
|
|
accessedRoutes = filterAsyncRoutes(_routerResult)
|
|
|
|
// todo 这里控制是否过滤路由,经测试有些菜单不能予以设置,比如系统设置等等
|
|
|
|
// todo 这里控制是否过滤路由,经测试有些菜单不能予以设置,比如系统设置等等
|
|
|
|
this.state.settings.showSettings = false
|
|
|
|
this.state.settings.showSettings = false
|
|
|
|
commit('SET_ROUTES', menus)
|
|
|
|
commit('SET_ROUTES', menusAll)
|
|
|
|
commit('SET_TOPBAR_ROUTES', menus)
|
|
|
|
commit('SET_TOPBAR_ROUTES', menusAll)
|
|
|
|
if(this.state.settings.topNav){
|
|
|
|
if(this.state.settings.topNav){
|
|
|
|
commit('SET_SIDEBAR_ROUTERS', state.sidebarRouters.length ? state.sidebarRouters : menus[0].child)
|
|
|
|
commit('SET_SIDEBAR_ROUTERS', state.sidebarRouters.length ? state.sidebarRouters : menusAll[0].child)
|
|
|
|
}else{
|
|
|
|
}else{
|
|
|
|
commit('SET_SIDEBAR_ROUTERS', menus)
|
|
|
|
commit('SET_SIDEBAR_ROUTERS', menusAll)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// resolve(menus)
|
|
|
|
// resolve(menus)
|
|
|
|
// commit('SET_ROUTES', accessedRoutes)
|
|
|
|
// commit('SET_ROUTES', accessedRoutes)
|
|
|
|
|
|
|
|
console.log('转化后的路由:', accessedRoutes);
|
|
|
|
resolve(accessedRoutes)
|
|
|
|
resolve(accessedRoutes)
|
|
|
|
|
|
|
|
|
|
|
|
// commit('SET_ROUTES', asyncRoutes)
|
|
|
|
// commit('SET_ROUTES', asyncRoutes)
|
|
|
|
@ -99,6 +110,128 @@ function comRouter(menus, asyncRouter, hasLeft) {
|
|
|
|
return res
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function copyRoutes(routes) {
|
|
|
|
|
|
|
|
const res = []
|
|
|
|
|
|
|
|
routes.forEach(route => {
|
|
|
|
|
|
|
|
const tmp = { ...route }
|
|
|
|
|
|
|
|
if (tmp.children) {
|
|
|
|
|
|
|
|
tmp.children = copyRoutes(tmp.children)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
res.push(tmp)
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function findRoutes(menus, level = 0, prePath = '/') {
|
|
|
|
|
|
|
|
const accessedRoutes = []
|
|
|
|
|
|
|
|
menus.forEach(menu => {
|
|
|
|
|
|
|
|
let components = menu.component.split('/');
|
|
|
|
|
|
|
|
components = components.filter(item => !!item);
|
|
|
|
|
|
|
|
const newPath = components.join('/');
|
|
|
|
|
|
|
|
const tempRoute = findRoute(newPath, tempRoutes) || {};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let component = tempRoute.component || (level === 0 ? Layout : loadView(newPath));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let path = newPath;
|
|
|
|
|
|
|
|
if (newPath.startsWith(prePath)) {
|
|
|
|
|
|
|
|
path = newPath.replace(prePath, '');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const route = {
|
|
|
|
|
|
|
|
path: tempRoute.path || (level === 0 ? '/' + path : path), // 路由地址
|
|
|
|
|
|
|
|
component,
|
|
|
|
|
|
|
|
name: tempRoute.name || components[components.length - 1], // 路由名称
|
|
|
|
|
|
|
|
meta: {
|
|
|
|
|
|
|
|
title: menu.name,
|
|
|
|
|
|
|
|
icon: menu.icon
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tempRoute.redirect) {
|
|
|
|
|
|
|
|
route.redirect = tempRoute.redirect
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (menu.childList && menu.childList.length > 0) {
|
|
|
|
|
|
|
|
route.children = findRoutes(menu.childList, level + 1, newPath + '/');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!tempRoute.path || (route.children && route.children.length > 0)) {
|
|
|
|
|
|
|
|
accessedRoutes.push(route)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
return accessedRoutes
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function combineRoutes(routers, fromRoutes) {
|
|
|
|
|
|
|
|
fromRoutes.forEach(frouter => {
|
|
|
|
|
|
|
|
const findex = routers.findIndex(r => r.path === frouter.path);
|
|
|
|
|
|
|
|
if (findex > -1) {
|
|
|
|
|
|
|
|
combineRoutes(routers[findex].children, frouter.children);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
routers.push(frouter);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// function menusToRoutes(menus, level = 0) {
|
|
|
|
|
|
|
|
// const accessedRoutes = []
|
|
|
|
|
|
|
|
// menus.forEach(menu => {
|
|
|
|
|
|
|
|
// const components = menu.component.split('/');
|
|
|
|
|
|
|
|
// let path = components[components.length - 1];
|
|
|
|
|
|
|
|
// // const isML = menu.menuType === 'M';
|
|
|
|
|
|
|
|
// const newPath = components.filter(item => !!item).join('/');
|
|
|
|
|
|
|
|
// const tempRoute = findRoute(newPath, tempRoutes) || {};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// let component = tempRoute.component || (level === 0 ? Layout : loadView(newPath));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// const route = {
|
|
|
|
|
|
|
|
// path: tempRoute.path || (level === 0 ? '/' + path : path), // 路由地址
|
|
|
|
|
|
|
|
// component,
|
|
|
|
|
|
|
|
// name: tempRoute.name || path, // 路由名称
|
|
|
|
|
|
|
|
// meta: {
|
|
|
|
|
|
|
|
// title: menu.name,
|
|
|
|
|
|
|
|
// icon: menu.icon
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if (tempRoute.redirect) {
|
|
|
|
|
|
|
|
// route.redirect = tempRoute.redirect
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if (menu.childList && menu.childList.length > 0) {
|
|
|
|
|
|
|
|
// route.children = menusToRoutes(menu.childList, level + 1);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// accessedRoutes.push(route)
|
|
|
|
|
|
|
|
// })
|
|
|
|
|
|
|
|
// return accessedRoutes
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function loadView(view) {
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
|
|
|
|
|
return (resolve) => require([`@/views/${view}`], resolve)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// 使用 import 实现生产环境的路由懒加载
|
|
|
|
|
|
|
|
return () => import(`@/views/${view}`)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function findRoute(component, routes) {
|
|
|
|
|
|
|
|
const paths = component.split('/');
|
|
|
|
|
|
|
|
let tempRoute = null;
|
|
|
|
|
|
|
|
paths.forEach((item, index) => {
|
|
|
|
|
|
|
|
if (index === 0) {
|
|
|
|
|
|
|
|
tempRoute = findItemByPath(item, routes);
|
|
|
|
|
|
|
|
} else if (tempRoute) {
|
|
|
|
|
|
|
|
tempRoute = findItemByPath(item, tempRoute.children || []);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
return tempRoute;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function findItemByPath(path, list) {
|
|
|
|
|
|
|
|
const index = list.findIndex(item => {
|
|
|
|
|
|
|
|
return item.path === path || item.path === '/' + path || item.path.startsWith(path + '/:');
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
return index > -1 ? list[index] : null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
export default {
|
|
|
|
namespaced: true,
|
|
|
|
namespaced: true,
|
|
|
|
state,
|
|
|
|
state,
|
|
|
|
|