From 6aa514738bb40d8739d1cdae9903d8c8d768c6d3 Mon Sep 17 00:00:00 2001 From: yujialong <479214531@qq.com> Date: Mon, 25 Oct 2021 19:26:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=9D=83=E9=99=90bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/layouts/header/index.vue | 10 +- src/router/index.js | 232 ++++++++++++++++++----------------- src/router/routes.js | 29 +---- 3 files changed, 130 insertions(+), 141 deletions(-) diff --git a/src/layouts/header/index.vue b/src/layouts/header/index.vue index 6aaab84..94faba7 100644 --- a/src/layouts/header/index.vue +++ b/src/layouts/header/index.vue @@ -10,7 +10,7 @@ {{ customer ? customerName : userName }} - 退出 + 退出 @@ -35,8 +35,14 @@ export default { }, methods: { ...mapActions("user", [ - "logout", "setTitle", "setLogoUrl", "setAvatar", "setUserName" + "setTitle", "setLogoUrl", "setAvatar", "setUserName" ]), + loginout() { + this.$router.push('/login'); + localStorage.clear(); + sessionStorage.clear() + location.reload(); + }, toPersonal() { if (!this.customer) { this.$router.push("/setting/person"); diff --git a/src/router/index.js b/src/router/index.js index 0ae11f4..3d31bd2 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -16,7 +16,7 @@ const createRouter = () => new Router({ routes }); - +console.log(routes); export function resetRouter() { const newRouter = createRouter(); router.matcher = newRouter.matcher; @@ -57,7 +57,7 @@ function getPermission() { // 取得路由的值,使用addRouter添加进路由里 // 该项目的路由实际上做了两层处理,一层指向了默认的路由,对应模块,用于基础布局的nav,下面一层才是实际上的路由跳转, // 所以需要默认进行赋值,实际操作时根据path去禁用掉对应的nav即可。 - const arr = ["achievement", "assessment", "course", "evaluation", "information", "match", "project", "setting", "student", "system"]; + const arr = ["student", "evaluation", "assessment", "achievement", "course", "information", "match", "project", "system"]; let permissionRouters = []; // 最后处理好的路由数组 let data = res.permissionMenu[0].children; @@ -66,123 +66,128 @@ function getPermission() { // 第一级路由,需要根据匹配到的path,塞进对应的路由children里 let str = e.path.split("/")[1]; let index = arr.indexOf(str); - let obj = { - path: e.path, - name: arr[index] + "-list", - component: () => import(`@/pages${e.path}`), - meta: { title: e.name, btn: [] }, - children: [] - }; - let meta = []; - let children = []; - // 第二级是按钮级数据,一级归属当前页的同级页面,实际上一共就一级路由 - if (e.children && e.children.length > 0) { - e.children.forEach(i => { - // 避免重复生成路由 - if (i.path && !permissionRouters[index].children.some(e => i.path === e.path)) { - // console.log(i.path,'当前二级路由的路径') - let obj = { - path: i.path, - name: arr[index] + "-" + i.path.split("/").pop(), - component: () => import(`@/pages${i.path}`), - meta: { title: i.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中 + console.log(index); + if (index !== -1) { + let obj = { + path: e.path, + name: arr[index] + "-list", + component: () => import(`@/pages${e.path}`), + meta: { title: e.name, btn: [] }, + children: [] + }; + let meta = []; + let children = []; + // 第二级是按钮级数据,一级归属当前页的同级页面,实际上一共就一级路由 + if (e.children && e.children.length > 0) { + e.children.forEach(i => { + // 避免重复生成路由 + console.log(i.path); + console.log(permissionRouters[index]); + if (i.path && permissionRouters[index] && !permissionRouters[index].children.some(e => i.path === e.path)) { + // console.log(i.path,'当前二级路由的路径') + let obj = { + path: i.path, + name: arr[index] + "-" + i.path.split("/").pop(), + component: () => import(`@/pages${i.path}`), + meta: { title: i.name, btn: [] }, + 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); + // 没找到第一级就生成一个 + 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 { + // 找到第一级,判断没有重复地址直接塞进去 + let nowIndex = permissionRouters.findIndex(e => { + return e.path === `/${str}`; + }); + if (nowIndex != -1 && permissionRouters[nowIndex] && !permissionRouters[nowIndex].children.some(e => e.path === obj.path)) { + permissionRouters[nowIndex].children.push(obj); + } } + // permissionRouters.push(obj) // path是页面,要塞入上一级同级里,需要单独push进去生成路由 + meta.push(i.name); + } else { + meta.push(i.name); } - // permissionRouters.push(obj) // path是页面,要塞入上一级同级里,需要单独push进去生成路由 - meta.push(i.name); - } 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中 + if (i.children && i.children.length) { + i.children.forEach(j => { + if (j.path && permissionRouters[index] && !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: [] }; - 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); + // 没找到第一级就生成一个 + 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 { + // 找到第一级,判断没有重复地址直接塞进去 + let nowIndex = permissionRouters.findIndex(e => { + return e.path === `/${str}`; + }); + if (nowIndex != -1 && permissionRouters[nowIndex] && !permissionRouters[nowIndex].children.some(e => e.path === obj.path)) { + permissionRouters[nowIndex].children.push(obj); + } } + // permissionRouters.push(obj) // path是页面,要塞入上一级同级里,需要单独push进去生成路由 + meta.push(i.name+"-"+j.name); + } else { + meta.push(i.name+"-"+j.name); } - // permissionRouters.push(obj) // path是页面,要塞入上一级同级里,需要单独push进去生成路由 - meta.push(i.name+"-"+j.name); - } else { - meta.push(i.name+"-"+j.name); - } - }) - } - }); + }) + } + }); - } - obj.meta.btn = meta; - obj.children = children; - // 处理好的路由,需要进行一次匹配,arr中必然存在对应的路由,所以在此处才生成第一级路由,免得额外判断是否有children - if (permissionRouters.length === 0 || !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); - // console.log(one,'当前的第一级别',obj,'第二级别') - } else { - // 找到第一级直接塞进去 - let nowIndex = permissionRouters.findIndex(e => { - return e.path === `/${str}`; - }); - if (nowIndex != -1) { - permissionRouters[nowIndex].children.push(obj); + } + obj.meta.btn = meta; + obj.children = children; + // 处理好的路由,需要进行一次匹配,arr中必然存在对应的路由,所以在此处才生成第一级路由,免得额外判断是否有children + if (permissionRouters.length === 0 || !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); + // console.log(one,'当前的第一级别',obj,'第二级别') + } else { + // 找到第一级直接塞进去 + let nowIndex = permissionRouters.findIndex(e => { + return e.path === `/${str}`; + }); + if (nowIndex != -1) { + permissionRouters[nowIndex].children.push(obj); + } } } }); @@ -191,6 +196,11 @@ function getPermission() { // 记录一下路由的名称,用于nav let nav = data.map(e => e.path); router.app.$options.store.commit("routers", nav); + let redirect = [{ + path: "/index", + redirect: permissionRouters[0].path + }]; + router.addRoutes(redirect); router.addRoutes(permissionRouters); router.addRoutes(errorPage); } diff --git a/src/router/routes.js b/src/router/routes.js index a1ffc1d..790529a 100644 --- a/src/router/routes.js +++ b/src/router/routes.js @@ -7,10 +7,6 @@ const frameIn = [ path: "/", redirect: "/login" }, - { - path: "/index", - redirect: "/student" - }, { path: "/", component: () => BasicLayout, @@ -44,28 +40,6 @@ const frameOut = [ } ]; -/** - * 错误页面 - */ - -const errorPage = [ - { - path: "/403", - name: "403", - meta: { - title: "403" - }, - component: () => import("@/pages/exception/error/403") - }, - { - path: "*", - name: "404", - meta: { - title: "404" - }, - component: () => import("@/pages/exception/error/404") - } -]; // 导出需要显示菜单的 export const frameInRoutes = frameIn; @@ -73,6 +47,5 @@ export const frameInRoutes = frameIn; // 重新组织后导出 export default [ ...frameIn, - ...frameOut, - // ...errorPage + ...frameOut ]; \ No newline at end of file