refactor(menu): 外链/内嵌改为 type=E+component 统一判断
This commit is contained in:
@@ -9,6 +9,7 @@ import lombok.Getter;
|
|||||||
*
|
*
|
||||||
* C:目录
|
* C:目录
|
||||||
* M:菜单
|
* M:菜单
|
||||||
|
* E:外链
|
||||||
* B:按钮
|
* B:按钮
|
||||||
*/
|
*/
|
||||||
@Getter
|
@Getter
|
||||||
@@ -16,6 +17,7 @@ public enum MenuTypeEnum implements IBaseEnum<String> {
|
|||||||
|
|
||||||
CATALOG("C", "目录"),
|
CATALOG("C", "目录"),
|
||||||
MENU("M", "菜单"),
|
MENU("M", "菜单"),
|
||||||
|
EXTERNAL("E", "外链"),
|
||||||
BUTTON("B", "按钮");
|
BUTTON("B", "按钮");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ public class Menu extends BaseEntity {
|
|||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 菜单类型(C-目录 M-菜单 B-按钮)
|
* 菜单类型(C-目录 M-菜单 E-外链 B-按钮)
|
||||||
*/
|
*/
|
||||||
private String type;
|
private String type;
|
||||||
|
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ public class MenuForm {
|
|||||||
@Schema(description = "菜单名称")
|
@Schema(description = "菜单名称")
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@Schema(description = "菜单类型(C-目录 M-菜单 B-按钮)")
|
@Schema(description = "菜单类型(C-目录 M-菜单 E-外链 B-按钮)")
|
||||||
private String type;
|
private String type;
|
||||||
|
|
||||||
@Schema(description = "路由名称")
|
@Schema(description = "路由名称")
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ public class MenuVO {
|
|||||||
@Schema(description = "菜单名称")
|
@Schema(description = "菜单名称")
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@Schema(description="菜单类型(C-目录 M-菜单 B-按钮)")
|
@Schema(description="菜单类型(C-目录 M-菜单 E-外链 B-按钮)")
|
||||||
private String type;
|
private String type;
|
||||||
|
|
||||||
@Schema(description = "路由名称")
|
@Schema(description = "路由名称")
|
||||||
|
|||||||
@@ -54,6 +54,10 @@ public class RouteVO {
|
|||||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||||
private Boolean alwaysShow;
|
private Boolean alwaysShow;
|
||||||
|
|
||||||
|
@Schema(description = "外链地址(内嵌模式用)", example = "https://www.youlai.tech")
|
||||||
|
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||||
|
private String externalUrl;
|
||||||
|
|
||||||
@Schema(description = "路由参数")
|
@Schema(description = "路由参数")
|
||||||
private Map<String,String> params;
|
private Map<String,String> params;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -227,36 +227,50 @@ public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements Me
|
|||||||
private RouteVO toRouteVo(Menu menu) {
|
private RouteVO toRouteVo(Menu menu) {
|
||||||
RouteVO routeVo = new RouteVO();
|
RouteVO routeVo = new RouteVO();
|
||||||
String routePath = menu.getRoutePath();
|
String routePath = menu.getRoutePath();
|
||||||
boolean externalLink = StrUtil.startWithAny(routePath, "http://", "https://");
|
String externalUrl = menu.getExternalUrl();
|
||||||
|
|
||||||
// 获取路由名称
|
// 外链(type=E):component 为空是新标签页,component=iframe 是系统内嵌
|
||||||
|
boolean isExternal = MenuTypeEnum.EXTERNAL.getValue().equals(menu.getType());
|
||||||
|
boolean isEmbedded = isExternal && "iframe".equals(menu.getComponent());
|
||||||
|
|
||||||
|
// 新标签页:path 用外链地址;内嵌:path 用内部路由路径
|
||||||
|
String path = isExternal && !isEmbedded && StrUtil.isNotBlank(externalUrl)
|
||||||
|
? externalUrl
|
||||||
|
: routePath;
|
||||||
|
routeVo.setPath(path);
|
||||||
|
|
||||||
|
// 路由名称:外链不设 name(前端 filterRoutes 会过滤出路由注册表)
|
||||||
String routeName = menu.getRouteName();
|
String routeName = menu.getRouteName();
|
||||||
if (StrUtil.isBlank(routeName)) {
|
if (StrUtil.isBlank(routeName) && !isExternal) {
|
||||||
// 外链不做驼峰转换,使用唯一占位,避免 http:// 被解析异常
|
routeName = StringUtils.capitalize(StrUtil.toCamelCase(path, '-'));
|
||||||
routeName = externalLink
|
|
||||||
? "ext-" + menu.getId()
|
|
||||||
: StringUtils.capitalize(StrUtil.toCamelCase(routePath, '-'));
|
|
||||||
}
|
}
|
||||||
// 根据name路由跳转 this.$router.push({name:xxx})
|
|
||||||
routeVo.setName(routeName);
|
routeVo.setName(routeName);
|
||||||
|
|
||||||
// 根据path路由跳转 this.$router.push({path:xxx})
|
|
||||||
routeVo.setPath(routePath);
|
|
||||||
routeVo.setRedirect(menu.getRedirect());
|
routeVo.setRedirect(menu.getRedirect());
|
||||||
// 外链无组件
|
|
||||||
routeVo.setComponent(externalLink ? null : menu.getComponent());
|
if (isEmbedded) {
|
||||||
|
routeVo.setComponent("iframe");
|
||||||
|
} else if (isExternal) {
|
||||||
|
routeVo.setComponent(null);
|
||||||
|
} else {
|
||||||
|
routeVo.setComponent(menu.getComponent());
|
||||||
|
}
|
||||||
|
|
||||||
RouteVO.Meta meta = new RouteVO.Meta();
|
RouteVO.Meta meta = new RouteVO.Meta();
|
||||||
meta.setTitle(menu.getName());
|
meta.setTitle(menu.getName());
|
||||||
meta.setIcon(menu.getIcon());
|
meta.setIcon(menu.getIcon());
|
||||||
meta.setHidden(StatusEnum.DISABLE.getValue().equals(menu.getVisible()));
|
meta.setHidden(StatusEnum.DISABLE.getValue().equals(menu.getVisible()));
|
||||||
// 【菜单】是否开启页面缓存
|
// 【菜单/内嵌外链】是否开启页面缓存
|
||||||
if (MenuTypeEnum.MENU.getValue().equals(menu.getType())
|
if ((MenuTypeEnum.MENU.getValue().equals(menu.getType()) || isEmbedded)
|
||||||
&& ObjectUtil.equals(menu.getKeepAlive(), 1)) {
|
&& ObjectUtil.equals(menu.getKeepAlive(), 1)) {
|
||||||
meta.setKeepAlive(true);
|
meta.setKeepAlive(true);
|
||||||
}
|
}
|
||||||
meta.setAlwaysShow(ObjectUtil.equals(menu.getAlwaysShow(), 1));
|
meta.setAlwaysShow(ObjectUtil.equals(menu.getAlwaysShow(), 1));
|
||||||
|
|
||||||
|
if (isEmbedded && StrUtil.isNotBlank(externalUrl)) {
|
||||||
|
meta.setExternalUrl(externalUrl);
|
||||||
|
}
|
||||||
|
|
||||||
Map<String, Object> paramsMap = menu.getParams();
|
Map<String, Object> paramsMap = menu.getParams();
|
||||||
if (paramsMap != null && !paramsMap.isEmpty()) {
|
if (paramsMap != null && !paramsMap.isEmpty()) {
|
||||||
Map<String, String> paramMap = paramsMap.entrySet().stream()
|
Map<String, String> paramMap = paramsMap.entrySet().stream()
|
||||||
@@ -275,8 +289,8 @@ public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements Me
|
|||||||
public boolean saveMenu(MenuForm menuForm) {
|
public boolean saveMenu(MenuForm menuForm) {
|
||||||
|
|
||||||
String menuType = menuForm.getType();
|
String menuType = menuForm.getType();
|
||||||
boolean isExternalLink = MenuTypeEnum.MENU.getValue().equals(menuType)
|
boolean isExternal = MenuTypeEnum.EXTERNAL.getValue().equals(menuType);
|
||||||
&& StrUtil.startWithAny(menuForm.getRoutePath(), "http://", "https://");
|
boolean isEmbedded = isExternal && "iframe".equals(menuForm.getComponent());
|
||||||
|
|
||||||
if (MenuTypeEnum.CATALOG.getValue().equals(menuType)) { // 如果是目录
|
if (MenuTypeEnum.CATALOG.getValue().equals(menuType)) { // 如果是目录
|
||||||
String path = menuForm.getRoutePath();
|
String path = menuForm.getRoutePath();
|
||||||
@@ -284,8 +298,8 @@ public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements Me
|
|||||||
menuForm.setRoutePath("/" + path); // 一级目录需以 / 开头
|
menuForm.setRoutePath("/" + path); // 一级目录需以 / 开头
|
||||||
}
|
}
|
||||||
menuForm.setComponent("Layout");
|
menuForm.setComponent("Layout");
|
||||||
} else if (isExternalLink) {
|
} else if (isExternal && !isEmbedded) {
|
||||||
// 外链菜单组件设置为 null,通过 routePath 判断外链
|
// 外链新标签页:component 留空
|
||||||
menuForm.setComponent(null);
|
menuForm.setComponent(null);
|
||||||
}
|
}
|
||||||
if (Objects.equals(menuForm.getParentId(), menuForm.getId())) {
|
if (Objects.equals(menuForm.getParentId(), menuForm.getId())) {
|
||||||
@@ -302,8 +316,9 @@ public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements Me
|
|||||||
} else {
|
} else {
|
||||||
entity.setParams(null);
|
entity.setParams(null);
|
||||||
}
|
}
|
||||||
// 新增类型为菜单时候 路由名称唯一
|
// 菜单(M)和内嵌外链(E+iframe)需要路由名称唯一
|
||||||
if (MenuTypeEnum.MENU.getValue().equals(menuType) && !isExternalLink) {
|
boolean needsRouteName = MenuTypeEnum.MENU.getValue().equals(menuType) || isEmbedded;
|
||||||
|
if (needsRouteName) {
|
||||||
Assert.isFalse(this.exists(new LambdaQueryWrapper<Menu>()
|
Assert.isFalse(this.exists(new LambdaQueryWrapper<Menu>()
|
||||||
.eq(Menu::getRouteName, entity.getRouteName())
|
.eq(Menu::getRouteName, entity.getRouteName())
|
||||||
.ne(menuForm.getId() != null, Menu::getId, menuForm.getId())
|
.ne(menuForm.getId() != null, Menu::getId, menuForm.getId())
|
||||||
|
|||||||
Reference in New Issue
Block a user