| | |
| | | * a base page that does not have permission requirements |
| | | * all roles can be accessed |
| | | */ |
| | | export const constantRoutes = [ |
| | | function getRoutes() { |
| | | let constantRoutes = [ |
| | | { |
| | | path: '/login', |
| | | component: () => import('@/views/login/index'), |
| | |
| | | path: '/baseData', |
| | | component: Layout, |
| | | redirect: '/baseData/basicDataMessage', |
| | | name: 'BaseData', |
| | | meta: { title: '基础数据', icon: 'el-icon-s-tools' }, |
| | | children: [ |
| | | { |
| | | path: '/basicDataMessage', |
| | | path: 'basicDataMessage', |
| | | name: 'BasicDataMessage', |
| | | component: () => import('@/views/basicData/index'), |
| | | meta: { title: '基础数据', icon: 'el-icon-s-tools' } |
| | | } |
| | | ] |
| | | }, |
| | | // 404 page must be placed at the end !!! |
| | | { path: '*', redirect: '/404', hidden: true } |
| | | ] |
| | | |
| | | const createRouter = () => new Router({ |
| | | // mode: 'history', // require service support |
| | | scrollBehavior: () => ({ y: 0 }), |
| | | routes: constantRoutes |
| | | }) |
| | | |
| | | const router = createRouter() |
| | | |
| | | // Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465 |
| | | export function resetRouter() { |
| | | const newRouter = createRouter() |
| | | router.matcher = newRouter.matcher // reset router |
| | | return constantRoutes |
| | | } |
| | | |
| | | const baseRouter = [{ |
| | | path: '/login', |
| | | component: () => import('@/views/login/index'), |
| | | hidden: true |
| | | }, |
| | | { |
| | | path: '/404', |
| | | component: () => import('@/views/404'), |
| | | hidden: true |
| | | }, |
| | | { |
| | | path: '/addCommision', |
| | | component: () => import('@/views/inspectionManagement/commissionInspection/addCommision'), |
| | | hidden: true |
| | | }, |
| | | { |
| | | path: '/', |
| | | component: Layout, |
| | | redirect: '/home', |
| | | // meta: { title: '主页', icon: 'el-icon-s-home' }, |
| | | children: [{ |
| | | path: 'home', |
| | | name: 'Home', |
| | | component: () => import('@/views/home/index'), |
| | | meta: { title: '主页', icon: 'el-icon-s-home' } |
| | | }] |
| | | }, { |
| | | path: '/addCommision/:viewId', |
| | | hidden: true, |
| | | component: () => import('@/views/inspectionManagement/commissionInspection/addCommision'), |
| | | }] |
| | | |
| | | function fn3(tempArr) { |
| | | let result = []; |
| | | let obj = {}; |
| | | for (let i = 0; i < tempArr.length; i++) { |
| | | if (!obj[tempArr[i].path]) { |
| | | result.push(tempArr[i]); |
| | | obj[tempArr[i].path] = true; |
| | | }; |
| | | }; |
| | | return result; |
| | | }; |
| | | |
| | | |
| | | function createRouter(Routees) { |
| | | if (JSON.parse(localStorage.getItem("user")) != undefined && JSON.parse(localStorage.getItem("user")) != null) { |
| | | let role = JSON.parse(localStorage.getItem("user")).role |
| | | let menuFather = baseRouter |
| | | const dataMenuFather = fn3(menuFather) |
| | | role.roleMenuList.forEach((r) => { |
| | | dataMenuFather.push(Routees.filter(item => { |
| | | return item.path === r.menuUrl |
| | | })[0]) |
| | | }) |
| | | let eqChildren = []; |
| | | role.roleMenuList.forEach((r) => { |
| | | dataMenuFather.forEach(m => { |
| | | if (r.menuUrl === m.path) { |
| | | r.children.forEach(rc => { |
| | | m.children.forEach(mc => { |
| | | if (mc.meta != undefined) { |
| | | eqChildren.push({ "path": mc.path, "meta": JSON.parse(JSON.stringify(mc.meta)) }) |
| | | delete mc["meta"] |
| | | } |
| | | }) |
| | | }) |
| | | } |
| | | }) |
| | | }) |
| | | let once = [] |
| | | role.roleMenuList.forEach((r) => { |
| | | r.children.forEach(rc => { |
| | | eqChildren.forEach(eq => { |
| | | if (eq != undefined) { |
| | | if (eq.path == rc.menuUrl.split("/")[1]) { |
| | | once.push(eq) |
| | | } |
| | | } |
| | | }) |
| | | }) |
| | | }) |
| | | dataMenuFather.forEach(m => { |
| | | let i = 0 |
| | | if (m.children != undefined) { |
| | | m.children.forEach(mc => { |
| | | once.forEach(eq => { |
| | | if (eq != undefined) { |
| | | if (mc.path == eq.path) { |
| | | if (i === 0) { |
| | | m.redirect = m.path + "/" + eq.path |
| | | } |
| | | i++; |
| | | mc.meta = eq.meta |
| | | } |
| | | } |
| | | }) |
| | | }) |
| | | } |
| | | }) |
| | | dataMenuFather.push({path: '*', redirect: '/404', hidden: true}) |
| | | dataMenuFather.push() |
| | | Routees = dataMenuFather |
| | | } else { |
| | | Routees = baseRouter |
| | | } |
| | | return new Router({ |
| | | // mode: 'history', // require service support |
| | | scrollBehavior: () => ({ y: 0 }), |
| | | routes: Routees |
| | | }) |
| | | } |
| | | let router = createRouter(getRoutes()) |
| | | |
| | | // Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465 |
| | | export async function resetRouter() { |
| | | let newRouter = createRouter(getRoutes()) |
| | | router.matcher = newRouter.matcher |
| | | } |
| | | router.beforeEach(async (to, from, next) => { |
| | | // 1. 判断是不是登录页面 |
| | | // 是登录页面 |
| | | if (to.path === '/login') { |
| | | next() |
| | | } else { |
| | | // 不是登录页面 |
| | | // 2. 判断 是否登录过 |
| | | let token = localStorage.getItem('user') |
| | | if(token!=null&&token!=undefined){ |
| | | await resetRouter() |
| | | } |
| | | token ? next() : next('/login') |
| | | } |
| | | }) |
| | | export default router |