修改用户

This commit is contained in:
石皮幼鸟 2024-04-11 19:26:45 +08:00
parent 38a35aefed
commit 347801ba4e
4 changed files with 54 additions and 9 deletions

View File

@ -19,6 +19,8 @@ public enum HttpStatus {
TOKEN_EXCEPTION(401, "token验证错误"), TOKEN_EXCEPTION(401, "token验证错误"),
USERNAME_CONFLICT(409, "用户名已存在"),
ERROR(500, "系统内部错误"); ERROR(500, "系统内部错误");
private final Integer code; private final Integer code;

View File

@ -41,8 +41,8 @@ public class AuthController {
@ApiOperation(value = "验证token不会返回密码") @ApiOperation(value = "验证token不会返回密码")
@GetMapping("/verify") @GetMapping("/verify")
public R<User> verify(@RequestHeader("token") String token) { public R<UserVo> verify(@RequestHeader("token") String token) {
return R.ok(authService.verify(token)); return R.ok(authService.verify(token).toVo());
} }
@ApiOperation(value = "用户注册") @ApiOperation(value = "用户注册")
@ -62,8 +62,8 @@ public class AuthController {
} }
@ApiOperation(value = "用户信息修改") @ApiOperation(value = "用户信息修改")
@PostMapping("/update") @PutMapping("/update")
public R update(@RequestBody User user) { public R<UserVo> update(@RequestBody User user, @RequestHeader("token") String token) {
return null; return authService.update(user, token);
} }
} }

View File

@ -60,5 +60,12 @@ public interface AuthService {
*/ */
User getUserByName(String username); User getUserByName(String username);
/**
* 用户信息修改
*
* @param user 用户信息
* @param token token
* @return 新的用户信息
*/
R<UserVo> update(User user, String token);
} }

View File

@ -1,6 +1,7 @@
package com.example.catchTheLetters.service.impl; package com.example.catchTheLetters.service.impl;
import com.example.catchTheLetters.constant.CommonConstant; import com.example.catchTheLetters.constant.CommonConstant;
import com.example.catchTheLetters.constant.HttpStatus;
import com.example.catchTheLetters.model.dto.LoginDto; import com.example.catchTheLetters.model.dto.LoginDto;
import com.example.catchTheLetters.model.dto.RegisterDto; import com.example.catchTheLetters.model.dto.RegisterDto;
import com.example.catchTheLetters.entity.User; import com.example.catchTheLetters.entity.User;
@ -56,9 +57,9 @@ public class AuthServiceImpl implements AuthService {
return R.fail("密码错误"); return R.fail("密码错误");
} }
Map<String, String> map = new HashMap<>(); Map<String, String> map = new HashMap<>();
map.put("id", user.getId().toString()); map.put("id", user.getId());
var token = JwtUtil.getToken(map, CommonConstant.TOKEN_EXPIRE_TIME); var token = JwtUtil.getToken(map, CommonConstant.TOKEN_EXPIRE_TIME);
redisService.hSet(token, user.getId().toString(), CommonConstant.REDIS_EXPIRE_TIME); redisService.hSet(token, user.getId(), CommonConstant.REDIS_EXPIRE_TIME);
return R.ok(token); return R.ok(token);
} }
@ -87,11 +88,13 @@ public class AuthServiceImpl implements AuthService {
// 用户名重复 // 用户名重复
if (regedUser != null) { if (regedUser != null) {
R.fail("用户名重复"); R.fail(HttpStatus.USERNAME_CONFLICT.getCode(), HttpStatus.USERNAME_CONFLICT.getDesc());
} }
var user = new User(); var user = new User();
user.setUsername(registerDto.getUsername()); user.setUsername(registerDto.getUsername());
user.setPassword(registerDto.getPassword()); user.setPassword(registerDto.getPassword());
user.setCreateTime(System.currentTimeMillis());
user.setUpdateTime(System.currentTimeMillis());
var insert = mongoTemplate.insert(user); var insert = mongoTemplate.insert(user);
if (insert.getId() != null) { if (insert.getId() != null) {
return R.ok(user.toVo()); return R.ok(user.toVo());
@ -109,4 +112,37 @@ public class AuthServiceImpl implements AuthService {
public User getUserByName(String username) { public User getUserByName(String username) {
return mongoTemplate.findOne(new Query(Criteria.where("username").is(username).and(CommonConstant.STATUS).ne(0)), User.class); return mongoTemplate.findOne(new Query(Criteria.where("username").is(username).and(CommonConstant.STATUS).ne(0)), User.class);
} }
@Override
public R<UserVo> update(User user, String token) {
// 先获取当前账户比对id是否一致然后再插入
var verify = verify(token);
if (verify == null || !verify.getId().equals(user.getId())) {
return R.fail(HttpStatus.TOKEN_EXCEPTION.getCode(), HttpStatus.TOKEN_EXCEPTION.getDesc());
}
// 如果用户名修改了检查是否有重复有重复则返回null否则修改
if (!verify.getUsername().equals(user.getUsername())) {
var regedUser = getUserByName(user.getUsername());
if (regedUser != null) {
return R.fail(HttpStatus.USERNAME_CONFLICT.getCode(), HttpStatus.USERNAME_CONFLICT.getDesc());
}
verify.setUsername(user.getUsername());
}
// 如果密码为空意味着没修改密码否则修改密码
if (user.getPassword() != null && !user.getPassword().isEmpty()) {
verify.setPassword(user.getPassword());
}
verify.setAvatar(user.getAvatar());
verify.setEmail(user.getEmail());
verify.setPhone(user.getPhone());
verify.setSex(user.getSex());
verify.setIntroduction(user.getIntroduction());
verify.setUpdateTime(System.currentTimeMillis());
return R.ok(mongoTemplate.save(verify).toVo());
}
} }