Commit afca2e1f by xinla

路由问题修复及优化

1 parent 0c1804a8
...@@ -16,19 +16,19 @@ ...@@ -16,19 +16,19 @@
</template> </template>
<script> <script>
import { constantRoutes } from '@/router' import { constantRoutes } from '@/router'
import { isExternal } from '@/utils/validate' import { isExternal } from '@/utils/validate'
export default { export default {
data() { data() {
return { return {
name: '', // name: '',
// 顶部栏初始数 // 顶部栏初始数
visibleNumber: 5, visibleNumber: 5,
// 是否为首次加载 // 是否为首次加载
isFrist: false, isFrist: false,
// 当前激活菜单的 // 当前激活菜单的
currentIndex: '' currentIndex: '',
} }
}, },
computed: { computed: {
...@@ -50,52 +50,22 @@ ...@@ -50,52 +50,22 @@
}) })
return topMenus return topMenus
}, },
// 所有的路由信息
routers() { routers() {
return this.$store.state.permission.topbarRouters return this.$store.state.permission.topbarRouters
}, },
// 设置子路由 // 默认激活的一级菜单路径 eg:/drainageCode
childrenMenus() {
var childrenMenus = []
this.routers.map((router) => {
for (var item in router.children) {
if (router.children[item].parentPath === undefined) {
if (router.path === '/') {
router.children[item].path = router.children[item].path
} else {
if (!this.ishttp(router.children[item].path)) {
router.children[item].path = router.path + '/' + router.children[item].path
}
}
router.children[item].parentPath = router.path
}
childrenMenus.push(router.children[item])
}
})
return constantRoutes.concat(childrenMenus)
},
// 默认激活的菜单
activeMenu() { activeMenu() {
const path = this.$route.path const path = this.$route.path
let activePath = this.defaultRouter() let activePath = this.routers.filter((e) => !e.hidden).path
if (path.lastIndexOf('/') > 0) { if (path.lastIndexOf('/') > 0) {
const tmpPath = path.substring(1, path.length) activePath = path.substring(0, path.indexOf('/', 1))
activePath = '/' + tmpPath.substring(0, tmpPath.indexOf('/'))
} }
// else if ('/index' == path || '' == path) {
// if (!this.isFrist) {
// this.isFrist = true
// activePath = '/index'
// } else {
// activePath = '/index'
// }
// }
var routes = this.activeRoutes(activePath) var routes = this.activeRoutes(activePath)
if (routes.length === 0) { if (routes.length === 0) {
activePath = path activePath = path
} }
return activePath return activePath
} },
}, },
beforeMount() { beforeMount() {
window.addEventListener('resize', this.setVisibleNumber) window.addEventListener('resize', this.setVisibleNumber)
...@@ -105,40 +75,29 @@ ...@@ -105,40 +75,29 @@
}, },
mounted() { mounted() {
this.setVisibleNumber() this.setVisibleNumber()
this.setName() // this.setName()
// this.handleSelect(this.topMenus[0].path) // this.handleSelect(this.topMenus[0].path)
}, },
methods: { methods: {
setName() { // setName() {
this.topMenus.map((dd) => { // this.topMenus.map((dd) => {
if (this.activeMenu == dd.path) { // if (this.activeMenu == dd.path) {
this.name = dd.meta.title // this.name = dd.meta.title
} // }
}) // })
}, // },
// 根据宽度计算设置显示栏数 // 根据宽度计算设置显示栏数
setVisibleNumber() { setVisibleNumber() {
const width = document.body.getBoundingClientRect().width / 3 const width = document.body.getBoundingClientRect().width / 3
this.visibleNumber = parseInt(width / 85) this.visibleNumber = parseInt(width / 85)
}, },
// 默认激活的路由
defaultRouter() {
let router
Object.keys(this.routers).some((key) => {
if (!this.routers[key].hidden) {
router = this.routers[key].path
return true
}
})
return router
},
// 菜单选择事件 // 菜单选择事件
handleSelect(path, keyPath) { handleSelect(path, keyPath) {
this.currentIndex = path this.currentIndex = path
let index = '' let index = ''
this.topMenus.map((dd, cc) => { this.topMenus.map((dd, cc) => {
if (path == dd.path) { if (path == dd.path) {
this.name = dd.meta.title // this.name = dd.meta.title
index = cc index = cc
} }
}) })
...@@ -149,81 +108,60 @@ ...@@ -149,81 +108,60 @@
// /redirect 路径内部打开 // /redirect 路径内部打开
this.$router.push({ path: path.replace('/redirect', '') }) this.$router.push({ path: path.replace('/redirect', '') })
} else { } else {
// 显示左侧联动菜单
// if (path === '/index') {
// this.$router.push(path)
// } else {
// }
this.activeRoutes(path) this.activeRoutes(path)
let obj = this.topMenus[index]
// let path = this.topMenus[index].children[0].path
// console.log(this.topMenus[index].children[0])
let path = ''
if (obj.children && obj.children.length) {
path = path + obj.children[0].path
}
if (obj.children[0].children && obj.children[0].children.length) {
path = path + '/' + obj.children[0].children[0].path
}
if (!isExternal(path)) { if (!isExternal(path)) {
this.$router.push({ path }) this.$router.push({ path })
} }
} }
}, },
// 当前激活的路由 // 当前激活的路由
activeRoutes(key) { activeRoutes(path) {
var routes = [] let routes = this.routers.filter((e) => e.path == path)
if (this.childrenMenus && this.childrenMenus.length > 0) {
this.childrenMenus.map((item) => {
if (key == item.parentPath) {
routes.push(item)
}
})
}
if (routes.length > 0) { if (routes.length > 0) {
this.$store.commit('SET_SIDEBAR_ROUTERS', routes) this.$store.commit('SET_SIDEBAR_ROUTERS', routes)
} }
return routes return routes
}, },
ishttp(url) { ishttp(url) {
return url.indexOf('http://') !== -1 || url.indexOf('https://') !== -1 return /^(\/|http)/.test(url)
} },
} },
} }
</script> </script>
<style lang="scss"> <style lang="scss">
.el-menu { .el-menu {
background-color: transparent !important; background-color: transparent !important;
} }
.el-menu.el-menu--horizontal { .el-menu.el-menu--horizontal {
border: none !important; border: none !important;
} }
.el-menu--horizontal > .el-menu-item { .el-menu--horizontal > .el-menu-item {
float: left; float: left;
height: 50px !important; height: 50px !important;
line-height: 50px !important; line-height: 50px !important;
color: #fff !important; color: #fff !important;
// padding: 0 5px !important; // padding: 0 5px !important;
margin: 0 10px !important; margin: 0 10px !important;
} }
.el-menu-item:hover { .el-menu-item:hover {
background-color: transparent !important; background-color: transparent !important;
} }
.el-menu--horizontal > .el-menu-item.is-active, .el-menu--horizontal > .el-menu-item.is-active,
.el-menu--horizontal > .el-submenu.is-active .el-submenu__title { .el-menu--horizontal > .el-submenu.is-active .el-submenu__title {
border-bottom: 2px solid #fff !important; border-bottom: 2px solid #fff !important;
// color: #303133; // color: #303133;
} }
/* submenu item */ /* submenu item */
.el-menu--horizontal > .el-submenu .el-submenu__title { .el-menu--horizontal > .el-submenu .el-submenu__title {
float: left; float: left;
height: 50px !important; height: 50px !important;
line-height: 50px !important; line-height: 50px !important;
color: #999093 !important; color: #999093 !important;
padding: 0 5px !important; padding: 0 5px !important;
margin: 0 10px !important; margin: 0 10px !important;
} }
</style> </style>
...@@ -30,8 +30,12 @@ import visitor from '@/layout/visitor' ...@@ -30,8 +30,12 @@ import visitor from '@/layout/visitor'
} }
*/ */
// 开发路由,总后台配置完菜单务必删除或注释,只在开发环境生效 export let devRoutes = []
const devRoutes = []
// 开发环境路由,总后台配置完菜单务必删除或注释,只在开发环境生效
if (process.env.VUE_APP_ENV === 'development') {
devRoutes = devRoutes.concat()
}
// 公共路由 // 公共路由
export const constantRoutes = [ export const constantRoutes = [
...@@ -44,26 +48,26 @@ export const constantRoutes = [ ...@@ -44,26 +48,26 @@ export const constantRoutes = [
isNoLogin: true, isNoLogin: true,
}, },
}, },
{ // {
path: '/drainageCode/qrCode', // path: '',
component: Layout, // component: Layout,
hidden: true, // hidden: true,
meta: { // meta: {
title: '客群活码', // title: '客群活码',
}, // },
children: [ // children: [
{ // {
path: 'analyse', // path: '/drainageCode/qrCode/analyse',
name: 'group-code-analyse', // name: 'group-code-analyse',
component: (resolve) => require(['@/views/drainageCode/group/analyse'], resolve), // component: (resolve) => require(['@/views/drainageCode/group/analyse'], resolve),
meta: { // meta: {
title: '活码统计', // title: '活码统计',
activeMenu: '/drainageCode/qrCode/customerGroup', // activeMenu: '/drainageCode/qrCode/customerGroup',
breadcrumb: true, // breadcrumb: true,
}, // },
}, // },
], // ],
}, // },
{ {
path: '/redirect', path: '/redirect',
component: Layout, component: Layout,
......
const getters = { const getters = {
sidebar: state => state.app.sidebar, sidebar: (state) => state.app.sidebar,
size: state => state.app.size, size: (state) => state.app.size,
device: state => state.app.device, device: (state) => state.app.device,
visitedViews: state => state.tagsView.visitedViews, visitedViews: (state) => state.tagsView.visitedViews,
cachedViews: state => state.tagsView.cachedViews, cachedViews: (state) => state.tagsView.cachedViews,
token: state => state.user.token, token: (state) => state.user.token,
avatar: state => state.user.avatar, avatar: (state) => state.user.avatar,
name: state => state.user.name, name: (state) => state.user.name,
introduction: state => state.user.introduction, introduction: (state) => state.user.introduction,
roles: state => state.user.roles, roles: (state) => state.user.roles,
permissions: state => state.user.permissions, permissions: (state) => state.user.permissions,
configParamInfo: state => state.user.configParamInfo, configParamInfo: (state) => state.user.configParamInfo,
permission_routes: state => state.permission.routes, permission_routes: (state) => state.permission.routes,
topbarRouters: state => state.permission.topbarRouters, topbarRouters: (state) => state.permission.topbarRouters,
defaultRoutes: state => state.permission.defaultRoutes, sidebarRouters: (state) => state.permission.sidebarRouters,
sidebarRouters: state => state.permission.sidebarRouters,
} }
export default getters export default getters
import router, { constantRoutes } from '@/router' import router, { constantRoutes, devRoutes } from '@/router'
import { getRouters } from '@/api/menu' import { getRouters } from '@/api/menu'
import Layout from '@/layout/index' import Layout from '@/layout/index'
import View from '@/layout/components/View' import View from '@/layout/components/View'
...@@ -8,9 +8,8 @@ const permission = { ...@@ -8,9 +8,8 @@ const permission = {
state: { state: {
routes: [], routes: [],
addRoutes: [], addRoutes: [],
defaultRoutes: [],
topbarRouters: [], topbarRouters: [],
sidebarRouters: [], sidebarRouters: [], // 当前显示的侧边栏菜单,通过topbarRouters筛选当前激活的顶部菜单下的子菜单
}, },
mutations: { mutations: {
SET_ROUTES: (state, routes) => { SET_ROUTES: (state, routes) => {
...@@ -19,17 +18,8 @@ const permission = { ...@@ -19,17 +18,8 @@ const permission = {
// 根据roles权限生成可访问的路由表 // 根据roles权限生成可访问的路由表
routes.forEach((e) => router.addRoute(e)) routes.forEach((e) => router.addRoute(e))
}, },
SET_DEFAULT_ROUTES: (state, routes) => {
state.defaultRoutes = constantRoutes.concat(routes)
},
SET_TOPBAR_ROUTES: (state, routes) => { SET_TOPBAR_ROUTES: (state, routes) => {
// 顶部导航菜单默认添加统计报表栏指向首页 state.topbarRouters = routes.concat(devRoutes)
// const index = [{
// path: 'index',
// meta: { title: '统计报表', icon: 'dashboard' }
// }]
// state.topbarRouters = routes.concat(index);
state.topbarRouters = routes
}, },
SET_SIDEBAR_ROUTERS: (state, routes) => { SET_SIDEBAR_ROUTERS: (state, routes) => {
state.sidebarRouters = routes state.sidebarRouters = routes
...@@ -42,11 +32,6 @@ const permission = { ...@@ -42,11 +32,6 @@ const permission = {
// 向后端请求路由数据 // 向后端请求路由数据
getRouters() getRouters()
.then(({ data }) => { .then(({ data }) => {
// const accessedRoutes = filterAsyncRouter(data)
// // 设置为匹配路由重定向404
// accessedRoutes.push({ path: '*', redirect: '/404', hidden: true })
// commit('SET_ROUTES', accessedRoutes)
// resolve(accessedRoutes)
if (!data || !data.length) { if (!data || !data.length) {
MessageBox.confirm('您的角色菜单权限不足,请联系所在企业管理员', '系统提示', { MessageBox.confirm('您的角色菜单权限不足,请联系所在企业管理员', '系统提示', {
confirmButtonText: '重新登录', confirmButtonText: '重新登录',
...@@ -59,10 +44,8 @@ const permission = { ...@@ -59,10 +44,8 @@ const permission = {
}) })
return reject() return reject()
} }
const sdata = JSON.parse(JSON.stringify(data))
const rdata = JSON.parse(JSON.stringify(data)) const rdata = JSON.parse(JSON.stringify(data))
const sidebarRoutes = filterAsyncRouter(sdata) const rewriteRoutes = filterAsyncRouter(rdata)
const rewriteRoutes = filterAsyncRouter(rdata, false, true)
// // 设置首页重定向 // // 设置首页重定向
let indexRoute = router.getRoutes().find((e) => e.path == '') let indexRoute = router.getRoutes().find((e) => e.path == '')
...@@ -70,9 +53,7 @@ const permission = { ...@@ -70,9 +53,7 @@ const permission = {
rewriteRoutes.push({ path: '*', redirect: '/404', hidden: true }) rewriteRoutes.push({ path: '*', redirect: '/404', hidden: true })
commit('SET_ROUTES', rewriteRoutes) commit('SET_ROUTES', rewriteRoutes)
commit('SET_SIDEBAR_ROUTERS', constantRoutes.concat(sidebarRoutes)) commit('SET_TOPBAR_ROUTES', rewriteRoutes)
commit('SET_DEFAULT_ROUTES', sidebarRoutes)
commit('SET_TOPBAR_ROUTES', sidebarRoutes)
resolve(rewriteRoutes) resolve(rewriteRoutes)
}) })
.catch((error) => { .catch((error) => {
...@@ -84,30 +65,6 @@ const permission = { ...@@ -84,30 +65,6 @@ const permission = {
}, },
} }
function filterChildren(childrenMap, lastRouter = false) {
var children = []
childrenMap.forEach((el, index) => {
if (el.children && el.children.length) {
if (el.component === 'ParentView' && !lastRouter) {
el.children.forEach((c) => {
c.path = el.path + '/' + c.path
if (c.children && c.children.length) {
children = children.concat(filterChildren(c.children, c))
return
}
children.push(c)
})
return
}
}
if (lastRouter) {
el.path = lastRouter.path + '/' + el.path
}
children = children.concat(el)
})
return children
}
// export const loadView = (view) => { // export const loadView = (view) => {
// if (process.env.NODE_ENV === 'development') { // if (process.env.NODE_ENV === 'development') {
// return (resolve) => require([`@/views/${view}`], resolve) // return (resolve) => require([`@/views/${view}`], resolve)
...@@ -119,20 +76,24 @@ function filterChildren(childrenMap, lastRouter = false) { ...@@ -119,20 +76,24 @@ function filterChildren(childrenMap, lastRouter = false) {
// // 遍历后台传来的路由字符串,转换为组件对象 // // 遍历后台传来的路由字符串,转换为组件对象
let level = 0 let level = 0
function filterAsyncRouter(asyncRouterMap) { function filterAsyncRouter(asyncRouterMap, parentPath) {
level++ level++
let res = asyncRouterMap.filter((route) => { let res = asyncRouterMap.filter((route) => {
route.meta = route.meta || {} route.meta = route.meta || {}
route.meta.hidden = route.hidden route.meta.hidden = route.hidden
if (parentPath) {
let path = route.path
route.path = /^(\/|http)/.test(path) ? path : parentPath + '/' + path
}
if (route.component) { if (route.component) {
// Layout组件特殊处理 // Layout组件特殊处理
if (route.component === 'Layout') {
if (route.redirect === 'noRedirect' && route.children.length) { if (route.redirect === 'noRedirect' && route.children.length) {
let firstChildPath = route.children[0].path let firstChildPath = route.children[0].path
route.redirect = firstChildPath.startsWith('/') route.redirect = firstChildPath.startsWith('/') ? firstChildPath : route.path + '/' + firstChildPath
? firstChildPath
: route.path + '/' + firstChildPath
} }
if (route.component === 'Layout') {
if (level != 1) { if (level != 1) {
route.component = View route.component = View
} else { } else {
...@@ -142,8 +103,9 @@ function filterAsyncRouter(asyncRouterMap) { ...@@ -142,8 +103,9 @@ function filterAsyncRouter(asyncRouterMap) {
route.component = loadView(route.component) route.component = loadView(route.component)
} }
} }
if (route.children != null && route.children && route.children.length) { let children = route.children
route.children = filterAsyncRouter(route.children) if (children && children.length) {
route.children = filterAsyncRouter(route.children, route.path)
} }
return true return true
}) })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!