Skip to content

Commit 9e91f56

Browse files
committed
fix: 修复目录、菜单的组件名称重复的错误问题
1 parent d56b9aa commit 9e91f56

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

continew-admin-system/src/main/java/top/continew/admin/system/service/impl/MenuServiceImpl.java

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ public class MenuServiceImpl extends BaseServiceImpl<MenuMapper, MenuDO, MenuRes
5252
@Override
5353
public Long add(MenuReq req) {
5454
String title = req.getTitle();
55-
CheckUtils.throwIf(this.isNameExists(title, req.getParentId(), null), "新增失败,[{}] 已存在", title);
55+
CheckUtils.throwIf(this.isTitleExists(title, req.getParentId(), null), "新增失败,标题 [{}] 已存在", title);
56+
// 目录和菜单的组件名称不能重复
57+
if (!MenuTypeEnum.BUTTON.equals(req.getType())) {
58+
String name = req.getName();
59+
CheckUtils.throwIf(this.isNameExists(name, null), "新增失败,组件名称 [{}] 已存在", name);
60+
}
5661
// 目录类型菜单,默认为 Layout
5762
if (MenuTypeEnum.DIR.equals(req.getType())) {
5863
req.setComponent(StrUtil.blankToDefault(req.getComponent(), "Layout"));
@@ -64,7 +69,12 @@ public Long add(MenuReq req) {
6469
@Override
6570
public void update(MenuReq req, Long id) {
6671
String title = req.getTitle();
67-
CheckUtils.throwIf(this.isNameExists(title, req.getParentId(), id), "修改失败,[{}] 已存在", title);
72+
CheckUtils.throwIf(this.isTitleExists(title, req.getParentId(), id), "修改失败,标题 [{}] 已存在", title);
73+
// 目录和菜单的组件名称不能重复
74+
if (!MenuTypeEnum.BUTTON.equals(req.getType())) {
75+
String name = req.getName();
76+
CheckUtils.throwIf(this.isNameExists(name, id), "修改失败,组件名称 [{}] 已存在", name);
77+
}
6878
MenuDO oldMenu = super.getById(id);
6979
CheckUtils.throwIfNotEqual(req.getType(), oldMenu.getType(), "不允许修改菜单类型");
7080
super.update(req, id);
@@ -100,18 +110,33 @@ public List<MenuResp> listByRoleCode(String roleCode) {
100110
}
101111

102112
/**
103-
* 名称是否存在
113+
* 标题是否存在
104114
*
105-
* @param name 名称
115+
* @param title 标题
106116
* @param parentId 上级 ID
107117
* @param id ID
108-
* @return 是否存在
118+
* @return true:存在;false:不存在
109119
*/
110-
private boolean isNameExists(String name, Long parentId, Long id) {
120+
private boolean isTitleExists(String title, Long parentId, Long id) {
111121
return baseMapper.lambdaQuery()
112-
.eq(MenuDO::getTitle, name)
122+
.eq(MenuDO::getTitle, title)
113123
.eq(MenuDO::getParentId, parentId)
114124
.ne(null != id, MenuDO::getId, id)
115125
.exists();
116126
}
127+
128+
/**
129+
* 名称是否存在
130+
*
131+
* @param name 标题
132+
* @param id ID
133+
* @return true:存在;false:不存在
134+
*/
135+
private boolean isNameExists(String name, Long id) {
136+
return baseMapper.lambdaQuery()
137+
.eq(MenuDO::getName, name)
138+
.ne(MenuDO::getType, MenuTypeEnum.BUTTON)
139+
.ne(null != id, MenuDO::getId, id)
140+
.exists();
141+
}
117142
}

0 commit comments

Comments
 (0)