角色菜单更新

This commit is contained in:
ludj
2022-11-30 19:18:35 +08:00
parent 4004f6d4df
commit 66aaa6f2b8
7 changed files with 144 additions and 151 deletions

View File

@@ -19,6 +19,8 @@ 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.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
@@ -29,6 +31,7 @@ import org.nl.modules.system.service.RoleService;
import org.nl.modules.system.service.dto.RoleDto;
import org.nl.modules.system.service.dto.RoleQueryCriteria;
import org.nl.modules.system.service.dto.RoleSmallDto;
import org.nl.modules.wql.core.bean.WQLObject;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@@ -60,44 +63,29 @@ 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);
}
@ApiOperation("导出角色数据")
@GetMapping(value = "/download")
@SaCheckPermission("role:list")
public void download(HttpServletResponse response, RoleQueryCriteria criteria) throws IOException {
roleService.download(roleService.queryAll(criteria), response);
}
@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);
}
@ApiOperation("查询角色")
@GetMapping
@SaCheckPermission("roles:list")
public ResponseEntity<Object> query(Map criteria, Pageable pageable){
return new ResponseEntity<>(roleService.queryAll(criteria,pageable),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<>(Dict.create().set("level", getLevels(null)),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 Role resources){
public ResponseEntity<Object> create(@Validated @RequestBody Role resources) {
if (resources.getId() != null) {
throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
throw new BadRequestException("A new " + ENTITY_NAME + " cannot already have an ID");
}
getLevels(resources.getLevel());
// roleService.create(resources);
@@ -108,7 +96,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,11 +105,20 @@ public class RoleController {
@Log("修改角色菜单")
@ApiOperation("修改角色菜单")
@PutMapping(value = "/menu")
@SaCheckPermission("roles:edit")
public ResponseEntity<Object> updateMenu(@RequestBody Role resources){
RoleDto role = roleService.findById(resources.getId());
getLevels(role.getLevel());
roleService.updateMenu(resources,role);
// @SaCheckPermission("roles:edit")
public ResponseEntity<Object> updateMenu(@RequestBody JSONObject form) {
String role_id = form.getString("id");
JSONArray menus = form.getJSONArray("menus");
WQLObject rmTab = WQLObject.getWQLObject("sys_roles_menus");
rmTab.delete("role_id = " + role_id + "");
for (int i = 0; i < menus.size(); i++) {
JSONObject json = menus.getJSONObject(i);
JSONObject param = new JSONObject();
param.put("role_id", role_id);
param.put("menu_id", json.getString("menu_id"));
rmTab.insert(param);
}
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
@@ -129,22 +126,17 @@ public class RoleController {
@ApiOperation("删除角色")
@DeleteMapping
@SaCheckPermission("roles:del")
public ResponseEntity<Object> delete(@RequestBody Set<Long> ids){
for (Long id : ids) {
RoleDto role = roleService.findById(id);
getLevels(role.getLevel());
}
// 验证是否被用户关联
// roleService.verification(ids);
public ResponseEntity<Object> delete(@RequestBody Set<Long> 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(Role::getLevel).collect(Collectors.toList());
int min = Collections.min(levels);
if(level != null){

View File

@@ -36,10 +36,12 @@ import java.util.Set;
public interface RoleService {
/**
* 查询全部数据
* @return /
* 查询数据分页
* @param whereJson 条件
* @param page 分页参数
* @return Map<String,Object>
*/
List<RoleDto> queryAll();
JSONObject queryAll(Map whereJson, Pageable page);
/**
* 根据ID查询
@@ -92,15 +94,6 @@ public interface RoleService {
* @param id /
*/
void untiedMenu(Long id);
/**
* 待条件分页查询
* @param whereJson 条件
* @param page 分页参数
* @return /
*/
Map<String,Object> queryAll(Map whereJson, Pageable page);
/**
* 查询全部
* @param criteria 条件
@@ -108,13 +101,6 @@ public interface RoleService {
*/
List<RoleDto> queryAll(RoleQueryCriteria criteria);
/**
* 导出数据
* @param queryAll 待导出的数据
* @param response /
* @throws IOException /
*/
void download(List<RoleDto> queryAll, HttpServletResponse response) throws IOException;
/**
* 获取用户权限信息

View File

@@ -182,7 +182,6 @@ public class MenuServiceImpl implements MenuService {
if ("0".equals(form.getString("pid"))) {
form.remove("pid");
}
//外链外联菜单
if ("1".equals(form.getString("i_frame"))) {
String http = "http://", https = "https://";
@@ -193,6 +192,42 @@ public class MenuServiceImpl implements MenuService {
menuTab.insert(form);
updateSubCnt(form.getString("pid"));
//更新所属系统system_type字段
updateSystemType(menu_id);
}
/**
* 更新某个菜单属于某个系统
*
* @param menu_id 菜单标识
*/
void updateSystemType(String menu_id) {
String rootMenuId = this.findRootMenuId(menu_id);
WQLObject menuTab = WQLObject.getWQLObject("sys_menu");
JSONObject rootMenuObj = menuTab.query("menu_id = '" + rootMenuId + "'").uniqueResult(0);
JSONObject param = new JSONObject();
param.put("system_type", rootMenuObj.getString("title"));
param.put("menu_id", menu_id);
menuTab.update(param);
}
/**
* 查找菜单顶级菜单标识
*
* @param menu_id
* @return
*/
private String findRootMenuId(String menu_id) {
WQLObject menuTab = WQLObject.getWQLObject("sys_menu");
JSONObject menuObj = menuTab.query("menu_id = '" + menu_id + "'").uniqueResult(0);
String pid = menuObj.getString("pid");
if (StrUtil.isEmpty(pid)) {
return menu_id;
} else {
return findRootMenuId(pid);
}
}
@Override
@@ -226,6 +261,8 @@ public class MenuServiceImpl implements MenuService {
// 计算父级菜单节点数目
updateSubCnt(oldPid);
updateSubCnt(newPid);
updateSystemType(newMenu.getMenu_id());
// 清理缓存
// delCaches(resources.getId());
}

View File

@@ -17,6 +17,7 @@ package org.nl.modules.system.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
@@ -38,6 +39,7 @@ import org.nl.modules.wql.WQL;
import org.nl.modules.wql.core.bean.ResultBean;
import org.nl.modules.wql.core.bean.WQLObject;
import org.nl.modules.wql.util.WqlUtil;
import org.nl.wms.sch.service.dto.RegionDto;
import org.nl.wms.util.IdUtil;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
@@ -69,21 +71,26 @@ public class RoleServiceImpl implements RoleService {
// private final UserCacheClean userCacheClean;
@Override
public List<RoleDto> queryAll() {
Sort sort = Sort.by(Sort.Direction.ASC, "level");
return roleMapper.toDto(roleRepository.findAll(sort));
public JSONObject queryAll(Map whereJson, Pageable page) {
WQLObject wo = WQLObject.getWQLObject("sys_role");
WQLObject rmTab = WQLObject.getWQLObject("sys_roles_menus");
ResultBean rb = wo.pagequery(WqlUtil.getHttpContext(page), "", "update_time desc");
final JSONObject json = rb.pageResult();
JSONArray arr = json.getJSONArray("content");
for (int i = 0; i < arr.size(); i++) {
JSONObject roleObj = arr.getJSONObject(i);
JSONArray menus = rmTab.query("role_id = '" + roleObj.getString("role_id") + "'").getResultJSONArray(0);
roleObj.put("menus", menus);
}
return json;
}
@Override
public List<RoleDto> queryAll(RoleQueryCriteria criteria) {
return roleMapper.toDto(roleRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
return null;
}
@Override
public Map<String, Object> queryAll(Map whereJson, Pageable page) {
ResultBean rb = WQLObject.getWQLObject("sys_role").pagequery(WqlUtil.getHttpContext(page), "", "");
return rb.pageResult();
}
@Override
@Cacheable(key = "'id:' + #p0")
@@ -153,11 +160,12 @@ public class RoleServiceImpl implements RoleService {
@Override
@Transactional(rollbackFor = Exception.class)
public void delete(Set<Long> ids) {
WQLObject wo = WQLObject.getWQLObject("sys_role");
WQLObject rmTab = WQLObject.getWQLObject("sys_roles_menus");
for (Long id : ids) {
// 更新相关缓存
delCaches(id, null);
wo.delete("role_id =" + id);
rmTab.delete("role_id =" + id);
}
roleRepository.deleteAllByIdIn(ids);
}
@Override
@@ -179,10 +187,10 @@ public class RoleServiceImpl implements RoleService {
public List<String> getPermissionList(JSONObject userDto) {
List<String> permission = new LinkedList<>();
// 查看是否为管理员
permission.add("admin");
permission.add("admin");
HashMap<String, String> map = new HashMap<>();
map.put("flag", "1");
map.put("user_id",userDto.getString("user_id"));
map.put("user_id", userDto.getString("user_id"));
JSONArray rows = WQL.getWO("SYS_MENU").addParamMap(map).process().getResultJSONArray(0);
for (int i = 0; i < rows.size(); i++) {
JSONObject jsonObject = rows.getJSONObject(i);
@@ -191,20 +199,6 @@ public class RoleServiceImpl implements RoleService {
return permission;
}
@Override
public void download(List<RoleDto> roles, HttpServletResponse response) throws IOException {
List<Map<String, Object>> list = new ArrayList<>();
for (RoleDto role : roles) {
Map<String, Object> map = new LinkedHashMap<>();
map.put("角色名称", role.getName());
map.put("角色级别", role.getLevel());
map.put("描述", role.getDescription());
// map.put("创建日期", role.getCreateTime());
list.add(map);
}
FileUtil.downloadExcel(list, response);
}
@Override
public void verification(Set<Long> ids) {
if (userRepository.countByRoles(ids) > 0) {