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}