Browse Source

菜单管理:增加删除按钮

WANGJIALIANG 4 years ago
parent
commit
dac02a43f1

+ 45 - 2
src/main/java/cn/hnthyy/thmz/controller/mz/MenuController.java

@@ -9,8 +9,6 @@ import cn.hnthyy.thmz.service.his.mz.EmployeeService;
 import cn.hnthyy.thmz.service.his.zd.ZdUnitCodeService;
 import cn.hnthyy.thmz.service.thmz.*;
 import cn.hnthyy.thmz.vo.MenuVo;
-import cn.hnthyy.thmz.vo.RoleMenuRelationVo;
-import cn.hnthyy.thmz.vo.RoleMenuVo;
 import cn.hnthyy.thmz.vo.UserMenuRelationVo;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
@@ -38,6 +36,8 @@ public class MenuController {
     private EmployeeService employeeService;
     @Autowired
     private UserService userService;
+    @Autowired
+    private RoleService roleService;
 
     /**
      * 获取所有的父菜单
@@ -514,5 +514,48 @@ public class MenuController {
         nodesMap.put(zd.getCode().trim(), tempParent);
     }
 
+    /**
+     * 删除菜单
+     *
+     * @param id
+     * @return
+     */
+    @UserLoginToken
+    @RequestMapping(value = "/deleteMenu", method = {RequestMethod.GET})
+    public Map<String, Object> deleteMenu(@RequestParam Long id) {
+        Map<String, Object> resultMap = new HashMap<>();
+        try {
+            //判断是否含有子菜单、角色关联菜单数据
+            List<Menu> menus = menuService.queryByParentId(id);
+            if (menus.size() > 0) {
+                resultMap.put("code", -1);
+                resultMap.put("message", "该菜单含有子菜单,禁止删除");
+                return resultMap;
+            }
+            List<RoleMenuRelation> roleMenuRelations = roleMenuRelationService.queryByMenuId(id);
+            for (int i = 0; i < roleMenuRelations.size(); i++) {
+                Role role = roleService.queryById(roleMenuRelations.get(i).getRoleId());
+                if(role.getDelFlag() == 1){//判断菜单关联的角色是否有效
+                    roleMenuRelations.remove(i);
+                    i--;
+                }
+            }
+            if (roleMenuRelations.size() > 0) {
+                resultMap.put("code", -1);
+                resultMap.put("message", "该菜单已与角色权限关联,禁止删除");
+                return resultMap;
+            }
+            menuService.removeById(id);
+            resultMap.put("code", 0);
+            resultMap.put("message", "删除菜单成功");
 
+            return resultMap;
+        } catch (Exception e) {
+            e.printStackTrace();
+            log.error("查询角色失败,错误信息{}", e.getMessage());
+            resultMap.put("code", -1);
+            resultMap.put("message", "删除角色失败");
+            return resultMap;
+        }
+    }
 }

+ 9 - 0
src/main/java/cn/hnthyy/thmz/mapper/thmz/MenuMapper.java

@@ -42,6 +42,15 @@ public interface MenuMapper {
     @Select("select * from t_menu where id=#{id,jdbcType=BIGINT}")
     Menu selectById(@Param("id") Long id);
 
+    /**
+     * 根据父菜单id查询菜单数据
+     *
+     * @param parentId
+     * @return
+     */
+    @Select("select * from t_menu where parent_id=#{parentId,jdbcType=BIGINT}")
+    List<Menu> selectByParentId(Long parentId);
+
     /**
      * 根据id集合查询菜单
      *

+ 9 - 0
src/main/java/cn/hnthyy/thmz/mapper/thmz/RoleMenuRelationMapper.java

@@ -31,6 +31,15 @@ public interface RoleMenuRelationMapper {
     @Select("select * from t_role_menu_relation where role_id=#{roleId,jdbcType=BIGINT}")
     List<RoleMenuRelation> selectByRoleId(@Param("roleId") Long roleId);
 
+    /**
+     * 根据menuId查询角色菜单关系
+     *
+     * @param menuId
+     * @return
+     */
+    @Select("select * from t_role_menu_relation where menu_id=#{menuId,jdbcType=BIGINT}")
+    List<RoleMenuRelation> selectByMenuId(@Param("menuId") Long menuId);
+
 
     /**
      * 根据角色id集合查询菜单关系

+ 5 - 0
src/main/java/cn/hnthyy/thmz/service/impl/thmz/MenuServiceImpl.java

@@ -42,6 +42,11 @@ public class MenuServiceImpl  implements MenuService {
         return menuMapper.selectById(id);
     }
 
+    @Override
+    public List<Menu> queryByParentId(Long parentId) {
+        return menuMapper.selectByParentId(parentId);
+    }
+
     @Override
     public List<Menu> queryByIds(List<Long> ids) {
         return menuMapper.selectByIds(ids);

+ 5 - 0
src/main/java/cn/hnthyy/thmz/service/impl/thmz/RoleMenuRelationServiceImpl.java

@@ -32,6 +32,11 @@ public class RoleMenuRelationServiceImpl implements RoleMenuRelationService {
         return roleMenuRelationMapper.selectByRoleIds(roleIds);
     }
 
+    @Override
+    public List<RoleMenuRelation> queryByMenuId(Long menuId) {
+        return roleMenuRelationMapper.selectByMenuId(menuId);
+    }
+
     @Override
     public List<RoleMenuRelation> queryByRoleId(Long roleId) {
         return roleMenuRelationMapper.selectByRoleId(roleId);

+ 8 - 0
src/main/java/cn/hnthyy/thmz/service/thmz/MenuService.java

@@ -30,6 +30,14 @@ public interface MenuService {
      */
     Menu queryById(Long id);
 
+    /**
+     * 根据父菜单id查询菜单数据
+     *
+     * @param parentId
+     * @return
+     */
+    List<Menu> queryByParentId(Long parentId);
+
     /**
      * 根据id集合查询菜单
      *

+ 8 - 0
src/main/java/cn/hnthyy/thmz/service/thmz/RoleMenuRelationService.java

@@ -26,6 +26,14 @@ public interface RoleMenuRelationService {
      */
     List<RoleMenuRelation> queryByRoleIds(List<Long> roleIds);
 
+    /**
+     * 根据菜单id查询角色菜单关系
+     *
+     * @param menuId
+     * @return
+     */
+    List<RoleMenuRelation> queryByMenuId(Long menuId);
+
     /**
      * 根据roleId查询菜单关系
      *

+ 28 - 2
src/main/resources/static/js/menu_manage.js

@@ -153,6 +153,7 @@ function getAllMenu() {
                 valign: 'middle',
                 formatter: function (value, row, index) {
                     var str = '<button type="button" class="btn btn-primary  btn-sm" onclick="editMenu(' + row.id + ')">编辑</button>';
+                    str += '<button type="button" class="btn btn-warning  btn-sm" onclick="deleteMenu(' + row.id + ')">删除</button>';
                     return [str].join('');
                 }
             },
@@ -265,9 +266,34 @@ function editMenu(id) {
             }
         }
     });
+}
 
-
-
+/**
+ * 删除菜单
+ * @param id
+ */
+function deleteMenu(id) {
+    if (!confirm("是否要删除当前菜单?")) {
+        return;
+    }
+    $.ajax({
+        type: "GET",
+        url: '/thmz/deleteMenu?id=' + id,
+        dataType: "json",
+        headers: {'Accept': 'application/json', 'Authorization': 'Bearer ' + localStorage.getItem("token")},
+        success: function (res) {
+            if (res == '401' || res == 401) {
+                window.location.href = '/thmz/login/view'
+                return;
+            }
+            if (res.code == 0) {
+                $('#tb_table').bootstrapTable('refresh');
+                successMesage(res);
+            } else {
+                errorMesage(res);
+            }
+        }
+    });
 }