|
|
@ -5,7 +5,7 @@ import Setting from "@/setting"; |
|
|
|
import { get } from "@/plugins/requests/index.js"; |
|
|
|
import { get } from "@/plugins/requests/index.js"; |
|
|
|
import api from "@/api"; |
|
|
|
import api from "@/api"; |
|
|
|
import util from "@/libs/util"; |
|
|
|
import util from "@/libs/util"; |
|
|
|
import { permission } from '@/router/permission'; |
|
|
|
import { permission } from "@/router/permission"; |
|
|
|
import BasicLayout from "@/layouts/home"; |
|
|
|
import BasicLayout from "@/layouts/home"; |
|
|
|
|
|
|
|
|
|
|
|
Vue.use(Router); |
|
|
|
Vue.use(Router); |
|
|
@ -20,7 +20,7 @@ const createRouter = () => new Router({ |
|
|
|
|
|
|
|
|
|
|
|
export function resetRouter() { |
|
|
|
export function resetRouter() { |
|
|
|
const newRouter = createRouter(); |
|
|
|
const newRouter = createRouter(); |
|
|
|
router.matcher = newRouter.matcher |
|
|
|
router.matcher = newRouter.matcher; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let router = createRouter(); |
|
|
|
let router = createRouter(); |
|
|
@ -50,32 +50,33 @@ export default router; |
|
|
|
component: () => import("@/pages/exception/error/404") |
|
|
|
component: () => import("@/pages/exception/error/404") |
|
|
|
} |
|
|
|
} |
|
|
|
]; |
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
// 处理路由权限的方法
|
|
|
|
// 处理路由权限的方法
|
|
|
|
function getPermission() { |
|
|
|
function getPermission() { |
|
|
|
sessionStorage.setItem('handelPermission',true)// 记录已处理权限
|
|
|
|
sessionStorage.setItem("handelPermission", true);// 记录已处理权限
|
|
|
|
get(api.getUserRolesPermissionMenu).then(res => { |
|
|
|
get(api.getUserRolesPermissionMenu).then(res => { |
|
|
|
// 取得路由的值,使用addRouter添加进路由里
|
|
|
|
// 取得路由的值,使用addRouter添加进路由里
|
|
|
|
// 该项目的路由实际上做了两层处理,一层指向了默认的路由,对应模块,用于基础布局的nav,下面一层才是实际上的路由跳转,
|
|
|
|
// 该项目的路由实际上做了两层处理,一层指向了默认的路由,对应模块,用于基础布局的nav,下面一层才是实际上的路由跳转,
|
|
|
|
// 所以需要默认进行赋值,实际操作时根据path去禁用掉对应的nav即可。
|
|
|
|
// 所以需要默认进行赋值,实际操作时根据path去禁用掉对应的nav即可。
|
|
|
|
const arr = ['achievement','assessment','course',"evaluation",'information','match','project','setting','student','system'] |
|
|
|
const arr = ["achievement", "assessment", "course", "evaluation", "information", "match", "project", "setting", "student", "system"]; |
|
|
|
let permissionRouters = [] // 最后处理好的路由数组
|
|
|
|
let permissionRouters = []; // 最后处理好的路由数组
|
|
|
|
|
|
|
|
|
|
|
|
let data = res.permissionMenu[0].children
|
|
|
|
let data = res.permissionMenu[0].children; |
|
|
|
if (data.length > 0) { |
|
|
|
if (data.length > 0) { |
|
|
|
|
|
|
|
|
|
|
|
data.forEach(e => { |
|
|
|
data.forEach(e => { |
|
|
|
// 第一级路由,需要根据匹配到的path,塞进对应的路由children里
|
|
|
|
// 第一级路由,需要根据匹配到的path,塞进对应的路由children里
|
|
|
|
let str = e.path.split('/')[1] |
|
|
|
let str = e.path.split("/")[1]; |
|
|
|
let index = arr.indexOf(str) |
|
|
|
let index = arr.indexOf(str); |
|
|
|
let obj = { |
|
|
|
let obj = { |
|
|
|
path: e.path, |
|
|
|
path: e.path, |
|
|
|
name:arr[index]+'-list', |
|
|
|
name: arr[index] + "-list", |
|
|
|
component: () => import(`@/pages${e.path}`), |
|
|
|
component: () => import(`@/pages${e.path}`), |
|
|
|
meta: { title: e.name, btn: [] }, |
|
|
|
meta: { title: e.name, btn: [] }, |
|
|
|
children: [] |
|
|
|
children: [] |
|
|
|
} |
|
|
|
}; |
|
|
|
let meta = [] |
|
|
|
let meta = []; |
|
|
|
let children = [] |
|
|
|
let children = []; |
|
|
|
// 第二级是按钮级数据,一级归属当前页的同级页面,实际上一共就一级路由
|
|
|
|
// 第二级是按钮级数据,一级归属当前页的同级页面,实际上一共就一级路由
|
|
|
|
if (e.children && e.children.length > 0) { |
|
|
|
if (e.children && e.children.length > 0) { |
|
|
|
e.children.forEach(i => { |
|
|
|
e.children.forEach(i => { |
|
|
@ -84,11 +85,11 @@ function getPermission(){ |
|
|
|
// console.log(i.path,'当前二级路由的路径')
|
|
|
|
// console.log(i.path,'当前二级路由的路径')
|
|
|
|
let obj = { |
|
|
|
let obj = { |
|
|
|
path: i.path, |
|
|
|
path: i.path, |
|
|
|
name:arr[index]+'-'+i.path.split('/').pop(), |
|
|
|
name: arr[index] + "-" + i.path.split("/").pop(), |
|
|
|
component: () => import(`@/pages${i.path}`), |
|
|
|
component: () => import(`@/pages${i.path}`), |
|
|
|
meta: { title: i.name, btn: [] }, |
|
|
|
meta: { title: i.name, btn: [] }, |
|
|
|
children: [] |
|
|
|
children: [] |
|
|
|
} |
|
|
|
}; |
|
|
|
// 没找到第一级就生成一个
|
|
|
|
// 没找到第一级就生成一个
|
|
|
|
if (!permissionRouters.some(e => e.path === `/${str}`)) { |
|
|
|
if (!permissionRouters.some(e => e.path === `/${str}`)) { |
|
|
|
let one = { |
|
|
|
let one = { |
|
|
@ -99,25 +100,67 @@ function getPermission(){ |
|
|
|
}, |
|
|
|
}, |
|
|
|
component: BasicLayout, |
|
|
|
component: BasicLayout, |
|
|
|
children: []// 传来的路由实际上传入对应的children中
|
|
|
|
children: []// 传来的路由实际上传入对应的children中
|
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
one.children.push(obj); |
|
|
|
|
|
|
|
permissionRouters.push(one); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
// 找到第一级,判断没有重复地址直接塞进去
|
|
|
|
|
|
|
|
let nowIndex = permissionRouters.findIndex(e => { |
|
|
|
|
|
|
|
return e.path === `/${str}`; |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
if (nowIndex != -1 && !permissionRouters[nowIndex].children.some(e => e.path === obj.path)) { |
|
|
|
|
|
|
|
permissionRouters[nowIndex].children.push(obj); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
one.children.push(obj) |
|
|
|
// permissionRouters.push(obj) // path是页面,要塞入上一级同级里,需要单独push进去生成路由
|
|
|
|
permissionRouters.push(one) |
|
|
|
} else { |
|
|
|
|
|
|
|
meta.push(i.name); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (i.children && i.children.length) { |
|
|
|
|
|
|
|
i.children.forEach(j => { |
|
|
|
|
|
|
|
if (j.path && !permissionRouters[index].children.some(e => j.path === e.path)) { |
|
|
|
|
|
|
|
// console.log(j.path,'当前三级路由的路径')
|
|
|
|
|
|
|
|
let obj = { |
|
|
|
|
|
|
|
path: j.path, |
|
|
|
|
|
|
|
name: arr[index] + "-" + j.path.split("/").pop(), |
|
|
|
|
|
|
|
component: () => import(`@/pages${j.path}`), |
|
|
|
|
|
|
|
meta: { title: j.name, btn: [] }, |
|
|
|
|
|
|
|
children: [] |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
// 没找到第一级就生成一个
|
|
|
|
|
|
|
|
if (!permissionRouters.some(e => e.path === `/${str}`)) { |
|
|
|
|
|
|
|
let one = { |
|
|
|
|
|
|
|
path: "/" + arr[index], |
|
|
|
|
|
|
|
name: arr[index], |
|
|
|
|
|
|
|
redirect: { |
|
|
|
|
|
|
|
name: `${arr[index]}-list` |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
component: BasicLayout, |
|
|
|
|
|
|
|
children: []// 传来的路由实际上传入对应的children中
|
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
one.children.push(obj); |
|
|
|
|
|
|
|
permissionRouters.push(one); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
// 找到第一级,判断没有重复地址直接塞进去
|
|
|
|
// 找到第一级,判断没有重复地址直接塞进去
|
|
|
|
let nowIndex = permissionRouters.findIndex(e=>{return e.path===`/${str}`}) |
|
|
|
let nowIndex = permissionRouters.findIndex(e => { |
|
|
|
|
|
|
|
return e.path === `/${str}`; |
|
|
|
|
|
|
|
}); |
|
|
|
if (nowIndex != -1 && !permissionRouters[nowIndex].children.some(e => e.path === obj.path)) { |
|
|
|
if (nowIndex != -1 && !permissionRouters[nowIndex].children.some(e => e.path === obj.path)) { |
|
|
|
permissionRouters[nowIndex].children.push(obj) |
|
|
|
permissionRouters[nowIndex].children.push(obj); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
// permissionRouters.push(obj) // path是页面,要塞入上一级同级里,需要单独push进去生成路由
|
|
|
|
// permissionRouters.push(obj) // path是页面,要塞入上一级同级里,需要单独push进去生成路由
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
// 其他的是归属于当前页的按钮级信息,以此为依据来做按钮权限即可
|
|
|
|
meta.push(i.name+"-"+j.name); |
|
|
|
meta.push(i.name) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
obj.meta.btn = meta |
|
|
|
}); |
|
|
|
obj.children = children |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
obj.meta.btn = meta; |
|
|
|
|
|
|
|
obj.children = children; |
|
|
|
// 处理好的路由,需要进行一次匹配,arr中必然存在对应的路由,所以在此处才生成第一级路由,免得额外判断是否有children
|
|
|
|
// 处理好的路由,需要进行一次匹配,arr中必然存在对应的路由,所以在此处才生成第一级路由,免得额外判断是否有children
|
|
|
|
if (permissionRouters.length === 0 || !permissionRouters.some(e => e.path === `/${str}`)) { |
|
|
|
if (permissionRouters.length === 0 || !permissionRouters.some(e => e.path === `/${str}`)) { |
|
|
|
// 生成第一级
|
|
|
|
// 生成第一级
|
|
|
@ -129,52 +172,56 @@ function getPermission(){ |
|
|
|
}, |
|
|
|
}, |
|
|
|
component: BasicLayout, |
|
|
|
component: BasicLayout, |
|
|
|
children: []// 传来的路由实际上传入对应的children中
|
|
|
|
children: []// 传来的路由实际上传入对应的children中
|
|
|
|
} |
|
|
|
}; |
|
|
|
one.children.push(obj) |
|
|
|
one.children.push(obj); |
|
|
|
permissionRouters.push(one) |
|
|
|
permissionRouters.push(one); |
|
|
|
// console.log(one,'当前的第一级别',obj,'第二级别')
|
|
|
|
// console.log(one,'当前的第一级别',obj,'第二级别')
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
// 找到第一级直接塞进去
|
|
|
|
// 找到第一级直接塞进去
|
|
|
|
let nowIndex = permissionRouters.findIndex(e=>{return e.path===`/${str}`}) |
|
|
|
let nowIndex = permissionRouters.findIndex(e => { |
|
|
|
|
|
|
|
return e.path === `/${str}`; |
|
|
|
|
|
|
|
}); |
|
|
|
if (nowIndex != -1) { |
|
|
|
if (nowIndex != -1) { |
|
|
|
permissionRouters[nowIndex].children.push(obj) |
|
|
|
permissionRouters[nowIndex].children.push(obj); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
}); |
|
|
|
console.log(permissionRouters,'处理好的') |
|
|
|
console.log(permissionRouters, "处理好的"); |
|
|
|
// 把处理完成的路由,add到router里
|
|
|
|
// 把处理完成的路由,add到router里
|
|
|
|
// 记录一下路由的名称,用于nav
|
|
|
|
// 记录一下路由的名称,用于nav
|
|
|
|
let nav = data.map(e=>e.path) |
|
|
|
let nav = data.map(e => e.path); |
|
|
|
router.app.$options.store.commit('routers',nav) |
|
|
|
router.app.$options.store.commit("routers", nav); |
|
|
|
router.addRoutes(permissionRouters) |
|
|
|
router.addRoutes(permissionRouters); |
|
|
|
router.addRoutes(errorPage) |
|
|
|
router.addRoutes(errorPage); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
}) |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 权限
|
|
|
|
// 权限
|
|
|
|
router.beforeEach(function(to, from, next) { |
|
|
|
router.beforeEach(function(to, from, next) { |
|
|
|
// console.log(router.app.$options.store.commit('routers'),'router.app.$options.store')
|
|
|
|
// console.log(router.app.$options.store.commit('routers'),'router.app.$options.store')
|
|
|
|
|
|
|
|
|
|
|
|
if(to.path == '/login'){ |
|
|
|
if (to.path == "/login") { |
|
|
|
localStorage.removeItem(Setting.tokenKey) |
|
|
|
localStorage.removeItem(Setting.tokenKey); |
|
|
|
sessionStorage.removeItem('handelPermission') |
|
|
|
sessionStorage.removeItem("handelPermission"); |
|
|
|
} |
|
|
|
} |
|
|
|
// console.log(util.local.get(Setting.tokenKey),'token')
|
|
|
|
// console.log(util.local.get(Setting.tokenKey),'token')
|
|
|
|
// console.log(sessionStorage.getItem('handelPermission'),'保存的是否处理')
|
|
|
|
// console.log(sessionStorage.getItem('handelPermission'),'保存的是否处理')
|
|
|
|
if (util.local.get(Setting.tokenKey)) { |
|
|
|
if (util.local.get(Setting.tokenKey)) { |
|
|
|
if(!sessionStorage.getItem('handelPermission')){// 是否已处理权限
|
|
|
|
if (!sessionStorage.getItem("handelPermission")) {// 是否已处理权限
|
|
|
|
getPermission() |
|
|
|
getPermission(); |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
if (to.path !== '/login') { |
|
|
|
if (to.path !== "/login") { |
|
|
|
next({ path: '/login', |
|
|
|
next({ |
|
|
|
|
|
|
|
path: "/login", |
|
|
|
query: { redirect: to.path } |
|
|
|
query: { redirect: to.path } |
|
|
|
}) |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
// 根据路由元信息设置文档标题
|
|
|
|
// 根据路由元信息设置文档标题
|
|
|
|
window.document.title = to.meta.title |
|
|
|
window.document.title = to.meta.title; |
|
|
|
next() |
|
|
|
next(); |
|
|
|
|
|
|
|
|
|
|
|
}) |
|
|
|
}); |