1: 添加用户注册和用户修改密码功能接口

pull/1/head
xueqingkun 11 months ago
parent 99e542f872
commit 588bbbed12

@ -6,15 +6,13 @@ import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.supervision.exception.BusinessException;
import com.supervision.model.User;
import com.supervision.pojo.vo.LoginReqVO;
import com.supervision.pojo.vo.LoginResVO;
import com.supervision.pojo.vo.UserInfoReqVo;
import com.supervision.pojo.vo.UserInfoResVo;
import com.supervision.pojo.vo.*;
import com.supervision.service.UserManageService;
import com.supervision.service.UserService;
import com.supervision.util.TokenUtil;
import com.supervision.util.UserUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
@ -31,8 +29,6 @@ public class UserController {
private final UserService userService;
private final RedisTemplate<String, String> redisTemplate;
private final UserManageService userManageService;
@ -58,14 +54,27 @@ public class UserController {
return loginResVO;
}
@Operation(summary = "用户注册")
@PostMapping("register")
public boolean register(@RequestBody UserInfoReqVo userInfo) {
return userManageService.register(userInfo);
}
@Operation(summary = "用户账号校验")
@GetMapping("checkAccount")
public UserAccountCheckResVo checkAccount(@Parameter(name = "account", description = "账户")String account) {
@Operation(summary = "修改用户信息")
@PutMapping("updateUserInfo")
public boolean updateUserInfo(@RequestBody UserInfoReqVo userInfo) {
return userManageService.checkAccount(account);
}
@Operation(summary = "修改密码")
@PostMapping("changePassWord")
public boolean changePassWord(@RequestBody UserInfoReqVo userInfo) {
return userManageService.updateUserInfo(userInfo);
return userManageService.changePassWord(userInfo);
}
@Operation(summary = "查看账号信息")
@GetMapping("getUserAccountInfo")
public UserInfoResVo getUserAccountInfo() {

@ -0,0 +1,12 @@
package com.supervision.pojo.vo;
import lombok.Data;
@Data
public class UserAccountCheckResVo {
// 状态码 0账号可用 1账号不可用
private String code;
private String message;
}

@ -6,15 +6,22 @@ import lombok.Data;
@Data
public class UserInfoReqVo {
/**
*
*/
@Schema(description = "用户账户")
private String account;
@Schema(description = "用户id")
private String id;
/**
*
*/
@Schema(description = "用户名称")
private String name;
@Schema(description = "旧密码")
private String oldPassword;
@Schema(description = "新密码")
private String newPassword;
/**
*
*/
@Schema(description = "用户密码")
private String password;
}

@ -1,12 +1,16 @@
package com.supervision.service;
import com.supervision.model.User;
import com.supervision.pojo.vo.UserAccountCheckResVo;
import com.supervision.pojo.vo.UserInfoReqVo;
import com.supervision.pojo.vo.UserInfoResVo;
public interface UserManageService {
boolean updateUserInfo(UserInfoReqVo userInfo);
UserInfoResVo getUserAccountInfo(String userId);
boolean changePassWord(UserInfoReqVo userInfo);
boolean register(UserInfoReqVo userInfo);
UserAccountCheckResVo checkAccount(String account);
}

@ -4,6 +4,7 @@ import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.supervision.model.User;
import com.supervision.pojo.vo.UserAccountCheckResVo;
import com.supervision.pojo.vo.UserInfoReqVo;
import com.supervision.pojo.vo.UserInfoResVo;
import com.supervision.service.UserManageService;
@ -11,6 +12,10 @@ import com.supervision.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Slf4j
@Service
@ -18,36 +23,12 @@ import org.springframework.stereotype.Service;
public class UserManageServiceImpl implements UserManageService {
private final UserService userService;
@Override
public boolean updateUserInfo(UserInfoReqVo userInfo) {
Assert.notEmpty(userInfo.getId(),"用户id不能为空");
if (StrUtil.isNotEmpty(userInfo.getNewPassword())){
//todo密码复杂度规则匹配
Assert.notEmpty(userInfo.getOldPassword(),"旧密码不允许为空");
}
User user = userService.getById(userInfo.getId());
Assert.notNull(user,"用户信息不存在");
boolean change = false;
if (StrUtil.isNotEmpty(userInfo.getName())){
user.setName(userInfo.getName());
change = true;
}
String newPassword = userInfo.getNewPassword();
String oldPassword = userInfo.getOldPassword();
if (StrUtil.isAllNotEmpty(newPassword,oldPassword)){
Assert.isTrue(user.getPassword().equals(oldPassword),"密码不正确");
user.setPassword(newPassword);
change = true;
}
// 密码6-18位 不包含中文特殊字符和空格
private static final String passwordRegex = "^(?![\\u4e00-\\u9fa5\\s])[\\x21-\\x7e]{6,18}$";
if (!change){
return false;
}
return userService.updateById(user);
}
// 账号校验正则 6-18位 英文或者数字
private static final String accountRegex = "^[a-zA-Z0-9]{6,18}$";
@Override
public UserInfoResVo getUserAccountInfo(String userId) {
@ -58,4 +39,97 @@ public class UserManageServiceImpl implements UserManageService {
return BeanUtil.toBean(user,UserInfoResVo.class);
}
@Override
@Transactional(rollbackFor = Exception.class)
public boolean changePassWord(UserInfoReqVo userInfo) {
Assert.notEmpty(userInfo.getAccount(),"账号不能为空");
Assert.notEmpty(userInfo.getPassword(),"密码不能为空");
String match = passwordRegexMatch(userInfo.getPassword());
Assert.isFalse(StrUtil.isNotEmpty(match),match);
Long count = userService.lambdaQuery().eq(User::getAccount, userInfo.getAccount()).count();
Assert.isFalse(count==0,"密码设置失败");
userService.lambdaUpdate().eq(User::getAccount, userInfo.getAccount()).set(User::getPassword,userInfo.getPassword()).update();
return true;
}
@Override
@Transactional(rollbackFor = Exception.class)
public boolean register(UserInfoReqVo userInfo) {
Assert.notEmpty(userInfo.getAccount(),"账号不能为空");
Assert.notEmpty(userInfo.getName(),"用户名不能为空");
// 用户规则校验
String accountRegexMatch = accountRegexMatch(userInfo.getAccount());
Assert.isFalse(StrUtil.isNotEmpty(accountRegexMatch),accountRegexMatch);
// 校验账号是否已存在
Long count = userService.lambdaQuery().eq(User::getAccount, userInfo.getAccount()).count();
Assert.isTrue(count==0,"账号已存在");
// 密码强度校验
String passwordRegexMatch = passwordRegexMatch(userInfo.getPassword());
Assert.isFalse(StrUtil.isNotEmpty(passwordRegexMatch),passwordRegexMatch);
User user = new User();
user.setAccount(userInfo.getAccount());
user.setName(userInfo.getName());
user.setPassword(userInfo.getPassword());
user.setRoleCode("1");
user.setStatus(0);
userService.save(user);
return false;
}
@Override
public UserAccountCheckResVo checkAccount(String account) {
UserAccountCheckResVo checkResult = new UserAccountCheckResVo();
if (StrUtil.isEmpty(account)){
checkResult.setCode("1");
checkResult.setMessage("账号不能为空");
}
//复杂度校验
String accountRegexMatch = accountRegexMatch(account);
if (StrUtil.isNotEmpty(accountRegexMatch)){
checkResult.setCode("1");
checkResult.setMessage(accountRegexMatch);
return checkResult;
}
Long count = userService.lambdaQuery().eq(User::getAccount, account).count();
if (count > 0){
checkResult.setCode("1");
checkResult.setMessage("账号已存在");
}
checkResult.setCode("0");
return checkResult;
}
private String passwordRegexMatch(String password){
if (StrUtil.isEmpty(password)){
return "密码不能为空";
}
Pattern pattern = Pattern.compile(passwordRegex);
Matcher matcher = pattern.matcher(password);
if (!matcher.matches()){
return "密码强度不符合要求";
}
return null;
}
private String accountRegexMatch(String account){
if (StrUtil.isEmpty(account)){
return "账号不能为空";
}
Pattern pattern = Pattern.compile(accountRegex);
Matcher matcher = pattern.matcher(account);
if (!matcher.matches()){
return "账号格式不符合要求";
}
return null;
}
}

Loading…
Cancel
Save