From 42aa2dfb34837b8c5c1c6e5099a611d4bcabad92 Mon Sep 17 00:00:00 2001 From: liuxiaoxu <1793812695@qq.com> Date: Fri, 11 Oct 2024 15:50:23 +0800 Subject: [PATCH] =?UTF-8?q?[feat]=20=E7=B3=BB=E7=BB=9F=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=20=E7=B3=BB=E7=BB=9F=E8=A7=92=E8=89=B2=E5=89=8D=E7=AB=AF?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=EF=BC=9A=E6=96=B0=E5=A2=9E=E5=AF=BC=E5=87=BA?= =?UTF-8?q?=E8=A7=92=E8=89=B2=E6=9D=83=E9=99=90=E5=89=8D=E7=AB=AF=E6=8C=89?= =?UTF-8?q?=E9=92=AE=EF=BC=8C=E6=96=B0=E5=A2=9E=E5=AF=BC=E5=87=BA=E5=85=A8?= =?UTF-8?q?=E9=83=A8=E5=92=8C=E5=AF=BC=E5=87=BA=E9=80=89=E6=8B=A9=E7=9A=84?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=9A=84js=E6=96=B9=E6=B3=95=20=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E8=A7=92=E8=89=B2=E5=90=8E=E7=AB=AF=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=EF=BC=9A=E6=96=B0=E5=A2=9E=E5=AF=BC=E5=87=BA=E5=85=A8=E9=83=A8?= =?UTF-8?q?=E8=A7=92=E8=89=B2=E6=9D=83=E9=99=90=E5=90=8E=E7=AB=AF=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=EF=BC=8C=E6=96=B0=E5=A2=9E=E5=AF=BC=E5=87=BA=E8=AF=BE?= =?UTF-8?q?=E9=80=89=E6=8B=A9=E7=9A=84=E6=95=B0=E6=8D=AE=E5=90=8E=E7=AB=AF?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=EF=BC=9B=20=E7=B3=BB=E7=BB=9F=E8=A7=92?= =?UTF-8?q?=E8=89=B2mapper=EF=BC=8Cservice=E5=B1=82=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E8=A7=92=E8=89=B2id=E6=89=B9=E9=87=8F?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E8=A7=92=E8=89=B2=E6=95=B0=E6=8D=AE=E9=9B=86?= =?UTF-8?q?=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/system/SysRoleController.java | 106 +++++++++++++++++- .../resources/templates/system/role/role.html | 59 ++++++++++ .../ruoyi/system/mapper/SysRoleMapper.java | 8 ++ .../ruoyi/system/service/ISysRoleService.java | 10 ++ .../service/impl/SysRoleServiceImpl.java | 12 ++ .../resources/mapper/system/SysRoleMapper.xml | 12 +- 6 files changed, 200 insertions(+), 7 deletions(-) diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysRoleController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysRoleController.java index 57d9aeac..514ca1be 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysRoleController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysRoleController.java @@ -1,16 +1,20 @@ package com.ruoyi.web.controller.system; +import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; + +import com.alibaba.excel.EasyExcel; +import com.ruoyi.common.core.domain.Ztree; +import com.ruoyi.common.core.domain.entity.vo.ExportRoleMenuVo; +import com.ruoyi.system.domain.SysSalesShippingInform; +import com.ruoyi.system.service.ISysMenuService; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.validation.annotation.Validated; -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.ResponseBody; +import org.springframework.web.bind.annotation.*; import com.ruoyi.common.annotation.Log; import com.ruoyi.common.constant.UserConstants; import com.ruoyi.common.core.controller.BaseController; @@ -43,6 +47,9 @@ public class SysRoleController extends BaseController @Autowired private ISysUserService userService; + @Autowired + private ISysMenuService menuService; + @RequiresPermissions("system:role:view") @GetMapping() public String role() @@ -295,4 +302,93 @@ public class SysRoleController extends BaseController { return toAjax(roleService.insertAuthUsers(roleId, userIds)); } + + /** + * 导出全部角色权限 + */ + @RequiresPermissions("system:role:export") + @Log(title = "角色管理", businessType = BusinessType.EXPORT) + @PostMapping("/exportAllRoleMenu") + @ResponseBody + public AjaxResult exportAllRoleMenu(SysRole role) { + try { + // 获取所有角色列表 + List sysRoles = roleService.selectRoleList(role); + + // 获取当前用户的ID + Long userId = ShiroUtils.getUserId(); + + // 初始化导出的角色菜单VO列表 + List exportRoleMenuVos = new ArrayList<>(); + + // 处理每个角色及其关联的已选菜单 + sysRoles.forEach(sysRole -> { + List ztrees = menuService.roleMenuTreeData(sysRole, userId); + List checkedMenus = ztrees.stream() + .filter(Ztree::isChecked) //只需要勾选的菜单权限 + .collect(Collectors.toList()); + + // 缓存角色信息以减少数据库查询次数 + SysRole tempSysRole = roleService.selectRoleById(sysRole.getRoleId()); + + checkedMenus.forEach(data -> { + ExportRoleMenuVo exportRoleMenuVo = new ExportRoleMenuVo(); + exportRoleMenuVo.setRoleName(tempSysRole.getRoleName()); + exportRoleMenuVo.setRoleKey(tempSysRole.getRoleKey()); + exportRoleMenuVo.setTitle(data.getTitle()); + exportRoleMenuVos.add(exportRoleMenuVo); + }); + }); + + ExcelUtil util = new ExcelUtil(ExportRoleMenuVo.class); + return util.exportExcel(exportRoleMenuVos, "角色菜单数据"); + } catch (Exception e) { + return AjaxResult.error("导出角色菜单数据失败"); + } + } + + + /** + * 导出选择的角色权限 + */ + @RequiresPermissions("system:role:export") + @Log(title = "角色管理", businessType = BusinessType.EXPORT) + @PostMapping("/exportRoleMenu") + @ResponseBody + public AjaxResult exportRoleMenu(@RequestBody String[] roleIds) { + try { + // 获取所有角色列表 + List sysRoles = roleService.selectRoleListByRoleIds(roleIds); + + // 获取当前用户的ID + Long userId = ShiroUtils.getUserId(); + + // 初始化导出的角色菜单VO列表 + List exportRoleMenuVos = new ArrayList<>(); + + // 处理每个角色及其关联的已选菜单 + sysRoles.forEach(sysRole -> { + List ztrees = menuService.roleMenuTreeData(sysRole, userId); + List checkedMenus = ztrees.stream() + .filter(Ztree::isChecked) //只需要勾选的菜单权限 + .collect(Collectors.toList()); + + // 缓存角色信息以减少数据库查询次数 + SysRole tempSysRole = roleService.selectRoleById(sysRole.getRoleId()); + + checkedMenus.forEach(data -> { + ExportRoleMenuVo exportRoleMenuVo = new ExportRoleMenuVo(); + exportRoleMenuVo.setRoleName(tempSysRole.getRoleName()); + exportRoleMenuVo.setRoleKey(tempSysRole.getRoleKey()); + exportRoleMenuVo.setTitle(data.getTitle()); + exportRoleMenuVos.add(exportRoleMenuVo); + }); + }); + ExcelUtil util = new ExcelUtil(ExportRoleMenuVo.class); + return util.exportExcel(exportRoleMenuVos, "角色菜单数据"); + } catch (Exception e) { + return AjaxResult.error("导出角色菜单数据失败"); + } + } + } \ No newline at end of file diff --git a/ruoyi-admin/src/main/resources/templates/system/role/role.html b/ruoyi-admin/src/main/resources/templates/system/role/role.html index b3836611..7cbc4880 100644 --- a/ruoyi-admin/src/main/resources/templates/system/role/role.html +++ b/ruoyi-admin/src/main/resources/templates/system/role/role.html @@ -50,6 +50,10 @@ 导出 + + 导出角色权限 + +
@@ -160,6 +164,61 @@ $.operate.post(prefix + "/changeStatus", { "roleId": roleId, "status": 0 }); }) } + + + //导出角色权限 + function exportRoleMenu(formId) { + // $.table.set(); + var roleIdData = []; + var selections = $("#bootstrap-table").bootstrapTable("getSelections"); + if(selections.length === 0){ + $.modal.confirm("确定导出所有角色权限吗?", function() { + var currentId = $.common.isEmpty(formId) ? $('form').attr('id') : formId; + var params = $("#bootstrap-table").bootstrapTable('getOptions'); + var dataParam = $("#" + currentId).serializeArray(); + dataParam.push({ "name": "orderByColumn", "value": params.sortName }); + dataParam.push({ "name": "isAsc", "value": params.sortOrder }); + $.modal.loading("正在导出数据,请稍后..."); + $.post(prefix + '/exportAllRoleMenu', dataParam, function(result) { + if (result.code == web_status.SUCCESS) { + window.location.href = ctx + "common/download?fileName=" + encodeURI(result.msg) + "&delete=" + true; + } else if (result.code == web_status.WARNING) { + $.modal.alertWarning(result.msg) + } else { + $.modal.alertError(result.msg); + } + $.modal.closeLoading(); + }); + }); + }else { + $.modal.confirm("确定导出选中的所有角色权限吗?", function () { + //·拼接单号 + for(let i=0;i \ No newline at end of file diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysRoleMapper.java b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysRoleMapper.java index 78a3ec72..0b375e65 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysRoleMapper.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysRoleMapper.java @@ -81,4 +81,12 @@ public interface SysRoleMapper * @return 角色信息 */ public SysRole checkRoleKeyUnique(String roleKey); + + /** + * 批量查询角色数据 + * + * @param roleIds 角色id集合 + * @return 角色数据集合信息 + */ + List selectRoleListByRoleIds(String[] roleIds); } diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysRoleService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysRoleService.java index af6921ef..02763fbb 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysRoleService.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysRoleService.java @@ -20,6 +20,16 @@ public interface ISysRoleService */ public List selectRoleList(SysRole role); + + /** + * 根据条件分页查询角色数据 + * + * @param roleIds 角色id集合 + * @return 角色数据集合信息 + */ + public List selectRoleListByRoleIds(String[] roleIds); + + /** * 根据用户ID查询角色 * diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysRoleServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysRoleServiceImpl.java index 3543f45a..ed4ee6d7 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysRoleServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysRoleServiceImpl.java @@ -57,6 +57,18 @@ public class SysRoleServiceImpl implements ISysRoleService return roleMapper.selectRoleList(role); } + /** + * 批量查询角色数据 + * + * @param roleIds 角色id集合 + * @return 角色数据集合信息 + */ + @Override + public List selectRoleListByRoleIds(String[] roleIds) { + List list = roleMapper.selectRoleListByRoleIds(roleIds); + return list; + } + /** * 根据用户ID查询权限 * diff --git a/ruoyi-system/src/main/resources/mapper/system/SysRoleMapper.xml b/ruoyi-system/src/main/resources/mapper/system/SysRoleMapper.xml index e2972c66..588f2364 100644 --- a/ruoyi-system/src/main/resources/mapper/system/SysRoleMapper.xml +++ b/ruoyi-system/src/main/resources/mapper/system/SysRoleMapper.xml @@ -77,8 +77,16 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" where r.role_key=#{roleKey} limit 1 - - + + + + update sys_role set del_flag = '2' where role_id = #{roleId}