懒羊羊
2023-08-30 1ac2bc1590406d9babec036e154d8d08f34a6aa1
提交 | 用户 | 时间
1ac2bc 1 /**
2  * Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng)
3  * <p>
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * <p>
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * <p>
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package cn.stylefeng.guns.sys.modular.rest.controller;
17
18 import cn.hutool.core.bean.BeanUtil;
19 import cn.stylefeng.guns.base.log.BussinessLog;
20 import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
21 import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
22 import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
23 import cn.stylefeng.guns.sys.core.constant.dictmap.DeleteDict;
24 import cn.stylefeng.guns.sys.core.constant.dictmap.RoleDict;
25 import cn.stylefeng.guns.sys.core.constant.factory.ConstantFactory;
26 import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
27 import cn.stylefeng.guns.sys.modular.rest.entity.RestRole;
28 import cn.stylefeng.guns.sys.modular.rest.entity.RestUser;
29 import cn.stylefeng.guns.sys.modular.rest.service.RestMenuService;
30 import cn.stylefeng.guns.sys.modular.rest.service.RestRoleService;
31 import cn.stylefeng.guns.sys.modular.rest.service.RestUserService;
32 import cn.stylefeng.guns.sys.modular.system.model.RoleDto;
33 import cn.stylefeng.guns.sys.modular.system.warpper.RoleWrapper;
34 import cn.stylefeng.roses.core.base.controller.BaseController;
35 import cn.stylefeng.roses.core.util.ToolUtil;
36 import cn.stylefeng.roses.kernel.model.exception.ServiceException;
37 import cn.stylefeng.roses.kernel.model.response.ResponseData;
38 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.web.bind.annotation.*;
41
42 import java.util.ArrayList;
43 import java.util.List;
44 import java.util.Map;
45
46 /**
47  * 角色控制器
48  *
49  * @author fengshuonan
50  * @Date 2017年2月12日21:59:14
51  */
52 @RestController
53 @RequestMapping("/rest/role")
54 public class RestRoleController extends BaseController {
55
56     @Autowired
57     private RestUserService restUserService;
58
59     @Autowired
60     private RestRoleService restRoleService;
61
62     @Autowired
63     private RestMenuService restMenuService;
64
65     /**
66      * 获取角色列表
67      *
68      * @author fengshuonan
69      * @Date 2018/12/23 6:31 PM
70      */
71     @RequestMapping(value = "/list")
72     public LayuiPageInfo list(@RequestParam(value = "roleName", required = false) String roleName) {
73         Page<Map<String, Object>> roles = this.restRoleService.selectRoles(roleName);
74         Page<Map<String, Object>> wrap = new RoleWrapper(roles).wrap();
75         return LayuiPageFactory.createPageInfo(wrap);
76     }
77
78     /**
79      * 角色新增
80      *
81      * @author fengshuonan
82      * @Date 2018/12/23 6:31 PM
83      */
84     @RequestMapping(value = "/add")
85     @BussinessLog(value = "添加角色", key = "name", dict = RoleDict.class)
86     public ResponseData add(@RequestBody RestRole restRole) {
87         this.restRoleService.addRole(restRole);
88         return SUCCESS_TIP;
89     }
90
91     /**
92      * 角色修改
93      *
94      * @author fengshuonan
95      * @Date 2018/12/23 6:31 PM
96      */
97     @RequestMapping(value = "/edit")
98     @BussinessLog(value = "修改角色", key = "name", dict = RoleDict.class)
99     public ResponseData edit(@RequestBody RoleDto roleDto) {
100         this.restRoleService.editRole(roleDto);
101         return SUCCESS_TIP;
102     }
103
104     /**
105      * 删除角色
106      *
107      * @author fengshuonan
108      * @Date 2018/12/23 6:31 PM
109      */
110     @RequestMapping(value = "/remove")
111     @BussinessLog(value = "删除角色", key = "roleId", dict = DeleteDict.class)
112     public ResponseData remove(@RequestParam Long roleId) {
113         this.restRoleService.delRoleById(roleId);
114         return SUCCESS_TIP;
115     }
116
117     /**
118      * 查看角色
119      *
120      * @author fengshuonan
121      * @Date 2018/12/23 6:31 PM
122      */
123     @RequestMapping(value = "/view/{roleId}")
124     public ResponseData view(@PathVariable Long roleId) {
125         if (ToolUtil.isEmpty(roleId)) {
126             throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
127         }
128         RestRole role = this.restRoleService.getById(roleId);
129         Map<String, Object> roleMap = BeanUtil.beanToMap(role);
130
131         Long pid = role.getPid();
132         String pName = ConstantFactory.me().getSingleRoleName(pid);
133         roleMap.put("pName", pName);
134
135         //获取角色绑定的菜单
136         List<Long> menuIds = this.restMenuService.getMenuIdsByRoleId(roleId);
137         List<String> menuNames = this.restMenuService.getBaseMapper().getMenuNamesByRoleId(roleId);
138         if (ToolUtil.isNotEmpty(menuIds)) {
139             roleMap.put("menuIds", menuIds);
140             roleMap.put("menuNames", menuNames);
141         } else {
142             roleMap.put("menuIds", new ArrayList<>());
143             roleMap.put("menuNames", new ArrayList<>());
144         }
145
146         return ResponseData.success(roleMap);
147     }
148
149     /**
150      * 配置权限
151      *
152      * @author fengshuonan
153      * @Date 2018/12/23 6:31 PM
154      */
155     @RequestMapping("/setAuthority")
156     @BussinessLog(value = "配置权限", key = "roleId,ids", dict = RoleDict.class)
157     public ResponseData setAuthority(@RequestParam("roleId") Long roleId, @RequestParam("ids") String ids) {
158         if (ToolUtil.isOneEmpty(roleId)) {
159             throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
160         }
161         this.restRoleService.setAuthority(roleId, ids);
162         return SUCCESS_TIP;
163     }
164
165     /**
166      * 获取角色列表
167      *
168      * @author fengshuonan
169      * @Date 2018/12/23 6:31 PM
170      */
171     @RequestMapping(value = "/roleTreeList")
172     public List<ZTreeNode> roleTreeList() {
173         List<ZTreeNode> roleTreeList = this.restRoleService.roleTreeList();
174         roleTreeList.add(ZTreeNode.createParent());
175         return roleTreeList;
176     }
177
178     /**
179      * 获取角色列表,通过用户id
180      *
181      * @author fengshuonan
182      * @Date 2018/12/23 6:31 PM
183      */
184     @RequestMapping(value = "/roleTreeListByUserId/{userId}")
185     public List<ZTreeNode> roleTreeListByUserId(@PathVariable Long userId) {
186         RestUser theUser = this.restUserService.getById(userId);
187         String roleId = theUser.getRoleId();
188         if (ToolUtil.isEmpty(roleId)) {
189             return this.restRoleService.roleTreeList();
190         } else {
191
192             String[] strArray = roleId.split(",");
193
194             //转化成Long[]
195             Long[] longArray = new Long[strArray.length];
196             for (int i = 0; i < strArray.length; i++) {
197                 longArray[i] = Long.valueOf(strArray[i]);
198             }
199
200             return this.restRoleService.roleTreeListByRoleId(longArray);
201         }
202     }
203
204 }