SysAppUserController.java
8.0 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package com.skua.modules.app.user;
import cn.hutool.core.util.StrUtil;
import com.skua.core.api.ISysBaseAPI;
import com.skua.core.api.vo.LoginUser;
import com.skua.core.api.vo.Result;
import com.skua.core.aspect.annotation.AutoLog;
import com.skua.core.util.ConvertUtils;
import com.skua.core.util.PasswordUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.regex.Pattern;
/**
* app用户管理
* @author
*/
@Api(tags="APP用户管理")
@Slf4j
@RestController
@RequestMapping("/v1/app/sys/user")
public class SysAppUserController {
/**校验手机号格式*/
private static final Pattern PATTERN_PHONE = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
@Autowired
private ISysBaseAPI sysBaseAPI;
@Value("${skua.viewPath}")
private String viewPath;
@AutoLog(value = "修改用户名")
@ApiOperation(value="修改用户信息", notes="用户管理")
@PostMapping(value = "/changeUser")
public Result<LoginUser> changeUser(@RequestBody LoginUser loginUser){
Result<LoginUser> result = new Result<>();
try{
LoginUser oldUser = sysBaseAPI.getUserById(loginUser.getId());
String newRealName = loginUser.getRealname();
String newPassWord = loginUser.getPassword();
String newPhone = loginUser.getPhone();
String newEmail = loginUser.getEmail();
String cid = loginUser.getCid();
Date newBirthday = loginUser.getBirthday();
if (oldUser == null){
result.error500("未找到对应实体");
return result;
}
if (StrUtil.isNotBlank(cid)){
//修改手机cid
oldUser.setCid(cid);
sysBaseAPI.updateUser(oldUser);
result.success("cid修改成功");
}
if (StrUtil.isNotBlank(newEmail)){
//修改邮箱
oldUser.setEmail(newEmail);
sysBaseAPI.updateUser(oldUser);
result.success("邮箱修改成功");
}
if (StrUtil.isNotBlank((CharSequence) newBirthday)){
//修改生日
oldUser.setEmail(String.valueOf(newBirthday));
sysBaseAPI.updateUser(oldUser);
result.success("生日修改成功");
}
if (StrUtil.isNotBlank(newRealName)){
//修改用户名
oldUser.setRealname(newRealName);
sysBaseAPI.updateUser(oldUser);
result.success("用户名修改成功");
}
if (StrUtil.isNotBlank(newPhone)){
//修改手机号
boolean matches = PATTERN_PHONE.matcher(newPhone).matches();
if (!matches){
result.error500("手机号格式不正确");
}else {
sysBaseAPI.updateUser(oldUser.setPhone(newPhone));
result.success("手机号修改成功");
}
}
if (StrUtil.isNotBlank(newPassWord)){
//修改密码
String salt = ConvertUtils.randomGen(8);
//md5密码盐
oldUser.setSalt(salt);
String passwordEncode = PasswordUtil.encrypt(oldUser.getUsername(), newPassWord, salt);
oldUser.setPassword(passwordEncode);
sysBaseAPI.updateUser(oldUser);
result.success("密码修改完成!");
}
}catch (Exception e) {
log.error(e.getMessage(), e);
result.error500("操作失败");
}
return result;
}
@AutoLog(value = "修改用户头像")
@ApiOperation(value="修改用户头像", notes="用户管理")
@PostMapping(value = "/changeUserAvatar")
public Result<LoginUser> changeUserAvatar(@RequestBody LoginUser loginUser) {
Result<LoginUser> result = new Result<LoginUser>();
try {
LoginUser oldUser = sysBaseAPI.getUserById(loginUser.getId());
if(oldUser==null) {
result.error500("未找到对应实体");
}else {
sysBaseAPI.updateUser(oldUser);
oldUser.setAvatar(viewPath+oldUser.getAvatar());
result.setResult(oldUser);
result.success("修改成功!");
}
} catch (Exception e) {
log.error(e.getMessage(), e);
result.error500("操作失败");
}
return result;
}
@AutoLog(value = "根据id查询用户")
@ApiOperation(value="根据id查询用户", notes="用户管理")
@GetMapping(value = "/queryUserById")
public Result<LoginUser> queryUserById(@RequestParam String userId) {
Result<LoginUser> result = new Result<LoginUser>();
try {
LoginUser oldUser = sysBaseAPI.getUserById(userId);
if(oldUser==null) {
result.error500("未找到对应实体");
}else {
result.setResult(oldUser);
result.success("查询成功!");
}
} catch (Exception e) {
log.error(e.getMessage(), e);
result.error500("操作失败");
}
return result;
}
// @AutoLog(value = "修改用户密码")
// @ApiOperation(value="用户管理", notes="修改用户密码")
// @PostMapping(value = "/changePassword")
// public Result<LoginUser> changePassword(@RequestBody LoginUser sysUser) {
// Result<LoginUser> result = new Result<LoginUser>();
// String newPassword = sysUser.getPassword();
// LoginUser oldUser = sysBaseAPI.getUserById(sysUser.getId());
// if (oldUser == null) {
// result.error500("未找到对应实体");
// } else {
// String salt = ConvertUtils.randomGen(8);
//
// String passwordEncode = PasswordUtil.encrypt(oldUser.getUsername(), newPassword, salt);
// oldUser.setPassword(passwordEncode);
// sysBaseAPI.updateUser(oldUser);
// result.success("密码修改完成!");
// }
// return result;
// }
//
// @AutoLog(value = "修改手机号")
// @ApiOperation(value="用户管理", notes="修改手机号")
// @PostMapping (value = "/changePhone")
// public Result<LoginUser> changePhone(@RequestBody LoginUser sysUser){
// Result<LoginUser> result = new Result<>();
// LoginUser oldUser = sysBaseAPI.getUserById(sysUser.getId());
// //修改手机号
// boolean matches = PATTERN_PHONE.matcher(sysUser.getPhone()).matches();
// if (!matches){
// result.error500("手机号格式不正确");
// }else {
// sysBaseAPI.updateUser(oldUser.setPhone(sysUser.getPhone()));
// result.success("手机号修改成功");
// }
// return result;
// }
//
// @AutoLog(value = "修改用户名")
// @ApiOperation(value="用户管理", notes="修改用户信息")
// @PostMapping(value = "/changeAvatar")
// public Result<LoginUser> changeAvatar(@RequestBody LoginUser loginUser) {
// Result<LoginUser> result = new Result<LoginUser>();
// try {
// LoginUser oldUser = sysBaseAPI.getUserById(loginUser.getId());
// if(oldUser==null) {
// result.error500("未找到对应实体");
// }else {
// sysBaseAPI.updateUser(loginUser);
// loginUser.setAvatar(viewPath+loginUser.getAvatar());
// result.setResult(loginUser);
// result.success("修改成功!");
// }
// } catch (Exception e) {
// log.error(e.getMessage(), e);
// result.error500("操作失败");
// }
// return result;
// }
}