简化代码

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" ref="tag"
v-for="tag in visitedViews" v-for="tag in visitedViews"
:key="tag.path" :key="tag.path"
:class="{active:isActive(tag)}" :class="{'tags-view-item': true, active: isActive(tag)}"
class="tags-view-item"
:to="{ path: tag.path, query: tag.query, fullPath: tag.fullPath }" :to="{ path: tag.path, query: tag.query, fullPath: tag.fullPath }"
tag="div" tag="div"
@contextmenu.prevent.native="e=>openMenu(tag,e)" @contextmenu.prevent.native="e => openMenu(tag, e)"
@dblclick.prevent.native="()=>closeSelectedTag(tag)" @dblclick.prevent.native="() => closeSelectedTag(tag)"
> >
{{ tag.title }} {{ 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> </router-link>
</scroll-pane> </scroll-pane>
<context-menu v-model="contextmenu.show" :left="contextmenu.left" :top="contextmenu.top"> <context-menu v-model="contextmenu.show" :left="contextmenu.left" :top="contextmenu.top">
<context-menu-item @click="refreshSelectedTag(selectedTag)">刷新</context-menu-item> <context-menu-item @click="() => refreshSelectedTag(selectedTag)">刷新</context-menu-item>
<context-menu-item v-show="!isAffix(selectedTag)" @click="()=>closeSelectedTag(selectedTag)"> <context-menu-item v-show="!isAffix(selectedTag)" @click="() => closeSelectedTag(selectedTag)">
关闭 关闭
</context-menu-item> </context-menu-item>
<context-menu-item @click="closeOthersTags">关闭其他</context-menu-item> <context-menu-item @click="closeOthersTags">关闭其他</context-menu-item>
@ -57,7 +56,6 @@ export default {
visitedViews() { visitedViews() {
return this.$store.state.tagsView.visitedViews return this.$store.state.tagsView.visitedViews
}, },
routes() { routes() {
return this.$store.state.resource.routes return this.$store.state.resource.routes
} }
@ -68,7 +66,6 @@ export default {
this.decideRouteTransition && this.decideRouteTransition(to, from) this.decideRouteTransition && this.decideRouteTransition(to, from)
this.addTags(to).then(() => this.moveToCurrentTag()) this.addTags(to).then(() => this.moveToCurrentTag())
}, },
'contextmenu.show'(v) { 'contextmenu.show'(v) {
this.$emit('menu-show', v) this.$emit('menu-show', v)
} }
@ -87,7 +84,7 @@ export default {
routes.forEach(route => { routes.forEach(route => {
if (route.name && route.meta && route.meta.affix) { if (route.name && route.meta && route.meta.affix) {
tags.push({ tags.push({
//fullPath$route.fullPath //fullPath$route.fullPath
fullPath: route.fullPath, fullPath: route.fullPath,
path: route.fullPath, path: route.fullPath,
name: route.name, name: route.name,
@ -103,7 +100,7 @@ export default {
}, },
initTags() { initTags() {
this.affixTags = this.filterAffixTags(this.routes) this.affixTags = this.filterAffixTags(this.routes)
for (let tag of this.affixTags) { for (const tag of this.affixTags) {
this.$store.commit('tagsView/addVisitedView', tag) this.$store.commit('tagsView/addVisitedView', tag)
} }
}, },
@ -117,8 +114,7 @@ export default {
moveToCurrentTag() { moveToCurrentTag() {
this.$nextTick(() => { this.$nextTick(() => {
const tag = this.$refs.tag.find(i => i.to.path === this.$route.path) const tag = this.$refs.tag.find(i => i.to.path === this.$route.path)
if (!tag) return tag && this.$refs.scrollPane.moveToTarget(tag)
this.$refs.scrollPane.moveToTarget(tag)
}) })
}, },
@ -165,9 +161,9 @@ export default {
const left = e.clientX - offsetLeft + 15 // 15: margin right const left = e.clientX - offsetLeft + 15 // 15: margin right
this.contextmenu.left = left > maxLeft ? maxLeft : left this.contextmenu.left = left > maxLeft ? maxLeft : left
this.contextmenu.top = e.clientY this.contextmenu.top = e.clientY
this.contextmenu.show = true this.contextmenu.show = true
this.selectedTag = tag this.selectedTag = tag
} }
}, },

@ -59,7 +59,7 @@ export function needExtraRedirect(to, from) {
//初始化菜单和权限 //初始化菜单和权限
export function initMenuAndResource() { export function initMenuAndResource() {
if (!store.state.resource.hasInitRoutes) { if (!store.state.resource.init) {
return store.dispatch('resource/init', store.state.user) return store.dispatch('resource/init', store.state.user)
} }
return Promise.resolve() return Promise.resolve()

@ -17,7 +17,7 @@ const state = {
sidebarMenus: [], sidebarMenus: [],
dataMap: {}, dataMap: {},
tree: [], tree: [],
hasInitRoutes: false init: false
} }
const mutations = { const mutations = {
@ -42,8 +42,8 @@ const mutations = {
}, {}) }, {})
state.tree = createTree(data.filter(resource => resource.admin === false)) state.tree = createTree(data.filter(resource => resource.admin === false))
}, },
hasInitRoutes(state, sign) { setInit(state, sign) {
state.hasInitRoutes = sign state.init = sign
} }
} }
@ -59,7 +59,7 @@ const actions = {
return new Promise(resolve => { return new Promise(resolve => {
const accessedRoutes = getAuthorizedRoutes({resources, admin}) const accessedRoutes = getAuthorizedRoutes({resources, admin})
commit('routes', accessedRoutes) commit('routes', accessedRoutes)
commit('hasInitRoutes', true) commit('setInit', true)
resolve() resolve()
}) })
}, },
@ -77,9 +77,7 @@ const actions = {
//在原始路由数组基础上添加全路径 //在原始路由数组基础上添加全路径
function transformOriginRoutes(routes) { function transformOriginRoutes(routes) {
const res = JSON.parse(JSON.stringify(routes)) const res = JSON.parse(JSON.stringify(routes))
addFullPath(res) addFullPath(res)
return res return res
} }
@ -108,7 +106,7 @@ function clean(routes, cleanHidden = true) {
//路由添加全路径 //路由添加全路径
function addFullPath(routes, basePath = '/') { function addFullPath(routes, basePath = '/') {
routes.forEach(route => { routes.forEach(route => {
delete route.components delete route.component
route.fullPath = isExternal(route.path) ? route.path : path.resolve(basePath, route.path) route.fullPath = isExternal(route.path) ? route.path : path.resolve(basePath, route.path)
route.children && addFullPath(route.children, route.fullPath) route.children && addFullPath(route.children, route.fullPath)
}) })
@ -118,52 +116,47 @@ function addFullPath(routes, basePath = '/') {
function getAuthorizedRoutes({resources, admin}) { function getAuthorizedRoutes({resources, admin}) {
if (admin === true) return finalAuthorityRoutes if (admin === true) return finalAuthorityRoutes
if (!resources) return [] if (!resources) return []
const arr = JSON.parse(JSON.stringify(finalAuthorityRoutes)) filter(finalAuthorityRoutes, i => !needAuth(i) || i.fullPath in resources)
filter(arr, i => !needAuth(i) || i.fullPath in resources) return finalAuthorityRoutes
return arr
} }
//若没有children且未通过,则删除,若有,当children长度为0时删除 //若没有children且未通过,则删除,若有,当children长度为0时删除
function filter(arr, fun) { function filter(arr, fun) {
for (let i = 0; i < arr.length; i++) { for (let i = arr.length - 1; i >= 0; i--) {
if (!arr[i].children && !fun(arr[i])) { const {children} = arr[i]
arr.splice(i, 1)
i-- if (!children) {
!fun(arr[i]) && arr.splice(i, 1)
continue continue
} }
if (!arr[i].children) continue filter(children, fun)
filter(arr[i].children, fun) children.length <= 0 && arr.splice(i, 1)
if (arr[i].children.length <= 0) {
arr.splice(i, 1)
i--
}
} }
} }
//菜单排序 //菜单排序
function sort(routes, getSortValue = defaultGetSortValue) { function sort(routes) {
routes.sort((pre, next) => { routes.sort((pre, next) => {
const preSort = getSortValue(pre), pre = getSortValue(pre)
nextSort = getSortValue(next) next = getSortValue(next)
if (preSort < nextSort) return -1 if (pre < next) return -1
else if (preSort === nextSort) return 0 else if (pre === next) return 0
else return 1 else return 1
}) })
routes.forEach(route => { routes.forEach(route => {
if (route.children && route.children.length > 1) { const {children} = route
sort(route.children, getSortValue) if (children && children.length) {
sort(children)
} }
}) })
} }
const defaultGetSortValue = item => { const getSortValue = item => {
const {meta: {sort} = {}} = deepTap(item) || {} const {meta: {sort} = {}} = deepTap(item) || {}
return isEmpty(sort) ? 10000 : sort return isEmpty(sort) ? 10000 : sort
} }
const deepTap = item => { const deepTap = item => {
const {name, children, meta: {title, hidden, sort} = {}} = item const {name, children, meta: {title, hidden, sort} = {}} = item
if (hidden) return null if (hidden) return null

@ -46,7 +46,7 @@ const actions = {
commit('prepareLogout', 'yes') commit('prepareLogout', 'yes')
logout(state.token) logout(state.token)
.then(() => { .then(() => {
commit('resource/hasInitRoutes', false, {root: true}) commit('resource/setInit', false, {root: true})
return Promise.all([ return Promise.all([
dispatch('socket/close', null, {root: true}), dispatch('socket/close', null, {root: true}),
dispatch('removeUser'), dispatch('removeUser'),

@ -2,7 +2,7 @@ import store from '@/store'
export function needAuth(route) { export function needAuth(route) {
if (route.path.startsWith('/redirect')) return false 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) { export function auth(path) {

Loading…
Cancel
Save