权限修改
This commit is contained in:
parent
fe4fcd82f1
commit
45a704d4f7
|
|
@ -0,0 +1,40 @@
|
||||||
|
package com.czg.account.entity;
|
||||||
|
|
||||||
|
import com.mybatisflex.annotation.Id;
|
||||||
|
import com.mybatisflex.annotation.KeyType;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Administrator
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class BaseMenu {
|
||||||
|
@Id(keyType = KeyType.Auto)
|
||||||
|
private Long menuId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 图标
|
||||||
|
*/
|
||||||
|
private String icon;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 组件
|
||||||
|
*/
|
||||||
|
private String component;
|
||||||
|
/**
|
||||||
|
* 链接地址
|
||||||
|
*/
|
||||||
|
private String path;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 包含的接口
|
||||||
|
*/
|
||||||
|
private String apiInfo;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 组件名称
|
||||||
|
*/
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -7,10 +7,7 @@ import java.io.Serializable;
|
||||||
|
|
||||||
import java.io.Serial;
|
import java.io.Serial;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.*;
|
||||||
import lombok.Builder;
|
|
||||||
import lombok.Data;
|
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 实体类。
|
* 实体类。
|
||||||
|
|
@ -18,26 +15,14 @@ import lombok.NoArgsConstructor;
|
||||||
* @author zs
|
* @author zs
|
||||||
* @since 2025-05-26
|
* @since 2025-05-26
|
||||||
*/
|
*/
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
@Data
|
@Data
|
||||||
@Builder
|
@Builder
|
||||||
@NoArgsConstructor
|
|
||||||
@AllArgsConstructor
|
|
||||||
@Table("sys_cash_menu")
|
@Table("sys_cash_menu")
|
||||||
public class CashMenu implements Serializable {
|
public class CashMenu extends BaseMenu implements Serializable {
|
||||||
|
|
||||||
@Serial
|
@Serial
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
@Id(keyType = KeyType.Auto)
|
|
||||||
private Long menuId;
|
|
||||||
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
private String component;
|
|
||||||
|
|
||||||
private String path;
|
|
||||||
|
|
||||||
private String icon;
|
|
||||||
private String apiInfo;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import com.mybatisflex.annotation.KeyType;
|
||||||
import com.mybatisflex.annotation.Table;
|
import com.mybatisflex.annotation.Table;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
import java.io.Serial;
|
import java.io.Serial;
|
||||||
|
|
@ -18,21 +19,16 @@ import java.time.LocalDateTime;
|
||||||
* @author Administrator
|
* @author Administrator
|
||||||
* @since 2025-02-10
|
* @since 2025-02-10
|
||||||
*/
|
*/
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
@Data
|
@Data
|
||||||
|
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@Table("sys_menu")
|
@Table("sys_menu")
|
||||||
public class SysMenu implements Serializable {
|
public class SysMenu extends BaseMenu implements Serializable{
|
||||||
|
|
||||||
@Serial
|
@Serial
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
/**
|
|
||||||
* ID
|
|
||||||
*/
|
|
||||||
@Id(keyType = KeyType.Auto)
|
|
||||||
private Long menuId;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 上级菜单ID
|
* 上级菜单ID
|
||||||
|
|
@ -54,30 +50,13 @@ public class SysMenu implements Serializable {
|
||||||
*/
|
*/
|
||||||
private String title;
|
private String title;
|
||||||
|
|
||||||
/**
|
|
||||||
* 组件名称
|
|
||||||
*/
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 组件
|
|
||||||
*/
|
|
||||||
private String component;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 排序
|
* 排序
|
||||||
*/
|
*/
|
||||||
private Integer menuSort;
|
private Integer menuSort;
|
||||||
|
|
||||||
/**
|
|
||||||
* 图标
|
|
||||||
*/
|
|
||||||
private String icon;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 链接地址
|
|
||||||
*/
|
|
||||||
private String path;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 是否外链
|
* 是否外链
|
||||||
|
|
@ -139,10 +118,7 @@ public class SysMenu implements Serializable {
|
||||||
* 小程序组件
|
* 小程序组件
|
||||||
*/
|
*/
|
||||||
private String miniComponent;
|
private String miniComponent;
|
||||||
/**
|
|
||||||
* 包含的接口
|
|
||||||
*/
|
|
||||||
private String apiInfo;
|
|
||||||
/**
|
/**
|
||||||
* 接口地址
|
* 接口地址
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -9,10 +9,7 @@ import com.czg.account.dto.menu.MenuApiInfoItemDTO;
|
||||||
import com.czg.account.dto.role.RoleAddDTO;
|
import com.czg.account.dto.role.RoleAddDTO;
|
||||||
import com.czg.account.dto.role.RoleEditDTO;
|
import com.czg.account.dto.role.RoleEditDTO;
|
||||||
import com.czg.account.dto.role.RolePermissionDTO;
|
import com.czg.account.dto.role.RolePermissionDTO;
|
||||||
import com.czg.account.entity.CashMenu;
|
import com.czg.account.entity.*;
|
||||||
import com.czg.account.entity.SysMenu;
|
|
||||||
import com.czg.account.entity.SysRole;
|
|
||||||
import com.czg.account.entity.SysRolesMenus;
|
|
||||||
import com.czg.account.service.CashMenuService;
|
import com.czg.account.service.CashMenuService;
|
||||||
import com.czg.account.service.SysMenuService;
|
import com.czg.account.service.SysMenuService;
|
||||||
import com.czg.account.service.SysRoleService;
|
import com.czg.account.service.SysRoleService;
|
||||||
|
|
@ -28,7 +25,9 @@ import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import static com.mybatisflex.core.query.QueryMethods.column;
|
import static com.mybatisflex.core.query.QueryMethods.column;
|
||||||
|
|
@ -125,83 +124,82 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> imp
|
||||||
if (role == null) {
|
if (role == null) {
|
||||||
throw new ApiNotPrintException("角色不存在");
|
throw new ApiNotPrintException("角色不存在");
|
||||||
}
|
}
|
||||||
|
|
||||||
return sysRolesMenusService.queryChain().eq(SysRolesMenus::getRoleId, id).eq(SysRolesMenus::getType, type).list().stream().map(SysRolesMenus::getMenuId).toList();
|
return sysRolesMenusService.queryChain().eq(SysRolesMenus::getRoleId, id).eq(SysRolesMenus::getType, type).list().stream().map(SysRolesMenus::getMenuId).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean addMenu(Long roleId, List<Long> menuIds, boolean isAdmin) {
|
public boolean addMenu(Long roleId, List<Long> menuIds, boolean isAdmin) {
|
||||||
ArrayList<MenuApiInfoItemDTO> apiPathList = new ArrayList<>();
|
List<CompletableFuture<Void>> tasks = new ArrayList<>();
|
||||||
|
List<MenuApiInfoItemDTO> apiPathList = Collections.synchronizedList(new ArrayList<>());
|
||||||
|
List<SysRolesMenus> rolesMenus = Collections.synchronizedList(new ArrayList<>());
|
||||||
|
|
||||||
ArrayList<SysRolesMenus> rolesMenus = new ArrayList<>();
|
// Step 1: 获取菜单列表并校验
|
||||||
|
List<? extends BaseMenu> menuList;
|
||||||
if (isAdmin) {
|
if (isAdmin) {
|
||||||
List<SysMenu> sysMenuList = sysMenuService.queryChain().in(SysMenu::getMenuId, menuIds).list();
|
List<SysMenu> sysMenuList = sysMenuService.queryChain().in(SysMenu::getMenuId, menuIds).list();
|
||||||
if (sysMenuList.size() != menuIds.size()) {
|
if (sysMenuList.size() != menuIds.size()) throw new ApiNotPrintException("菜单id包含错误id");
|
||||||
throw new ApiNotPrintException("菜单id包含错误id");
|
menuList = sysMenuList;
|
||||||
|
} else {
|
||||||
|
List<CashMenu> cashMenuList = cashMenuService.queryChain().in(CashMenu::getMenuId, menuIds).list();
|
||||||
|
if (cashMenuList.size() != menuIds.size()) throw new ApiNotPrintException("菜单id包含错误id");
|
||||||
|
menuList = cashMenuList;
|
||||||
}
|
}
|
||||||
|
|
||||||
sysMenuList.forEach(item -> {
|
int type = isAdmin ? 0 : 1;
|
||||||
if (StrUtil.isNotBlank(item.getApiInfo())) {
|
|
||||||
List<MenuApiInfoItemDTO> itemDTOS = JSONArray.parseArray(item.getApiInfo()).toJavaList(MenuApiInfoItemDTO.class);
|
// Step 2: 并发处理 apiInfo 解析和绑定判断
|
||||||
|
for (BaseMenu menu : menuList) {
|
||||||
|
tasks.add(CompletableFuture.runAsync(() -> {
|
||||||
|
// 解析 apiInfo
|
||||||
|
if (StrUtil.isNotBlank(menu.getApiInfo())) {
|
||||||
|
List<MenuApiInfoItemDTO> itemDTOS = JSONArray.parseArray(menu.getApiInfo()).toJavaList(MenuApiInfoItemDTO.class);
|
||||||
if (!itemDTOS.isEmpty()) {
|
if (!itemDTOS.isEmpty()) {
|
||||||
apiPathList.addAll(itemDTOS);
|
apiPathList.addAll(itemDTOS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
for (SysMenu sysMenu : sysMenuList) {
|
// 检查是否已绑定
|
||||||
long count = sysRolesMenusService.count(new QueryWrapper().eq(SysRolesMenus::getMenuId, sysMenu.getMenuId())
|
long count = sysRolesMenusService.count(new QueryWrapper()
|
||||||
.eq(SysRolesMenus::getRoleId, roleId)
|
.eq("menu_id", menu.getMenuId())
|
||||||
.eq(SysRolesMenus::getType, isAdmin ? 0 : 1));
|
.eq("role_id", roleId)
|
||||||
|
.eq("type", type));
|
||||||
if (count == 0) {
|
if (count == 0) {
|
||||||
rolesMenus.add(new SysRolesMenus(sysMenu.getMenuId(), roleId, isAdmin ? 0 : 1));
|
rolesMenus.add(new SysRolesMenus(menu.getMenuId(), roleId, type));
|
||||||
}
|
|
||||||
}
|
|
||||||
}else {
|
|
||||||
List<CashMenu> list = cashMenuService.queryChain().in(CashMenu::getMenuId, menuIds).list();
|
|
||||||
if (list.size() != menuIds.size()) {
|
|
||||||
throw new ApiNotPrintException("菜单id包含错误id");
|
|
||||||
}
|
|
||||||
|
|
||||||
list.forEach(item -> {
|
|
||||||
if (StrUtil.isNotBlank(item.getApiInfo())) {
|
|
||||||
List<MenuApiInfoItemDTO> itemDTOS = JSONArray.parseArray(item.getApiInfo()).toJavaList(MenuApiInfoItemDTO.class);
|
|
||||||
if (!itemDTOS.isEmpty()) {
|
|
||||||
apiPathList.addAll(itemDTOS);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
for (CashMenu sysMenu : list) {
|
|
||||||
long count = sysRolesMenusService.count(new QueryWrapper().eq(SysRolesMenus::getMenuId, sysMenu.getMenuId())
|
|
||||||
.eq(SysRolesMenus::getRoleId, roleId)
|
|
||||||
.eq(SysRolesMenus::getType, isAdmin ? 0 : 1));
|
|
||||||
if (count == 0) {
|
|
||||||
rolesMenus.add(new SysRolesMenus(sysMenu.getMenuId(), roleId, isAdmin ? 0 : 1));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 等待上述任务完成
|
||||||
|
CompletableFuture.allOf(tasks.toArray(new CompletableFuture[0])).join();
|
||||||
|
|
||||||
|
// Step 3: 并发处理 apiPath 匹配菜单
|
||||||
if (!apiPathList.isEmpty()) {
|
if (!apiPathList.isEmpty()) {
|
||||||
String string = buildLikeSql(apiPathList, "url");
|
String likeSql = buildLikeSql(apiPathList, "url");
|
||||||
QueryWrapper wrapper = new QueryWrapper();
|
QueryWrapper wrapper = new QueryWrapper();
|
||||||
wrapper.where(string);
|
wrapper.where(likeSql);
|
||||||
List<SysMenu> sysMenus = sysMenuService.list(wrapper);
|
|
||||||
if (!sysMenus.isEmpty()) {
|
List<SysMenu> matchedMenus = sysMenuService.list(wrapper);
|
||||||
for (SysMenu sysMenu : sysMenus) {
|
List<CompletableFuture<Void>> matchTasks = new ArrayList<>();
|
||||||
long count = sysRolesMenusService.count(new QueryWrapper().eq(SysRolesMenus::getMenuId, sysMenu.getMenuId())
|
|
||||||
.eq(SysRolesMenus::getRoleId, roleId)
|
for (SysMenu matched : matchedMenus) {
|
||||||
.eq(SysRolesMenus::getType, isAdmin ? 0 : 1));
|
matchTasks.add(CompletableFuture.runAsync(() -> {
|
||||||
|
long count = sysRolesMenusService.count(new QueryWrapper()
|
||||||
|
.eq("menu_id", matched.getMenuId())
|
||||||
|
.eq("role_id", roleId)
|
||||||
|
.eq("type", type));
|
||||||
if (count == 0) {
|
if (count == 0) {
|
||||||
rolesMenus.add(new SysRolesMenus(sysMenu.getMenuId(), roleId, isAdmin ? 0 : 1));
|
rolesMenus.add(new SysRolesMenus(matched.getMenuId(), roleId, type));
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CompletableFuture.allOf(matchTasks.toArray(new CompletableFuture[0])).join();
|
||||||
|
}
|
||||||
|
|
||||||
return rolesMenus.isEmpty() || sysRolesMenusService.saveBatch(rolesMenus);
|
return rolesMenus.isEmpty() || sysRolesMenusService.saveBatch(rolesMenus);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public Boolean editPermission(long userId, RolePermissionDTO rolePermissionDTO) {
|
public Boolean editPermission(long userId, RolePermissionDTO rolePermissionDTO) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue