feat:新增菜单逻辑

新增菜单的上移和下移操作
This commit is contained in:
weitang 2025-06-27 11:39:32 +08:00
parent f2f9cb308b
commit ca3c5fd2c9
3 changed files with 66 additions and 0 deletions

View File

@ -47,6 +47,19 @@ public class MenuController {
}
}
/**
* 上移/下移菜单
*/
@PostMapping("/changeOrder")
public ResponseResult changeOrder(@RequestParam String menuId, @RequestParam String direction) {
boolean result = menuService.changeOrder(menuId, direction);
if (result) {
return ResponseResult.success();
} else {
return ResponseResult.error();
}
}
/***********************************
* 用途说明修改菜单及按钮根据ID
* 参数说明

View File

@ -49,4 +49,7 @@ public interface IMenuService extends IService<Menu> {
* 返回值说明: 菜单结构树集合
***********************************/
List<Map<String, Object>> permissionAssignment(String appId ,String roleId);
boolean changeOrder(String menuId, String direction);
}

View File

@ -214,6 +214,56 @@ public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IM
return listTree;
}
@Override
public boolean changeOrder(String menuId, String direction) {
Menu currentMenu = menuMapper.selectById(menuId);
if (currentMenu == null) {
return false;
}
String parentId = currentMenu.getParentid();
QueryWrapper<Menu> wrapper = new QueryWrapper<>();
wrapper.eq("parentid", parentId);
wrapper.orderByAsc("orderno");
List<Menu> siblings = menuMapper.selectList(wrapper);
int index = -1;
for (int i = 0; i < siblings.size(); i++) {
if (siblings.get(i).getId().equals(menuId)) {
index = i;
break;
}
}
if ("up".equals(direction)) {
if (index > 0) {
swapOrder(siblings.get(index), siblings.get(index - 1));
} else {
return false; // 已是第一个无法上移
}
} else if ("down".equals(direction)) {
if (index < siblings.size() - 1) {
swapOrder(siblings.get(index), siblings.get(index + 1));
} else {
return false; // 已是最后一个无法下移
}
} else {
return false; // 方向错误
}
return true;
}
private void swapOrder(Menu m1, Menu m2) {
Integer temp = m1.getOrderno();
m1.setOrderno(m2.getOrderno());
m2.setOrderno(temp);
menuMapper.updateById(m1);
menuMapper.updateById(m2);
}
/**********************************
* 用途说明: 生成权菜单权限树
* 参数说明 sysMenus