| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- <script setup lang="ts">
- import { useRoute, useRouter } from 'vue-router'
- import { useUserStore } from '@/stores/modules/user'
- const route = useRoute()
- const router = useRouter()
- const userStore = useUserStore()
- const isCollapse = defineModel<boolean>()
- // [
- // {
- // index: '1',
- // label: 'Dashboard',
- // icon: 'icon_data_fill_b',
- // path: '/dashboard'
- // },
- // {
- // index: '3',
- // label: 'Booking',
- // icon: 'icon_booking__fill_b',
- // path: '/booking'
- // },
- // {
- // index: '4',
- // label: 'Tracking',
- // icon: 'icon_tracking__fill_b',
- // path: '/tracking'
- // },
- // {
- // index: '6',
- // label: 'System Management',
- // icon: 'icon_system__management_fill_b',
- // type: 'list',
- // children: [
- // {
- // index: '5-4',
- // label: 'Operation Log',
- // path: '/Operationlog'
- // }
- // ]
- // }
- // ]
- const menuList = ref()
- watch(
- () => userStore.username,
- () => {
- getMenuList()
- }
- )
- const getMenuList = () => {
- $api.getMenuList().then((res) => {
- if (res.code === 200) {
- menuList.value = res.data
- }
- })
- }
- getMenuList()
- //监听窗口大小
- const handler = () => {
- return (() => {
- let screenWidth = document.body.clientWidth
- if (screenWidth < 1400) {
- isCollapse.value = true
- } else {
- isCollapse.value = false
- }
- })()
- }
- const listeningWindow = () => {
- window.addEventListener('resize', handler)
- }
- const unListeningWindow = () => {
- window.removeEventListener('resize', handler)
- }
- watchEffect(() => {
- listeningWindow()
- })
- onUnmounted(() => {
- unListeningWindow()
- })
- const activeMenu = ref()
- activeMenu.value = (route.meta?.activeMenu as string) || route.path
- // 未登录白名单
- const whiteList = ['/login', '/public-tracking', '/public-tracking/detail', '/reset-password']
- // 判断是否允许跳转
- const isAllowJump = (path: any) => {
- // 判断是否登录
- if (!whiteList.includes(path) && !localStorage.getItem('username')) {
- ElMessage.warning({
- message: 'Please log in to use this feature.',
- grouping: true
- })
- activeMenu.value = route.path // 保持选中状态不变
- return false
- }
- return true
- }
- // 路由后置守卫
- router.afterEach(() => {
- activeMenu.value = (route.meta?.activeMenu as string) || route.path
- })
- const isVisible = ref(false)
- const emits = defineEmits(['changeVisible'])
- // 路由跳转函数
- const changeRouter = (path: any) => {
- if (sessionStorage.getItem('searchTableQeury') == '{}') {
- sessionStorage.removeItem('searchTableQeury')
- }
- if (sessionStorage.getItem('searchTableQeuryTracking') == '{}') {
- sessionStorage.removeItem('searchTableQeuryTracking')
- }
- isVisible.value = false
- emits('changeVisible', isVisible.value)
- isVisible.value = false
- let toPath = path
- if (path === '/tracking' && !localStorage.getItem('username')) {
- toPath = '/public-tracking'
- }
- // 如果允许跳转,执行跳转
- if (isAllowJump(toPath)) {
- router.push(toPath)
- } else {
- // 如果不允许跳转,保持当前 activeMenu 不变
- nextTick(() => {
- activeMenu.value = (route.meta?.activeMenu as string) || route.path // 确保菜单栏选中状态为当前路径
- })
- }
- }
- const handleCollapseClick = () => {
- isCollapse.value = !isCollapse.value
- }
- const menuRef = ref()
- </script>
- <template>
- <el-menu
- ref="menuRef"
- class="layout-menu"
- @select="changeRouter"
- :default-active="activeMenu"
- :collapse="isCollapse"
- >
- <template v-for="item in menuList" :key="item.index">
- <el-menu-item
- :class="{
- 'clear-active-style': route.path === '/login' || route.path === '/reset-password'
- }"
- v-if="item.type !== 'list'"
- :index="item.path"
- >
- <span class="font_family" :class="[`icon-${item.icon}`]"></span>
- <template #title>{{ item.label }}</template>
- </el-menu-item>
- <el-sub-menu v-else :index="item.path">
- <template #title>
- <div class="font_family" style="font-size: 16px" :class="[`icon-${item.icon}`]"></div>
- <span>{{ item.label }}</span>
- </template>
- <template v-for="childrenItem in item.children" :key="childrenItem.index">
- <el-menu-item :index="childrenItem.path">{{ childrenItem.label }}</el-menu-item>
- </template>
- </el-sub-menu>
- </template>
- </el-menu>
- <div class="collapse-icon">
- <div @click="handleCollapseClick">
- <span
- :style="{ transform: isCollapse ? 'rotate(0deg)' : 'rotate(180deg)' }"
- class="font_family icon-icon_menu_collapse_b"
- ></span>
- </div>
- </div>
- </template>
- <style lang="scss" scoped>
- .layout-menu {
- height: calc(100% - 120px);
- border-bottom: 1px solid var(--color-border);
- border-right: none;
- .font_family {
- margin-left: 4px;
- margin-right: 8px;
- }
- }
- .collapse-icon {
- display: flex;
- justify-content: flex-end;
- align-items: center;
- height: 56px;
- padding-right: 10px;
- div {
- display: flex;
- justify-content: center;
- align-items: center;
- width: 42px;
- height: 100%;
- cursor: pointer;
- }
- }
- .el-menu-item.is-active {
- background-color: var(--color-mune-active-bg);
- color: var(--color-theme);
- font-weight: 700;
- & > span {
- color: var(--color-theme);
- }
- }
- .el-menu-item {
- height: 40px;
- margin: 0 12px 2px;
- padding-left: 8px !important;
- border-radius: 6px;
- &:hover {
- background-color: var(--color-mune-active-bg);
- }
- :deep(.el-menu-tooltip__trigger) {
- padding-left: 8px;
- }
- }
- li.clear-active-style {
- background-color: transparent !important;
- color: var(--color-neutral-1) !important;
- font-weight: normal !important;
- & > span {
- color: var(--color-neutral-1) !important;
- }
- }
- :deep(.el-sub-menu__title) {
- height: 40px;
- margin: 0 12px;
- padding-left: 8px !important;
- line-height: 40px;
- &:hover {
- background-color: var(--color-mune-active-bg);
- }
- }
- .el-sub-menu {
- margin-bottom: 2px;
- .el-menu-item {
- padding-left: 36px !important;
- }
- }
- </style>
|