|
|
@ -1,14 +1,14 @@ |
|
|
|
import { constantRoutes,resetRouter } from '@/router' |
|
|
|
import { getRouters } from '@/api/menu' |
|
|
|
import Layout from '@/layout/index' |
|
|
|
import { constantRoutes, resetRouter } from '@/router'; |
|
|
|
import { getRouters } from '@/api/menu'; |
|
|
|
import Layout from '@/layout/index'; |
|
|
|
|
|
|
|
/** |
|
|
|
* selectTab = 'reservoir', //水库运行管理
|
|
|
|
* selectTab = 'sluice', //水闸运行管理
|
|
|
|
* selectTab = 'dike', // 堤防运行管理
|
|
|
|
*/ |
|
|
|
/** |
|
|
|
* selectTab = 'reservoir', //水库运行管理
|
|
|
|
* selectTab = 'sluice', //水闸运行管理
|
|
|
|
* selectTab = 'dike', // 堤防运行管理
|
|
|
|
*/ |
|
|
|
// 提取路由
|
|
|
|
const pickRoutes = ['/reservoir','/dike','/sluice'] |
|
|
|
const pickRoutes = ['/reservoir', '/dike', '/sluice', '/aiSupervision']; |
|
|
|
|
|
|
|
const permission = { |
|
|
|
state: { |
|
|
@ -16,28 +16,28 @@ const permission = { |
|
|
|
routes: [], |
|
|
|
addRoutes: [], |
|
|
|
originRoutes: [], |
|
|
|
selectTab: sessionStorage.getItem('topTab') || 'dike', |
|
|
|
selectTab: sessionStorage.getItem('topTab') || 'dike' |
|
|
|
}, |
|
|
|
mutations: { |
|
|
|
SET_ROUTES: (state, {routes,originRoutes}) => { |
|
|
|
state.addRoutes = routes |
|
|
|
if(originRoutes) state.originRoutes = originRoutes |
|
|
|
state.routes = constantRoutes.concat(routes) |
|
|
|
SET_ROUTES: (state, { routes, originRoutes }) => { |
|
|
|
state.addRoutes = routes; |
|
|
|
if (originRoutes) state.originRoutes = originRoutes; |
|
|
|
state.routes = constantRoutes.concat(routes); |
|
|
|
}, |
|
|
|
SET_TAB_LIST: (state, payload) => { |
|
|
|
state.topTabList = payload |
|
|
|
state.topTabList = payload; |
|
|
|
}, |
|
|
|
SET_SELECT_TAB: (state, select) => { |
|
|
|
state.selectTab = select |
|
|
|
state.selectTab = select; |
|
|
|
} |
|
|
|
}, |
|
|
|
actions: { |
|
|
|
// 生成路由
|
|
|
|
GenerateRoutes({ commit }) { |
|
|
|
console.log('生成路由 >>>>> ') |
|
|
|
return new Promise(resolve => { |
|
|
|
console.log('生成路由 >>>>> '); |
|
|
|
return new Promise((resolve) => { |
|
|
|
// 向后端请求路由数据
|
|
|
|
getRouters().then(res => { |
|
|
|
getRouters().then((res) => { |
|
|
|
// 临时手动添加路由
|
|
|
|
// res.data.push(
|
|
|
|
// {
|
|
|
@ -138,87 +138,86 @@ const permission = { |
|
|
|
// ]
|
|
|
|
// }
|
|
|
|
// )
|
|
|
|
const accessedRoutes = filterAsyncRouter(res.data) |
|
|
|
accessedRoutes.push({ path: '*', redirect: '/404', hidden: true }) |
|
|
|
const {newRoutes: dealAccessedRoutes, topTabList} = resolveChildrenRoutes(accessedRoutes) |
|
|
|
commit('SET_ROUTES', {routes:dealAccessedRoutes,originRoutes: accessedRoutes}) |
|
|
|
commit('SET_TAB_LIST', topTabList) |
|
|
|
resolve(dealAccessedRoutes) |
|
|
|
}) |
|
|
|
}) |
|
|
|
const accessedRoutes = filterAsyncRouter(res.data); |
|
|
|
accessedRoutes.push({ path: '*', redirect: '/404', hidden: true }); |
|
|
|
const { newRoutes: dealAccessedRoutes, topTabList } = resolveChildrenRoutes(accessedRoutes); |
|
|
|
commit('SET_ROUTES', { routes: dealAccessedRoutes, originRoutes: accessedRoutes }); |
|
|
|
commit('SET_TAB_LIST', topTabList); |
|
|
|
resolve(dealAccessedRoutes); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}, |
|
|
|
|
|
|
|
// 根据顶部tab切换路由
|
|
|
|
changeRoutes({ commit }){ |
|
|
|
commit('SET_ROUTES',{routes:[]}) |
|
|
|
resetRouter() |
|
|
|
changeRoutes({ commit }) { |
|
|
|
commit('SET_ROUTES', { routes: [] }); |
|
|
|
resetRouter(); |
|
|
|
}, |
|
|
|
|
|
|
|
// 切换tab
|
|
|
|
changeTopTab({commit, dispatch}, payload){ |
|
|
|
commit('SET_SELECT_TAB', payload) |
|
|
|
dispatch('changeRoutes') |
|
|
|
}, |
|
|
|
// 切换tab
|
|
|
|
changeTopTab({ commit, dispatch }, payload) { |
|
|
|
commit('SET_SELECT_TAB', payload); |
|
|
|
dispatch('changeRoutes'); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
// 遍历后台传来的路由字符串,转换为组件对象
|
|
|
|
function filterAsyncRouter(asyncRouterMap) { |
|
|
|
return asyncRouterMap.filter(route => { |
|
|
|
return asyncRouterMap.filter((route) => { |
|
|
|
if (route.component) { |
|
|
|
// Layout组件特殊处理
|
|
|
|
if (route.component === 'Layout') { |
|
|
|
route.component = Layout |
|
|
|
route.component = Layout; |
|
|
|
} else { |
|
|
|
route.component = loadView(route.component) |
|
|
|
route.component = loadView(route.component); |
|
|
|
} |
|
|
|
} |
|
|
|
if (route.children != null && route.children && route.children.length) { |
|
|
|
route.children = filterAsyncRouter(route.children) |
|
|
|
route.children = filterAsyncRouter(route.children); |
|
|
|
} |
|
|
|
return true |
|
|
|
}) |
|
|
|
return true; |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
// 处理提取路由
|
|
|
|
function resolveChildrenRoutes(routes, pickRoute = permission.state.selectTab) { |
|
|
|
if(!routes?.length) return |
|
|
|
let pickRoutePath = `/${pickRoute}` |
|
|
|
let tempRoutesArr = [] |
|
|
|
let newChildrenRoutes = [] |
|
|
|
let newRoutes = routes.filter((v)=> { |
|
|
|
if(pickRoutes.includes(v.path)){ |
|
|
|
tempRoutesArr.push(v) |
|
|
|
if (!routes?.length) return; |
|
|
|
let pickRoutePath = `/${pickRoute}`; |
|
|
|
let tempRoutesArr = []; |
|
|
|
let newChildrenRoutes = []; |
|
|
|
let newRoutes = routes.filter((v) => { |
|
|
|
if (pickRoutes.includes(v.path)) { |
|
|
|
tempRoutesArr.push(v); |
|
|
|
} |
|
|
|
return !pickRoutes.includes(v.path) |
|
|
|
}) |
|
|
|
if(tempRoutesArr.length){ |
|
|
|
let parentRoute = tempRoutesArr.filter(v=>v.path === pickRoutePath)?.[0] |
|
|
|
if(parentRoute){ |
|
|
|
newChildrenRoutes = parentRoute.children?.map(v=> { |
|
|
|
return { |
|
|
|
...v, |
|
|
|
path: `${parentRoute.path}/${v.path}` |
|
|
|
} |
|
|
|
}) || [] |
|
|
|
return !pickRoutes.includes(v.path); |
|
|
|
}); |
|
|
|
if (tempRoutesArr.length) { |
|
|
|
let parentRoute = tempRoutesArr.filter((v) => v.path === pickRoutePath)?.[0]; |
|
|
|
if (parentRoute) { |
|
|
|
newChildrenRoutes = |
|
|
|
parentRoute.children?.map((v) => { |
|
|
|
return { |
|
|
|
...v, |
|
|
|
path: `${parentRoute.path}/${v.path}` |
|
|
|
}; |
|
|
|
}) || []; |
|
|
|
} |
|
|
|
} |
|
|
|
return { |
|
|
|
topTabList: tempRoutesArr.map(v=> { |
|
|
|
topTabList: tempRoutesArr.map((v) => { |
|
|
|
return { |
|
|
|
label: v.meta.title, |
|
|
|
value: v.path.slice(1) |
|
|
|
} |
|
|
|
}; |
|
|
|
}), |
|
|
|
newRoutes: [ |
|
|
|
...newChildrenRoutes, |
|
|
|
...newRoutes |
|
|
|
] |
|
|
|
} |
|
|
|
newRoutes: [...newChildrenRoutes, ...newRoutes] |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
export const loadView = (view) => { // 路由懒加载
|
|
|
|
return (resolve) => require([`@/views/${view}`], resolve) |
|
|
|
} |
|
|
|
export const loadView = (view) => { |
|
|
|
// 路由懒加载
|
|
|
|
return (resolve) => require([`@/views/${view}`], resolve); |
|
|
|
}; |
|
|
|
|
|
|
|
export default permission |
|
|
|
export default permission; |
|
|
|