package com.stylefeng.guns.modular.system.controller; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.stylefeng.guns.core.base.controller.BaseController; import com.stylefeng.guns.core.base.tips.Tip; import com.stylefeng.guns.core.cache.CacheKit; import com.stylefeng.guns.core.common.constant.Const; import com.stylefeng.guns.core.common.constant.cache.Cache; import com.stylefeng.guns.core.common.constant.factory.ConstantFactory; import com.stylefeng.guns.core.common.exception.BizExceptionEnum; import com.stylefeng.guns.core.exception.GunsException; import com.stylefeng.guns.core.log.LogObjectHolder; import com.stylefeng.guns.core.node.ZTreeNode; import com.stylefeng.guns.core.util.Convert; import com.stylefeng.guns.core.util.ToolUtil; import com.stylefeng.guns.modular.system.model.*; import com.stylefeng.guns.modular.system.service.IRoleService; import com.stylefeng.guns.modular.system.service.ITUserService; import com.stylefeng.guns.modular.system.service.IUserService; import com.stylefeng.guns.modular.system.service.SysOperationService; import com.stylefeng.guns.modular.system.utils.tips.SuccessTip; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; import java.util.Date; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** * 角色控制器 * * @author fengshuonan * @Date 2017年2月12日21:59:14 */ @Controller @Api(tags = "角色") @RequestMapping("/api/role") public class RoleController extends BaseController { @Autowired private IUserService userService; @Autowired private IRoleService roleService; @Autowired private SysOperationService sysOperation; @Autowired private ITUserService itUserService; /** * 获取角色列表 */ @ApiOperation(value = "获取角色列表",notes="获取角色列表") @ApiImplicitParams({ @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."), @ApiImplicitParam(name = "roleName", value = "名称", required = false, dataType = "String"), }) @GetMapping(value = "/list") @ResponseBody public Object list(@RequestParam(required = false) String roleName) { List> roles = this.roleService.selectRoles(super.getPara("roleName")); return new SuccessTip(roles); } /** * 角色新增 */ @ApiOperation(value = "角色新增",notes="角色新增") @ApiImplicitParams({ @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."), }) @PostMapping(value = "/add") @ResponseBody public Tip add(@Valid Role role) { role.setId(null); role.setCreateTime(new Date()); this.roleService.insert(role); return SUCCESS_TIP; } /** * 角色修改 */ @ApiOperation(value = "角色修改",notes="角色修改") @ApiImplicitParams({ @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."), }) @PostMapping(value = "/update") @ResponseBody public Tip edit(@Valid Role role) { this.roleService.updateById(role); //删除缓存 CacheKit.removeAll(Cache.CONSTANT); return SUCCESS_TIP; } /** * 删除角色 */ @ApiOperation(value = "删除角色",notes="删除角色") @ApiImplicitParams({ @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."), @ApiImplicitParam(name = "roleId", value = "角色Id", required = true, dataType = "int"), }) @DeleteMapping(value = "/delete") @ResponseBody public Tip remove(@RequestParam Integer roleId) { if (ToolUtil.isEmpty(roleId)) { throw new GunsException(BizExceptionEnum.REQUEST_NULL); } //不能删除超级管理员角色 if (roleId.equals(Const.ADMIN_ROLE_ID)) { throw new GunsException(BizExceptionEnum.CANT_DELETE_ADMIN); } //缓存被删除的角色名称 LogObjectHolder.me().set(ConstantFactory.me().getSingleRoleName(roleId)); this.roleService.delRoleById(roleId); //删除缓存 CacheKit.removeAll(Cache.CONSTANT); return SUCCESS_TIP; } /** * 查看角色 */ @RequestMapping(value = "/view/{roleId}") @ResponseBody public Tip view(@PathVariable Integer roleId) { if (ToolUtil.isEmpty(roleId)) { throw new GunsException(BizExceptionEnum.REQUEST_NULL); } this.roleService.selectById(roleId); return SUCCESS_TIP; } /** * 配置权限 */ @ApiOperation(value = "配置权限",notes="配置权限") @ApiImplicitParams({ @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."), @ApiImplicitParam(name = "roleId", value = "角色Id", required = true, dataType = "int"), @ApiImplicitParam(name = "ids", value = "菜单权限ids(逗号拼接)", required = true, dataType = "int"), }) @PostMapping("/setAuthority") @ResponseBody public Tip setAuthority(@RequestParam("roleId") Integer roleId, @RequestParam("ids") String ids) { if (ToolUtil.isOneEmpty(roleId)) { throw new GunsException(BizExceptionEnum.REQUEST_NULL); } this.roleService.setAuthority(roleId, ids); return SUCCESS_TIP; } /** * 配置权限 */ @ApiOperation(value = "配置权限-version1",notes="配置权限") @ApiImplicitParams({ @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."), }) @PostMapping("/setAuthority1") @ResponseBody public Tip setAuthority1(@RequestBody List relations) { if (ToolUtil.isOneEmpty(relations)) { throw new GunsException(BizExceptionEnum.REQUEST_NULL); } this.roleService.setAuthorityOne(relations); return SUCCESS_TIP; } /** * 根据菜单id获取所拥有的权限按钮 */ @ApiOperation(value = "根据菜单id获取所拥有的权限按钮",notes="根据菜单id获取所拥有的权限按钮") @ApiImplicitParams({ @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."), @ApiImplicitParam(name = "menuId", value = "菜单Id", required = true, dataType = "int"), }) @GetMapping("/getButton") @ResponseBody public Object getButton(@RequestParam Integer menuId) { List sysOperations = sysOperation.selectList(new EntityWrapper().eq("menu_id", menuId)); return new SuccessTip(sysOperations); } /** * 配置权限 */ @ApiOperation(value = "根据所有的权限按钮",notes="根据所有的权限按钮") @ApiImplicitParams({ @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."), }) @GetMapping("/getAllButton") @ResponseBody public Object getAllButton() { List sysOperations = sysOperation.selectList(null); return new SuccessTip(sysOperations); } @ApiOperation(value = "根据用户id获取权限信息",notes="根据用户id获取权限信息") @ApiImplicitParams({ @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."), @ApiImplicitParam(name = "userId", value = "用户Id", required = true, dataType = "int"), }) @GetMapping("/getAuthority") @ResponseBody public Object getAuthority(Integer userId) { TUser user = itUserService.selectById(userId); if(user==null){ throw new GunsException(BizExceptionEnum.NO_THIS_USER); } // 当前拥有的 List relations = roleService.selectAuthority(String.valueOf(userId)); List menuList = roleService.getRelation1(relations.stream().map(Relation::getMenuid).collect(Collectors.toList())); List parent = menuList.stream().filter(role -> role.getPcode().equals("0")).collect(Collectors.toList()); getChildren(parent,menuList); return new SuccessTip(parent); } private void getChildren(List parentList,List menuList) { for (Menu parent : parentList) { List children = menuList.stream().filter(menu -> menu.getPcode().equals(String.valueOf(parent.getId())) && menu.getLevels().equals(2)).collect(Collectors.toList()); parent.setList(children); if(!CollectionUtils.isEmpty(children)){ getChildren(children,menuList); }else { List child = menuList.stream().filter(menu -> menu.getPcode().equals(String.valueOf(parent.getId())) && menu.getLevels().equals(3)).collect(Collectors.toList()); parent.setChildren(child); continue; } } } @ApiOperation(value = "根据角色id获取权限信息",notes="根据角色id获取权限信息") @ApiImplicitParams({ @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."), @ApiImplicitParam(name = "roleId", value = "用户Id", required = true, dataType = "int"), }) @GetMapping("/getAuthorityFromRole") @ResponseBody public Object getAuthorityFromRole(String roleId) { List relations = roleService.selectAuthority(roleId); List menuList = roleService.getRelation1(relations.stream().map(Relation::getMenuid).collect(Collectors.toList())); List parent = menuList.stream().filter(role -> role.getPcode().equals("0")).collect(Collectors.toList()); getChildren(parent,menuList); return new SuccessTip(parent); } /** * 获取角色列表 */ @ApiOperation(value = "获取所有权限菜单",notes="获取所有权限菜单") @ApiImplicitParams({ @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."), }) @GetMapping("/getRelation") @ResponseBody public List getRelation() { return roleService.getRelation(); } /** * 获取角色列表 */ @ResponseBody public List roleTreeListByUserId(@PathVariable Integer userId) { User theUser = this.userService.selectById(userId); String roleid = theUser.getRoleid(); if (ToolUtil.isEmpty(roleid)) { List roleTreeList = this.roleService.roleTreeList(); return roleTreeList; } else { String[] strArray = Convert.toStrArray(",", roleid); List roleTreeListByUserId = this.roleService.roleTreeListByRoleId(strArray); return roleTreeListByUserId; } } }