food/src/views/layout/components/menubar.vue

205 lines
5.3 KiB
Vue
Raw Normal View History

2023-09-20 21:33:11 +08:00
<template>
<div>
<!-- logo -->
<div class="sidebar-logo-container" :class="{ 'collapse': isCollapse }">
<router-link v-if="isCollapse" key="isCollapse" class="sidebar-logo-link" to="/">
<img v-if="logo" src="@/assets/logo.png" class="sidebar-logo">
<h1 v-else class="sidebar-title">{{ title }}</h1>
</router-link>
<router-link v-else key="expand" class="sidebar-logo-link" to="/">
<img v-if="logo" src="@/assets/logo.png" class="sidebar-logo">
<h1 class="sidebar-title">{{ title }}</h1>
</router-link>
</div>
<!-- end logo -->
<!-- menu -->
<el-menu class="border-n" :router="true" :default-active="activeMenu" :unique-opened="true" background-color="#304156"
text-color="rgb(191, 203, 217)" active-text-color="#409EFF" :collapse-transition="false" :collapse="isCollapse">
<template v-for="(route, index) in routes">
<el-menu-item v-if="route.children.length < 2" :key="route.path" :index="route.children[0].path">
<i class="fc" :class="route.children[0].meta.icon"></i>
<span slot="title">{{ route.children[0].meta.title }}</span>
</el-menu-item>
<el-submenu v-else :key="index" :index="route.path">
<template slot="title">
<i class="fc" :class="route.meta.icon"></i>
<span>{{ route.meta.title }}</span>
</template>
<el-menu-item v-for="child in route.children" :key="child.path" :index="child.path">
<i class="fc" :class="child.meta.icon"></i>
<span>{{ child.meta.title }}</span>
</el-menu-item>
</el-submenu>
</template>
</el-menu>
<!-- end menu -->
</div>
</template>
<script>
export default {
name: 'Menubar',
data () {
return {
title: '无人机控制终端',
logo: '@/assets/logo.png'
}
},
computed: {
/**
* @description: 递归过滤路由列表同时过滤掉父级和子级中 hidden true 或权限不足的项
*/
routes () {
const filterRoutes = (routes, userPower) => {
return routes.filter(item => {
const roles = item.roles || [] // 设置默认空数组
if (item.hidden === true || roles.indexOf(userPower) === -1) {
return false
}
if (item.children && item.children.length > 0) {
item.children = filterRoutes(item.children, userPower) // 递归处理子级
if (item.children.length === 0) {
return false // 如果子级都被过滤掉了,也过滤掉当前项
}
}
return true
})
}
const userPower = this.$store.state.user.power.trim()
return filterRoutes(this.$router.options.routes, userPower)
},
/**
* @description: 当前激活的 路由路径
*/
activeMenu () {
const route = this.$route
const { meta, path } = route
if (meta.activeMenu) {
return meta.activeMenu
}
return path
},
/**
* @description: 获取飞机列表
*/
airList () {
return this.$store.state.airList.filter(element => element.shop_id === this.$store.state.user.shop_id)
},
/**
* @description: 侧边导航栏状态
*/
isCollapse () {
return this.$store.state.app.isCollapse
}
},
methods: {
/**
* @description: 动态加载路由
*/
loadRoute () {
const arr = new Array(0)
this.airList.map((item, index) => {
arr[index] = {
path: '/planes/index/' + item.id + '/' + item.name,
name: 'Planes',
component: () => import('@/views/layout/components/main/planes/index.vue'),
meta: { title: item.name, icon: 'iconfont icon-wurenji' },
roles: ['admin', 'editor']
}
})
this.routes.map((element) => {
if (element.meta.title === '无人机') {
element.children = arr
}
})
}
},
created () {
},
watch: {
/**
* @description: 监听飞机列表 有个新刷新导航栏
*/
airList () {
this.loadRoute()
this.$forceUpdate()// 刷新本组件
}
}
}
</script>
<style lang="scss" scoped>
@import "@/styles/theme.scss";
i {
font-size: 18px !important;
margin-right: 12px;
vertical-align: middle;
}
.el-submenu__title i,
.el-menu-item i {
color: rgb(191, 203, 217);
}
.el-submenu {
.el-menu-item {
background-color: #1f2d3d !important;
}
}
.el-submenu .el-menu-item:hover {
background-color: #001528 !important;
}
.sidebarLogoFade-enter,
.sidebarLogoFade-leave-to {
opacity: 0;
}
h1 {
color: $maintext-color !important;
}
.sidebar-logo-container {
position: relative;
width: 100%;
height: 50px;
line-height: 50px;
background: $graylight-color;
text-align: center;
overflow: hidden;
z-index: 100;
box-shadow: 0 1px 1px rgba(0, 21, 41, .08);
& .sidebar-logo-link {
height: 100%;
width: 100%;
& .sidebar-logo {
width: 24px;
height: 24px;
vertical-align: middle;
margin-right: 12px;
}
& .sidebar-title {
display: inline-block;
margin: 0;
color: #fff;
font-weight: 600;
line-height: 50px;
font-size: 14px;
font-family: Avenir, Helvetica Neue, Arial, Helvetica, sans-serif;
vertical-align: middle;
}
}
&.collapse {
.sidebar-logo {
margin-right: 0px;
}
}
}
</style>