From 38afe6cbb9fa0d456b2f52880f62b6c7bdeea334 Mon Sep 17 00:00:00 2001 From: g7hoo Date: Thu, 25 Aug 2022 13:53:28 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E8=B7=AF=E7=94=B1=E9=9A=90=E8=97=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/router/index.js | 15 ++++----- src/utils/index.js | 1 + src/views/main-sidebar.vue | 35 +++++++++++++++++--- src/views/modules/sys/user-add-or-update.vue | 3 +- 4 files changed, 41 insertions(+), 13 deletions(-) diff --git a/src/router/index.js b/src/router/index.js index 016650d..5b19ff6 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -29,7 +29,7 @@ export const pageRoutes = [ meta: { title: '登录' }, beforeEnter(to, from, next) { if (Cookies.get('token')) { - Vue.prototype.$message({ message:'已经登录过了', type: 'error' }) + Vue.prototype.$message({ message: '已经登录过了', type: 'error' }) next(false) } else { next() @@ -91,6 +91,7 @@ router.beforeEach((to, from, next) => { if (window.SITE_CONFIG['dynamicMenuRoutesHasAdded'] || fnCurrentRouteIsPageRoute(to, pageRoutes)) { return next() } + // 获取字典列表, 添加并全局变量保存 http.get(http.adornUrl('/sys/dict/type/all')).then(({ data: res }) => { if (res.code !== 0) { @@ -98,20 +99,15 @@ router.beforeEach((to, from, next) => { } window.SITE_CONFIG['dictList'] = res.data }).catch((err) => { - // console.log("catch /sys/dict/type/all", err) }) // 获取菜单列表, 添加并全局变量保存 http.get(http.adornUrl('/sys/menu/nav')).then(({ data: res }) => { - if (res.code !== 0) { - Vue.prototype.$message.error(res.msg) - return next({ name: 'login' }) - } - console.log('menulist: ', res.data) + /** axios 的拦截器已经拦截出错情况,此处只考虑正确情况即可 */ window.SITE_CONFIG['menuList'] = res.data fnAddDynamicMenuRoutes(window.SITE_CONFIG['menuList']) next({ ...to, replace: true }) }).catch((err) => { - next({ name: 'login' }) + // Vue.prototype.$message.error(err) }) }) @@ -140,6 +136,7 @@ function fnCurrentRouteIsPageRoute(route, pageRoutes = []) { */ function fnAddDynamicMenuRoutes(menuList = [], routes = []) { var temp = [] + for (var i = 0; i < menuList.length; i++) { if (menuList[i].children && menuList[i].children.length >= 1) { temp = temp.concat(menuList[i].children) @@ -168,9 +165,11 @@ function fnAddDynamicMenuRoutes(menuList = [], routes = []) { } routes.push(route) } + if (temp.length >= 1) { return fnAddDynamicMenuRoutes(temp, routes) } + // 添加路由 router.addRoutes([ { diff --git a/src/utils/index.js b/src/utils/index.js index 50decdf..4d4fe20 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -48,6 +48,7 @@ export function clearLoginInfo () { store.commit('resetStore') Cookies.remove('token') window.SITE_CONFIG['dynamicMenuRoutesHasAdded'] = false + window.SITE_CONFIG['dynamicMenuRoutes'] = [] } /** diff --git a/src/views/main-sidebar.vue b/src/views/main-sidebar.vue index 6c3be5c..2441158 100644 --- a/src/views/main-sidebar.vue +++ b/src/views/main-sidebar.vue @@ -8,7 +8,7 @@ :collapseTransition="false" class="aui-sidebar__menu" > - + @@ -18,13 +18,40 @@ import SubMenu from './main-sidebar-sub-menu' export default { data() { - return {} + return { + unhiddenMenuList: [] + } }, components: { SubMenu }, - created() { - this.$store.state.sidebarMenuList = window.SITE_CONFIG['menuList'] + mounted() { + // this.$store.state.sidebarMenuList = window.SITE_CONFIG['menuList'] + this.$nextTick(() => { + this.unhiddenMenuList = this.getUnhiddenRoutesListFrom(window.SITE_CONFIG['menuList']) + /** 本地保存一份,store保存一份,感觉 store 都不需要保存... */ + this.$store.state.sidebarMenuList = this.unhiddenMenuList + }) + }, + methods: { + getUnhiddenRoutesListFrom(fullList) { + const list = [] + if (fullList.length) { + fullList.forEach(menu => { + if (menu.sort !== 99) { + /** 前后端约定,路由排序值为 99 时不在前端的侧边栏展示该路由 */ + const newRouteItem = JSON.parse(JSON.stringify(menu)) + if (menu.children) { + newRouteItem.children = this.getUnhiddenRoutesListFrom(menu.children) + } + list.push(newRouteItem) + } else { + console.log(menu.name, '是应该被隐藏的路由') + } + }) + } + return list + } } } diff --git a/src/views/modules/sys/user-add-or-update.vue b/src/views/modules/sys/user-add-or-update.vue index 02fb2d6..4b3192a 100644 --- a/src/views/modules/sys/user-add-or-update.vue +++ b/src/views/modules/sys/user-add-or-update.vue @@ -105,7 +105,8 @@ export default { confirmPassword: [{ validator: validateConfirmPassword, trigger: 'blur' }], realName: [{ required: true, message: this.$t('validate.required'), trigger: 'blur' }], email: [{ validator: validateEmail, trigger: 'blur' }], - mobile: [{ validator: validateMobile, trigger: 'blur' }] + mobile: [{ validator: validateMobile, trigger: 'blur' }], + roleIdList: [{ required: true, message: '至少选择一个角色', trigger: 'change' }] } } },