路由模块扁平化

UP
This commit is contained in:
sc 2021-10-16 17:08:20 +08:00
parent a7a472cd06
commit 984fb1bb08
11 changed files with 97 additions and 70 deletions

View File

@ -4,7 +4,7 @@ import http from "@/utils/request"
export default { export default {
menu: { menu: {
myMenus: { myMenus: {
url: `${config.API_URL}/system/menu/my/1.2.8`, url: `${config.API_URL}/system/menu/my/dev`,
name: "获取我的菜单", name: "获取我的菜单",
get: async function(){ get: async function(){
return await http.get(this.url); return await http.get(this.url);

View File

@ -2,6 +2,9 @@ const DEFAULT_CONFIG = {
//标题 //标题
APP_NAME: "SCUI", APP_NAME: "SCUI",
//首页地址
DASHBOARD_URL: "/dashboard",
//版本号 //版本号
APP_VER: "1.2.7", APP_VER: "1.2.7",

View File

@ -67,16 +67,28 @@
} }
}, },
created() { created() {
const dashboardRoute = this.$router.options.routes[0].children[0].children[0] var menu = this.$TOOL.data.get("MENU")
var dashboardRoute = this.treeFind(menu, node => node.path==this.$CONFIG.DASHBOARD_URL)
dashboardRoute.fullPath = dashboardRoute.path dashboardRoute.fullPath = dashboardRoute.path
this.addViewTags(dashboardRoute); this.addViewTags(dashboardRoute)
this.addViewTags(this.$route); this.addViewTags(this.$route)
}, },
mounted() { mounted() {
this.tagDrop(); this.tagDrop();
this.scrollInit() this.scrollInit()
}, },
methods: { methods: {
//
treeFind(tree, func){
for (const data of tree) {
if (func(data)) return data
if (data.children) {
const res = this.treeFind(data.children, func)
if (res) return res
}
}
return null
},
// //
tagDrop(){ tagDrop(){
const target = this.$refs.tags const target = this.$refs.tags

View File

@ -6,7 +6,7 @@
</div> </div>
<el-breadcrumb separator-class="el-icon-arrow-right" class="hidden-sm-and-down"> <el-breadcrumb separator-class="el-icon-arrow-right" class="hidden-sm-and-down">
<transition-group name="breadcrumb" mode="out-in"> <transition-group name="breadcrumb" mode="out-in">
<template v-for="item in breadList" :key="item.meta.title" > <template v-for="item in breadList" :key="item.title" >
<el-breadcrumb-item v-if="item.path!='/' && !item.meta.hiddenBreadcrumb" :key="item.meta.title">{{item.meta.title}}</el-breadcrumb-item> <el-breadcrumb-item v-if="item.path!='/' && !item.meta.hiddenBreadcrumb" :key="item.meta.title">{{item.meta.title}}</el-breadcrumb-item>
</template> </template>
</transition-group> </transition-group>
@ -36,7 +36,7 @@
}, },
methods: { methods: {
getBreadcrumb(){ getBreadcrumb(){
let matched = this.$route.matched; let matched = this.$route.meta.breadcrumb;
this.breadList = matched; this.breadList = matched;
} }
} }

View File

@ -36,7 +36,11 @@
<Topbar v-if="!ismobile"></Topbar> <Topbar v-if="!ismobile"></Topbar>
<Tags v-if="!ismobile && layoutTags"></Tags> <Tags v-if="!ismobile && layoutTags"></Tags>
<div class="adminui-main" id="adminui-main"> <div class="adminui-main" id="adminui-main">
<router-view></router-view> <router-view v-slot="{ Component }">
<keep-alive :include="this.$store.state.keepAlive.keepLiveRoute">
<component :is="Component" :key="$route.fullPath" v-if="$store.state.keepAlive.routeShow"/>
</keep-alive>
</router-view>
<iframe-view></iframe-view> <iframe-view></iframe-view>
</div> </div>
</div> </div>
@ -71,7 +75,11 @@
<Topbar v-if="!ismobile"></Topbar> <Topbar v-if="!ismobile"></Topbar>
<Tags v-if="!ismobile && layoutTags"></Tags> <Tags v-if="!ismobile && layoutTags"></Tags>
<div class="adminui-main" id="adminui-main"> <div class="adminui-main" id="adminui-main">
<router-view></router-view> <router-view v-slot="{ Component }">
<keep-alive :include="this.$store.state.keepAlive.keepLiveRoute">
<component :is="Component" :key="$route.fullPath" v-if="$store.state.keepAlive.routeShow"/>
</keep-alive>
</router-view>
<iframe-view></iframe-view> <iframe-view></iframe-view>
</div> </div>
</div> </div>
@ -101,7 +109,11 @@
</el-menu> </el-menu>
</div> </div>
<div class="adminui-main" id="adminui-main"> <div class="adminui-main" id="adminui-main">
<router-view></router-view> <router-view v-slot="{ Component }">
<keep-alive :include="this.$store.state.keepAlive.keepLiveRoute">
<component :is="Component" :key="$route.fullPath" v-if="$store.state.keepAlive.routeShow"/>
</keep-alive>
</router-view>
<iframe-view></iframe-view> <iframe-view></iframe-view>
</div> </div>
</div> </div>
@ -143,7 +155,11 @@
</Topbar> </Topbar>
<Tags v-if="!ismobile && layoutTags"></Tags> <Tags v-if="!ismobile && layoutTags"></Tags>
<div class="adminui-main" id="adminui-main"> <div class="adminui-main" id="adminui-main">
<router-view></router-view> <router-view v-slot="{ Component }">
<keep-alive :include="this.$store.state.keepAlive.keepLiveRoute">
<component :is="Component" :key="$route.fullPath" v-if="$store.state.keepAlive.routeShow"/>
</keep-alive>
</router-view>
<iframe-view></iframe-view> <iframe-view></iframe-view>
</div> </div>
</div> </div>
@ -204,8 +220,6 @@
this.onLayoutResize(); this.onLayoutResize();
window.addEventListener('resize', this.onLayoutResize); window.addEventListener('resize', this.onLayoutResize);
var menu = this.$TOOL.data.get("MENU"); var menu = this.$TOOL.data.get("MENU");
var home = this.$router.options.routes[0].children[0];
menu.unshift(home);
this.menu = this.filterUrl(menu); this.menu = this.filterUrl(menu);
this.showThis() this.showThis()
}, },
@ -234,8 +248,7 @@
}, },
// //
showThis(){ showThis(){
var home = this.$router.options.routes[0].children[0]; this.pmenu = this.$route.meta.breadcrumb ? this.$route.meta.breadcrumb[0] : {}
this.pmenu = this.$route.matched[1] || home;
this.nextMenu = this.filterUrl(this.pmenu.children); this.nextMenu = this.filterUrl(this.pmenu.children);
this.$nextTick(()=>{ this.$nextTick(()=>{
this.active = this.$route.meta.active || this.$route.fullPath; this.active = this.$route.meta.active || this.$route.fullPath;

View File

@ -16,6 +16,7 @@ const routes_404 = {
hidden: true, hidden: true,
component: () => import(/* webpackChunkName: "404" */ '@/views/other/404'), component: () => import(/* webpackChunkName: "404" */ '@/views/other/404'),
} }
let routes_404_r = ()=>{}
const router = createRouter({ const router = createRouter({
history: createWebHashHistory(), history: createWebHashHistory(),
@ -29,14 +30,18 @@ document.title = config.APP_NAME
var isGetApiRouter = false; var isGetApiRouter = false;
router.beforeEach(async (to, from, next) => { router.beforeEach(async (to, from, next) => {
NProgress.start()
NProgress.start()
//动态标题 //动态标题
document.title = to.meta.title ? `${to.meta.title} - ${config.APP_NAME}` : `${config.APP_NAME}` document.title = to.meta.title ? `${to.meta.title} - ${config.APP_NAME}` : `${config.APP_NAME}`
let token = tool.data.get("TOKEN"); let token = tool.data.get("TOKEN");
if(to.path === "/login"){ if(to.path === "/login"){
//删除路由(替换当前layout路由)
router.addRoute(routes[0])
//删除路由(404)
routes_404_r()
isGetApiRouter = false; isGetApiRouter = false;
next(); next();
return false; return false;
@ -53,10 +58,11 @@ router.beforeEach(async (to, from, next) => {
if(!isGetApiRouter){ if(!isGetApiRouter){
let menu = tool.data.get("MENU"); let menu = tool.data.get("MENU");
var apiRouter = filterAsyncRouter(menu); var apiRouter = filterAsyncRouter(menu);
apiRouter = flatAsyncRoutes(apiRouter)
apiRouter.forEach(item => { apiRouter.forEach(item => {
router.addRoute("layout", item) router.addRoute("layout", item)
}) })
router.addRoute(routes_404) routes_404_r = router.addRoute(routes_404)
if (to.matched.length == 0) { if (to.matched.length == 0) {
router.push(to.fullPath); router.push(to.fullPath);
} }
@ -79,7 +85,6 @@ router.onError((error) => {
}); });
}); });
//转换 //转换
function filterAsyncRouter(routerMap) { function filterAsyncRouter(routerMap) {
const accessedRouters = [] const accessedRouters = []
@ -112,5 +117,30 @@ function loadComponent(component){
} }
//路由扁平化
function flatAsyncRoutes(routes, breadcrumb=[]) {
let res = []
routes.forEach(route => {
const tmp = {...route}
if (tmp.children) {
let childrenBreadcrumb = [...breadcrumb]
childrenBreadcrumb.push(route)
let tmpRoute = { ...route }
tmpRoute.meta.breadcrumb = childrenBreadcrumb
delete tmpRoute.children
res.push(tmpRoute)
let childrenRoutes = flatAsyncRoutes(tmp.children, childrenBreadcrumb)
childrenRoutes.map(item => {
res.push(item)
})
} else {
let tmpBreadcrumb = [...breadcrumb]
tmpBreadcrumb.push(tmp)
tmp.meta.breadcrumb = tmpBreadcrumb
res.push(tmp)
}
})
return res
}
export default router export default router

View File

@ -1,41 +1,13 @@
import config from "@/config"
//系统路由 //系统路由
const routes = [{ const routes = [
{
name: "layout", name: "layout",
path: "/", path: "/",
component: () => import(/* webpackChunkName: "layout" */ '@/layout'), component: () => import(/* webpackChunkName: "layout" */ '@/layout'),
redirect: '/dashboard', redirect: config.DASHBOARD_URL || '/dashboard',
children: [ children: []
{
name: "home",
path: "/home",
component: () => import(`@/views/other/empty`),
meta: {
title: "首页",
icon: "el-icon-platform-eleme"
},
children: [
{
name: "dashboard",
path: "/dashboard",
meta: {
title: "控制台",
icon: "el-icon-menu",
affix: true
},
component: () => import(/* webpackChunkName: "home" */ '@/views/home'),
},
{
name: "userCenter",
path: "/usercenter",
meta: {
title: "个人信息",
icon: "el-icon-user",
},
component: () => import(/* webpackChunkName: "usercenter" */ '@/views/userCenter'),
}
]
}
]
}, },
{ {
path: "/cmd", path: "/cmd",

View File

@ -1,6 +1,6 @@
export default { export default {
state: { state: {
keepLiveRoute: ['empty'], keepLiveRoute: [],
routeKey: null, routeKey: null,
routeShow: true routeShow: true
}, },
@ -17,7 +17,7 @@ export default {
} }
}, },
clearKeepLive(state){ clearKeepLive(state){
state.keepLiveRoute = ['empty'] state.keepLiveRoute = []
}, },
setRouteKey(state, key){ setRouteKey(state, key){
state.routeKey = key state.routeKey = key

View File

@ -6,6 +6,7 @@
<h4>啊呀~页面不存在!</h4> <h4>啊呀~页面不存在!</h4>
<p>您可以先检查网址然后重新输入或给我们反馈问题</p> <p>您可以先检查网址然后重新输入或给我们反馈问题</p>
<el-button type="primary" plain round @click="gohome">返回首页</el-button> <el-button type="primary" plain round @click="gohome">返回首页</el-button>
<el-button type="primary" plain round @click="gologin">重新登录</el-button>
<el-button type="primary" round @click="goback">返回上一页</el-button> <el-button type="primary" round @click="goback">返回上一页</el-button>
</el-empty> </el-empty>
@ -20,6 +21,9 @@
}, },
goback(){ goback(){
this.$router.go(-1); this.$router.go(-1);
},
gologin(){
this.$router.push("/login");
} }
} }
} }

View File

@ -1,18 +1,3 @@
<template> <template>
<router-view v-slot="{ Component }"> <router-view></router-view>
<keep-alive :include="this.$store.state.keepAlive.keepLiveRoute"> </template>
<component :is="Component" :key="$route.fullPath" v-if="$store.state.keepAlive.routeShow"/>
</keep-alive>
</router-view>
</template>
<script>
export default {
name: 'empty',
data() {
return {
}
}
}
</script>

View File

@ -174,6 +174,14 @@
menu = await this.$API.demo.menu.get() menu = await this.$API.demo.menu.get()
} }
if(menu.code == 200){ if(menu.code == 200){
if(menu.data.menu.length==0){
this.islogin = false
this.$alert("当前用户无任何菜单权限,请联系系统管理员", "无权限访问", {
type: 'error',
center: true
})
return false
}
this.$TOOL.data.set("MENU", menu.data.menu) this.$TOOL.data.set("MENU", menu.data.menu)
this.$TOOL.data.set("PERMISSIONS", menu.data.permissions) this.$TOOL.data.set("PERMISSIONS", menu.data.permissions)
}else{ }else{