提交 | 用户 | 时间
|
fd2207
|
1 |
package com.jcdm.web.controller.system; |
懒 |
2 |
|
|
3 |
import java.util.List; |
|
4 |
import java.util.stream.Collectors; |
|
5 |
import javax.servlet.http.HttpServletResponse; |
|
6 |
import org.apache.commons.lang3.ArrayUtils; |
|
7 |
import org.springframework.beans.factory.annotation.Autowired; |
|
8 |
import org.springframework.security.access.prepost.PreAuthorize; |
|
9 |
import org.springframework.validation.annotation.Validated; |
|
10 |
import org.springframework.web.bind.annotation.DeleteMapping; |
|
11 |
import org.springframework.web.bind.annotation.GetMapping; |
|
12 |
import org.springframework.web.bind.annotation.PathVariable; |
|
13 |
import org.springframework.web.bind.annotation.PostMapping; |
|
14 |
import org.springframework.web.bind.annotation.PutMapping; |
|
15 |
import org.springframework.web.bind.annotation.RequestBody; |
|
16 |
import org.springframework.web.bind.annotation.RequestMapping; |
|
17 |
import org.springframework.web.bind.annotation.RestController; |
|
18 |
import org.springframework.web.multipart.MultipartFile; |
|
19 |
import com.jcdm.common.annotation.Log; |
|
20 |
import com.jcdm.common.core.controller.BaseController; |
|
21 |
import com.jcdm.common.core.domain.AjaxResult; |
|
22 |
import com.jcdm.common.core.domain.entity.SysDept; |
|
23 |
import com.jcdm.common.core.domain.entity.SysRole; |
|
24 |
import com.jcdm.common.core.domain.entity.SysUser; |
|
25 |
import com.jcdm.common.core.page.TableDataInfo; |
|
26 |
import com.jcdm.common.enums.BusinessType; |
|
27 |
import com.jcdm.common.utils.SecurityUtils; |
|
28 |
import com.jcdm.common.utils.StringUtils; |
|
29 |
import com.jcdm.common.utils.poi.ExcelUtil; |
|
30 |
import com.jcdm.system.service.ISysDeptService; |
|
31 |
import com.jcdm.system.service.ISysPostService; |
|
32 |
import com.jcdm.system.service.ISysRoleService; |
|
33 |
import com.jcdm.system.service.ISysUserService; |
|
34 |
|
|
35 |
/** |
|
36 |
* 用户信息 |
|
37 |
* |
|
38 |
* @author jc |
|
39 |
*/ |
|
40 |
@RestController |
|
41 |
@RequestMapping("/system/user") |
|
42 |
public class SysUserController extends BaseController |
|
43 |
{ |
|
44 |
@Autowired |
|
45 |
private ISysUserService userService; |
|
46 |
|
|
47 |
@Autowired |
|
48 |
private ISysRoleService roleService; |
|
49 |
|
|
50 |
@Autowired |
|
51 |
private ISysDeptService deptService; |
|
52 |
|
|
53 |
@Autowired |
|
54 |
private ISysPostService postService; |
|
55 |
|
|
56 |
/** |
|
57 |
* 获取用户列表 |
|
58 |
*/ |
|
59 |
@PreAuthorize("@ss.hasPermi('system:user:list')") |
|
60 |
@GetMapping("/list") |
|
61 |
public TableDataInfo list(SysUser user) |
|
62 |
{ |
|
63 |
startPage(); |
|
64 |
List<SysUser> list = userService.selectUserList(user); |
|
65 |
return getDataTable(list); |
|
66 |
} |
|
67 |
|
|
68 |
@Log(title = "用户管理", businessType = BusinessType.EXPORT) |
|
69 |
@PreAuthorize("@ss.hasPermi('system:user:export')") |
|
70 |
@PostMapping("/export") |
|
71 |
public void export(HttpServletResponse response, SysUser user) |
|
72 |
{ |
|
73 |
List<SysUser> list = userService.selectUserList(user); |
|
74 |
ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class); |
|
75 |
util.exportExcel(response, list, "用户数据"); |
|
76 |
} |
|
77 |
|
|
78 |
@Log(title = "用户管理", businessType = BusinessType.IMPORT) |
|
79 |
@PreAuthorize("@ss.hasPermi('system:user:import')") |
|
80 |
@PostMapping("/importData") |
|
81 |
public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception |
|
82 |
{ |
|
83 |
ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class); |
|
84 |
List<SysUser> userList = util.importExcel(file.getInputStream()); |
|
85 |
String operName = getUsername(); |
|
86 |
String message = userService.importUser(userList, updateSupport, operName); |
|
87 |
return success(message); |
|
88 |
} |
|
89 |
|
|
90 |
@PostMapping("/importTemplate") |
|
91 |
public void importTemplate(HttpServletResponse response) |
|
92 |
{ |
|
93 |
ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class); |
|
94 |
util.importTemplateExcel(response, "用户数据"); |
|
95 |
} |
|
96 |
|
|
97 |
/** |
|
98 |
* 根据用户编号获取详细信息 |
|
99 |
*/ |
|
100 |
@PreAuthorize("@ss.hasPermi('system:user:query')") |
|
101 |
@GetMapping(value = { "/", "/{userId}" }) |
|
102 |
public AjaxResult getInfo(@PathVariable(value = "userId", required = false) Long userId) |
|
103 |
{ |
|
104 |
userService.checkUserDataScope(userId); |
|
105 |
AjaxResult ajax = AjaxResult.success(); |
|
106 |
List<SysRole> roles = roleService.selectRoleAll(); |
|
107 |
ajax.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList())); |
|
108 |
ajax.put("posts", postService.selectPostAll()); |
|
109 |
if (StringUtils.isNotNull(userId)) |
|
110 |
{ |
|
111 |
SysUser sysUser = userService.selectUserById(userId); |
|
112 |
ajax.put(AjaxResult.DATA_TAG, sysUser); |
|
113 |
ajax.put("postIds", postService.selectPostListByUserId(userId)); |
|
114 |
ajax.put("roleIds", sysUser.getRoles().stream().map(SysRole::getRoleId).collect(Collectors.toList())); |
|
115 |
} |
|
116 |
return ajax; |
|
117 |
} |
|
118 |
|
|
119 |
/** |
|
120 |
* 新增用户 |
|
121 |
*/ |
|
122 |
@PreAuthorize("@ss.hasPermi('system:user:add')") |
|
123 |
@Log(title = "用户管理", businessType = BusinessType.INSERT) |
|
124 |
@PostMapping |
|
125 |
public AjaxResult add(@Validated @RequestBody SysUser user) |
|
126 |
{ |
|
127 |
if (!userService.checkUserNameUnique(user)) |
|
128 |
{ |
|
129 |
return error("新增用户'" + user.getUserName() + "'失败,登录账号已存在"); |
|
130 |
} |
|
131 |
else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user)) |
|
132 |
{ |
|
133 |
return error("新增用户'" + user.getUserName() + "'失败,手机号码已存在"); |
|
134 |
} |
|
135 |
else if (StringUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(user)) |
|
136 |
{ |
|
137 |
return error("新增用户'" + user.getUserName() + "'失败,邮箱账号已存在"); |
|
138 |
} |
|
139 |
user.setCreateBy(getUsername()); |
|
140 |
user.setPassword(SecurityUtils.encryptPassword(user.getPassword())); |
|
141 |
return toAjax(userService.insertUser(user)); |
|
142 |
} |
|
143 |
|
|
144 |
/** |
|
145 |
* 修改用户 |
|
146 |
*/ |
|
147 |
@PreAuthorize("@ss.hasPermi('system:user:edit')") |
|
148 |
@Log(title = "用户管理", businessType = BusinessType.UPDATE) |
|
149 |
@PutMapping |
|
150 |
public AjaxResult edit(@Validated @RequestBody SysUser user) |
|
151 |
{ |
|
152 |
userService.checkUserAllowed(user); |
|
153 |
userService.checkUserDataScope(user.getUserId()); |
|
154 |
if (!userService.checkUserNameUnique(user)) |
|
155 |
{ |
|
156 |
return error("修改用户'" + user.getUserName() + "'失败,登录账号已存在"); |
|
157 |
} |
|
158 |
else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user)) |
|
159 |
{ |
|
160 |
return error("修改用户'" + user.getUserName() + "'失败,手机号码已存在"); |
|
161 |
} |
|
162 |
else if (StringUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(user)) |
|
163 |
{ |
|
164 |
return error("修改用户'" + user.getUserName() + "'失败,邮箱账号已存在"); |
|
165 |
} |
|
166 |
user.setUpdateBy(getUsername()); |
|
167 |
return toAjax(userService.updateUser(user)); |
|
168 |
} |
|
169 |
|
|
170 |
/** |
|
171 |
* 删除用户 |
|
172 |
*/ |
|
173 |
@PreAuthorize("@ss.hasPermi('system:user:remove')") |
|
174 |
@Log(title = "用户管理", businessType = BusinessType.DELETE) |
|
175 |
@DeleteMapping("/{userIds}") |
|
176 |
public AjaxResult remove(@PathVariable Long[] userIds) |
|
177 |
{ |
|
178 |
if (ArrayUtils.contains(userIds, getUserId())) |
|
179 |
{ |
|
180 |
return error("当前用户不能删除"); |
|
181 |
} |
|
182 |
return toAjax(userService.deleteUserByIds(userIds)); |
|
183 |
} |
|
184 |
|
|
185 |
/** |
|
186 |
* 重置密码 |
|
187 |
*/ |
|
188 |
@PreAuthorize("@ss.hasPermi('system:user:resetPwd')") |
|
189 |
@Log(title = "用户管理", businessType = BusinessType.UPDATE) |
|
190 |
@PutMapping("/resetPwd") |
|
191 |
public AjaxResult resetPwd(@RequestBody SysUser user) |
|
192 |
{ |
|
193 |
userService.checkUserAllowed(user); |
|
194 |
userService.checkUserDataScope(user.getUserId()); |
|
195 |
user.setPassword(SecurityUtils.encryptPassword(user.getPassword())); |
|
196 |
user.setUpdateBy(getUsername()); |
|
197 |
return toAjax(userService.resetPwd(user)); |
|
198 |
} |
|
199 |
|
|
200 |
/** |
|
201 |
* 状态修改 |
|
202 |
*/ |
|
203 |
@PreAuthorize("@ss.hasPermi('system:user:edit')") |
|
204 |
@Log(title = "用户管理", businessType = BusinessType.UPDATE) |
|
205 |
@PutMapping("/changeStatus") |
|
206 |
public AjaxResult changeStatus(@RequestBody SysUser user) |
|
207 |
{ |
|
208 |
userService.checkUserAllowed(user); |
|
209 |
userService.checkUserDataScope(user.getUserId()); |
|
210 |
user.setUpdateBy(getUsername()); |
|
211 |
return toAjax(userService.updateUserStatus(user)); |
|
212 |
} |
|
213 |
|
|
214 |
/** |
|
215 |
* 根据用户编号获取授权角色 |
|
216 |
*/ |
|
217 |
@PreAuthorize("@ss.hasPermi('system:user:query')") |
|
218 |
@GetMapping("/authRole/{userId}") |
|
219 |
public AjaxResult authRole(@PathVariable("userId") Long userId) |
|
220 |
{ |
|
221 |
AjaxResult ajax = AjaxResult.success(); |
|
222 |
SysUser user = userService.selectUserById(userId); |
|
223 |
List<SysRole> roles = roleService.selectRolesByUserId(userId); |
|
224 |
ajax.put("user", user); |
|
225 |
ajax.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList())); |
|
226 |
return ajax; |
|
227 |
} |
|
228 |
|
|
229 |
/** |
|
230 |
* 用户授权角色 |
|
231 |
*/ |
|
232 |
@PreAuthorize("@ss.hasPermi('system:user:edit')") |
|
233 |
@Log(title = "用户管理", businessType = BusinessType.GRANT) |
|
234 |
@PutMapping("/authRole") |
|
235 |
public AjaxResult insertAuthRole(Long userId, Long[] roleIds) |
|
236 |
{ |
|
237 |
userService.checkUserDataScope(userId); |
|
238 |
userService.insertUserAuth(userId, roleIds); |
|
239 |
return success(); |
|
240 |
} |
|
241 |
|
|
242 |
/** |
|
243 |
* 获取部门树列表 |
|
244 |
*/ |
|
245 |
@PreAuthorize("@ss.hasPermi('system:user:list')") |
|
246 |
@GetMapping("/deptTree") |
|
247 |
public AjaxResult deptTree(SysDept dept) |
|
248 |
{ |
|
249 |
return success(deptService.selectDeptTreeList(dept)); |
|
250 |
} |
|
251 |
} |