package com.cloud.user.controller;
|
|
//import com.cloud.common.utils.AppUserUtil;
|
import com.cloud.model.common.Result;
|
import com.cloud.model.sys.LoginAppUser;
|
import com.cloud.model.sys.SysMenu;
|
import com.cloud.model.sys.SysRole;
|
import com.cloud.user.service.SysMenuService;
|
import com.cloud.user.service.TokenService;
|
import io.swagger.annotations.Api;
|
import io.swagger.annotations.ApiImplicitParam;
|
import io.swagger.annotations.ApiImplicitParams;
|
import io.swagger.annotations.ApiOperation;
|
import lombok.extern.log4j.Log4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.http.HttpStatus;
|
import org.springframework.http.MediaType;
|
import org.springframework.util.CollectionUtils;
|
import org.springframework.web.bind.annotation.*;
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
@RestController
|
@RequestMapping("/data/api-u/sysmenus")
|
@Api(value = "SysMenuController", description = "菜单控制层")
|
public class SysMenuController {
|
|
@Autowired
|
private SysMenuService sysMenuService;
|
@Autowired
|
private TokenService tokenService;
|
/**
|
* 当前登录用户的菜单
|
*/
|
@GetMapping("/me")
|
@ApiOperation(value = "查询用户菜单", notes = "当前登录用户的菜单", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
public Result findMyMenu(@RequestParam Map<String,Object> params) {
|
LoginAppUser loginAppUser = tokenService.currentUser();
|
if(loginAppUser ==null){//redis中token过期
|
return Result.custom("登录过期,请重新登录!", 401, false, "登录过期,请重新登录!");
|
}
|
Set<SysRole> roles = loginAppUser.getSysRoles();
|
if (CollectionUtils.isEmpty(roles)) {
|
return Result.ok("查询成功", Collections.emptyList());
|
// return Collections.emptyList();
|
}
|
|
Set<Long> roleIds = roles.parallelStream().map(SysRole::getId).collect(Collectors.toSet());
|
params.put("roleIds",roleIds);
|
Set<SysMenu> menusSet = sysMenuService
|
.findByRoles(params);
|
|
List<SysMenu> menus = new ArrayList<SysMenu>(menusSet);
|
Collections.sort(menus, new Comparator<SysMenu>() {
|
@Override
|
public int compare(SysMenu o1, SysMenu o2) {
|
return o1.getSort()<o2.getSort() ? -1 :1;
|
}
|
});
|
|
//判断是否选择模块
|
List<SysMenu> firstLevelMenus = null;
|
if(params.containsKey("module")&¶ms.get("module")!=null&&!params.get("module").toString().equals("")){
|
String module = params.get("module").toString();
|
for(SysMenu menu: menus){
|
if(menu.getType()==0&&menu.getModule().equals(module)){
|
final Object parentId = menu.getId();
|
firstLevelMenus = menus.stream().filter(m -> m.getParentId().equals(parentId))
|
.collect(Collectors.toList());
|
}
|
}
|
}
|
if(firstLevelMenus == null){
|
firstLevelMenus = menus.stream().filter(m -> m.getParentId().equals(0L))
|
.collect(Collectors.toList());
|
}
|
firstLevelMenus.forEach(m -> {
|
setChild(m, menus);
|
});
|
// return firstLevelMenus;
|
return Result.ok("查询成功", firstLevelMenus);
|
}
|
|
private void setChild(SysMenu menu, List<SysMenu> menus) {
|
List<SysMenu> child = menus.stream().filter(m -> m.getParentId().equals(menu.getId()))
|
.collect(Collectors.toList());
|
if (!CollectionUtils.isEmpty(child)) {
|
menu.setChild(child);
|
child.forEach(m -> {
|
setChild(m, menus);
|
});
|
}
|
}
|
|
/**
|
* 菜单树ztree
|
*/
|
@GetMapping("/tree")
|
@ApiOperation(value = "菜单树", notes = "用作修改权限时菜单树的查询", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
public List<SysMenu> findMenuTree() {
|
LoginAppUser loginAppUser = tokenService.currentUser();
|
List<SysMenu> list = new ArrayList<>();
|
//判断当前用户类型是超级管理员查全部菜单 0代表超级管理员
|
if(loginAppUser.getUserType()!=null && loginAppUser.getUserType().equals("0")){
|
List<SysMenu> all = sysMenuService.findAll();
|
setMenuTree(0L, all, list);
|
}else{
|
// list = findMyMenu(new HashMap<>());
|
Result result = findMyMenu(new HashMap<>());
|
if(result.isSuccess())
|
list = (List)result.getData();
|
}
|
return list;
|
}
|
|
/**
|
* 菜单树
|
*/
|
private void setMenuTree(Long parentId, List<SysMenu> all, List<SysMenu> list) {
|
all.forEach(menu -> {
|
if (parentId.equals(menu.getParentId())) {
|
list.add(menu);
|
|
List<SysMenu> child = new ArrayList<>();
|
menu.setChild(child);
|
setMenuTree(menu.getId(), all, child);
|
}
|
});
|
}
|
|
/**
|
* 获取角色的菜单
|
*/
|
@GetMapping("menusByRole")
|
@ApiOperation(value = "获取角色菜单", notes = "获取角色的菜单,用户修改用户权限时候的回显", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "roleId", value = "角色ID", required = true, dataType = "Long", paramType = "query"),
|
@ApiImplicitParam(name = "orgId", value = "组织ID", required = true, dataType = "Long", paramType = "query"),
|
})
|
public Set<Long> findMenuIdsByRoleId(@RequestParam Map<String,Object> params) {
|
return sysMenuService.findMenuIdsByRoleId(params);
|
}
|
|
/**
|
* 根据用户id获取角色的菜单
|
*/
|
@GetMapping("findMenuIdsByUserId")
|
@ApiOperation(value = "根据用户id获取角色的菜单", notes = "根据用户id获取角色的菜单,用户组织管理赋权限回显", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "userId", value = "用户Id", required = true, dataType = "Long", paramType = "query"),
|
@ApiImplicitParam(name = "orgId", value = "组织ID", required = true, dataType = "Long", paramType = "query"),
|
})
|
public Set<Long> findMenuIdsByUserId(@RequestParam Map<String,Object> params) {
|
return sysMenuService.findMenuIdsByUserId(params);
|
}
|
|
/**
|
* 添加菜单
|
*/
|
@PostMapping
|
@ApiOperation(value = "添加菜单", notes = "添加菜单模块", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "menu", value = "SysMenu实例", required = true, dataType = "SysMenu", paramType = "body")
|
})
|
public SysMenu save(@RequestBody SysMenu menu) {
|
|
menu.setOrgId(tokenService.currentUser().getOrgId());
|
sysMenuService.save(menu);
|
return menu;
|
}
|
|
/**
|
* 修改菜单
|
*/
|
@PutMapping
|
@ApiOperation(value = "修改菜单", notes = "修改菜单模块", httpMethod = "PUT", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "menu", value = "SysMenu实例", required = true, dataType = "SysMenu", paramType = "body")
|
})
|
public SysMenu update(@RequestBody SysMenu menu) {
|
|
menu.setOrgId(tokenService.currentUser().getOrgId());
|
sysMenuService.update(menu);
|
return menu;
|
}
|
|
/**
|
* 删除菜单
|
*
|
* @param id
|
*/
|
@DeleteMapping("/{id}")
|
@ApiOperation(value = "删除菜单", notes = "删除菜单模块", httpMethod = "DELETE", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "id", value = "菜单id", required = true, dataType = "Long", paramType = "path")
|
})
|
public Integer delete(@PathVariable Long id) {
|
|
return sysMenuService.delete(id,tokenService.currentUser().getOrgId());
|
}
|
|
/**
|
* 查询所有菜单
|
*
|
* @return
|
*/
|
@GetMapping("/all")
|
@ApiOperation(value = "查询所有菜单", notes = "查询所有菜单模块", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
public List<SysMenu> findAll() {
|
|
List<SysMenu> all = sysMenuService.findAll();
|
List<SysMenu> list = new ArrayList<>();
|
setSortTable(0L, all, list);
|
return list;
|
}
|
|
/**
|
* 菜单table
|
*/
|
private void setSortTable(Long parentId, List<SysMenu> all, List<SysMenu> list) {
|
all.forEach(a -> {
|
if (a.getParentId().equals(parentId)) {
|
list.add(a);
|
setSortTable(a.getId(), all, list);
|
}
|
});
|
}
|
|
@GetMapping("/{id}")
|
@ApiOperation(value = "查询单个菜单", notes = "根据id查询单个菜单", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "id", value = "菜单id", required = true, dataType = "Long", paramType = "path")
|
})
|
public SysMenu findById(@PathVariable Long id) {
|
|
return sysMenuService.findById(id,tokenService.currentUser().getOrgId());
|
}
|
|
@GetMapping("/findMenuByUser")
|
@ApiOperation(value = "查询当前登录人可以访问是模块名称",response = Map.class, responseContainer = "Map", notes = "查询当前登录人可以访问是模块名称", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "orgId", value = "机构id", required = true, dataType = "Long", paramType = "query"),
|
@ApiImplicitParam(name = "userId", value = "当前登录人id", required = true, dataType = "Long", paramType = "query"),
|
@ApiImplicitParam(name = "tags", value = "查询的模块device or map", required = true, dataType = "Long", paramType = "query"),
|
})
|
public List<Map<String, Object>> findMenuByUser(@RequestParam Map<String, Object> map) {
|
return sysMenuService.findMenuByUser(map);
|
}
|
|
@GetMapping("/findMenuByUserHome")
|
@ApiOperation(value = "查询当前登录人可以访问是模块名称(包含首页)",response = Map.class, responseContainer = "Map", notes = "查询当前登录人可以访问是模块名称(包含首页)", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "orgId", value = "机构id", required = true, dataType = "Long", paramType = "query"),
|
@ApiImplicitParam(name = "userId", value = "当前登录人id", required = true, dataType = "Long", paramType = "query"),
|
@ApiImplicitParam(name = "tags", value = "查询的模块device or map", required = true, dataType = "Long", paramType = "query"),
|
})
|
public List<Map<String, Object>> findMenuByUserHome(@RequestParam Map<String, Object> map) {
|
|
return sysMenuService.findMenuByUserHome(map);
|
}
|
|
@GetMapping("/findAllMenuByUser")
|
@ApiOperation(value = "查询所有模块名称",response = Map.class, responseContainer = "Map", notes = "查询所有模块名称", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "orgId", value = "机构id", required = true, dataType = "Long", paramType = "query"),
|
@ApiImplicitParam(name = "userId", value = "当前登录人id", required = true, dataType = "Long", paramType = "query"),
|
@ApiImplicitParam(name = "tags", value = "查询的模块device or map", required = true, dataType = "Long", paramType = "query"),
|
})
|
public List<Map<String, Object>> findAllMenuByUser(@RequestParam Map<String, Object> map) {
|
|
return sysMenuService.findAllMenuByUser(map);
|
}
|
|
|
@Log4j
|
@RestControllerAdvice
|
public static class ExceptionHandlerAdvice {
|
|
@ExceptionHandler({ IllegalArgumentException.class })
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
public Map<String, Object> badRequestException(IllegalArgumentException exception) {
|
Map<String, Object> data = new HashMap<>();
|
data.put("code", HttpStatus.BAD_REQUEST.value());
|
data.put("message", exception.getMessage());
|
|
return data;
|
}
|
|
|
@ExceptionHandler({Exception.class, Throwable.class})
|
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
public Map<String, Object> serverException(Throwable throwable) {
|
ExceptionHandlerAdvice.log.error("服务端异常", throwable);
|
Map<String, Object> data = new HashMap<>();
|
data.put("code", HttpStatus.INTERNAL_SERVER_ERROR.value());
|
data.put("message", "服务端异常,请联系管理员 ,错误信息:"+throwable.getLocalizedMessage());
|
|
return data;
|
}
|
|
@ExceptionHandler({ SecurityException.class })
|
@ResponseStatus(HttpStatus.UNAUTHORIZED)
|
public Map<String, Object> badRequestException(SecurityException exception) {
|
Map<String, Object> data = new HashMap<>();
|
data.put("code", HttpStatus.UNAUTHORIZED.value());
|
data.put("message", exception.getMessage());
|
|
return data;
|
}
|
}
|
}
|