feat: 菜单侧栏标题与标签标题拆分并支持 tab_title 回退

This commit is contained in:
2026-09-05 18:27:53 +08:00
parent d99d8352b6
commit ebca4d093a
19 changed files with 119 additions and 21 deletions
@@ -13,7 +13,8 @@ CREATE TABLE `sys_menu` (
`super` tinyint NOT NULL DEFAULT 0 COMMENT '是否超级菜单 (0-否 1-是)',
`name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '菜单名称(业务标识)',
`route_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '路由名称(Vue Router name,英文唯一;空则前端回退用 name)',
`title` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '菜单标题',
`title` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '菜单标题(侧栏)',
`tab_title` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '标签页标题(可空;空则回退 title)',
`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 '从定向地址',
@@ -0,0 +1,3 @@
-- 菜单标签标题:可空;空则前端/下发回退用 title,非空优先作标签页标题
ALTER TABLE `sys_menu`
ADD COLUMN `tab_title` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '标签页标题(可空;空则回退 title)' AFTER `title`;
@@ -35,10 +35,15 @@ public class CreateMenuDTO {
private String routeName;
/**
* 菜单标题
* 菜单标题(侧栏展示)
*/
private String title;
/**
* 标签页标题(可空;空则回退 title)
*/
private String tabTitle;
/**
* 路由路径
*/
@@ -35,10 +35,15 @@ public class UpdateMenuDTO {
private String routeName;
/**
* 菜单标题
* 菜单标题(侧栏展示)
*/
private String title;
/**
* 标签页标题(可空;空则回退 title)
*/
private String tabTitle;
/**
* 路由路径
*/
@@ -45,11 +45,17 @@ public class MenuPO {
private String routeName;
/**
* 菜单标题
* 菜单标题(侧栏展示)
*/
@TableField("title")
private String title;
/**
* 标签页标题(可空;空则回退 title;ALWAYS 保证清空可写库)
*/
@TableField(value = "tab_title", updateStrategy = FieldStrategy.ALWAYS)
private String tabTitle;
/**
* 路由路径
*/
@@ -42,10 +42,15 @@ public class MenuVO {
private String routeName;
/**
* 菜单标题
* 菜单标题(侧栏展示)
*/
private String title;
/**
* 标签页标题(可空;空则回退 title)
*/
private String tabTitle;
/**
* 路由路径
*/
@@ -62,11 +62,17 @@ public class UserMenuVO {
private Long parentId;
/**
* 路由标题
* 侧栏菜单标题
* 对应数据库: title 字段
*/
private String title;
/**
* 标签页标题(可空;空则前端回退 title)
* 对应数据库: tab_title 字段
*/
private String tabTitle;
/**
* 路由图标:优先为 icon_svg 原文,否则为 Iconify 名
* 对应数据库: icon_svg / icon
@@ -64,6 +64,7 @@ public class MenuServiceImpl extends ServiceImpl<MenuMapper, MenuPO> implements
// 创建菜单
MenuPO menu = new MenuPO();
BeanCopyUtils.copyProperties(createMenuDTO, menu);
normalizeBlankTabTitle(menu);
// 设置默认值
if (menu.getSuperMenu() == null) {
@@ -128,6 +129,7 @@ public class MenuServiceImpl extends ServiceImpl<MenuMapper, MenuPO> implements
// 更新菜单
BeanCopyUtils.copyProperties(updateMenuDTO, existingMenu);
existingMenu.setId(id); // 确保ID正确设置
normalizeBlankTabTitle(existingMenu);
// 设置更新人ID
existingMenu.setUpdateBy(currentUserContext.getCurrentUserId());
@@ -432,6 +434,15 @@ public class MenuServiceImpl extends ServiceImpl<MenuMapper, MenuPO> implements
return children;
}
/**
* 空白 tabTitle 落库为 null,便于「空则回退 title」语义
*/
private void normalizeBlankTabTitle(MenuPO menu) {
if (menu.getTabTitle() != null && menu.getTabTitle().isBlank()) {
menu.setTabTitle(null);
}
}
/**
* 转换为VO
*/
@@ -181,6 +181,10 @@ public class UserMenuServiceImpl implements UserMenuService {
meta.setId(menu.getId());
meta.setParentId(menu.getParentId());
meta.setTitle(menu.getTitle());
// 标签标题:非空才下发;空则前端回退用 title
if (menu.getTabTitle() != null && !menu.getTabTitle().isBlank()) {
meta.setTabTitle(menu.getTabTitle().trim());
}
// 侧栏图标:有 SVG 时写入 meta.icon,供 VbenIcon 优先渲染
if (menu.getIconSvg() != null && !menu.getIconSvg().isBlank()) {
meta.setIcon(menu.getIconSvg().trim());
@@ -13,8 +13,10 @@ export namespace MenuDTO {
name: string;
/** 路由名称(Vue Router name,建议英文唯一) */
routeName?: string;
/** 菜单标题 */
/** 菜单标题(侧栏) */
title?: string;
/** 标签页标题(可空;空则回退 title) */
tabTitle?: string;
/** 路由路径 */
path?: string;
/** 组件路径 */
@@ -47,8 +49,10 @@ export namespace MenuDTO {
name: string;
/** 路由名称(Vue Router name,建议英文唯一) */
routeName?: string;
/** 菜单标题 */
/** 菜单标题(侧栏) */
title?: string;
/** 标签页标题(可空;空则回退 title) */
tabTitle?: string;
/** 路由路径 */
path?: string;
/** 组件路径 */
@@ -16,8 +16,10 @@ export namespace MenuVO {
name: string;
/** 路由名称(Vue Router name) */
routeName?: string;
/** 菜单标题 */
/** 菜单标题(侧栏) */
title?: string;
/** 标签页标题(可空;空则回退 title) */
tabTitle?: string;
/** 路由路径 */
path?: string;
/** 组件路径 */
@@ -72,8 +72,10 @@ export namespace UserVO {
id: number;
/** 父路由ID */
parentId?: number;
/** 路由标题 */
/** 路由标题(侧栏) */
title: string;
/** 标签页标题(可空;空则回退 title) */
tabTitle?: string;
/** 路由图标 */
icon?: string;
/** 顺序 */
@@ -68,6 +68,8 @@ interface FormData {
name: string;
routeName: string;
title: string;
/** 标签页标题,空则回退菜单标题 */
tabTitle: string;
path: string;
component: string;
redirect: string;
@@ -87,6 +89,7 @@ const formData = ref<FormData>({
name: '',
routeName: '',
title: '',
tabTitle: '',
path: '',
component: '',
redirect: '',
@@ -350,6 +353,7 @@ function open(edit: boolean, record?: MenuVO.MenuInfo) {
name: record.name,
routeName: record.routeName || '',
title: record.title || '',
tabTitle: record.tabTitle || '',
path: record.path || '',
component: record.component || '',
redirect: record.redirect || '',
@@ -380,6 +384,7 @@ function open(edit: boolean, record?: MenuVO.MenuInfo) {
name: '',
routeName: '',
title: '',
tabTitle: '',
path: '',
component: '',
redirect: '',
@@ -399,6 +404,7 @@ function open(edit: boolean, record?: MenuVO.MenuInfo) {
name: '',
routeName: '',
title: '',
tabTitle: '',
path: '',
component: '',
redirect: '',
@@ -490,6 +496,7 @@ async function handleSubmit() {
...formData.value,
parentId: formData.value.parentId ?? null,
routeName: formData.value.routeName?.trim() || null,
tabTitle: formData.value.tabTitle?.trim() || null,
icon: formData.value.icon?.trim() || null,
iconSvg: formData.value.iconSvg?.trim() || null,
superMenu: formData.value.superMenu ? 1 : 0,
@@ -635,7 +642,17 @@ defineExpose({
</Form.Item>
<Form.Item label="菜单标题" name="title">
<Input v-model:value="formData.title" placeholder="请输入菜单标题" />
<Input
v-model:value="formData.title"
placeholder="侧边菜单显示名称"
/>
</Form.Item>
<Form.Item label="标签标题" name="tabTitle">
<Input
v-model:value="formData.tabTitle"
placeholder="可空;空则标签用菜单标题"
/>
</Form.Item>
<Form.Item label="路由路径" name="path">
@@ -92,6 +92,12 @@ const menuTableBaseColumns: VxeGridProps<any>['columns'] = [
minWidth: 150,
showOverflow: 'tooltip' as const,
},
{
field: 'tabTitle',
title: '标签标题',
minWidth: 120,
showOverflow: 'tooltip' as const,
},
{
field: 'icon',
title: '图标',
@@ -118,9 +118,17 @@ interface RouteMeta {
*/
query?: Recordable;
/**
* 标题名称
* 标题名称(侧栏菜单)
*/
title: string;
/**
* 标签页标题(可空;空则回退 title;运行时 setTabTitle 写入 newTabTitle 优先生效)
*/
tabTitle?: string;
/**
* 运行时覆盖的标签标题(如概览页动态名)
*/
newTabTitle?: string;
}
// 定义递归类型以将 RouteRecordRaw 的 component 属性更改为 string
@@ -41,7 +41,8 @@ const style = computed(() => {
const tabsView = computed(() => {
return props.tabs.map((tab) => {
const { fullPath, meta, name, path, key } = tab || {};
const { affixTab, icon, newTabTitle, tabClosable, title } = meta || {};
const { affixTab, icon, newTabTitle, tabClosable, tabTitle, title } =
meta || {};
return {
affixTab: !!affixTab,
closable: Reflect.has(meta, 'tabClosable') ? !!tabClosable : true,
@@ -51,7 +52,8 @@ const tabsView = computed(() => {
meta,
name,
path,
title: (newTabTitle || title || name) as string,
// 运行时覆盖 > 配置 tabTitle > 侧栏 title
title: (newTabTitle || tabTitle || title || name) as string,
} as TabConfig;
});
});
@@ -48,7 +48,8 @@ const typeWithClass = computed(() => {
const tabsView = computed(() => {
return props.tabs.map((tab) => {
const { fullPath, meta, name, path, key } = tab || {};
const { affixTab, icon, newTabTitle, tabClosable, title } = meta || {};
const { affixTab, icon, newTabTitle, tabClosable, tabTitle, title } =
meta || {};
return {
affixTab: !!affixTab,
closable: Reflect.has(meta, 'tabClosable') ? !!tabClosable : true,
@@ -58,7 +59,8 @@ const tabsView = computed(() => {
meta,
name,
path,
title: (newTabTitle || title || name) as string,
// 运行时覆盖 > 配置 tabTitle > 侧栏 title
title: (newTabTitle || tabTitle || title || name) as string,
} as TabConfig;
});
});
@@ -87,27 +87,35 @@ export function useTabbar() {
function wrapperTabLocale(tab: RouteLocationNormalizedGeneric) {
const title = tab?.meta?.title;
const tabTitle = tab?.meta?.tabTitle;
let processedTitle = 'Unknown';
let processedTabTitle: string | undefined;
try {
if (title && typeof title === 'string' && title.trim()) {
// 尝试国际化处理
processedTitle = $t(title);
} else if (tab?.name) {
// 如果没有 title,使用路由名称
processedTitle = tab.name as string;
}
if (tabTitle && typeof tabTitle === 'string' && tabTitle.trim()) {
processedTabTitle = $t(tabTitle);
}
} catch (error) {
console.warn('国际化处理失败,使用原始标题:', title, error);
// 如果国际化失败,直接使用原始标题
processedTitle = title || (tab?.name as string) || 'Unknown';
processedTabTitle =
tabTitle && typeof tabTitle === 'string' && tabTitle.trim()
? tabTitle
: undefined;
}
return {
...tab,
meta: {
...tab?.meta,
title: processedTitle,
// 空则不写入,展示层回退 title
...(processedTabTitle ? { tabTitle: processedTabTitle } : { tabTitle: undefined }),
},
};
}
@@ -579,6 +579,7 @@ function cloneTab(route: TabDefinition): TabDefinition {
meta: {
...meta,
newTabTitle: meta.newTabTitle,
tabTitle: meta.tabTitle,
},
};
}