mirror of
http://192.168.1.205:9980/cf_devdept2/cf_imes_server.git
synced 2026-08-13 05:12:07 +08:00
1、个人中心修改手机短信验证码相关逻辑完善;2、个人中心接口入参检查完善;
This commit is contained in:
+1
@@ -16,6 +16,7 @@ public class ErrorCodeConstants {
|
||||
public static final ErrorCode AUTH_THIRD_LOGIN_NOT_BIND = new ErrorCode(1_002_000_005, "未绑定账号,需要进行绑定");
|
||||
public static final ErrorCode AUTH_TOKEN_EXPIRED = new ErrorCode(1_002_000_006, "Token 已经过期");
|
||||
public static final ErrorCode AUTH_MOBILE_NOT_EXISTS = new ErrorCode(1_002_000_007, "手机号不存在");
|
||||
public static final ErrorCode AUTH_MOBILE_NO_CHANGE = new ErrorCode(1_002_000_008, "手机号未发生改变,无需修改");
|
||||
|
||||
// ========== 菜单模块 1-002-001-000 ==========
|
||||
public static final ErrorCode MENU_NAME_DUPLICATE = new ErrorCode(1_002_001_000, "已经存在该名字的菜单");
|
||||
|
||||
+2
-2
@@ -12,12 +12,12 @@ public class UserProfileUpdatePasswordReqVO {
|
||||
|
||||
@Schema(description = "旧密码", requiredMode = Schema.RequiredMode.REQUIRED, example = "123456")
|
||||
@NotEmpty(message = "旧密码不能为空")
|
||||
@Length(min = 4, max = 16, message = "密码长度为 4-16 位")
|
||||
@Length(min = 8, max = 16, message = "密码长度为 4-16 位")
|
||||
private String oldPassword;
|
||||
|
||||
@Schema(description = "新密码", requiredMode = Schema.RequiredMode.REQUIRED, example = "654321")
|
||||
@NotEmpty(message = "新密码不能为空")
|
||||
@Length(min = 4, max = 16, message = "密码长度为 4-16 位")
|
||||
@Length(min = 8, max = 16, message = "密码长度为 4-16 位")
|
||||
private String newPassword;
|
||||
|
||||
}
|
||||
|
||||
+2
-7
@@ -1,9 +1,8 @@
|
||||
package com.cf.imes.module.system.controller.admin.user.vo.profile;
|
||||
|
||||
import com.cf.imes.framework.common.validation.Mobile;
|
||||
import com.cf.imes.module.system.validation.system.user.Sex;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
|
||||
import javax.validation.constraints.Email;
|
||||
import javax.validation.constraints.Size;
|
||||
@@ -22,12 +21,8 @@ public class UserProfileUpdateReqVO {
|
||||
@Size(max = 50, message = "邮箱长度不能超过 50 个字符")
|
||||
private String email;
|
||||
|
||||
@Schema(description = "手机号码", example = "15601691300")
|
||||
@Length(min = 11, max = 11, message = "手机号长度必须 11 位")
|
||||
@Mobile
|
||||
private String mobile;
|
||||
|
||||
@Schema(description = "用户性别,参见 SexEnum 枚举类", example = "1")
|
||||
@Sex
|
||||
private Integer sex;
|
||||
|
||||
private Long organId;
|
||||
|
||||
+31
-1
@@ -4,6 +4,10 @@ import cn.hutool.core.date.LocalDateTimeUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.cf.imes.framework.common.exception.ServiceException;
|
||||
import com.cf.imes.framework.common.exception.enums.GlobalErrorCodeConstants;
|
||||
import com.cf.imes.framework.security.core.LoginUser;
|
||||
import com.cf.imes.framework.security.core.util.SecurityFrameworkUtils;
|
||||
import com.cf.imes.module.system.api.sms.dto.code.SmsCodeSendReqDTO;
|
||||
import com.cf.imes.module.system.api.sms.dto.code.SmsCodeUseReqDTO;
|
||||
import com.cf.imes.module.system.api.sms.dto.code.SmsCodeValidateReqDTO;
|
||||
@@ -54,8 +58,16 @@ public class SmsCodeServiceImpl implements SmsCodeService {
|
||||
|
||||
@Override
|
||||
public void sendSmsCode(SmsCodeSendReqDTO reqDTO) {
|
||||
// 非修改手机号的场景时校验手机号是否存在
|
||||
AdminUserDO user = userService.getUserByUsernameAndOrganId(reqDTO.getMobile(), null);
|
||||
LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
|
||||
if (ObjectUtil.isNull(loginUser)) {
|
||||
throw new ServiceException(GlobalErrorCodeConstants.UNAUTHORIZED);
|
||||
}
|
||||
// 更新手机号场景检查
|
||||
if (ObjectUtil.equal(SmsSceneEnum.USER_UPDATE_MOBILE.getScene(), reqDTO.getScene())) {
|
||||
userUpdateMobileValid(user, loginUser);
|
||||
}
|
||||
// 非修改手机号的场景时校验手机号是否存在
|
||||
if (ObjectUtil.isNull(user) && ObjectUtil.notEqual(SmsSceneEnum.USER_UPDATE_MOBILE.getScene(), reqDTO.getScene())) {
|
||||
throw ServiceExceptionUtil.exception(ErrorCodeConstants.AUTH_MOBILE_NOT_EXISTS);
|
||||
}
|
||||
@@ -70,6 +82,24 @@ public class SmsCodeServiceImpl implements SmsCodeService {
|
||||
sceneEnum.getTemplateCode(), map);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新手机号的场景检查
|
||||
*
|
||||
* @param user
|
||||
* @param loginUser
|
||||
*/
|
||||
private void userUpdateMobileValid(AdminUserDO user, LoginUser loginUser) {
|
||||
if (ObjectUtil.isNotNull(user)) {
|
||||
if (ObjectUtil.equal(user.getId(), loginUser.getId())) {
|
||||
// 手机号没有改变无需验证码
|
||||
throw ServiceExceptionUtil.exception(ErrorCodeConstants.AUTH_MOBILE_NO_CHANGE);
|
||||
} else {
|
||||
// 修改手机号的场景校验手机是否已存在
|
||||
throw ServiceExceptionUtil.exception(ErrorCodeConstants.USER_MOBILE_EXISTS);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成验证码
|
||||
*
|
||||
|
||||
+8
-7
@@ -220,7 +220,6 @@ public class AdminUserServiceImpl implements AdminUserService {
|
||||
// 校验正确性
|
||||
validateUserExists(id);
|
||||
validateEmailUnique(id, reqVO.getEmail(), organId);
|
||||
validateMobileUnique(id, reqVO.getMobile());
|
||||
// 执行更新
|
||||
userMapper.updateById(BeanUtils.toBean(reqVO, AdminUserDO.class).setId(id));
|
||||
}
|
||||
@@ -580,13 +579,10 @@ public class AdminUserServiceImpl implements AdminUserService {
|
||||
|
||||
@VisibleForTesting
|
||||
@OrganIgnore
|
||||
void validateMobileUnique(Long id, String mobile) {
|
||||
if (StringUtils.isBlank(mobile)) {
|
||||
return;
|
||||
}
|
||||
AdminUserDO validateMobileUnique(Long id, String mobile) {
|
||||
AdminUserDO user = userMapper.selectOne(AdminUserDO::getUsername, mobile);
|
||||
if (user == null) {
|
||||
return;
|
||||
return user;
|
||||
}
|
||||
// 如果 id 为空,说明不用比较是否为相同 id 的用户
|
||||
if (id == null) {
|
||||
@@ -595,6 +591,7 @@ public class AdminUserServiceImpl implements AdminUserService {
|
||||
if (!user.getId().equals(id)) {
|
||||
throw ServiceExceptionUtil.exception(ErrorCodeConstants.USER_MOBILE_EXISTS);
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -694,7 +691,11 @@ public class AdminUserServiceImpl implements AdminUserService {
|
||||
public void updateUserSecurityPhone(Long id, UserMobileUpdateReqVO reqVO) {
|
||||
String mobile = reqVO.getMobile();
|
||||
// 校验手机号唯一
|
||||
validateMobileUnique(id, mobile);
|
||||
AdminUserDO adminUserDO = validateMobileUnique(id, mobile);
|
||||
// 手机号没有改变无需请求
|
||||
if (ObjectUtil.isNotNull(adminUserDO) && ObjectUtil.equal(id, adminUserDO.getId())) {
|
||||
throw ServiceExceptionUtil.exception(ErrorCodeConstants.AUTH_MOBILE_NO_CHANGE);
|
||||
}
|
||||
|
||||
// 新手机号入库 -> setUsername
|
||||
userMapper.update(new LambdaUpdateWrapper<AdminUserDO>()
|
||||
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.cf.imes.module.system.validation.system.user;
|
||||
|
||||
import javax.validation.Constraint;
|
||||
import javax.validation.Payload;
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 性别校验注解
|
||||
*
|
||||
* @author Gqr
|
||||
* @since 2024/10/12 9:48
|
||||
*/
|
||||
@Target({
|
||||
ElementType.FIELD,
|
||||
})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Constraint(
|
||||
validatedBy = {SexValidator.class}
|
||||
)
|
||||
public @interface Sex {
|
||||
String message() default "性别不合法,1:男、2:女";
|
||||
|
||||
Class<?>[] groups() default {};
|
||||
|
||||
Class<? extends Payload>[] payload() default {};
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.cf.imes.module.system.validation.system.user;
|
||||
|
||||
import com.cf.imes.module.system.enums.common.SexEnum;
|
||||
|
||||
import javax.validation.ConstraintValidator;
|
||||
import javax.validation.ConstraintValidatorContext;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 性别校验器
|
||||
*
|
||||
* @author Gqr
|
||||
* @since 2024/10/12 9:50
|
||||
*/
|
||||
public class SexValidator implements ConstraintValidator<Sex, Integer> {
|
||||
|
||||
@Override
|
||||
public boolean isValid(Integer value, ConstraintValidatorContext context) {
|
||||
for (SexEnum sexEnum : SexEnum.values()) {
|
||||
if (Objects.equals(sexEnum.getSex(), value)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
-1
@@ -201,7 +201,6 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest {
|
||||
// 准备参数
|
||||
Long userId = dbUser.getId();
|
||||
UserProfileUpdateReqVO reqVO = randomPojo(UserProfileUpdateReqVO.class, o -> {
|
||||
o.setMobile(randomString());
|
||||
o.setSex(RandomUtil.randomEle(SexEnum.values()).getSex());
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user