提交 | 用户 | 时间
|
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.stylefeng.guns.base.auth.context.LoginContextHolder; |
|
19 |
import cn.stylefeng.guns.base.consts.ConstantsContext; |
|
20 |
import cn.stylefeng.guns.base.log.BussinessLog; |
|
21 |
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory; |
|
22 |
import cn.stylefeng.guns.sys.core.constant.Const; |
|
23 |
import cn.stylefeng.guns.sys.core.constant.dictmap.UserDict; |
|
24 |
import cn.stylefeng.guns.sys.core.constant.state.ManagerStatus; |
|
25 |
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum; |
|
26 |
import cn.stylefeng.guns.sys.core.util.SaltUtil; |
|
27 |
import cn.stylefeng.guns.sys.modular.rest.entity.RestUser; |
|
28 |
import cn.stylefeng.guns.sys.modular.rest.model.UserQueryParam; |
|
29 |
import cn.stylefeng.guns.sys.modular.rest.service.RestUserService; |
|
30 |
import cn.stylefeng.guns.sys.modular.system.model.UserDto; |
|
31 |
import cn.stylefeng.guns.sys.modular.system.warpper.UserWrapper; |
|
32 |
import cn.stylefeng.roses.core.base.controller.BaseController; |
|
33 |
import cn.stylefeng.roses.core.datascope.DataScope; |
|
34 |
import cn.stylefeng.roses.core.util.ToolUtil; |
|
35 |
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException; |
|
36 |
import cn.stylefeng.roses.kernel.model.exception.ServiceException; |
|
37 |
import cn.stylefeng.roses.kernel.model.response.ResponseData; |
|
38 |
import cn.stylefeng.roses.kernel.model.response.SuccessResponseData; |
|
39 |
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
|
40 |
import org.springframework.beans.factory.annotation.Autowired; |
|
41 |
import org.springframework.web.bind.annotation.*; |
|
42 |
import org.springframework.web.multipart.MultipartFile; |
|
43 |
|
|
44 |
import java.io.File; |
|
45 |
import java.util.Map; |
|
46 |
import java.util.UUID; |
|
47 |
|
|
48 |
/** |
|
49 |
* 系统管理员控制器 |
|
50 |
* |
|
51 |
* @author fengshuonan |
|
52 |
* @Date 2017年1月11日 下午1:08:17 |
|
53 |
*/ |
|
54 |
@RestController |
|
55 |
@RequestMapping("/rest/mgr") |
|
56 |
public class RestUserMgrController extends BaseController { |
|
57 |
|
|
58 |
@Autowired |
|
59 |
private RestUserService restUserService; |
|
60 |
|
|
61 |
/** |
|
62 |
* 通过用户id获取用户的信息 |
|
63 |
* |
|
64 |
* @author fengshuonan |
|
65 |
* @Date 2018/12/24 22:43 |
|
66 |
*/ |
|
67 |
@RequestMapping("/getUserById") |
|
68 |
public ResponseData getUserById(@RequestParam("userId") Long userId) { |
|
69 |
if (ToolUtil.isEmpty(userId)) { |
|
70 |
throw new ServiceException(BizExceptionEnum.REQUEST_NULL); |
|
71 |
} |
|
72 |
Map<String, Object> user = restUserService.getUserInfo(userId); |
|
73 |
return new SuccessResponseData(user); |
|
74 |
} |
|
75 |
|
|
76 |
/** |
|
77 |
* 获取用户详情 |
|
78 |
* |
|
79 |
* @author fengshuonan |
|
80 |
* @Date 2018/12/24 22:43 |
|
81 |
*/ |
|
82 |
@RequestMapping("/getUserInfo") |
|
83 |
public SuccessResponseData getUserInfo(@RequestParam("userId") Long userId) { |
|
84 |
if (ToolUtil.isEmpty(userId)) { |
|
85 |
throw new RequestEmptyException(); |
|
86 |
} |
|
87 |
this.restUserService.assertAuth(userId); |
|
88 |
return new SuccessResponseData(restUserService.getUserInfo(userId)); |
|
89 |
} |
|
90 |
|
|
91 |
/** |
|
92 |
* 修改当前用户的密码 |
|
93 |
* |
|
94 |
* @author fengshuonan |
|
95 |
* @Date 2018/12/24 22:43 |
|
96 |
*/ |
|
97 |
@RequestMapping("/changePwd") |
|
98 |
public Object changePwd(@RequestParam("oldPassword") String oldPassword, @RequestParam("newPassword") String newPassword) { |
|
99 |
if (ToolUtil.isOneEmpty(oldPassword, newPassword)) { |
|
100 |
throw new RequestEmptyException(); |
|
101 |
} |
|
102 |
this.restUserService.changePwd(oldPassword, newPassword); |
|
103 |
return SUCCESS_TIP; |
|
104 |
} |
|
105 |
|
|
106 |
/** |
|
107 |
* 查询管理员列表 |
|
108 |
* |
|
109 |
* @author fengshuonan |
|
110 |
* @Date 2018/12/24 22:43 |
|
111 |
*/ |
|
112 |
@RequestMapping("/list") |
|
113 |
public Object list(@RequestBody UserQueryParam userQueryParam) { |
|
114 |
|
|
115 |
//拼接查询条件 |
|
116 |
String beginTime = ""; |
|
117 |
String endTime = ""; |
|
118 |
|
|
119 |
if (ToolUtil.isNotEmpty(userQueryParam.getTimeLimit())) { |
|
120 |
String[] split = userQueryParam.getTimeLimit().split(" - "); |
|
121 |
beginTime = split[0]; |
|
122 |
endTime = split[1]; |
|
123 |
} |
|
124 |
|
|
125 |
if (LoginContextHolder.getContext().isAdmin()) { |
|
126 |
Page<Map<String, Object>> users = restUserService.selectUsers(null, userQueryParam.getName(), beginTime, endTime, userQueryParam.getDeptId()); |
|
127 |
Page wrapped = new UserWrapper(users).wrap(); |
|
128 |
return LayuiPageFactory.createPageInfo(wrapped); |
|
129 |
} else { |
|
130 |
DataScope dataScope = new DataScope(LoginContextHolder.getContext().getDeptDataScope()); |
|
131 |
Page<Map<String, Object>> users = restUserService.selectUsers(dataScope, userQueryParam.getName(), beginTime, endTime, userQueryParam.getDeptId()); |
|
132 |
Page wrapped = new UserWrapper(users).wrap(); |
|
133 |
return LayuiPageFactory.createPageInfo(wrapped); |
|
134 |
} |
|
135 |
} |
|
136 |
|
|
137 |
/** |
|
138 |
* 添加管理员 |
|
139 |
* |
|
140 |
* @author fengshuonan |
|
141 |
* @Date 2018/12/24 22:44 |
|
142 |
*/ |
|
143 |
@RequestMapping("/add") |
|
144 |
@BussinessLog(value = "添加管理员", key = "account", dict = UserDict.class) |
|
145 |
public ResponseData add(@RequestBody UserDto user) { |
|
146 |
this.restUserService.addUser(user); |
|
147 |
return SUCCESS_TIP; |
|
148 |
} |
|
149 |
|
|
150 |
/** |
|
151 |
* 修改管理员 |
|
152 |
* |
|
153 |
* @author fengshuonan |
|
154 |
* @Date 2018/12/24 22:44 |
|
155 |
*/ |
|
156 |
@RequestMapping("/edit") |
|
157 |
@BussinessLog(value = "修改管理员", key = "account", dict = UserDict.class) |
|
158 |
public ResponseData edit(@RequestBody UserDto user) { |
|
159 |
this.restUserService.editUser(user); |
|
160 |
return SUCCESS_TIP; |
|
161 |
} |
|
162 |
|
|
163 |
/** |
|
164 |
* 删除管理员(逻辑删除) |
|
165 |
* |
|
166 |
* @author fengshuonan |
|
167 |
* @Date 2018/12/24 22:44 |
|
168 |
*/ |
|
169 |
@RequestMapping("/delete") |
|
170 |
@BussinessLog(value = "删除管理员", key = "userId", dict = UserDict.class) |
|
171 |
public ResponseData delete(@RequestParam Long userId) { |
|
172 |
if (ToolUtil.isEmpty(userId)) { |
|
173 |
throw new ServiceException(BizExceptionEnum.REQUEST_NULL); |
|
174 |
} |
|
175 |
this.restUserService.deleteUser(userId); |
|
176 |
return SUCCESS_TIP; |
|
177 |
} |
|
178 |
|
|
179 |
/** |
|
180 |
* 重置管理员的密码 |
|
181 |
* |
|
182 |
* @author fengshuonan |
|
183 |
* @Date 2018/12/24 22:44 |
|
184 |
*/ |
|
185 |
@RequestMapping("/reset") |
|
186 |
@BussinessLog(value = "重置管理员密码", key = "userId", dict = UserDict.class) |
|
187 |
public ResponseData reset(@RequestParam Long userId) { |
|
188 |
if (ToolUtil.isEmpty(userId)) { |
|
189 |
throw new ServiceException(BizExceptionEnum.REQUEST_NULL); |
|
190 |
} |
|
191 |
this.restUserService.assertAuth(userId); |
|
192 |
RestUser restUser = this.restUserService.getById(userId); |
|
193 |
restUser.setSalt(SaltUtil.getRandomSalt()); |
|
194 |
restUser.setPassword(SaltUtil.md5Encrypt(ConstantsContext.getDefaultPassword(), restUser.getSalt())); |
|
195 |
this.restUserService.updateById(restUser); |
|
196 |
return SUCCESS_TIP; |
|
197 |
} |
|
198 |
|
|
199 |
/** |
|
200 |
* 冻结用户 |
|
201 |
* |
|
202 |
* @author fengshuonan |
|
203 |
* @Date 2018/12/24 22:44 |
|
204 |
*/ |
|
205 |
@RequestMapping("/changeStatus") |
|
206 |
public ResponseData changeStatus(@RequestParam("userId") Long userId, @RequestParam("status") String status) { |
|
207 |
|
|
208 |
//冻结用户 |
|
209 |
if (status.equals("freeze")) { |
|
210 |
|
|
211 |
if (ToolUtil.isEmpty(userId)) { |
|
212 |
throw new ServiceException(BizExceptionEnum.REQUEST_NULL); |
|
213 |
} |
|
214 |
|
|
215 |
//不能冻结超级管理员 |
|
216 |
if (userId.equals(Const.ADMIN_ID)) { |
|
217 |
throw new ServiceException(BizExceptionEnum.CANT_FREEZE_ADMIN); |
|
218 |
} |
|
219 |
|
|
220 |
this.restUserService.assertAuth(userId); |
|
221 |
this.restUserService.setStatus(userId, ManagerStatus.FREEZED.getCode()); |
|
222 |
|
|
223 |
return SUCCESS_TIP; |
|
224 |
|
|
225 |
} else if (status.equals("unfreeze")) { |
|
226 |
|
|
227 |
//解除冻结用户 |
|
228 |
if (ToolUtil.isEmpty(userId)) { |
|
229 |
throw new ServiceException(BizExceptionEnum.REQUEST_NULL); |
|
230 |
} |
|
231 |
|
|
232 |
this.restUserService.assertAuth(userId); |
|
233 |
this.restUserService.setStatus(userId, ManagerStatus.OK.getCode()); |
|
234 |
|
|
235 |
return SUCCESS_TIP; |
|
236 |
|
|
237 |
} else { |
|
238 |
|
|
239 |
return SUCCESS_TIP; |
|
240 |
|
|
241 |
} |
|
242 |
} |
|
243 |
|
|
244 |
/** |
|
245 |
* 分配角色 |
|
246 |
* |
|
247 |
* @author fengshuonan |
|
248 |
* @Date 2018/12/24 22:44 |
|
249 |
*/ |
|
250 |
@RequestMapping("/setRole") |
|
251 |
@BussinessLog(value = "分配角色", key = "userId,roleIds", dict = UserDict.class) |
|
252 |
public ResponseData setRole(@RequestParam("userId") Long userId, @RequestParam("roleIds") String roleIds) { |
|
253 |
if (ToolUtil.isOneEmpty(userId, roleIds)) { |
|
254 |
throw new ServiceException(BizExceptionEnum.REQUEST_NULL); |
|
255 |
} |
|
256 |
//不能修改超级管理员 |
|
257 |
if (userId.equals(Const.ADMIN_ID)) { |
|
258 |
throw new ServiceException(BizExceptionEnum.CANT_CHANGE_ADMIN); |
|
259 |
} |
|
260 |
this.restUserService.assertAuth(userId); |
|
261 |
this.restUserService.setRoles(userId, roleIds); |
|
262 |
return SUCCESS_TIP; |
|
263 |
} |
|
264 |
|
|
265 |
/** |
|
266 |
* 上传图片 |
|
267 |
* |
|
268 |
* @author fengshuonan |
|
269 |
* @Date 2018/12/24 22:44 |
|
270 |
*/ |
|
271 |
@RequestMapping(method = RequestMethod.POST, path = "/upload") |
|
272 |
public ResponseData upload(@RequestPart("file") MultipartFile picture) { |
|
273 |
|
|
274 |
String pictureName = UUID.randomUUID().toString() + "." + ToolUtil.getFileSuffix(picture.getOriginalFilename()); |
|
275 |
try { |
|
276 |
String fileSavePath = ConstantsContext.getFileUploadPath(); |
|
277 |
picture.transferTo(new File(fileSavePath + pictureName)); |
|
278 |
} catch (Exception e) { |
|
279 |
throw new ServiceException(BizExceptionEnum.UPLOAD_ERROR); |
|
280 |
} |
|
281 |
return new SuccessResponseData(pictureName); |
|
282 |
} |
|
283 |
} |