From 944e6d03444d202d81c90c075b3899ac035351d2 Mon Sep 17 00:00:00 2001 From: toesbieya <1647775459@qq.com> Date: Fri, 7 Aug 2020 23:32:15 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vue/src/layout/component/TagsView/index.vue | 24 ++++------ vue/src/router/util.js | 2 +- vue/src/store/module/resource.js | 53 +++++++++------------ vue/src/store/module/user.js | 2 +- vue/src/util/auth.js | 2 +- 5 files changed, 36 insertions(+), 47 deletions(-) diff --git a/vue/src/layout/component/TagsView/index.vue b/vue/src/layout/component/TagsView/index.vue index fb62d68..9f22efd 100644 --- a/vue/src/layout/component/TagsView/index.vue +++ b/vue/src/layout/component/TagsView/index.vue @@ -5,21 +5,20 @@ ref="tag" v-for="tag in visitedViews" :key="tag.path" - :class="{active:isActive(tag)}" - class="tags-view-item" + :class="{'tags-view-item': true, active: isActive(tag)}" :to="{ path: tag.path, query: tag.query, fullPath: tag.fullPath }" tag="div" - @contextmenu.prevent.native="e=>openMenu(tag,e)" - @dblclick.prevent.native="()=>closeSelectedTag(tag)" + @contextmenu.prevent.native="e => openMenu(tag, e)" + @dblclick.prevent.native="() => closeSelectedTag(tag)" > {{ tag.title }} - + - 刷新 - + 刷新 + 关闭 关闭其他 @@ -57,7 +56,6 @@ export default { visitedViews() { return this.$store.state.tagsView.visitedViews }, - routes() { return this.$store.state.resource.routes } @@ -68,7 +66,6 @@ export default { this.decideRouteTransition && this.decideRouteTransition(to, from) this.addTags(to).then(() => this.moveToCurrentTag()) }, - 'contextmenu.show'(v) { this.$emit('menu-show', v) } @@ -87,7 +84,7 @@ export default { routes.forEach(route => { if (route.name && route.meta && route.meta.affix) { tags.push({ - //注意,此处的fullPath并不是$route.fullPath + //注意,此处的fullPath并不是$route.fullPath,而是路由树拼接后的全路径 fullPath: route.fullPath, path: route.fullPath, name: route.name, @@ -103,7 +100,7 @@ export default { }, initTags() { this.affixTags = this.filterAffixTags(this.routes) - for (let tag of this.affixTags) { + for (const tag of this.affixTags) { this.$store.commit('tagsView/addVisitedView', tag) } }, @@ -117,8 +114,7 @@ export default { moveToCurrentTag() { this.$nextTick(() => { const tag = this.$refs.tag.find(i => i.to.path === this.$route.path) - if (!tag) return - this.$refs.scrollPane.moveToTarget(tag) + tag && this.$refs.scrollPane.moveToTarget(tag) }) }, @@ -165,9 +161,9 @@ export default { const left = e.clientX - offsetLeft + 15 // 15: margin right this.contextmenu.left = left > maxLeft ? maxLeft : left - this.contextmenu.top = e.clientY this.contextmenu.show = true + this.selectedTag = tag } }, diff --git a/vue/src/router/util.js b/vue/src/router/util.js index f953f6f..434b38e 100644 --- a/vue/src/router/util.js +++ b/vue/src/router/util.js @@ -59,7 +59,7 @@ export function needExtraRedirect(to, from) { //初始化菜单和权限 export function initMenuAndResource() { - if (!store.state.resource.hasInitRoutes) { + if (!store.state.resource.init) { return store.dispatch('resource/init', store.state.user) } return Promise.resolve() diff --git a/vue/src/store/module/resource.js b/vue/src/store/module/resource.js index 41e5160..561b4d2 100644 --- a/vue/src/store/module/resource.js +++ b/vue/src/store/module/resource.js @@ -17,7 +17,7 @@ const state = { sidebarMenus: [], dataMap: {}, tree: [], - hasInitRoutes: false + init: false } const mutations = { @@ -42,8 +42,8 @@ const mutations = { }, {}) state.tree = createTree(data.filter(resource => resource.admin === false)) }, - hasInitRoutes(state, sign) { - state.hasInitRoutes = sign + setInit(state, sign) { + state.init = sign } } @@ -59,7 +59,7 @@ const actions = { return new Promise(resolve => { const accessedRoutes = getAuthorizedRoutes({resources, admin}) commit('routes', accessedRoutes) - commit('hasInitRoutes', true) + commit('setInit', true) resolve() }) }, @@ -77,9 +77,7 @@ const actions = { //在原始路由数组基础上添加全路径 function transformOriginRoutes(routes) { const res = JSON.parse(JSON.stringify(routes)) - addFullPath(res) - return res } @@ -108,7 +106,7 @@ function clean(routes, cleanHidden = true) { //路由添加全路径 function addFullPath(routes, basePath = '/') { routes.forEach(route => { - delete route.components + delete route.component route.fullPath = isExternal(route.path) ? route.path : path.resolve(basePath, route.path) route.children && addFullPath(route.children, route.fullPath) }) @@ -118,52 +116,47 @@ function addFullPath(routes, basePath = '/') { function getAuthorizedRoutes({resources, admin}) { if (admin === true) return finalAuthorityRoutes if (!resources) return [] - const arr = JSON.parse(JSON.stringify(finalAuthorityRoutes)) - filter(arr, i => !needAuth(i) || i.fullPath in resources) - return arr + filter(finalAuthorityRoutes, i => !needAuth(i) || i.fullPath in resources) + return finalAuthorityRoutes } //若没有children且未通过,则删除,若有,当children长度为0时删除 function filter(arr, fun) { - for (let i = 0; i < arr.length; i++) { - if (!arr[i].children && !fun(arr[i])) { - arr.splice(i, 1) - i-- + for (let i = arr.length - 1; i >= 0; i--) { + const {children} = arr[i] + + if (!children) { + !fun(arr[i]) && arr.splice(i, 1) continue } - if (!arr[i].children) continue + filter(children, fun) - filter(arr[i].children, fun) - - if (arr[i].children.length <= 0) { - arr.splice(i, 1) - i-- - } + children.length <= 0 && arr.splice(i, 1) } } //菜单排序 -function sort(routes, getSortValue = defaultGetSortValue) { +function sort(routes) { routes.sort((pre, next) => { - const preSort = getSortValue(pre), - nextSort = getSortValue(next) - if (preSort < nextSort) return -1 - else if (preSort === nextSort) return 0 + pre = getSortValue(pre) + next = getSortValue(next) + if (pre < next) return -1 + else if (pre === next) return 0 else return 1 }) routes.forEach(route => { - if (route.children && route.children.length > 1) { - sort(route.children, getSortValue) + const {children} = route + if (children && children.length) { + sort(children) } }) } -const defaultGetSortValue = item => { +const getSortValue = item => { const {meta: {sort} = {}} = deepTap(item) || {} return isEmpty(sort) ? 10000 : sort } - const deepTap = item => { const {name, children, meta: {title, hidden, sort} = {}} = item if (hidden) return null diff --git a/vue/src/store/module/user.js b/vue/src/store/module/user.js index d130619..d676625 100644 --- a/vue/src/store/module/user.js +++ b/vue/src/store/module/user.js @@ -46,7 +46,7 @@ const actions = { commit('prepareLogout', 'yes') logout(state.token) .then(() => { - commit('resource/hasInitRoutes', false, {root: true}) + commit('resource/setInit', false, {root: true}) return Promise.all([ dispatch('socket/close', null, {root: true}), dispatch('removeUser'), diff --git a/vue/src/util/auth.js b/vue/src/util/auth.js index 66300d6..e4b6e72 100644 --- a/vue/src/util/auth.js +++ b/vue/src/util/auth.js @@ -2,7 +2,7 @@ import store from '@/store' export function needAuth(route) { if (route.path.startsWith('/redirect')) return false - return !(route.meta && (!('noAuth' in route) || route.meta.noAuth)) + return !route.meta || !route.meta.noAuth } export function auth(path) {