diff --git a/src/main/java/com/ruoyi/project/system/menu/controller/MenuController.java b/src/main/java/com/ruoyi/project/system/menu/controller/MenuController.java index 6b6a8fa2..ad47043a 100644 --- a/src/main/java/com/ruoyi/project/system/menu/controller/MenuController.java +++ b/src/main/java/com/ruoyi/project/system/menu/controller/MenuController.java @@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.ruoyi.common.utils.security.AuthorizationUtils; import com.ruoyi.framework.aspectj.lang.annotation.Log; @@ -139,6 +140,17 @@ public class MenuController extends BaseController return toAjax(menuService.updateMenu(menu)); } + /** + * 保存菜单排序 + */ + @PostMapping("/updateSort") + @ResponseBody + public AjaxResult updateSort(@RequestParam String[] menuIds, @RequestParam String[] orderNums) + { + menuService.updateMenuSort(menuIds, orderNums); + return success(); + } + /** * 选择菜单图标 */ diff --git a/src/main/java/com/ruoyi/project/system/menu/mapper/MenuMapper.java b/src/main/java/com/ruoyi/project/system/menu/mapper/MenuMapper.java index 8ad45c25..2448955a 100644 --- a/src/main/java/com/ruoyi/project/system/menu/mapper/MenuMapper.java +++ b/src/main/java/com/ruoyi/project/system/menu/mapper/MenuMapper.java @@ -121,6 +121,14 @@ public interface MenuMapper */ public int updateMenu(Menu menu); + /** + * 保存菜单排序 + * + * @param menuIds 菜单ID + * @param orderNums 排序ID + */ + public void updateMenuSort(Menu menu); + /** * 校验菜单名称是否唯一 * diff --git a/src/main/java/com/ruoyi/project/system/menu/service/IMenuService.java b/src/main/java/com/ruoyi/project/system/menu/service/IMenuService.java index 8f947f30..c9661656 100644 --- a/src/main/java/com/ruoyi/project/system/menu/service/IMenuService.java +++ b/src/main/java/com/ruoyi/project/system/menu/service/IMenuService.java @@ -125,6 +125,14 @@ public interface IMenuService */ public int updateMenu(Menu menu); + /** + * 保存菜单排序 + * + * @param menuIds 菜单ID + * @param orderNums 排序ID + */ + public void updateMenuSort(String[] menuIds, String[] orderNums); + /** * 校验菜单名称是否唯一 * diff --git a/src/main/java/com/ruoyi/project/system/menu/service/MenuServiceImpl.java b/src/main/java/com/ruoyi/project/system/menu/service/MenuServiceImpl.java index beffaad0..de04cbd3 100644 --- a/src/main/java/com/ruoyi/project/system/menu/service/MenuServiceImpl.java +++ b/src/main/java/com/ruoyi/project/system/menu/service/MenuServiceImpl.java @@ -10,10 +10,13 @@ import java.util.List; import java.util.Set; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import com.ruoyi.common.constant.UserConstants; +import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.TreeUtils; import com.ruoyi.common.utils.security.ShiroUtils; +import com.ruoyi.common.utils.text.Convert; import com.ruoyi.framework.web.domain.Ztree; import com.ruoyi.project.system.menu.domain.Menu; import com.ruoyi.project.system.menu.mapper.MenuMapper; @@ -325,6 +328,31 @@ public class MenuServiceImpl implements IMenuService return menuMapper.updateMenu(menu); } + /** + * 保存菜单排序 + * + * @param menuIds 菜单ID + * @param orderNums 排序ID + */ + @Transactional + public void updateMenuSort(String[] menuIds, String[] orderNums) + { + try + { + for (int i = 0; i < menuIds.length; i++) + { + Menu menu = new Menu(); + menu.setMenuId(Convert.toLong(menuIds[i])); + menu.setOrderNum(orderNums[i]); + menuMapper.updateMenuSort(menu); + } + } + catch (Exception e) + { + throw new ServiceException("保存排序异常,请联系管理员"); + } + } + /** * 校验菜单名称是否唯一 * diff --git a/src/main/resources/mybatis/system/MenuMapper.xml b/src/main/resources/mybatis/system/MenuMapper.xml index ce9f133e..be84ac19 100644 --- a/src/main/resources/mybatis/system/MenuMapper.xml +++ b/src/main/resources/mybatis/system/MenuMapper.xml @@ -188,4 +188,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" ) + + update sys_menu + set order_num = #{orderNum} + where menu_id = #{menuId} + + \ No newline at end of file diff --git a/src/main/resources/templates/system/menu/menu.html b/src/main/resources/templates/system/menu/menu.html index 45baba91..18093cff 100644 --- a/src/main/resources/templates/system/menu/menu.html +++ b/src/main/resources/templates/system/menu/menu.html @@ -35,7 +35,10 @@ 修改 - + + 保存排序 + + 展开/折叠 @@ -52,6 +55,7 @@ var removeFlag = [[${@permission.hasPermi('system:menu:remove')}]]; var datas = [[${@dict.getType('sys_show_hide')}]]; var prefix = ctx + "system/menu"; + var originalOrders = {}; $(function() { var options = { @@ -87,14 +91,20 @@ title: '排序', width: '10', widthUnit: '%', - align: "left" + align: "center", + formatter: function(value, row, index) { + var menuIdText = $.common.sprintf("", row.menuId); + var orderNumText = $.common.sprintf("", row.orderNum); + originalOrders[row.menuId] = row.orderNum; + return menuIdText + orderNumText; + } }, { field: 'url', title: '请求地址', width: '15', widthUnit: '%', - align: "left", + align: "center", formatter: function(value, row, index) { return $.table.tooltip(value); } @@ -104,7 +114,7 @@ field: 'menuType', width: '10', widthUnit: '%', - align: "left", + align: "center", formatter: function(value, item, index) { if (item.menuType == 'M') { return '目录'; @@ -122,7 +132,7 @@ title: '可见', width: '10', widthUnit: '%', - align: "left", + align: "center", formatter: function(value, row, index) { if (row.menuType == 'F') { return '-'; @@ -135,7 +145,7 @@ title: '权限标识', width: '15', widthUnit: '%', - align: "left", + align: "center", formatter: function(value, row, index) { return $.table.tooltip(value); } @@ -144,7 +154,7 @@ title: '操作', width: '20', widthUnit: '%', - align: "left", + align: "center", formatter: function(value, row, index) { var actions = []; actions.push('编辑 '); @@ -156,6 +166,25 @@ }; $.treeTable.init(options); }); + + /* 保存排序-菜单 */ + function saveSort() { + var changedMenuIds = []; + var changedOrderNums = []; + $("input[name='menuIds']").each(function() { + var menuId = $(this).val(); + var currentOrder = $(this).next("input[name='orderNums']").val(); + if (originalOrders[menuId] !== currentOrder) { + changedMenuIds.push(menuId); + changedOrderNums.push(currentOrder); + } + }); + if (changedMenuIds.length === 0) { + $.modal.alertWarning("未检测到排序修改"); + return; + } + $.operate.post(prefix + "/updateSort", { "menuIds": changedMenuIds.join(","), "orderNums": changedOrderNums.join(",") }); + } \ No newline at end of file