feat: 系统菜单增加自定义svg支持

This commit is contained in:
2026-08-19 17:44:48 +08:00
parent 22a5300e55
commit 4a634bb5db
13 changed files with 163 additions and 17 deletions
@@ -16,7 +16,8 @@ CREATE TABLE `sys_menu` (
`path` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '路由路径',
`component` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '页面组件',
`redirect` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '从定向地址',
`icon` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '路由图标',
`icon` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '路由图标(Iconify 名)',
`icon_svg` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '自定义 SVG 图标(有值时前端优先于 icon)',
`order` int NULL DEFAULT 0 COMMENT '顺序',
`type` enum('catalog','menu','button','embedded','link') CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '菜单类型 (目录,菜单,按钮,内联,外链)',
`visible` tinyint NOT NULL DEFAULT 1 COMMENT '是否显示在菜单中 (0-否 1-是)',
@@ -0,0 +1,4 @@
-- sys_menu:自定义 SVG 图标,有值时前端优先于 icon(Iconify)
ALTER TABLE `sys_menu`
ADD COLUMN `icon_svg` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL
COMMENT '自定义 SVG 图标(有值时前端优先于 icon)' AFTER `icon`;
@@ -50,10 +50,15 @@ public class CreateMenuDTO {
private String redirect;
/**
* 路由图标
* 路由图标(Iconify 名)
*/
private String icon;
/**
* 自定义 SVG 图标(有值时前端优先于 icon)
*/
private String iconSvg;
/**
* 顺序
*/
@@ -50,10 +50,15 @@ public class UpdateMenuDTO {
private String redirect;
/**
* 路由图标
* 路由图标(Iconify 名)
*/
private String icon;
/**
* 自定义 SVG 图标(有值时前端优先于 icon)
*/
private String iconSvg;
/**
* 顺序
*/
@@ -63,11 +63,17 @@ public class MenuPO {
private String redirect;
/**
* 路由图标
* 路由图标(Iconify 名)
*/
@TableField("icon")
private String icon;
/**
* 自定义 SVG 图标(有值时前端优先于 icon)
*/
@TableField(value = "icon_svg", updateStrategy = FieldStrategy.ALWAYS)
private String iconSvg;
/**
* 顺序
*/
@@ -57,10 +57,15 @@ public class MenuVO {
private String redirect;
/**
* 路由图标
* 路由图标(Iconify 名)
*/
private String icon;
/**
* 自定义 SVG 图标(有值时前端优先于 icon)
*/
private String iconSvg;
/**
* 顺序
*/
@@ -68,11 +68,16 @@ public class UserMenuVO {
private String title;
/**
* 路由图标
* 对应数据库: icon 字段
* 路由图标:优先为 icon_svg 原文,否则为 Iconify 名
* 对应数据库: icon_svg / icon
*/
private String icon;
/**
* 自定义 SVG(可选;侧栏渲染时已并入 icon 优先逻辑)
*/
private String iconSvg;
/**
* 顺序
* 对应数据库: order 字段
@@ -180,7 +180,13 @@ public class UserMenuServiceImpl implements UserMenuService {
meta.setId(menu.getId());
meta.setParentId(menu.getParentId());
meta.setTitle(menu.getTitle());
meta.setIcon(menu.getIcon());
// 侧栏图标:有 SVG 时写入 meta.icon,供 VbenIcon 优先渲染
if (menu.getIconSvg() != null && !menu.getIconSvg().isBlank()) {
meta.setIcon(menu.getIconSvg().trim());
meta.setIconSvg(menu.getIconSvg().trim());
} else {
meta.setIcon(menu.getIcon());
}
meta.setOrder(menu.getOrder());
meta.setType(menu.getType());
meta.setVisible(menu.getVisible());
@@ -19,8 +19,10 @@ export namespace MenuDTO {
component?: string;
/** 重定向路径 */
redirect?: string;
/** 菜单图标 */
/** 菜单图标(Iconify 名) */
icon?: string;
/** 自定义 SVG(有值时优先于 icon) */
iconSvg?: string;
/** 排序 */
order?: number;
/** 菜单类型 */
@@ -49,8 +51,10 @@ export namespace MenuDTO {
component?: string;
/** 重定向路径 */
redirect?: string;
/** 菜单图标 */
/** 菜单图标(Iconify 名) */
icon?: string;
/** 自定义 SVG(有值时优先于 icon) */
iconSvg?: string;
/** 排序 */
order?: number;
/** 菜单类型 */
@@ -22,8 +22,10 @@ export namespace MenuVO {
component?: string;
/** 重定向路径 */
redirect?: string;
/** 菜单图标 */
/** 菜单图标(Iconify 名) */
icon?: string;
/** 自定义 SVG(有值时优先于 icon) */
iconSvg?: string;
/** 排序 */
order?: number;
/** 菜单类型 */
@@ -71,6 +71,7 @@ interface FormData {
component: string;
redirect: string;
icon: string;
iconSvg: string;
order: number;
type: string;
visible: boolean;
@@ -88,6 +89,7 @@ const formData = ref<FormData>({
component: '',
redirect: '',
icon: '',
iconSvg: '',
order: 0,
type: 'menu',
visible: true,
@@ -349,6 +351,7 @@ function open(edit: boolean, record?: MenuVO.MenuInfo) {
component: record.component || '',
redirect: record.redirect || '',
icon: record.icon || '',
iconSvg: record.iconSvg || '',
order: record.order || 0,
type: record.type,
visible: record.visible === 1, // 转换为boolean
@@ -377,6 +380,7 @@ function open(edit: boolean, record?: MenuVO.MenuInfo) {
component: '',
redirect: '',
icon: '',
iconSvg: '',
order: 0,
type: 'menu',
visible: true,
@@ -394,6 +398,7 @@ function open(edit: boolean, record?: MenuVO.MenuInfo) {
component: '',
redirect: '',
icon: '',
iconSvg: '',
order: 0,
type: 'menu',
visible: true,
@@ -479,6 +484,8 @@ async function handleSubmit() {
const submitData = {
...formData.value,
parentId: formData.value.parentId ?? null,
icon: formData.value.icon?.trim() || null,
iconSvg: formData.value.iconSvg?.trim() || null,
superMenu: formData.value.superMenu ? 1 : 0,
visible: formData.value.visible ? 1 : 0,
status: formData.value.status ? 1 : 0,
@@ -662,6 +669,23 @@ defineExpose({
</Popover>
</Form.Item>
<Form.Item label="自定义 SVG" name="iconSvg">
<div class="flex gap-3">
<Input.TextArea
v-model:value="formData.iconSvg"
placeholder="粘贴完整 <svg>...</svg>;有值时优先于上方 Iconify 图标"
:rows="4"
class="flex-1"
/>
<div
v-if="formData.iconSvg?.trim().startsWith('<svg')"
class="flex size-10 shrink-0 items-center justify-center rounded border border-border [&>svg]:size-6"
title="SVG 预览"
v-html="formData.iconSvg"
></div>
</div>
</Form.Item>
<Form.Item label="排序" name="order">
<InputNumber
v-model:value="formData.order"
@@ -456,7 +456,13 @@ const [Grid, gridApi] = useVbenVxeGrid({
<!-- 图标列 -->
<template #icon="{ row }: { row: MenuVO.MenuInfo }">
<div
v-if="row.icon"
v-if="row.iconSvg?.trim()"
class="flex items-center justify-center [&>svg]:size-4"
title="自定义 SVG"
v-html="row.iconSvg"
></div>
<div
v-else-if="row.icon"
class="flex items-center justify-center"
:title="row.icon"
>
@@ -1,35 +1,108 @@
<script setup lang="ts">
import type { Component } from 'vue';
import { computed } from 'vue';
import { computed, useAttrs } from 'vue';
import { IconDefault, IconifyIcon } from '@vben-core/icons';
import {
cn,
isFunction,
isHttpUrl,
isObject,
isString,
} from '@vben-core/shared/utils';
defineOptions({ inheritAttrs: false });
const props = defineProps<{
// 没有是否显示默认图标
fallback?: boolean;
icon?: Component | Function | string;
}>();
const attrs = useAttrs();
const isRemoteIcon = computed(() => {
return isString(props.icon) && isHttpUrl(props.icon);
});
/** 直接内联的 SVG 字符串(如菜单 icon_svg) */
const isSvgIcon = computed(() => {
if (!isString(props.icon)) {
return false;
}
const trimmed = props.icon.trim();
return trimmed.startsWith('<svg') || trimmed.startsWith('<?xml');
});
const isComponent = computed(() => {
const { icon } = props;
return !isString(icon) && (isObject(icon) || isFunction(icon));
});
/**
* 将硬编码 fill/stroke 改为 currentColor,使其随深浅色主题(与 Iconify 一致)。
* 保留 none / currentColor / url(#...) 不改。
*/
function toThemeAwareSvg(raw: string): string {
const keep = (v: string) => {
const s = v.trim().toLowerCase();
return (
s === 'none' ||
s === 'currentcolor' ||
s.startsWith('url(') ||
s === 'transparent'
);
};
const attr = (name: 'fill' | 'stroke', v: string, q: string) =>
keep(v) ? ` ${name}=${q}${v}${q}` : ` ${name}=${q}currentColor${q}`;
const styleVal = (style: string) =>
style
.replace(/(?:^|;)\s*fill\s*:\s*([^;]+)/gi, (m, v: string) =>
keep(v) ? m : ';fill:currentColor',
)
.replace(/(?:^|;)\s*stroke\s*:\s*([^;]+)/gi, (m, v: string) =>
keep(v) ? m : ';stroke:currentColor',
);
return raw
.replace(/\sfill=(["'])([^"']*)\1/gi, (_, q: string, v: string) =>
attr('fill', v, q),
)
.replace(/\sstroke=(["'])([^"']*)\1/gi, (_, q: string, v: string) =>
attr('stroke', v, q),
)
.replace(/style=(["'])([^"']*)\1/gi, (_, q: string, style: string) =>
`style=${q}${styleVal(style)}${q}`,
);
}
const themedSvgHtml = computed(() =>
isSvgIcon.value && isString(props.icon)
? toThemeAwareSvg(props.icon.trim())
: '',
);
/** 默认 1em;外部 size-* 由 cn 合并覆盖 */
const svgRootClass = computed(() =>
cn(
// text-current:继承父级文字色;CSS 兜底把未改干净的 fill 也拉成 currentColor
'inline-flex size-[1em] shrink-0 items-center justify-center overflow-hidden text-current [&>svg]:!size-full [&>svg]:fill-current [&_path:not([fill=none])]:fill-current [&_g:not([fill=none])]:fill-current [&_circle:not([fill=none])]:fill-current [&_rect:not([fill=none])]:fill-current',
attrs.class as string | string[] | Record<string, boolean> | undefined,
),
);
</script>
<template>
<component :is="icon as Component" v-if="isComponent" v-bind="$attrs" />
<img v-else-if="isRemoteIcon" :src="icon as string" v-bind="$attrs" />
<IconifyIcon v-else-if="icon" v-bind="$attrs" :icon="icon as string" />
<IconDefault v-else-if="fallback" v-bind="$attrs" />
<component :is="icon as Component" v-if="isComponent" v-bind="attrs" />
<img v-else-if="isRemoteIcon" :src="icon as string" v-bind="attrs" />
<span
v-else-if="isSvgIcon"
v-bind="{ ...attrs, class: svgRootClass }"
v-html="themedSvgHtml"
></span>
<IconifyIcon v-else-if="icon" v-bind="attrs" :icon="icon as string" />
<IconDefault v-else-if="fallback" v-bind="attrs" />
</template>