feat: 菜单隐藏、禁用

This commit is contained in:
wang_fan_w 2024-08-18 15:55:25 +08:00
parent 0f59405e16
commit 7fb81563f6
7 changed files with 165 additions and 5 deletions

View File

@ -66,6 +66,8 @@ export default {
["close-right-side"]: "close right side", ["close-right-side"]: "close right side",
["close-other"]: "close other", ["close-other"]: "close other",
["close-all"]: "close all", ["close-all"]: "close all",
["disable-menu"]: "disable-menu",
["disable-menu-1"]: "disable-menu-1",
["internationalization"]: "internationalization", ["internationalization"]: "internationalization",
["switch-to-night-mode"]: "switch to night mode", ["switch-to-night-mode"]: "switch to night mode",
["switch-to-daytime-mode"]: "switch to daytime mode", ["switch-to-daytime-mode"]: "switch to daytime mode",

View File

@ -66,6 +66,10 @@ export default {
["close-right-side"]: "关闭右侧", ["close-right-side"]: "关闭右侧",
["close-other"]: "关闭其它", ["close-other"]: "关闭其它",
["close-all"]: "关闭全部", ["close-all"]: "关闭全部",
["disable-menu"]: "停用菜单",
["disable-menu-1"]: "停用菜单-1",
["hide-menu"]: "隐藏菜单",
["hide-menu-1"]: "隐藏菜单-1",
["internationalization"]: "国际化", ["internationalization"]: "国际化",
["switch-to-night-mode"]: "切换黑夜模式", ["switch-to-night-mode"]: "切换黑夜模式",
["switch-to-daytime-mode"]: "切换白天模式", ["switch-to-daytime-mode"]: "切换白天模式",

View File

@ -1,13 +1,13 @@
<template> <template>
<template v-for="item in props.routeTree" :key="item.name"> <template v-for="item in props.routeTree" :key="item.name">
<a-sub-menu v-if="item.children && item.children.length > 0" :key="item.name"> <a-sub-menu v-if="menuShow(item)" :key="item.name">
<template #icon v-if="item.meta.svgIcon || item.meta.icon"> <template #icon v-if="item.meta.svgIcon || item.meta.icon">
<MenuItemIcon :svg-icon="item.meta.svgIcon" :icon="item.meta.icon" /> <MenuItemIcon :svg-icon="item.meta.svgIcon" :icon="item.meta.icon" />
</template> </template>
<template #title>{{ $t(`language.${item.meta.title}`) }}</template> <template #title>{{ $t(`language.${item.meta.title}`) }}</template>
<MenuItem :route-tree="item.children" /> <MenuItem :route-tree="item.children" />
</a-sub-menu> </a-sub-menu>
<a-menu-item v-else :key="item?.name"> <a-menu-item v-else-if="aMenuShow(item)" :key="item?.name">
<template #icon v-if="item.meta.svgIcon || item.meta.icon"> <template #icon v-if="item.meta.svgIcon || item.meta.icon">
<MenuItemIcon :svg-icon="item.meta.svgIcon" :icon="item.meta.icon" /> <MenuItemIcon :svg-icon="item.meta.svgIcon" :icon="item.meta.icon" />
</template> </template>
@ -29,6 +29,17 @@ interface Props {
const props = withDefaults(defineProps<Props>(), { const props = withDefaults(defineProps<Props>(), {
routeTree: () => [] routeTree: () => []
}); });
//
const menuShow = (item: Menu.MenuOptions) => {
if (item.children && item.children?.length > 0 && !item.meta.hide) return true;
return false;
};
//
const aMenuShow = (item: Menu.MenuOptions) => {
if (!item.meta.hide) return true;
return false;
};
</script> </script>
<style lang="scss" scoped></style> <style lang="scss" scoped></style>

View File

@ -58,14 +58,19 @@ export function linearArray(tree: any) {
/** /**
* *
* 1访
* 2
* @param {array} tree * @param {array} tree
* @returns * @returns
*/ */
export const filterByRole = (tree: any) => { export const filterByRole = (tree: any) => {
return tree.filter((item: any) => { return tree.filter((item: any) => {
if (item.meta && item.meta.roles) { // 过滤角色权限
if (item?.meta?.roles) {
if (!roleBase(item.meta.roles)) return false; if (!roleBase(item.meta.roles)) return false;
} }
// 过滤是否禁用
if (item?.meta?.disable) return false;
if (item.children) item.children = filterByRole(item.children); if (item.children) item.children = filterByRole(item.children);
return true; return true;
}); });

View File

@ -6,7 +6,8 @@ import { testMultilevelMenu } from "@/mock/testRoute";
* meta对象参数meta对象中 * meta对象参数meta对象中
* meta: { * meta: {
* title: 菜单栏以及 tabsView * title: 菜单栏以及 tabsView
* hide: 是否隐藏此路由 * hide: 是否隐藏此路由访
* disable: 是否停用访
* keepAlive: 是否缓存组件状态 * keepAlive: 是否缓存组件状态
* affix: 是否固定在 tabsView * affix: 是否固定在 tabsView
* link: 是否是超链接菜单1 link 2iframe: false * link: 是否是超链接菜单1 link 2iframe: false
@ -42,6 +43,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "home", // 国际化 title: "home", // 国际化
hide: false, // 是否隐藏此路由 hide: false, // 是否隐藏此路由
disable: false, // 是否停用此路由
keepAlive: false, // 缓存组件状态 keepAlive: false, // 缓存组件状态
affix: true, // 固定在tagesView栏上 affix: true, // 固定在tagesView栏上
link: "", // 是否外链 link: "", // 是否外链
@ -58,6 +60,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "file-management", title: "file-management",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -74,6 +77,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "document-library", title: "document-library",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -93,6 +97,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "table-management", title: "table-management",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -109,6 +114,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "common-table", title: "common-table",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -125,6 +131,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "custom-table", title: "custom-table",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -143,6 +150,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "form-management", title: "form-management",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -159,6 +167,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "common-form", title: "common-form",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -175,6 +184,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "step-form", title: "step-form",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -193,6 +203,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "multilevel-menu", title: "multilevel-menu",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -209,6 +220,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "second-menu-1", title: "second-menu-1",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -225,6 +237,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "second-menu-2", title: "second-menu-2",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -241,6 +254,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "third-menu-1", title: "third-menu-1",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -257,6 +271,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "third-menu-2", title: "third-menu-2",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -278,6 +293,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "common-component", title: "common-component",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -294,6 +310,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "player", title: "player",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -311,6 +328,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "print", title: "print",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -328,6 +346,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "draggable", title: "draggable",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -345,6 +364,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "editor", title: "editor",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -362,6 +382,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "newbie", title: "newbie",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -379,6 +400,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "usre-center", title: "usre-center",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -398,6 +420,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "custom-instruction", title: "custom-instruction",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -414,6 +437,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "anti-shake", title: "anti-shake",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -431,6 +455,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "throttle", title: "throttle",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -448,6 +473,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "test-instruction", title: "test-instruction",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -467,6 +493,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "personal-center", title: "personal-center",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -483,6 +510,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "userinfo", title: "userinfo",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -500,6 +528,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "user-settings", title: "user-settings",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -519,6 +548,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "system-management", title: "system-management",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -535,6 +565,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "account-management", title: "account-management",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -552,6 +583,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "role-management", title: "role-management",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -569,6 +601,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "menu-management", title: "menu-management",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -586,6 +619,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "division-management", title: "division-management",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -603,6 +637,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "dictionary-management", title: "dictionary-management",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -620,6 +655,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "system-log", title: "system-log",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -639,6 +675,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "external-links", title: "external-links",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -655,6 +692,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "inline-page", title: "inline-page",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -672,6 +710,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
title: "uigradients", title: "uigradients",
hide: false, hide: false,
keepAlive: true, keepAlive: true,
disable: false,
affix: false, affix: false,
link: "https://uigradients.com/#HoneyDew", // 链接 link: "https://uigradients.com/#HoneyDew", // 链接
iframe: true, // 区分是否内链 true内链 false外链 iframe: true, // 区分是否内链 true内链 false外链
@ -688,6 +727,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "color-taking-tool", title: "color-taking-tool",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "https://photokit.com/colors/eyedropper/?lang=zh", // 链接 link: "https://photokit.com/colors/eyedropper/?lang=zh", // 链接
@ -705,6 +745,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "grid-generator", title: "grid-generator",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "https://cssgrid-generator.netlify.app/", // 链接 link: "https://cssgrid-generator.netlify.app/", // 链接
@ -724,6 +765,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "external-page", title: "external-page",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -740,6 +782,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "link-SnowAdmin-Docs", title: "link-SnowAdmin-Docs",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "http://101.126.93.137:81/", // 链接 link: "http://101.126.93.137:81/", // 链接
@ -757,6 +800,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "link-vue", title: "link-vue",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "https://cn.vuejs.org/", // 链接 link: "https://cn.vuejs.org/", // 链接
@ -774,6 +818,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "link-vite", title: "link-vite",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "https://www.vitejs.net/", // 链接 link: "https://www.vitejs.net/", // 链接
@ -791,6 +836,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "link-gitee", title: "link-gitee",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "https://gitee.com/wang_fan_w/SnowAdmin", // 链接 link: "https://gitee.com/wang_fan_w/SnowAdmin", // 链接
@ -808,6 +854,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "link-juejin", title: "link-juejin",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "https://juejin.cn/user/1728883023940600", // 链接 link: "https://juejin.cn/user/1728883023940600", // 链接
@ -822,6 +869,78 @@ export const dynamicRoutes: RouteRecordRaw[] = [
} }
] ]
}, },
{
path: "/disable-menu",
name: "disable-menu",
redirect: "/disable-menu/disable-menu-1",
meta: {
title: "disable-menu",
hide: false,
disable: true,
keepAlive: true,
affix: false,
link: "",
iframe: false,
roles: ["admin"],
svgIcon: "switch",
sort: 5
},
children: [
{
path: "/disable-menu/disable-menu-1",
name: "disable-menu-1",
component: () => import("@/views/disable-menu/disable-menu-1/disable-menu-1.vue"),
meta: {
title: "disable-menu-1",
hide: false,
disable: true,
keepAlive: true,
affix: false,
link: "",
iframe: false,
roles: ["admin"],
icon: "icon-menu",
sort: 1
}
}
]
},
{
path: "/hide-menu",
name: "hide-menu",
redirect: "/hide-menu/hide-menu-1",
meta: {
title: "hide-menu",
hide: true,
disable: false,
keepAlive: true,
affix: false,
link: "",
iframe: false,
roles: ["admin"],
svgIcon: "switch",
sort: 5
},
children: [
{
path: "/hide-menu/hide-menu-1",
name: "hide-menu-1",
component: () => import("@/views/hide-menu/hide-menu-1/hide-menu-1.vue"),
meta: {
title: "hide-menu-1",
hide: true,
disable: false,
keepAlive: true,
affix: false,
link: "",
iframe: false,
roles: ["admin"],
icon: "icon-menu",
sort: 1
}
}
]
},
{ {
path: "/internationalization", path: "/internationalization",
name: "internationalization", name: "internationalization",
@ -829,6 +948,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "internationalization", title: "internationalization",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -845,6 +965,7 @@ export const dynamicRoutes: RouteRecordRaw[] = [
meta: { meta: {
title: "about-project", title: "about-project",
hide: false, hide: false,
disable: false,
keepAlive: true, keepAlive: true,
affix: false, affix: false,
link: "", link: "",
@ -880,7 +1001,10 @@ export const staticRoutes = [
]; ];
/** /**
* 404401 * 401404500
* 401
* 404
* 500
* @link https://router.vuejs.org/zh/guide/essentials/history-mode.html#netlify * @link https://router.vuejs.org/zh/guide/essentials/history-mode.html#netlify
*/ */
export const notFoundAndNoPower = [ export const notFoundAndNoPower = [

View File

@ -0,0 +1,7 @@
<template>
<div class="dc-page">停用菜单</div>
</template>
<script setup lang="ts"></script>
<style lang="scss" scoped></style>

View File

@ -0,0 +1,7 @@
<template>
<div class="dc-page">隐藏菜单</div>
</template>
<script setup lang="ts"></script>
<style lang="scss" scoped></style>