菜单角色更新

This commit is contained in:
ludj
2022-11-30 14:52:48 +08:00
parent dc2bcc17eb
commit fb28f2a7f4
8 changed files with 85 additions and 108 deletions

View File

@@ -17,24 +17,17 @@ package org.nl.modules.system.rest;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.nl.modules.common.exception.BadRequestException;
import org.nl.modules.common.utils.PageUtil;
import org.nl.modules.common.utils.SecurityUtils;
import org.nl.modules.logging.annotation.Log;
import org.nl.modules.system.domain.Menu;
import org.nl.modules.system.service.MenuService;
import org.nl.modules.system.service.dto.MenuDto;
import org.nl.modules.system.service.dto.MenuQueryCriteria;
import org.nl.modules.system.service.mapstruct.MenuMapper;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@@ -60,7 +53,7 @@ public class MenuController {
@GetMapping(value = "/build")
@ApiOperation("根据用户获取菜单")
public ResponseEntity<Object> buildMenus() {
List<MenuDto> menuDtoList = menuService.findByUser(SecurityUtils.getCurrentUserId());
List<MenuDto> menuDtoList = menuService.findByUser(String.valueOf(SecurityUtils.getCurrentUserId()));
List<MenuDto> menuDtos = menuService.buildTree(menuDtoList);
return new ResponseEntity<>(menuService.buildMenus(menuDtos),HttpStatus.OK);
}
@@ -68,7 +61,7 @@ public class MenuController {
@ApiOperation("返回全部的菜单")
@GetMapping(value = "/lazy")
@SaCheckPermission(value = {"menu:list", "roles:list"}, mode = SaMode.AND)
public ResponseEntity<Object> query(@RequestParam Long pid) {
public ResponseEntity<Object> query(@RequestParam String pid) {
return new ResponseEntity<>(menuService.getMenus(pid), HttpStatus.OK);
}
@@ -92,12 +85,12 @@ public class MenuController {
@ApiOperation("根据菜单ID返回所有子节点ID包含自身ID")
@GetMapping(value = "/child")
@SaCheckPermission(value = {"menu:list", "roles:list"}, mode = SaMode.AND)
public ResponseEntity<Object> child(@RequestParam Long id) {
public ResponseEntity<Object> child(@RequestParam String id) {
Set<MenuDto> menuSet = new HashSet<>();
List<MenuDto> menuList = menuService.getMenus(id);
menuSet.add(menuService.findById(id));
menuSet = menuService.getChildMenus(menuList, menuSet);
Set<Long> ids = menuSet.stream().map(MenuDto::getMenu_id).collect(Collectors.toSet());
Set<String> ids = menuSet.stream().map(MenuDto::getMenu_id).collect(Collectors.toSet());
return new ResponseEntity<>(ids, HttpStatus.OK);
}
@@ -113,10 +106,10 @@ public class MenuController {
@ApiOperation("查询菜单:根据ID获取同级与上级数据")
@PostMapping("/superior")
@SaCheckPermission("menu:list")
public ResponseEntity<Object> getSuperior(@RequestBody List<Long> ids) {
public ResponseEntity<Object> getSuperior(@RequestBody List<String> ids) {
Set<MenuDto> menuDtos = new LinkedHashSet<>();
if (CollectionUtil.isNotEmpty(ids)) {
for (Long id : ids) {
for (String id : ids) {
MenuDto menuDto = menuService.findById(id);
menuDtos.addAll(menuService.getSuperior(menuDto, new ArrayList<>()));
}
@@ -147,12 +140,12 @@ public class MenuController {
@ApiOperation("删除菜单")
@DeleteMapping
@SaCheckPermission("menu:del")
public ResponseEntity<Object> delete(@RequestBody Set<Long> ids) {
public ResponseEntity<Object> delete(@RequestBody Set<String> ids) {
Set<MenuDto> menuSet = new HashSet<>();
for (Long id : ids) {
/* List<MenuDto> menuList = menuService.getMenus(id);*/
for (String id : ids) {
List<MenuDto> menuList = menuService.getMenus(id);
menuSet.add(menuService.findById(id));
/*menuSet = menuService.getChildMenus(menuMapper.toEntity(menuList), menuSet);*/
menuSet = menuService.getChildMenus(menuList, menuSet);
}
menuService.delete(menuSet);
return new ResponseEntity<>(HttpStatus.OK);

View File

@@ -19,8 +19,6 @@ import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.Dict;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
@@ -62,7 +60,7 @@ public class RoleController {
@ApiOperation("获取单个role")
@GetMapping(value = "/{id}")
@SaCheckPermission("roles:list")
public ResponseEntity<Object> query(@PathVariable Long id) {
public ResponseEntity<Object> query(@PathVariable Long id){
return new ResponseEntity<>(roleService.findById(id), HttpStatus.OK);
}
@@ -76,30 +74,33 @@ public class RoleController {
@ApiOperation("返回全部的角色")
@GetMapping(value = "/all")
@SaCheckPermission(value = {"roles:list", "user:add", "user:edit"}, mode = SaMode.AND)
public ResponseEntity<Object> query() {
return new ResponseEntity<>(roleService.queryAll(), HttpStatus.OK);
public ResponseEntity<Object> query(){
return new ResponseEntity<>(roleService.queryAll(),HttpStatus.OK);
}
@ApiOperation("查询角色")
@GetMapping
@SaCheckPermission("roles:list")
public ResponseEntity<Object> query(@RequestParam Map whereJson, Pageable page) {
JSONObject param = JSONObject.parseObject(JSON.toJSONString(whereJson));
return new ResponseEntity<>(roleService.queryAll(param, page), HttpStatus.OK);
public ResponseEntity<Object> query(Map criteria, Pageable pageable){
return new ResponseEntity<>(roleService.queryAll(criteria,pageable),HttpStatus.OK);
}
@ApiOperation("获取用户级别")
@GetMapping(value = "/level")
public ResponseEntity<Object> getLevel() {
return new ResponseEntity<>(2, HttpStatus.OK);
public ResponseEntity<Object> getLevel(){
return new ResponseEntity<>(Dict.create().set("level", getLevels(null)),HttpStatus.OK);
}
@Log("新增角色")
@ApiOperation("新增角色")
@PostMapping
@SaCheckPermission("roles:add")
public ResponseEntity<Object> create(@Validated @RequestBody JSONObject form) {
roleService.create(form);
public ResponseEntity<Object> create(@Validated @RequestBody Role resources){
if (resources.getId() != null) {
throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
}
getLevels(resources.getLevel());
// roleService.create(resources);
return new ResponseEntity<>(HttpStatus.CREATED);
}
@@ -107,7 +108,7 @@ public class RoleController {
@ApiOperation("修改角色")
@PutMapping
@SaCheckPermission("roles:edit")
public ResponseEntity<Object> update(@Validated(Role.Update.class) @RequestBody Role resources) {
public ResponseEntity<Object> update(@Validated(Role.Update.class) @RequestBody Role resources){
getLevels(resources.getLevel());
roleService.update(resources);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
@@ -117,10 +118,10 @@ public class RoleController {
@ApiOperation("修改角色菜单")
@PutMapping(value = "/menu")
@SaCheckPermission("roles:edit")
public ResponseEntity<Object> updateMenu(@RequestBody Role resources) {
public ResponseEntity<Object> updateMenu(@RequestBody Role resources){
RoleDto role = roleService.findById(resources.getId());
getLevels(role.getLevel());
roleService.updateMenu(resources, role);
roleService.updateMenu(resources,role);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
@@ -128,27 +129,26 @@ public class RoleController {
@ApiOperation("删除角色")
@DeleteMapping
@SaCheckPermission("roles:del")
public ResponseEntity<Object> delete(@RequestBody Set<Long> ids) {
public ResponseEntity<Object> delete(@RequestBody Set<Long> ids){
for (Long id : ids) {
RoleDto role = roleService.findById(id);
getLevels(role.getLevel());
}
// 验证是否被用户关联
roleService.verification(ids);
// roleService.verification(ids);
roleService.delete(ids);
return new ResponseEntity<>(HttpStatus.OK);
}
/**
* 获取用户的角色级别
*
* @return /
*/
private int getLevels(Integer level) {
private int getLevels(Integer level){
List<Integer> levels = roleService.findByUsersId(StpUtil.getLoginIdAsLong()).stream().map(RoleSmallDto::getLevel).collect(Collectors.toList());
int min = Collections.min(levels);
if (level != null) {
if (level < min) {
if(level != null){
if(level < min){
throw new BadRequestException("权限不足,你的角色级别:" + min + ",低于操作的角色级别:" + level);
}
}

View File

@@ -71,7 +71,7 @@ public interface MenuService {
* @param id /
* @return /
*/
MenuDto findById(long id);
MenuDto findById(String id);
/**
* 创建
@@ -126,7 +126,7 @@ public interface MenuService {
* @param pid /
* @return /
*/
List<MenuDto> getMenus(Long pid);
List<MenuDto> getMenus(String pid);
/**
* 根据ID获取同级与上级数据
@@ -143,5 +143,5 @@ public interface MenuService {
* @param currentUserId /
* @return /
*/
List<MenuDto> findByUser(Long currentUserId);
List<MenuDto> findByUser(String currentUserId);
}

View File

@@ -91,7 +91,7 @@ public interface RoleService {
* 解绑菜单
* @param id /
*/
void untiedMenu(Long id);
void untiedMenu(String id);
/**
* 待条件分页查询
@@ -134,7 +134,7 @@ public interface RoleService {
* 验证是否被用户关联
* @param ids /
*/
void verification(Set<Long> ids);
void verification(Set<String> ids);
/**
* 根据菜单Id查询

View File

@@ -32,7 +32,7 @@ import java.util.Objects;
@Setter
public class MenuDto extends BaseDTO implements Serializable {
private Long menu_id;
private String menu_id;
private List<MenuDto> children;
@@ -48,7 +48,7 @@ public class MenuDto extends BaseDTO implements Serializable {
private String component;
private Long pid;
private String pid;
private Integer sub_count;

View File

@@ -6,6 +6,7 @@ import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import org.nl.modules.common.exception.BadRequestException;
import org.nl.modules.common.utils.*;
import org.nl.modules.system.domain.Menu;
import org.nl.modules.system.domain.Role;
@@ -48,8 +49,8 @@ public class MenuServiceImpl implements MenuService {
JSONArray res = new JSONArray();
for (int i = 0; i < content.size(); i++) {
JSONObject obj = content.getJSONObject(i);
obj.put("menu_id", obj.getLong("menu_id"));
obj.put("pid", obj.getLong("pid"));
obj.put("menu_id", obj.getString("menu_id"));
obj.put("pid", obj.getString("pid"));
//构建前端需要的数据结构树
Integer sub_count = obj.getInteger("sub_count");
@@ -108,14 +109,14 @@ public class MenuServiceImpl implements MenuService {
@Override
public MenuDto menuJsonToMenuDto(JSONObject json) {
MenuDto menuDto = new MenuDto();
menuDto.setMenu_id(json.getLong("menu_id"));
menuDto.setMenu_id(json.getString("menu_id"));
menuDto.setType(json.getInteger("type"));
menuDto.setPermission(json.getString("permission"));
menuDto.setTitle(json.getString("title"));
menuDto.setMenu_sort(json.getInteger("menu_sort"));
menuDto.setPath(json.getString("path"));
menuDto.setComponent(json.getString("component"));
menuDto.setPid(json.getLong("pid"));
menuDto.setPid(json.getString("pid"));
menuDto.setSub_count(json.getInteger("sub_count"));
menuDto.setI_frame("1".equals(json.getString("i_frame")));
menuDto.setCache("1".equals(json.getString("cache")));
@@ -138,7 +139,7 @@ public class MenuServiceImpl implements MenuService {
@Override
// @Cacheable(key = "'id:' + #p0")
public MenuDto findById(long id) {
public MenuDto findById(String id) {
WQLObject menuTab = WQLObject.getWQLObject("sys_menu");
JSONObject json = menuTab.query("menu_id = '" + id + "'").uniqueResult(0);
return this.menuJsonToMenuDto(json);
@@ -152,48 +153,12 @@ public class MenuServiceImpl implements MenuService {
*/
@Override
// @Cacheable(key = "'user:' + #p0")
public List<MenuDto> findByUser(Long currentUserId) {
public List<MenuDto> findByUser(String currentUserId) {
JSONArray arr = WQL.getWO("QSYS_MENU01").addParam("flag", "4").addParam("user_id", String.valueOf(currentUserId)).process().getResultJSONArray(0);
List<MenuDto> list = new ArrayList<>();
for (int i = 0; i < arr.size(); i++) {
JSONObject json = arr.getJSONObject(i);
MenuDto dto = new MenuDto();
dto.setMenu_id(json.getLong("menu_id"));
dto.setType(json.getInteger("type"));
dto.setPermission(json.getString("permission"));
dto.setTitle(json.getString("title"));
dto.setPath(json.getString("path"));
dto.setComponent_name(json.getString("name"));
dto.setComponent(json.getString("component"));
dto.setIcon(json.getString("icon"));
dto.setMenu_sort(json.getInteger("menu_sort"));
dto.setSub_count(json.getInteger("sub_count"));
dto.setPid(json.getLong("pid"));
String cache = json.getString("cache");
String iFrame = json.getString("i_frame");
String hidden = json.getString("hidden");
if (StrUtil.equals(cache, "1")) {
dto.setCache(true);
} else {
dto.setCache(false);
}
if (StrUtil.equals(iFrame, "1")) {
dto.setI_frame(true);
} else {
dto.setI_frame(false);
}
if (StrUtil.equals(hidden, "1")) {
dto.setHidden(true);
} else {
dto.setHidden(false);
}
list.add(dto);
list.add(this.menuJsonToMenuDto(json));
}
return list;
}
@@ -210,10 +175,22 @@ public class MenuServiceImpl implements MenuService {
form.put("update_id", SecurityUtils.getCurrentUserId());
form.put("update_name", SecurityUtils.getCurrentNickName());
form.put("update_time", DateUtil.now());
//根节点数据库存为null
if ("0".equals(form.getString("pid"))){
form.remove("pid");
}
//外链外联菜单
if("1".equals(form.getString("i_frame"))){
String http = "http://", https = "https://";
if (!(form.getString("path").toLowerCase().startsWith(http)||form.getString("path").toLowerCase().startsWith(https))) {
throw new BadRequestException("外链必须以http://或者https://开头");
}
}
menuTab.insert(form);
//TODO 更新子节点数量
updateSubCnt(menu_id);
}
@Override
@@ -249,12 +226,12 @@ public class MenuServiceImpl implements MenuService {
}
@Override
public List<MenuDto> getMenus(Long pid) {
public List<MenuDto> getMenus(String pid) {
// 菜单表【sys_menu】
WQLObject menuTab = WQLObject.getWQLObject("sys_menu");
JSONArray menus;
if (pid != null && !pid.equals(0L)) {
menus = menuTab.query("pid = '" + pid + "'").getResultJSONArray(0);
menus = menuTab.query("menu_id = '" + pid + "'").getResultJSONArray(0);
} else {
menus = menuTab.query("(pid =0 or pid is null)").getResultJSONArray(0);
}
@@ -280,7 +257,7 @@ public class MenuServiceImpl implements MenuService {
return menus;
}
//pid 不为null
JSONArray arr = menuTab.query("pid = '"+menuDto.getPid()+"'").getResultJSONArray(0);
JSONArray arr = menuTab.query("pid = '" + menuDto.getPid() + "'").getResultJSONArray(0);
for (int i = 0; i < arr.size(); i++) {
JSONObject json = arr.getJSONObject(i);
menus.add(this.menuJsonToMenuDto(json));
@@ -291,7 +268,7 @@ public class MenuServiceImpl implements MenuService {
@Override
public List<MenuDto> buildTree(List<MenuDto> menuDtos) {
List<MenuDto> trees = new ArrayList<>();
Set<Long> ids = new HashSet<>();
Set<String> ids = new HashSet<>();
for (MenuDto menuDTO : menuDtos) {
if (menuDTO.getPid() == null) {
trees.add(menuDTO);
@@ -365,7 +342,7 @@ public class MenuServiceImpl implements MenuService {
return list;
}
private void updateSubCnt(Long menuId) {
private void updateSubCnt(String menuId) {
if (menuId != null) {
WQLObject menuTab = WQLObject.getWQLObject("sys_menu");
JSONArray arr = menuTab.query("pid = '" + menuId + "'").getResultJSONArray(0);
@@ -380,14 +357,14 @@ public class MenuServiceImpl implements MenuService {
*
* @param id 菜单ID
*/
public void delCaches(Long id) {
List<User> users = userRepository.findByMenuId(id);
public void delCaches(String id) {
/* List<User> users = userRepository.findByMenuId(id);
redisUtils.del("menu::id:" + id);
redisUtils.delByKeys("menu::user:", users.stream().map(User::getId).collect(Collectors.toSet()));
// 清除 Role 缓存
List<Role> roles = roleService.findInMenuId(new ArrayList<Long>() {{
List<Role> roles = roleService.findInMenuId(new ArrayList<String>() {{
add(id);
}});
redisUtils.delByKeys("role::id:", roles.stream().map(Role::getId).collect(Collectors.toSet()));
redisUtils.delByKeys("role::id:", roles.stream().map(Role::getId).collect(Collectors.toSet()));*/
}
}

View File

@@ -141,11 +141,16 @@ public class RoleServiceImpl implements RoleService {
}
@Override
public void untiedMenu(String id) {
}
/* @Override
@Transactional(rollbackFor = Exception.class)
public void untiedMenu(Long menuId) {
// 更新菜单
roleRepository.untiedMenu(menuId);
}
}*/
@Override
@Transactional(rollbackFor = Exception.class)
@@ -203,10 +208,10 @@ public class RoleServiceImpl implements RoleService {
}
@Override
public void verification(Set<Long> ids) {
if (userRepository.countByRoles(ids) > 0) {
public void verification(Set<String> ids) {
/*if (userRepository.countByRoles(ids) > 0) {
throw new BadRequestException("所选角色存在用户关联,请解除关联再试!");
}
}*/
}
@Override

View File

@@ -141,6 +141,7 @@
v-loading="crud.loading"
lazy
:load="getMenus"
:auto-load-root-options="false"
:data="crud.data"
:tree-props="{children: 'children', hasChildren: 'hasChildren'}"
row-key="menu_id"
@@ -151,6 +152,7 @@
>
<el-table-column type="selection" width="55" />
<el-table-column show-overflow-tooltip label="菜单标题" width="125px" prop="title" />
<el-table-column show-overflow-tooltip label="父Id" width="125px" prop="pid" />
<el-table-column show-overflow-tooltip label="子系统" width="125px" prop="system_type" />
<el-table-column show-overflow-tooltip label="菜单类别" width="125px" prop="category" />
<el-table-column prop="icon" label="图标" align="center" width="60px">
@@ -242,7 +244,7 @@ export default {
name: 'Menu',
components: { Treeselect, IconSelect, crudOperation, rrOperation, udOperation, DateRangePicker },
cruds() {
return CRUD({ title: '菜单', url: 'api/menus', crudMethod: { ...crudMenu }})
return CRUD({ title: '菜单', idField: 'menu_id', url: 'api/menus', crudMethod: { ...crudMenu }})
},
mixins: [presenter(), header(), form(defaultForm), crud()],
data() {
@@ -274,12 +276,12 @@ export default {
// 新增与编辑前做的操作
[CRUD.HOOK.afterToCU](crud, form) {
this.menus = []
if (form.menu_id) {
if (form.pid === null) {
if (form.menu_id) { // 修改
if (form.pid === null) { // 一级菜单一级的父级菜单的pid为0.
form.pid = 0
}
this.getSupDepts(form.menu_id)
} else {
} else { // 新增
this.menus.push({ menu_id: 0, title: '顶级类目', children: null })
}
},