UserController.java
7.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package com.uccc.admin.controller;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.uccc.admin.domain.Permission;
import com.uccc.admin.exception.ApiException;
import com.uccc.admin.service.PermissionService;
import com.uccc.admin.service.RoleService;
import com.uccc.admin.service.UserService;
import com.uccc.pretty.common.Result;
import com.uccc.pretty.common.SystemLog;
import com.uccc.pretty.common.User;
import com.uccc.pretty.constants.ErrorCode;
import com.uccc.pretty.utils.VerifyUtil;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Date;
import java.util.List;
import static com.uccc.pretty.constants.ActionEnum.*;
import static com.uccc.pretty.constants.ResultEnum.RESULT_ERR;
import static com.uccc.pretty.constants.ResultEnum.RESULT_OK;
import static com.uccc.pretty.constants.UserStatusEnum.*;
/**
* Created by bert on 2021-09-11 11:50
*/
@RestController
@RequestMapping("/admin/")
public class UserController {
private Logger logger = LoggerFactory.getLogger(UserController.class);
@Autowired
private UserService userService;
@Autowired
private PermissionService permissionService;
@Autowired
private RoleService roleService;
/**
*登陆
* @param user
* @param ip
* @return
* @throws NullPointerException
*/
@RequestMapping(value = "login",method = RequestMethod.POST)
public Result doLogin(@RequestBody User user, @RequestParam String ip) throws NullPointerException{
Result result = new Result();
if (user == null) {
throw new ApiException(ErrorCode.USER_LOGIN_WRONG);
}else {
logger.info("user:{}", user.toString());
if (user.getAccount().equals("")) {
throw new ApiException(ErrorCode.USER_ACCOUNT_NOT_FOUND);
}else if (user.getPassword().equals("")){
throw new ApiException(ErrorCode.USER_PASSWORD_NOT_FOUND);
}
byte[] pwdBytes = user.getPassword().getBytes();
//base64 encode
String pwdEncoded = Base64.getEncoder().encodeToString(pwdBytes);
user.setPassword(pwdEncoded);
user = userService.getUserByCondition(user);
if (user == null) throw new ApiException(ErrorCode.USER_PASSWORD_WRONG);
if (user.getStatus() == USER_STATUS_OFF.getCode() || user.getStatus() == USER_STATUS_FROZEN.getCode()) {
throw new ApiException(ErrorCode.LOGIN_ACCOUNT_CLOSE);
}
//get user permission
List<Permission> permissions = permissionService.getPermissionByUserId(user.getId());
if (permissions.size() == 0) {
throw new ApiException(ErrorCode.USER_PERMISSION_NOT_FOUND);
}else {
user.setPermission(permissionService.formatPermissionList(permissions));
}
User updateUser = new User();
updateUser.setId(user.getId());
updateUser.setLastLoginIp(ip);
updateUser.setLastLoginTime(new Date());
userService.updateUser(updateUser);
SystemLog systemLog = new SystemLog(USER_LOGIN.getCode(),USER_LOGIN.getMessage(),USER_LOGIN.getMessage(),new Date(),"",ip);
userService.sendSystemLogToRabbitMq(systemLog.toString());
}
result.setCode(RESULT_OK.getCode());
result.setMessage(RESULT_OK.getMessage());
result.setData(user);
return result;
}
/**
* update user
* @param user
* @param ip
* @return
* @throws NullPointerException
*/
@RequestMapping(value = "user",method = RequestMethod.PUT)//改
public Result modifyUser (@RequestBody User user, @RequestParam String ip) throws NullPointerException{
Result result = new Result();
if (user.getId() == null) throw new ApiException(ErrorCode.USER_ID_NOT_FOUND);
SystemLog systemLog = new SystemLog(USER_INFO_MODIFY.getCode(),USER_INFO_MODIFY.getMessage(),USER_INFO_MODIFY.getMessage(),new Date(),"modify user id is"+user.getId(),ip);
userService.sendSystemLogToRabbitMq(systemLog.toString());
int updateResult = userService.updateUser(user);
if (updateResult > 0) {
result.setCode(RESULT_OK.getCode());
result.setMessage(RESULT_OK.getMessage());
}else {
throw new ApiException(ErrorCode.USER_INFO_UPDATE_FAILED);
}
return result;
}
/**
* search user
* @param user
* @return
* @throws NullPointerException
*/
@RequestMapping(value = "user",method = RequestMethod.GET)//查
public Result findUser (@RequestBody User user) throws NullPointerException{
Result result = new Result();
// if (user.getId() == null) throw new ApiException(ErrorCode.USER_ID_NOT_FOUND);
List<User> users = userService.getUserListByCondition(user);
result.setCode(RESULT_OK.getCode());
result.setMessage(RESULT_OK.getMessage());
result.setData(users);
return result;
}
/**
* 添加用户
* @param user
* @return
* @throws NullPointerException
*/
@RequestMapping(value = "user",method = RequestMethod.POST)
public Result createUser (@RequestBody User user, @RequestParam String ip) throws NullPointerException{
Result result = new Result();
if (user.getAccount().equals("")) {
throw new ApiException(ErrorCode.USER_ACCOUNT_NOT_FOUND);
}else if (user.getPassword().equals("")){
throw new ApiException(ErrorCode.USER_PASSWORD_NOT_FOUND);
}else if (user.getName().equals("")) {
throw new ApiException(ErrorCode.USER_NAME_NOT_FOUND);
}else if (user.getPhone().equals("")) {
throw new ApiException(ErrorCode.USER_PHONE_NOT_FOUND);
}else if (user.getAddress().equals("")) {
throw new ApiException(ErrorCode.USER_ADDRESS_NOT_FOUND);
}else if (user.getQq().equals("")) {
throw new ApiException(ErrorCode.USER_ADDRESS_NOT_FOUND);
}else if (user.getRoleIds().length == 0) {
throw new ApiException(ErrorCode.USER_ROLE_NOT_FOUND);
}
//check account repeat
int accountCount = userService.checkAccountRepeat(user.getAccount());
logger.info("accountCount:{}", accountCount);
if (accountCount > 0) throw new ApiException(ErrorCode.USER_ACCOUNT_REPEAT);
byte[] pwdBytes = user.getPassword().getBytes();
//base64 encode
String pwdEncoded = Base64.getEncoder().encodeToString(pwdBytes);
user.setPassword(pwdEncoded);
Long createdUserId = userService.createUser(user);
int[] roles = user.getRoleIds();
boolean createRoleResult = true;
int i = 0;
for (i++; i<roles.length;) {
int roleResultTemp = roleService.createUserRoleByUserId(createdUserId, roles[i]);
if (roleResultTemp == 0) createRoleResult = false;
}
if (createdUserId != null && createRoleResult) {
result.setCode(RESULT_OK.getCode());
result.setMessage(RESULT_OK.getMessage());
}else {
result.setCode(RESULT_ERR.getCode());
result.setMessage(RESULT_ERR.getMessage());
}
return result;
}
}