Commit f5b7142ab6b8e82931dd937a067530bd6e3bd728

Authored by YunaiV
1 parent cdaa0d91

完成用户列表的前后端对接

Showing 18 changed files with 480 additions and 319 deletions
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java
... ... @@ -2,6 +2,7 @@ package com.ruoyi.system.service.impl;
2 2  
3 3 import java.util.ArrayList;
4 4 import java.util.List;
  5 +
5 6 import org.slf4j.Logger;
6 7 import org.slf4j.LoggerFactory;
7 8 import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -27,86 +28,38 @@ import com.ruoyi.system.service.ISysUserService;
27 28  
28 29 /**
29 30 * 用户 业务层处理
30   - *
  31 + *
31 32 * @author ruoyi
32 33 */
33 34 @Service
34   -public class SysUserServiceImpl implements ISysUserService
35   -{
36   - private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);
37   -
38   - @Autowired
39   - private SysUserMapper userMapper;
40   -
41   - @Autowired
42   - private SysRoleMapper roleMapper;
43   -
44   - @Autowired
45   - private SysPostMapper postMapper;
46   -
47   - @Autowired
48   - private SysUserRoleMapper userRoleMapper;
49   -
50   - @Autowired
51   - private SysUserPostMapper userPostMapper;
52   -
53   - @Autowired
54   - private ISysConfigService configService;
  35 +public class SysUserServiceImpl implements ISysUserService {
55 36  
56 37 /**
57 38 * 根据条件分页查询用户列表
58   - *
  39 + *
59 40 * @param user 用户信息
60 41 * @return 用户信息集合信息
61 42 */
62 43 @Override
63 44 @DataScope(deptAlias = "d", userAlias = "u")
64   - public List<SysUser> selectUserList(SysUser user)
65   - {
  45 + public List<SysUser> selectUserList(SysUser user) {
66 46 return userMapper.selectUserList(user);
67 47 }
68 48  
69 49 /**
70   - * 通过用户名查询用户
71   - *
72   - * @param userName 用户名
73   - * @return 用户对象信息
74   - */
75   - @Override
76   - public SysUser selectUserByUserName(String userName)
77   - {
78   - return userMapper.selectUserByUserName(userName);
79   - }
80   -
81   - /**
82   - * 通过用户ID查询用户
83   - *
84   - * @param userId 用户ID
85   - * @return 用户对象信息
86   - */
87   - @Override
88   - public SysUser selectUserById(Long userId)
89   - {
90   - return userMapper.selectUserById(userId);
91   - }
92   -
93   - /**
94 50 * 查询用户所属角色组
95   - *
  51 + *
96 52 * @param userName 用户名
97 53 * @return 结果
98 54 */
99 55 @Override
100   - public String selectUserRoleGroup(String userName)
101   - {
  56 + public String selectUserRoleGroup(String userName) {
102 57 List<SysRole> list = roleMapper.selectRolesByUserName(userName);
103 58 StringBuffer idsStr = new StringBuffer();
104   - for (SysRole role : list)
105   - {
  59 + for (SysRole role : list) {
106 60 idsStr.append(role.getRoleName()).append(",");
107 61 }
108   - if (StringUtils.isNotEmpty(idsStr.toString()))
109   - {
  62 + if (StringUtils.isNotEmpty(idsStr.toString())) {
110 63 return idsStr.substring(0, idsStr.length() - 1);
111 64 }
112 65 return idsStr.toString();
... ... @@ -114,74 +67,34 @@ public class SysUserServiceImpl implements ISysUserService
114 67  
115 68 /**
116 69 * 查询用户所属岗位组
117   - *
  70 + *
118 71 * @param userName 用户名
119 72 * @return 结果
120 73 */
121 74 @Override
122   - public String selectUserPostGroup(String userName)
123   - {
  75 + public String selectUserPostGroup(String userName) {
124 76 List<SysPost> list = postMapper.selectPostsByUserName(userName);
125 77 StringBuffer idsStr = new StringBuffer();
126   - for (SysPost post : list)
127   - {
  78 + for (SysPost post : list) {
128 79 idsStr.append(post.getPostName()).append(",");
129 80 }
130   - if (StringUtils.isNotEmpty(idsStr.toString()))
131   - {
  81 + if (StringUtils.isNotEmpty(idsStr.toString())) {
132 82 return idsStr.substring(0, idsStr.length() - 1);
133 83 }
134 84 return idsStr.toString();
135 85 }
136 86  
137 87 /**
138   - * 校验用户名称是否唯一
139   - *
140   - * @param userName 用户名称
141   - * @return 结果
142   - */
143   - @Override
144   - public String checkUserNameUnique(String userName)
145   - {
146   - int count = userMapper.checkUserNameUnique(userName);
147   - if (count > 0)
148   - {
149   - return UserConstants.NOT_UNIQUE;
150   - }
151   - return UserConstants.UNIQUE;
152   - }
153   -
154   - /**
155   - * 校验用户名称是否唯一
156   - *
157   - * @param user 用户信息
158   - * @return
159   - */
160   - @Override
161   - public String checkPhoneUnique(SysUser user)
162   - {
163   - Long userId = StringUtils.isNull(user.getUserId()) ? -1L : user.getUserId();
164   - SysUser info = userMapper.checkPhoneUnique(user.getPhonenumber());
165   - if (StringUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue())
166   - {
167   - return UserConstants.NOT_UNIQUE;
168   - }
169   - return UserConstants.UNIQUE;
170   - }
171   -
172   - /**
173 88 * 校验email是否唯一
174 89 *
175 90 * @param user 用户信息
176 91 * @return
177 92 */
178 93 @Override
179   - public String checkEmailUnique(SysUser user)
180   - {
  94 + public String checkEmailUnique(SysUser user) {
181 95 Long userId = StringUtils.isNull(user.getUserId()) ? -1L : user.getUserId();
182 96 SysUser info = userMapper.checkEmailUnique(user.getEmail());
183   - if (StringUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue())
184   - {
  97 + if (StringUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue()) {
185 98 return UserConstants.NOT_UNIQUE;
186 99 }
187 100 return UserConstants.UNIQUE;
... ... @@ -189,28 +102,25 @@ public class SysUserServiceImpl implements ISysUserService
189 102  
190 103 /**
191 104 * 校验用户是否允许操作
192   - *
  105 + *
193 106 * @param user 用户信息
194 107 */
195 108 @Override
196   - public void checkUserAllowed(SysUser user)
197   - {
198   - if (StringUtils.isNotNull(user.getUserId()) && user.isAdmin())
199   - {
  109 + public void checkUserAllowed(SysUser user) {
  110 + if (StringUtils.isNotNull(user.getUserId()) && user.isAdmin()) {
200 111 throw new CustomException("不允许操作超级管理员用户");
201 112 }
202 113 }
203 114  
204 115 /**
205 116 * 新增保存用户信息
206   - *
  117 + *
207 118 * @param user 用户信息
208 119 * @return 结果
209 120 */
210 121 @Override
211 122 @Transactional
212   - public int insertUser(SysUser user)
213   - {
  123 + public int insertUser(SysUser user) {
214 124 // 新增用户信息
215 125 int rows = userMapper.insertUser(user);
216 126 // 新增用户岗位关联
... ... @@ -222,14 +132,13 @@ public class SysUserServiceImpl implements ISysUserService
222 132  
223 133 /**
224 134 * 修改保存用户信息
225   - *
  135 + *
226 136 * @param user 用户信息
227 137 * @return 结果
228 138 */
229 139 @Override
230 140 @Transactional
231   - public int updateUser(SysUser user)
232   - {
  141 + public int updateUser(SysUser user) {
233 142 Long userId = user.getUserId();
234 143 // 删除用户与角色关联
235 144 userRoleMapper.deleteUserRoleByUserId(userId);
... ... @@ -243,88 +152,68 @@ public class SysUserServiceImpl implements ISysUserService
243 152 }
244 153  
245 154 /**
246   - * 修改用户状态
247   - *
248   - * @param user 用户信息
249   - * @return 结果
250   - */
251   - @Override
252   - public int updateUserStatus(SysUser user)
253   - {
254   - return userMapper.updateUser(user);
255   - }
256   -
257   - /**
258 155 * 修改用户基本信息
259   - *
  156 + *
260 157 * @param user 用户信息
261 158 * @return 结果
262 159 */
263 160 @Override
264   - public int updateUserProfile(SysUser user)
265   - {
  161 + public int updateUserProfile(SysUser user) {
266 162 return userMapper.updateUser(user);
267 163 }
268 164  
269 165 /**
270 166 * 修改用户头像
271   - *
  167 + *
272 168 * @param userName 用户名
273   - * @param avatar 头像地址
  169 + * @param avatar 头像地址
274 170 * @return 结果
275 171 */
276 172 @Override
277   - public boolean updateUserAvatar(String userName, String avatar)
278   - {
  173 + public boolean updateUserAvatar(String userName, String avatar) {
279 174 return userMapper.updateUserAvatar(userName, avatar) > 0;
280 175 }
281 176  
282 177 /**
283 178 * 重置用户密码
284   - *
  179 + *
285 180 * @param user 用户信息
286 181 * @return 结果
287 182 */
288 183 @Override
289   - public int resetPwd(SysUser user)
290   - {
  184 + public int resetPwd(SysUser user) {
291 185 return userMapper.updateUser(user);
292 186 }
293 187  
294 188 /**
295 189 * 重置用户密码
296   - *
  190 + *
297 191 * @param userName 用户名
298 192 * @param password 密码
299 193 * @return 结果
300 194 */
301 195 @Override
302   - public int resetUserPwd(String userName, String password)
303   - {
  196 + public int resetUserPwd(String userName, String password) {
304 197 return userMapper.resetUserPwd(userName, password);
305 198 }
306 199  
307 200 /**
308 201 * 新增用户角色信息
309   - *
  202 + *
310 203 * @param user 用户对象
311 204 */
312   - public void insertUserRole(SysUser user)
313   - {
  205 + public void insertUserRole(SysUser user) {
314 206 Long[] roles = user.getRoleIds();
315   - if (StringUtils.isNotNull(roles))
316   - {
  207 + if (StringUtils.isNotNull(roles)) {
317 208 // 新增用户与角色管理
318 209 List<SysUserRole> list = new ArrayList<SysUserRole>();
319   - for (Long roleId : roles)
320   - {
  210 + for (Long roleId : roles) {
321 211 SysUserRole ur = new SysUserRole();
322 212 ur.setUserId(user.getUserId());
323 213 ur.setRoleId(roleId);
324 214 list.add(ur);
325 215 }
326   - if (list.size() > 0)
327   - {
  216 + if (list.size() > 0) {
328 217 userRoleMapper.batchUserRole(list);
329 218 }
330 219 }
... ... @@ -332,25 +221,21 @@ public class SysUserServiceImpl implements ISysUserService
332 221  
333 222 /**
334 223 * 新增用户岗位信息
335   - *
  224 + *
336 225 * @param user 用户对象
337 226 */
338   - public void insertUserPost(SysUser user)
339   - {
  227 + public void insertUserPost(SysUser user) {
340 228 Long[] posts = user.getPostIds();
341   - if (StringUtils.isNotNull(posts))
342   - {
  229 + if (StringUtils.isNotNull(posts)) {
343 230 // 新增用户与岗位管理
344 231 List<SysUserPost> list = new ArrayList<SysUserPost>();
345   - for (Long postId : posts)
346   - {
  232 + for (Long postId : posts) {
347 233 SysUserPost up = new SysUserPost();
348 234 up.setUserId(user.getUserId());
349 235 up.setPostId(postId);
350 236 list.add(up);
351 237 }
352   - if (list.size() > 0)
353   - {
  238 + if (list.size() > 0) {
354 239 userPostMapper.batchUserPost(list);
355 240 }
356 241 }
... ... @@ -358,14 +243,13 @@ public class SysUserServiceImpl implements ISysUserService
358 243  
359 244 /**
360 245 * 通过用户ID删除用户
361   - *
  246 + *
362 247 * @param userId 用户ID
363 248 * @return 结果
364 249 */
365 250 @Override
366 251 @Transactional
367   - public int deleteUserById(Long userId)
368   - {
  252 + public int deleteUserById(Long userId) {
369 253 // 删除用户与角色关联
370 254 userRoleMapper.deleteUserRoleByUserId(userId);
371 255 // 删除用户与岗位表
... ... @@ -375,16 +259,14 @@ public class SysUserServiceImpl implements ISysUserService
375 259  
376 260 /**
377 261 * 批量删除用户信息
378   - *
  262 + *
379 263 * @param userIds 需要删除的用户ID
380 264 * @return 结果
381 265 */
382 266 @Override
383 267 @Transactional
384   - public int deleteUserByIds(Long[] userIds)
385   - {
386   - for (Long userId : userIds)
387   - {
  268 + public int deleteUserByIds(Long[] userIds) {
  269 + for (Long userId : userIds) {
388 270 checkUserAllowed(new SysUser(userId));
389 271 }
390 272 // 删除用户与角色关联
... ... @@ -396,17 +278,15 @@ public class SysUserServiceImpl implements ISysUserService
396 278  
397 279 /**
398 280 * 导入用户数据
399   - *
400   - * @param userList 用户数据列表
  281 + *
  282 + * @param userList 用户数据列表
401 283 * @param isUpdateSupport 是否更新支持,如果已存在,则进行更新数据
402   - * @param operName 操作用户
  284 + * @param operName 操作用户
403 285 * @return 结果
404 286 */
405 287 @Override
406   - public String importUser(List<SysUser> userList, Boolean isUpdateSupport, String operName)
407   - {
408   - if (StringUtils.isNull(userList) || userList.size() == 0)
409   - {
  288 + public String importUser(List<SysUser> userList, Boolean isUpdateSupport, String operName) {
  289 + if (StringUtils.isNull(userList) || userList.size() == 0) {
410 290 throw new CustomException("导入用户数据不能为空!");
411 291 }
412 292 int successNum = 0;
... ... @@ -414,48 +294,36 @@ public class SysUserServiceImpl implements ISysUserService
414 294 StringBuilder successMsg = new StringBuilder();
415 295 StringBuilder failureMsg = new StringBuilder();
416 296 String password = configService.selectConfigByKey("sys.user.initPassword");
417   - for (SysUser user : userList)
418   - {
419   - try
420   - {
  297 + for (SysUser user : userList) {
  298 + try {
421 299 // 验证是否存在这个用户
422 300 SysUser u = userMapper.selectUserByUserName(user.getUserName());
423   - if (StringUtils.isNull(u))
424   - {
  301 + if (StringUtils.isNull(u)) {
425 302 user.setPassword(SecurityUtils.encryptPassword(password));
426 303 user.setCreateBy(operName);
427 304 this.insertUser(user);
428 305 successNum++;
429 306 successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 导入成功");
430   - }
431   - else if (isUpdateSupport)
432   - {
  307 + } else if (isUpdateSupport) {
433 308 user.setUpdateBy(operName);
434 309 this.updateUser(user);
435 310 successNum++;
436 311 successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 更新成功");
437   - }
438   - else
439   - {
  312 + } else {
440 313 failureNum++;
441 314 failureMsg.append("<br/>" + failureNum + "、账号 " + user.getUserName() + " 已存在");
442 315 }
443   - }
444   - catch (Exception e)
445   - {
  316 + } catch (Exception e) {
446 317 failureNum++;
447 318 String msg = "<br/>" + failureNum + "、账号 " + user.getUserName() + " 导入失败:";
448 319 failureMsg.append(msg + e.getMessage());
449 320 log.error(msg, e);
450 321 }
451 322 }
452   - if (failureNum > 0)
453   - {
  323 + if (failureNum > 0) {
454 324 failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
455 325 throw new CustomException(failureMsg.toString());
456   - }
457   - else
458   - {
  326 + } else {
459 327 successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
460 328 }
461 329 return successMsg.toString();
... ...
ruoyi-ui/src/api/system/user.js
... ... @@ -4,7 +4,7 @@ import { praseStrEmpty } from &quot;@/utils/ruoyi&quot;;
4 4 // 查询用户列表
5 5 export function listUser(query) {
6 6 return request({
7   - url: '/system/user/list',
  7 + url: '/system/user/page',
8 8 method: 'get',
9 9 params: query
10 10 })
... ... @@ -13,7 +13,7 @@ export function listUser(query) {
13 13 // 查询用户详细
14 14 export function getUser(userId) {
15 15 return request({
16   - url: '/system/user/' + praseStrEmpty(userId),
  16 + url: '/system/user/get?id=' + praseStrEmpty(userId),
17 17 method: 'get'
18 18 })
19 19 }
... ... @@ -21,7 +21,7 @@ export function getUser(userId) {
21 21 // 新增用户
22 22 export function addUser(data) {
23 23 return request({
24   - url: '/system/user',
  24 + url: '/system/user/create',
25 25 method: 'post',
26 26 data: data
27 27 })
... ... @@ -30,7 +30,7 @@ export function addUser(data) {
30 30 // 修改用户
31 31 export function updateUser(data) {
32 32 return request({
33   - url: '/system/user',
  33 + url: '/system/user/update',
34 34 method: 'put',
35 35 data: data
36 36 })
... ... @@ -39,7 +39,7 @@ export function updateUser(data) {
39 39 // 删除用户
40 40 export function delUser(userId) {
41 41 return request({
42   - url: '/system/user/' + userId,
  42 + url: '/system/user/delete?id=' + userId,
43 43 method: 'delete'
44 44 })
45 45 }
... ... @@ -73,7 +73,7 @@ export function changeUserStatus(userId, status) {
73 73 status
74 74 }
75 75 return request({
76   - url: '/system/user/changeStatus',
  76 + url: '/system/user/updateStatus',
77 77 method: 'put',
78 78 data: data
79 79 })
... ...
ruoyi-ui/src/views/system/user/index.vue
... ... @@ -28,9 +28,9 @@
28 28 <!--用户数据-->
29 29 <el-col :span="20" :xs="24">
30 30 <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
31   - <el-form-item label="用户名称" prop="userName">
  31 + <el-form-item label="用户名称" prop="username">
32 32 <el-input
33   - v-model="queryParams.userName"
  33 + v-model="queryParams.username"
34 34 placeholder="请输入用户名称"
35 35 clearable
36 36 size="small"
... ... @@ -38,9 +38,9 @@
38 38 @keyup.enter.native="handleQuery"
39 39 />
40 40 </el-form-item>
41   - <el-form-item label="手机号码" prop="phonenumber">
  41 + <el-form-item label="手机号码" prop="mobile">
42 42 <el-input
43   - v-model="queryParams.phonenumber"
  43 + v-model="queryParams.mobile"
44 44 placeholder="请输入手机号码"
45 45 clearable
46 46 size="small"
... ... @@ -57,10 +57,10 @@
57 57 style="width: 240px"
58 58 >
59 59 <el-option
60   - v-for="dict in statusOptions"
61   - :key="dict.dictValue"
62   - :label="dict.dictLabel"
63   - :value="dict.dictValue"
  60 + v-for="dict in statusDictDatas"
  61 + :key="parseInt(dict.value)"
  62 + :label="dict.label"
  63 + :value="parseInt(dict.value)"
64 64 />
65 65 </el-select>
66 66 </el-form-item>
... ... @@ -94,26 +94,6 @@
94 94 </el-col>
95 95 <el-col :span="1.5">
96 96 <el-button
97   - type="success"
98   - icon="el-icon-edit"
99   - size="mini"
100   - :disabled="single"
101   - @click="handleUpdate"
102   - v-hasPermi="['system:user:edit']"
103   - >修改</el-button>
104   - </el-col>
105   - <el-col :span="1.5">
106   - <el-button
107   - type="danger"
108   - icon="el-icon-delete"
109   - size="mini"
110   - :disabled="multiple"
111   - @click="handleDelete"
112   - v-hasPermi="['system:user:remove']"
113   - >删除</el-button>
114   - </el-col>
115   - <el-col :span="1.5">
116   - <el-button
117 97 type="info"
118 98 icon="el-icon-upload2"
119 99 size="mini"
... ... @@ -133,20 +113,19 @@
133 113 <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
134 114 </el-row>
135 115  
136   - <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
137   - <el-table-column type="selection" width="50" align="center" />
138   - <el-table-column label="用户编号" align="center" prop="userId" />
139   - <el-table-column label="用户名称" align="center" prop="userName" :show-overflow-tooltip="true" />
140   - <el-table-column label="用户昵称" align="center" prop="nickName" :show-overflow-tooltip="true" />
141   - <el-table-column label="部门" align="center" prop="dept.deptName" :show-overflow-tooltip="true" />
142   - <el-table-column label="手机号码" align="center" prop="phonenumber" width="120" />
  116 + <el-table v-loading="loading" :data="userList">
  117 + <el-table-column label="用户编号" align="center" prop="id" />
  118 + <el-table-column label="用户名称" align="center" prop="username" :show-overflow-tooltip="true" />
  119 + <el-table-column label="用户昵称" align="center" prop="nickname" :show-overflow-tooltip="true" />
  120 + <el-table-column label="部门" align="center" prop="dept.name" :show-overflow-tooltip="true" />
  121 + <el-table-column label="手机号码" align="center" prop="mobile" width="120" />
143 122 <el-table-column label="状态" align="center">
144 123 <template slot-scope="scope">
145 124 <el-switch
146   - v-model="scope.row.status"
147   - active-value="0"
148   - inactive-value="1"
149   - @change="handleStatusChange(scope.row)"
  125 + v-model="scope.row.status"
  126 + :active-value="0"
  127 + :inactive-value="1"
  128 + @change="handleStatusChange(scope.row)"
150 129 ></el-switch>
151 130 </template>
152 131 </el-table-column>
... ... @@ -170,7 +149,7 @@
170 149 v-hasPermi="['system:user:edit']"
171 150 >修改</el-button>
172 151 <el-button
173   - v-if="scope.row.userId !== 1"
  152 + v-if="scope.row.id !== 1"
174 153 size="mini"
175 154 type="text"
176 155 icon="el-icon-delete"
... ... @@ -191,7 +170,7 @@
191 170 <pagination
192 171 v-show="total>0"
193 172 :total="total"
194   - :page.sync="queryParams.pageNum"
  173 + :page.sync="queryParams.pageNo"
195 174 :limit.sync="queryParams.pageSize"
196 175 @pagination="getList"
197 176 />
... ... @@ -203,8 +182,8 @@
203 182 <el-form ref="form" :model="form" :rules="rules" label-width="80px">
204 183 <el-row>
205 184 <el-col :span="12">
206   - <el-form-item label="用户昵称" prop="nickName">
207   - <el-input v-model="form.nickName" placeholder="请输入用户昵称" />
  185 + <el-form-item label="用户昵称" prop="nickname">
  186 + <el-input v-model="form.nickname" placeholder="请输入用户昵称" />
208 187 </el-form-item>
209 188 </el-col>
210 189 <el-col :span="12">
... ... @@ -215,8 +194,8 @@
215 194 </el-row>
216 195 <el-row>
217 196 <el-col :span="12">
218   - <el-form-item label="手机号码" prop="phonenumber">
219   - <el-input v-model="form.phonenumber" placeholder="请输入手机号码" maxlength="11" />
  197 + <el-form-item label="手机号码" prop="mobile">
  198 + <el-input v-model="form.mobile" placeholder="请输入手机号码" maxlength="11" />
220 199 </el-form-item>
221 200 </el-col>
222 201 <el-col :span="12">
... ... @@ -227,12 +206,12 @@
227 206 </el-row>
228 207 <el-row>
229 208 <el-col :span="12">
230   - <el-form-item v-if="form.userId == undefined" label="用户名称" prop="userName">
231   - <el-input v-model="form.userName" placeholder="请输入用户名称" />
  209 + <el-form-item v-if="form.id === undefined" label="用户名称" prop="username">
  210 + <el-input v-model="form.username" placeholder="请输入用户名称" />
232 211 </el-form-item>
233 212 </el-col>
234 213 <el-col :span="12">
235   - <el-form-item v-if="form.userId == undefined" label="用户密码" prop="password">
  214 + <el-form-item v-if="form.id === undefined" label="用户密码" prop="password">
236 215 <el-input v-model="form.password" placeholder="请输入用户密码" type="password" />
237 216 </el-form-item>
238 217 </el-col>
... ... @@ -344,6 +323,11 @@ import { treeselect } from &quot;@/api/system/dept&quot;;
344 323 import Treeselect from "@riophae/vue-treeselect";
345 324 import "@riophae/vue-treeselect/dist/vue-treeselect.css";
346 325  
  326 +import {listSimpleDepts} from "@/api/system/dept";
  327 +
  328 +import {SysCommonStatusEnum} from "@/utils/constants";
  329 +import {DICT_TYPE, getDictDatas} from "@/utils/dict";
  330 +
347 331 export default {
348 332 name: "User",
349 333 components: { Treeselect },
... ... @@ -351,12 +335,6 @@ export default {
351 335 return {
352 336 // 遮罩层
353 337 loading: true,
354   - // 选中数组
355   - ids: [],
356   - // 非单个禁用
357   - single: true,
358   - // 非多个禁用
359   - multiple: true,
360 338 // 显示搜索条件
361 339 showSearch: true,
362 340 // 总条数
... ... @@ -387,7 +365,7 @@ export default {
387 365 form: {},
388 366 defaultProps: {
389 367 children: "children",
390   - label: "label"
  368 + label: "name"
391 369 },
392 370 // 用户导入参数
393 371 upload: {
... ... @@ -406,19 +384,19 @@ export default {
406 384 },
407 385 // 查询参数
408 386 queryParams: {
409   - pageNum: 1,
  387 + pageNo: 1,
410 388 pageSize: 10,
411   - userName: undefined,
412   - phonenumber: undefined,
  389 + username: undefined,
  390 + mobile: undefined,
413 391 status: undefined,
414 392 deptId: undefined
415 393 },
416 394 // 表单校验
417 395 rules: {
418   - userName: [
  396 + username: [
419 397 { required: true, message: "用户名称不能为空", trigger: "blur" }
420 398 ],
421   - nickName: [
  399 + nickname: [
422 400 { required: true, message: "用户昵称不能为空", trigger: "blur" }
423 401 ],
424 402 password: [
... ... @@ -431,14 +409,19 @@ export default {
431 409 trigger: ["blur", "change"]
432 410 }
433 411 ],
434   - phonenumber: [
  412 + mobile: [
435 413 {
436 414 pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
437 415 message: "请输入正确的手机号码",
438 416 trigger: "blur"
439 417 }
440 418 ]
441   - }
  419 + },
  420 +
  421 + // 枚举
  422 + SysCommonStatusEnum: SysCommonStatusEnum,
  423 + // 数据字典
  424 + statusDictDatas: getDictDatas(DICT_TYPE.SYS_COMMON_STATUS),
442 425 };
443 426 },
444 427 watch: {
... ... @@ -450,12 +433,6 @@ export default {
450 433 created() {
451 434 this.getList();
452 435 this.getTreeselect();
453   - this.getDicts("sys_normal_disable").then(response => {
454   - this.statusOptions = response.data;
455   - });
456   - this.getDicts("sys_user_sex").then(response => {
457   - this.sexOptions = response.data;
458   - });
459 436 this.getConfigKey("sys.user.initPassword").then(response => {
460 437 this.initPassword = response.msg;
461 438 });
... ... @@ -464,23 +441,28 @@ export default {
464 441 /** 查询用户列表 */
465 442 getList() {
466 443 this.loading = true;
467   - listUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
468   - this.userList = response.rows;
469   - this.total = response.total;
  444 + listUser(this.addDateRange(this.queryParams, [
  445 + this.dateRange[0] ? this.dateRange[0] + ' 00:00:00' : undefined,
  446 + this.dateRange[1] ? this.dateRange[1] + ' 23:59:59' : undefined,
  447 + ])).then(response => {
  448 + this.userList = response.data.list;
  449 + this.total = response.data.list;
470 450 this.loading = false;
471 451 }
472 452 );
473 453 },
474 454 /** 查询部门下拉树结构 */
475 455 getTreeselect() {
476   - treeselect().then(response => {
477   - this.deptOptions = response.data;
  456 + listSimpleDepts().then(response => {
  457 + // 处理 menuOptions 参数
  458 + this.deptOptions = [];
  459 + this.deptOptions.push(...this.handleTree(response.data, "id"));
478 460 });
479 461 },
480 462 // 筛选节点
481 463 filterNode(value, data) {
482 464 if (!value) return true;
483   - return data.label.indexOf(value) !== -1;
  465 + return data.name.indexOf(value) !== -1;
484 466 },
485 467 // 节点单击事件
486 468 handleNodeClick(data) {
... ... @@ -490,12 +472,12 @@ export default {
490 472 // 用户状态修改
491 473 handleStatusChange(row) {
492 474 let text = row.status === "0" ? "启用" : "停用";
493   - this.$confirm('确认要"' + text + '""' + row.userName + '"用户吗?', "警告", {
  475 + this.$confirm('确认要"' + text + '""' + row.username + '"用户吗?', "警告", {
494 476 confirmButtonText: "确定",
495 477 cancelButtonText: "取消",
496 478 type: "warning"
497 479 }).then(function() {
498   - return changeUserStatus(row.userId, row.status);
  480 + return changeUserStatus(row.id, row.status);
499 481 }).then(() => {
500 482 this.msgSuccess(text + "成功");
501 483 }).catch(function() {
... ... @@ -510,12 +492,12 @@ export default {
510 492 // 表单重置
511 493 reset() {
512 494 this.form = {
513   - userId: undefined,
  495 + id: undefined,
514 496 deptId: undefined,
515   - userName: undefined,
516   - nickName: undefined,
  497 + username: undefined,
  498 + nickname: undefined,
517 499 password: undefined,
518   - phonenumber: undefined,
  500 + mobile: undefined,
519 501 email: undefined,
520 502 sex: undefined,
521 503 status: "0",
... ... @@ -536,12 +518,6 @@ export default {
536 518 this.resetForm("queryForm");
537 519 this.handleQuery();
538 520 },
539   - // 多选框选中数据
540   - handleSelectionChange(selection) {
541   - this.ids = selection.map(item => item.userId);
542   - this.single = selection.length != 1;
543   - this.multiple = !selection.length;
544   - },
545 521 /** 新增按钮操作 */
546 522 handleAdd() {
547 523 this.reset();
... ... @@ -558,8 +534,8 @@ export default {
558 534 handleUpdate(row) {
559 535 this.reset();
560 536 this.getTreeselect();
561   - const userId = row.userId || this.ids;
562   - getUser(userId).then(response => {
  537 + const id = row.id;
  538 + getUser(id).then(response => {
563 539 this.form = response.data;
564 540 this.postOptions = response.posts;
565 541 this.roleOptions = response.roles;
... ... @@ -572,11 +548,11 @@ export default {
572 548 },
573 549 /** 重置密码按钮操作 */
574 550 handleResetPwd(row) {
575   - this.$prompt('请输入"' + row.userName + '"的新密码', "提示", {
  551 + this.$prompt('请输入"' + row.username + '"的新密码', "提示", {
576 552 confirmButtonText: "确定",
577 553 cancelButtonText: "取消"
578 554 }).then(({ value }) => {
579   - resetUserPwd(row.userId, value).then(response => {
  555 + resetUserPwd(row.id, value).then(response => {
580 556 this.msgSuccess("修改成功,新密码是:" + value);
581 557 });
582 558 }).catch(() => {});
... ... @@ -585,7 +561,7 @@ export default {
585 561 submitForm: function() {
586 562 this.$refs["form"].validate(valid => {
587 563 if (valid) {
588   - if (this.form.userId != undefined) {
  564 + if (this.form.id != undefined) {
589 565 updateUser(this.form).then(response => {
590 566 this.msgSuccess("修改成功");
591 567 this.open = false;
... ... @@ -603,13 +579,13 @@ export default {
603 579 },
604 580 /** 删除按钮操作 */
605 581 handleDelete(row) {
606   - const userIds = row.userId || this.ids;
607   - this.$confirm('是否确认删除用户编号为"' + userIds + '"的数据项?', "警告", {
  582 + const ids = row.id || this.ids;
  583 + this.$confirm('是否确认删除用户编号为"' + ids + '"的数据项?', "警告", {
608 584 confirmButtonText: "确定",
609 585 cancelButtonText: "取消",
610 586 type: "warning"
611 587 }).then(function() {
612   - return delUser(userIds);
  588 + return delUser(ids);
613 589 }).then(() => {
614 590 this.getList();
615 591 this.msgSuccess("删除成功");
... ... @@ -657,4 +633,4 @@ export default {
657 633 }
658 634 }
659 635 };
660   -</script>
661 636 \ No newline at end of file
  637 +</script>
... ...
src/main/java/cn/iocoder/dashboard/common/pojo/PageResult.java
... ... @@ -5,6 +5,7 @@ import io.swagger.annotations.ApiModelProperty;
5 5 import lombok.Data;
6 6  
7 7 import java.io.Serializable;
  8 +import java.util.ArrayList;
8 9 import java.util.List;
9 10  
10 11 @ApiModel("分页结果")
... ... @@ -17,4 +18,17 @@ public final class PageResult&lt;T&gt; implements Serializable {
17 18 @ApiModelProperty(value = "总量", required = true)
18 19 private Long total;
19 20  
  21 + public PageResult() {
  22 + }
  23 +
  24 + public PageResult(List<T> list, Long total) {
  25 + this.list = list;
  26 + this.total = total;
  27 + }
  28 +
  29 + public PageResult(Long total) {
  30 + this.list = new ArrayList<>();
  31 + this.total = total;
  32 + }
  33 +
20 34 }
... ...
src/main/java/cn/iocoder/dashboard/framework/security/core/LoginUser.java
... ... @@ -21,7 +21,7 @@ public class LoginUser implements UserDetails {
21 21 /**
22 22 * 用户编号
23 23 */
24   - private Long userId;
  24 + private Long id;
25 25 /**
26 26 * 科室编号
27 27 */
... ...
src/main/java/cn/iocoder/dashboard/framework/security/core/handler/AccessDeniedHandlerImpl.java
... ... @@ -6,7 +6,6 @@ import cn.iocoder.dashboard.framework.security.core.util.SecurityUtils;
6 6 import cn.iocoder.dashboard.util.servlet.ServletUtils;
7 7 import lombok.extern.slf4j.Slf4j;
8 8 import org.springframework.security.access.AccessDeniedException;
9   -import org.springframework.security.core.AuthenticationException;
10 9 import org.springframework.security.web.access.AccessDeniedHandler;
11 10 import org.springframework.security.web.access.ExceptionTranslationFilter;
12 11 import org.springframework.stereotype.Component;
... ... @@ -36,7 +35,7 @@ public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
36 35 throws IOException, ServletException {
37 36 // 打印 warn 的原因是,不定期合并 warn,看看有没恶意破坏
38 37 log.warn("[commence][访问 URL({}) 时,用户({}) 权限不够]", request.getRequestURI(),
39   - SecurityUtils.getLoginUser().getUserId(), e);
  38 + SecurityUtils.getLoginUser().getId(), e);
40 39 // 返回 403
41 40 ServletUtils.writeJSON(response, CommonResult.error(UNAUTHORIZED));
42 41 }
... ...
src/main/java/cn/iocoder/dashboard/framework/security/core/util/SecurityUtils.java
... ... @@ -50,7 +50,7 @@ public class SecurityUtils {
50 50 * @return 用户编号
51 51 */
52 52 public static Long getLoginUserId() {
53   - return getLoginUser().getUserId();
  53 + return getLoginUser().getId();
54 54 }
55 55  
56 56 public static Set<Long> getLoginUserRoleIds() {
... ...
src/main/java/cn/iocoder/dashboard/modules/system/controller/user/SysUserController.java
1 1 package cn.iocoder.dashboard.modules.system.controller.user;
2 2  
  3 +import cn.hutool.core.collection.CollUtil;
  4 +import cn.iocoder.dashboard.common.pojo.CommonResult;
  5 +import cn.iocoder.dashboard.common.pojo.PageResult;
  6 +import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserPageItemRespVO;
  7 +import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserPageReqVO;
  8 +import cn.iocoder.dashboard.modules.system.convert.user.SysUserConvert;
  9 +import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.dept.SysDeptDO;
  10 +import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.user.SysUserDO;
  11 +import cn.iocoder.dashboard.modules.system.service.dept.SysDeptService;
  12 +import cn.iocoder.dashboard.modules.system.service.user.SysUserService;
  13 +import cn.iocoder.dashboard.util.collection.CollectionUtils;
  14 +import io.swagger.annotations.Api;
  15 +import io.swagger.annotations.ApiOperation;
  16 +import org.springframework.web.bind.annotation.GetMapping;
  17 +import org.springframework.web.bind.annotation.RequestMapping;
  18 +import org.springframework.web.bind.annotation.RestController;
  19 +
  20 +import javax.annotation.Resource;
  21 +import java.util.*;
  22 +
  23 +import static cn.iocoder.dashboard.common.pojo.CommonResult.success;
  24 +
  25 +@Api(tags = "用户 API")
  26 +@RestController
  27 +@RequestMapping("/system/user")
3 28 public class SysUserController {
4 29  
5   -// /**
6   -// * 获取用户列表
7   -// */
  30 + @Resource
  31 + private SysUserService userService;
  32 + @Resource
  33 + private SysDeptService deptService;
  34 +
  35 + @ApiOperation("获得用户分页列表")
  36 + @GetMapping("/page")
8 37 // @PreAuthorize("@ss.hasPermi('system:user:list')")
9   -// @GetMapping("/list")
10   -// public TableDataInfo list(SysUser user)
11   -// {
12   -// startPage();
13   -// List<SysUser> list = userService.selectUserList(user);
14   -// return getDataTable(list);
15   -// }
  38 + public CommonResult<PageResult<SysUserPageItemRespVO>> pageUsers(SysUserPageReqVO reqVO) {
  39 + // 获得用户分页列表
  40 + PageResult<SysUserDO> pageResult = userService.pageUsers(reqVO);
  41 + if (CollUtil.isEmpty(pageResult.getList())) {
  42 + return success(new PageResult<>(pageResult.getTotal())); // 返回空
  43 + }
  44 +
  45 + // 获得拼接需要的数据
  46 + Map<Long, SysDeptDO> deptMap;
  47 + Collection<Long> deptIds = CollectionUtils.convertList(pageResult.getList(), SysUserDO::getDeptId);
  48 + if (CollUtil.isNotEmpty(deptIds)) {
  49 + deptMap = CollectionUtils.convertMap(deptService.listDepts(deptIds), SysDeptDO::getId);
  50 + } else {
  51 + deptMap = Collections.emptyMap();
  52 + }
  53 + // 拼接结果返回
  54 + List<SysUserPageItemRespVO> userList = new ArrayList<>(pageResult.getList().size());
  55 + pageResult.getList().forEach(user -> {
  56 + SysUserPageItemRespVO respVO = SysUserConvert.INSTANCE.convert(user);
  57 + respVO.setDept(SysUserConvert.INSTANCE.convert(deptMap.get(user.getDeptId())));
  58 + userList.add(respVO);
  59 + });
  60 + return success(new PageResult<>(userList, pageResult.getTotal()));
  61 + }
16 62 //
17 63 // @Log(title = "用户管理", businessType = BusinessType.EXPORT)
18 64 // @PreAuthorize("@ss.hasPermi('system:user:export')")
... ...
src/main/java/cn/iocoder/dashboard/modules/system/controller/user/vo/user/SysUserPageItemRespVO.java 0 → 100644
  1 +package cn.iocoder.dashboard.modules.system.controller.user.vo.user;
  2 +
  3 +import io.swagger.annotations.ApiModel;
  4 +import io.swagger.annotations.ApiModelProperty;
  5 +import lombok.AllArgsConstructor;
  6 +import lombok.Data;
  7 +import lombok.EqualsAndHashCode;
  8 +import lombok.NoArgsConstructor;
  9 +
  10 +@ApiModel(value = "用户分页时的信息 Response VO", description = "相比用户基本信息来说,会多部门信息")
  11 +@Data
  12 +@NoArgsConstructor
  13 +@AllArgsConstructor
  14 +@EqualsAndHashCode(callSuper = true)
  15 +public class SysUserPageItemRespVO extends SysUserRespVO {
  16 +
  17 + /**
  18 + * 所在部门
  19 + */
  20 + private Dept dept;
  21 +
  22 + @ApiModel("部门")
  23 + @Data
  24 + public static class Dept {
  25 +
  26 + @ApiModelProperty(value = "部门编号", required = true, example = "1")
  27 + private Integer id;
  28 +
  29 + @ApiModelProperty(value = "部门名称", required = true, example = "研发部")
  30 + private String name;
  31 +
  32 + }
  33 +
  34 +}
... ...
src/main/java/cn/iocoder/dashboard/modules/system/controller/user/vo/user/SysUserPageReqVO.java 0 → 100644
  1 +package cn.iocoder.dashboard.modules.system.controller.user.vo.user;
  2 +
  3 +import cn.iocoder.dashboard.common.pojo.PageParam;
  4 +import io.swagger.annotations.ApiModel;
  5 +import io.swagger.annotations.ApiModelProperty;
  6 +import lombok.AllArgsConstructor;
  7 +import lombok.Data;
  8 +import lombok.EqualsAndHashCode;
  9 +import lombok.NoArgsConstructor;
  10 +import org.springframework.format.annotation.DateTimeFormat;
  11 +
  12 +import java.util.Date;
  13 +
  14 +import static cn.iocoder.dashboard.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
  15 +
  16 +@ApiModel("用户分页 Request VO")
  17 +@Data
  18 +@NoArgsConstructor
  19 +@AllArgsConstructor
  20 +@EqualsAndHashCode(callSuper = true)
  21 +public class SysUserPageReqVO extends PageParam {
  22 +
  23 + @ApiModelProperty(value = "用户账号", example = "yudao", notes = "模糊匹配")
  24 + private String username;
  25 +
  26 + @ApiModelProperty(value = "手机号码", example = "yudao", notes = "模糊匹配")
  27 + private String mobile;
  28 +
  29 + @ApiModelProperty(value = "展示状态", example = "1", notes = "参见 SysCommonStatusEnum 枚举类")
  30 + private Integer status;
  31 +
  32 + @ApiModelProperty(value = "开始时间", example = "2020-10-24")
  33 + @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
  34 + private Date beginTime;
  35 +
  36 + @ApiModelProperty(value = "结束时间", example = "2020-10-24")
  37 + @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
  38 + private Date endTime;
  39 +
  40 + @ApiModelProperty(value = "部门编号", example = "1024", notes = "同时筛选子部门")
  41 + private Long deptId;
  42 +
  43 +}
... ...
src/main/java/cn/iocoder/dashboard/modules/system/convert/user/SysUserConvert.java 0 → 100644
  1 +package cn.iocoder.dashboard.modules.system.convert.user;
  2 +
  3 +import cn.iocoder.dashboard.common.pojo.PageResult;
  4 +import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserPageItemRespVO;
  5 +import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.dept.SysDeptDO;
  6 +import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.user.SysUserDO;
  7 +import com.baomidou.mybatisplus.core.metadata.IPage;
  8 +import org.mapstruct.Mapper;
  9 +import org.mapstruct.Mapping;
  10 +import org.mapstruct.factory.Mappers;
  11 +
  12 +@Mapper
  13 +public interface SysUserConvert {
  14 +
  15 + SysUserConvert INSTANCE = Mappers.getMapper(SysUserConvert.class);
  16 +
  17 + SysUserPageItemRespVO convert(SysUserDO bean);
  18 +
  19 + SysUserPageItemRespVO.Dept convert(SysDeptDO bean);
  20 +
  21 + @Mapping(source = "records", target = "list")
  22 + PageResult<SysUserDO> convertPage(IPage<SysUserDO> page);
  23 +
  24 +}
... ...
src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dao/user/SysUserMapper.java
1 1 package cn.iocoder.dashboard.modules.system.dal.mysql.dao.user;
2 2  
  3 +import cn.iocoder.dashboard.framework.mybatis.core.query.QueryWrapperX;
  4 +import cn.iocoder.dashboard.framework.mybatis.core.util.MyBatisUtils;
  5 +import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserPageReqVO;
3 6 import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.user.SysUserDO;
4 7 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
5 8 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  9 +import com.baomidou.mybatisplus.core.metadata.IPage;
6 10 import org.apache.ibatis.annotations.Mapper;
7 11  
  12 +import java.util.List;
  13 +
8 14 @Mapper
9 15 public interface SysUserMapper extends BaseMapper<SysUserDO> {
10 16  
... ... @@ -12,4 +18,14 @@ public interface SysUserMapper extends BaseMapper&lt;SysUserDO&gt; {
12 18 return selectOne(new QueryWrapper<SysUserDO>().eq("username", username));
13 19 }
14 20  
  21 + default IPage<SysUserDO> selectList(SysUserPageReqVO reqVO, List<Long> deptIds) {
  22 + return selectPage(MyBatisUtils.buildPage(reqVO),
  23 + new QueryWrapperX<SysUserDO>().likeIfPresent("username", reqVO.getUsername())
  24 + .likeIfPresent("mobile", reqVO.getMobile())
  25 + .eqIfPresent("status", reqVO.getStatus())
  26 + .betweenIfPresent("create_time", reqVO.getBeginTime(), reqVO.getEndTime())
  27 + .inIfPresent("dept_id", deptIds));
  28 + }
  29 +
15 30 }
  31 +
... ...
src/main/java/cn/iocoder/dashboard/modules/system/service/auth/impl/SysAuthServiceImpl.java
... ... @@ -80,7 +80,7 @@ public class SysAuthServiceImpl implements SysAuthService {
80 80 // 创建 LoginUser 对象
81 81 LoginUser loginUser = SysAuthConvert.INSTANCE.convert(user);
82 82 loginUser.setUpdateTime(new Date());
83   - loginUser.setRoleIds(this.getUserRoleIds(loginUser.getUserId()));
  83 + loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId()));
84 84 return loginUser;
85 85 }
86 86  
... ... @@ -94,7 +94,7 @@ public class SysAuthServiceImpl implements SysAuthService {
94 94 // 缓存登陆用户到 Redis 中
95 95 String sessionId = IdUtil.fastSimpleUUID();
96 96 loginUser.setUpdateTime(new Date());
97   - loginUser.setRoleIds(this.getUserRoleIds(loginUser.getUserId()));
  97 + loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId()));
98 98 loginUserRedisDAO.set(sessionId, loginUser);
99 99  
100 100 // 创建 Token
... ... @@ -197,7 +197,7 @@ public class SysAuthServiceImpl implements SysAuthService {
197 197 }
198 198  
199 199 // 重新加载 SysUserDO 信息
200   - SysUserDO user = userService.getUser(loginUser.getUserId());
  200 + SysUserDO user = userService.getUser(loginUser.getId());
201 201 if (user == null || CommonStatusEnum.DISABLE.getStatus().equals(user.getStatus())) {
202 202 throw exception(TOKEN_EXPIRED); // 校验 token 时,用户被禁用的情况下,也认为 token 过期,方便前端跳转到登陆界面
203 203 }
... ... @@ -205,7 +205,7 @@ public class SysAuthServiceImpl implements SysAuthService {
205 205 // 刷新 LoginUser 缓存
206 206 loginUser.setDeptId(user.getDeptId());
207 207 loginUser.setUpdateTime(new Date());
208   - loginUser.setRoleIds(this.getUserRoleIds(loginUser.getUserId()));
  208 + loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId()));
209 209 loginUserRedisDAO.set(sessionId, loginUser);
210 210 }
211 211  
... ...
src/main/java/cn/iocoder/dashboard/modules/system/service/dept/SysDeptService.java
... ... @@ -3,6 +3,7 @@ package cn.iocoder.dashboard.modules.system.service.dept;
3 3 import cn.iocoder.dashboard.modules.system.controller.dept.vo.dept.SysDeptListReqVO;
4 4 import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.dept.SysDeptDO;
5 5  
  6 +import java.util.Collection;
6 7 import java.util.List;
7 8  
8 9 /**
... ... @@ -13,6 +14,11 @@ import java.util.List;
13 14 public interface SysDeptService {
14 15  
15 16 /**
  17 + * 初始化
  18 + */
  19 + void init();
  20 +
  21 + /**
16 22 * 获得所有部门列表
17 23 *
18 24 * @return 部门列表
... ... @@ -20,6 +26,14 @@ public interface SysDeptService {
20 26 List<SysDeptDO> listDepts();
21 27  
22 28 /**
  29 + * 获得指定编号的部门列表
  30 + *
  31 + * @param ids 部门编号数组
  32 + * @return 部门列表
  33 + */
  34 + List<SysDeptDO> listDepts(Collection<Long> ids);
  35 +
  36 + /**
23 37 * 筛选部门列表
24 38 *
25 39 * @param reqVO 筛选条件请求 VO
... ... @@ -27,4 +41,13 @@ public interface SysDeptService {
27 41 */
28 42 List<SysDeptDO> listDepts(SysDeptListReqVO reqVO);
29 43  
  44 + /**
  45 + * 获得所有子部门,从缓存中
  46 + *
  47 + * @param parentId 部门编号
  48 + * @param recursive 是否递归获取所有
  49 + * @return 子部门列表
  50 + */
  51 + List<SysDeptDO> listDeptsByParentIdFromCache(Long parentId, boolean recursive);
  52 +
30 53 }
... ...
src/main/java/cn/iocoder/dashboard/modules/system/service/dept/impl/SysDeptServiceImpl.java
1 1 package cn.iocoder.dashboard.modules.system.service.dept.impl;
2 2  
  3 +import cn.hutool.core.collection.CollUtil;
3 4 import cn.iocoder.dashboard.modules.system.controller.dept.vo.dept.SysDeptListReqVO;
4 5 import cn.iocoder.dashboard.modules.system.dal.mysql.dao.dept.SysDeptMapper;
5 6 import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.dept.SysDeptDO;
6 7 import cn.iocoder.dashboard.modules.system.service.dept.SysDeptService;
  8 +import com.google.common.collect.ImmutableMap;
  9 +import com.google.common.collect.ImmutableMultimap;
  10 +import com.google.common.collect.Multimap;
  11 +import lombok.extern.slf4j.Slf4j;
7 12 import org.springframework.stereotype.Service;
8 13  
  14 +import javax.annotation.PostConstruct;
9 15 import javax.annotation.Resource;
  16 +import java.util.ArrayList;
  17 +import java.util.Collection;
10 18 import java.util.List;
  19 +import java.util.Map;
11 20  
12 21 /**
13 22 * 部门 Service 实现类
... ... @@ -15,19 +24,94 @@ import java.util.List;
15 24 * @author 芋道源码
16 25 */
17 26 @Service
  27 +@Slf4j
18 28 public class SysDeptServiceImpl implements SysDeptService {
19 29  
  30 + /**
  31 + * 部门缓存
  32 + * key:部门编号 {@link SysDeptDO#getId()}
  33 + *
  34 + * 这里声明 volatile 修饰的原因是,每次刷新时,直接修改指向
  35 + */
  36 + private volatile Map<Long, SysDeptDO> deptCache;
  37 + /**
  38 + * 父部门缓存
  39 + * key:部门编号 {@link SysDeptDO#getParentId()}
  40 + * value: 直接子部门列表
  41 + *
  42 + * 这里声明 volatile 修饰的原因是,每次刷新时,直接修改指向
  43 + */
  44 + private volatile Multimap<Long, SysDeptDO> parentDeptCache;
  45 +
20 46 @Resource
21 47 private SysDeptMapper deptMapper;
22 48  
23 49 @Override
  50 + @PostConstruct
  51 + public void init() {
  52 + // 从数据库中读取
  53 + List<SysDeptDO> sysDeptDOList = deptMapper.selectList();
  54 + // 构建缓存
  55 + ImmutableMap.Builder<Long, SysDeptDO> builder = ImmutableMap.builder();
  56 + ImmutableMultimap.Builder<Long, SysDeptDO> parentBuilder = ImmutableMultimap.builder();
  57 + sysDeptDOList.forEach(sysRoleDO -> {
  58 + builder.put(sysRoleDO.getId(), sysRoleDO);
  59 + parentBuilder.put(sysRoleDO.getParentId(), sysRoleDO);
  60 + });
  61 + // 设置缓存
  62 + deptCache = builder.build();
  63 + parentDeptCache = parentBuilder.build();
  64 + log.info("[init][初始化 Dept 数量为 {}]", sysDeptDOList.size());
  65 + }
  66 +
  67 + @Override
24 68 public List<SysDeptDO> listDepts() {
25 69 return deptMapper.selectList();
26 70 }
27 71  
28 72 @Override
  73 + public List<SysDeptDO> listDepts(Collection<Long> ids) {
  74 + return deptMapper.selectBatchIds(ids);
  75 + }
  76 +
  77 + @Override
29 78 public List<SysDeptDO> listDepts(SysDeptListReqVO reqVO) {
30 79 return deptMapper.selectList(reqVO);
31 80 }
32 81  
  82 + @Override
  83 + public List<SysDeptDO> listDeptsByParentIdFromCache(Long parentId, boolean recursive) {
  84 + List<SysDeptDO> result = new ArrayList<>();
  85 + // 递归,简单粗暴
  86 + this.listDeptsByParentIdFromCache(result, parentId,
  87 + recursive ? Integer.MAX_VALUE : 1, // 如果递归获取,则无限;否则,只递归 1 次
  88 + parentDeptCache);
  89 + return result;
  90 + }
  91 +
  92 + /**
  93 + * 递归获取所有的子部门,添加到 result 结果
  94 + *
  95 + * @param result 结果
  96 + * @param parentId 父编号
  97 + * @param recursiveCount 递归次数
  98 + * @param parentDeptMap 父部门 Map,使用缓存,避免变化
  99 + */
  100 + private void listDeptsByParentIdFromCache(List<SysDeptDO> result, Long parentId, int recursiveCount,
  101 + Multimap<Long, SysDeptDO> parentDeptMap) {
  102 + // 递归次数为 0,结束!
  103 + if (recursiveCount == 0) {
  104 + return;
  105 + }
  106 + // 获得子部门
  107 + Collection<SysDeptDO> depts = parentDeptMap.get(parentId);
  108 + if (CollUtil.isEmpty(depts)) {
  109 + return;
  110 + }
  111 + result.addAll(depts);
  112 + // 继续递归
  113 + depts.forEach(dept -> listDeptsByParentIdFromCache(result, dept.getId(),
  114 + recursiveCount - 1, parentDeptMap));
  115 + }
  116 +
33 117 }
... ...
src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysRoleServiceImpl.java
... ... @@ -37,12 +37,6 @@ import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.*;
37 37 @Slf4j
38 38 public class SysRoleServiceImpl implements SysRoleService {
39 39  
40   - @Resource
41   - private SysPermissionService permissionService;
42   -
43   - @Resource
44   - private SysRoleMapper roleMapper;
45   -
46 40 /**
47 41 * 角色缓存
48 42 * key:角色编号 {@link SysRoleDO#getId()}
... ... @@ -51,6 +45,12 @@ public class SysRoleServiceImpl implements SysRoleService {
51 45 */
52 46 private volatile Map<Long, SysRoleDO> roleCache;
53 47  
  48 + @Resource
  49 + private SysPermissionService permissionService;
  50 +
  51 + @Resource
  52 + private SysRoleMapper roleMapper;
  53 +
54 54 /**
55 55 * 初始化 {@link #roleCache} 缓存
56 56 */
... ...
src/main/java/cn/iocoder/dashboard/modules/system/service/user/SysUserService.java
1 1 package cn.iocoder.dashboard.modules.system.service.user;
2 2  
  3 +import cn.iocoder.dashboard.common.pojo.PageResult;
  4 +import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserPageReqVO;
3 5 import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.user.SysUserDO;
4 6  
5 7 /**
6 8 * 用户 Service 接口
7 9 *
8   - * @author ruoyi
  10 + * @author 芋道源码
9 11 */
10 12 public interface SysUserService {
11 13 // /**
... ... @@ -32,6 +34,14 @@ public interface SysUserService {
32 34 */
33 35 SysUserDO getUser(Long userId);
34 36  
  37 + /**
  38 + * 获得用户分页列表
  39 + *
  40 + * @param reqVO 分页条件
  41 + * @return 分页列表
  42 + */
  43 + PageResult<SysUserDO> pageUsers(SysUserPageReqVO reqVO);
  44 +
35 45 //
36 46 // /**
37 47 // * 根据用户ID查询用户所属角色组
... ...
src/main/java/cn/iocoder/dashboard/modules/system/service/user/SysUserServiceImpl.java
1 1 package cn.iocoder.dashboard.modules.system.service.user;
2 2  
  3 +import cn.iocoder.dashboard.common.pojo.PageResult;
  4 +import cn.iocoder.dashboard.modules.system.controller.user.vo.user.SysUserPageReqVO;
  5 +import cn.iocoder.dashboard.modules.system.convert.user.SysUserConvert;
3 6 import cn.iocoder.dashboard.modules.system.dal.mysql.dao.user.SysUserMapper;
  7 +import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.dept.SysDeptDO;
4 8 import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.user.SysUserDO;
  9 +import cn.iocoder.dashboard.modules.system.service.dept.SysDeptService;
  10 +import cn.iocoder.dashboard.util.collection.CollectionUtils;
5 11 import lombok.extern.slf4j.Slf4j;
6 12 import org.springframework.stereotype.Service;
7 13  
8 14 import javax.annotation.Resource;
  15 +import java.util.Collections;
  16 +import java.util.List;
9 17  
10 18  
11 19 /**
12   - * 用户 业务层处理
  20 + * 用户 Service 实现类
13 21 *
14   - * @author ruoyi
  22 + * @author 芋道源码
15 23 */
16 24 @Service
17 25 @Slf4j
... ... @@ -20,6 +28,9 @@ public class SysUserServiceImpl implements SysUserService {
20 28 @Resource
21 29 private SysUserMapper userMapper;
22 30  
  31 + @Resource
  32 + private SysDeptService deptService;
  33 +
23 34 // @Autowired
24 35 // private SysUserMapper userMapper;
25 36 //
... ... @@ -61,6 +72,19 @@ public class SysUserServiceImpl implements SysUserService {
61 72 return userMapper.selectById(userId);
62 73 }
63 74  
  75 + @Override
  76 + public PageResult<SysUserDO> pageUsers(SysUserPageReqVO reqVO) {
  77 + // 处理部门查询条件
  78 + List<Long> deptIds = Collections.emptyList();
  79 + if (reqVO.getDeptId() != null) {
  80 + deptIds = CollectionUtils.convertList(deptService.listDeptsByParentIdFromCache(reqVO.getDeptId(), true),
  81 + SysDeptDO::getId);
  82 + deptIds.add(reqVO.getDeptId());
  83 + }
  84 + // 执行查询
  85 + return SysUserConvert.INSTANCE.convertPage(userMapper.selectList(reqVO, deptIds));
  86 + }
  87 +
64 88 // /**
65 89 // * 通过用户ID查询用户
66 90 // *
... ...