简化代码

master
toesbieya 6 years ago
parent 09d7ce4b45
commit 944e6d0344
  1. 24
      vue/src/layout/component/TagsView/index.vue
  2. 2
      vue/src/router/util.js
  3. 53
      vue/src/store/module/resource.js
  4. 2
      vue/src/store/module/user.js
  5. 2
      vue/src/util/auth.js

@ -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 }}
<i v-if="!isAffix(tag)" class="el-icon-close" @click.prevent.stop="()=>closeSelectedTag(tag)"/>
<i v-if="!isAffix(tag)" class="el-icon-close" @click.prevent.stop="() => closeSelectedTag(tag)"/>
</router-link>
</scroll-pane>
<context-menu v-model="contextmenu.show" :left="contextmenu.left" :top="contextmenu.top">
<context-menu-item @click="refreshSelectedTag(selectedTag)">刷新</context-menu-item>
<context-menu-item v-show="!isAffix(selectedTag)" @click="()=>closeSelectedTag(selectedTag)">
<context-menu-item @click="() => refreshSelectedTag(selectedTag)">刷新</context-menu-item>
<context-menu-item v-show="!isAffix(selectedTag)" @click="() => closeSelectedTag(selectedTag)">
关闭
</context-menu-item>
<context-menu-item @click="closeOthersTags">关闭其他</context-menu-item>
@ -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
}
},

@ -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()

@ -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

@ -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'),

@ -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) {

Loading…
Cancel
Save