From bac38cf2472b2ecc8c4b045bc08f4e98f4a38178 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A2=A8=E5=A4=A7=E5=8F=94?= <494979559@qq.com> Date: Fri, 11 Oct 2024 17:25:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=89=8B=E6=9C=BA=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../user/domain/dto/UserChangeMobileDTO.java | 31 +++++++++++++++++++ .../ruoyi/ss/user/mapper/SmUserMapper.java | 5 +++ .../com/ruoyi/ss/user/mapper/SmUserMapper.xml | 6 ++++ .../ruoyi/ss/user/service/ISmUserService.java | 5 +++ .../user/service/impl/SmUserServiceImpl.java | 26 ++++++++++++++++ .../web/controller/app/AppUserController.java | 17 ++++++++-- 6 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 smart-switch-service/src/main/java/com/ruoyi/ss/user/domain/dto/UserChangeMobileDTO.java diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/user/domain/dto/UserChangeMobileDTO.java b/smart-switch-service/src/main/java/com/ruoyi/ss/user/domain/dto/UserChangeMobileDTO.java new file mode 100644 index 00000000..5bd7c15b --- /dev/null +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/user/domain/dto/UserChangeMobileDTO.java @@ -0,0 +1,31 @@ +package com.ruoyi.ss.user.domain.dto; + +import com.ruoyi.common.utils.RegexpUtils; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import org.springframework.beans.factory.annotation.Autowired; + +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.Pattern; + +/** + * 更换用户手机号DTO + * @author wjh + * 2024/10/11 + */ +@Data +public class UserChangeMobileDTO { + + @ApiModelProperty("旧手机号验证码") + private String oldCode; + + @ApiModelProperty("新手机号") + @NotBlank(message = "新手机号不能为空") + @Pattern(regexp = RegexpUtils.MOBILE_PHONE_REGEXP, message = "新手机号格式错误") + private String newMobile; + + @ApiModelProperty("新手机号验证码") + @NotBlank(message = "新手机号验证码不允许为空") + private String newCode; + +} diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/user/mapper/SmUserMapper.java b/smart-switch-service/src/main/java/com/ruoyi/ss/user/mapper/SmUserMapper.java index a5239dcb..017e0340 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/ss/user/mapper/SmUserMapper.java +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/user/mapper/SmUserMapper.java @@ -117,4 +117,9 @@ public interface SmUserMapper * @return */ int addRiskCount(@Param("userId") Long userId,@Param("count") int count); + + /** + * 根据用户手机号查询数量 + */ + int selectCountByPhone(@Param("phonenumber") String phone); } diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/user/mapper/SmUserMapper.xml b/smart-switch-service/src/main/java/com/ruoyi/ss/user/mapper/SmUserMapper.xml index 5c5ce367..86097046 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/ss/user/mapper/SmUserMapper.xml +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/user/mapper/SmUserMapper.xml @@ -348,4 +348,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" + + diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/user/service/ISmUserService.java b/smart-switch-service/src/main/java/com/ruoyi/ss/user/service/ISmUserService.java index 233773b5..787690a1 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/ss/user/service/ISmUserService.java +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/user/service/ISmUserService.java @@ -249,4 +249,9 @@ public interface ISmUserService * 获取用户修改手机号的旧手机短信验证码 */ boolean getChangeMobileCodeOld(Long userId); + + /** + * 更新手机号 + */ + int updateMobile(Long userId, String mobile); } diff --git a/smart-switch-service/src/main/java/com/ruoyi/ss/user/service/impl/SmUserServiceImpl.java b/smart-switch-service/src/main/java/com/ruoyi/ss/user/service/impl/SmUserServiceImpl.java index 7cad48b6..2d57a600 100644 --- a/smart-switch-service/src/main/java/com/ruoyi/ss/user/service/impl/SmUserServiceImpl.java +++ b/smart-switch-service/src/main/java/com/ruoyi/ss/user/service/impl/SmUserServiceImpl.java @@ -477,6 +477,32 @@ public class SmUserServiceImpl implements ISmUserService return verificationCodeService.sendMobileCode(user.getPhonenumber(), CodeBusinessType.CHANGE_MOBILE_OLD); } + @Override + public int updateMobile(Long userId, String mobile) { + if (userId == null || StringUtils.isBlank(mobile)) { + return 0; + } + + Integer result = transactionTemplate.execute(status -> { + SmUser data = new SmUser(); + data.setUserId(userId); + data.setPhonenumber(mobile); + int update = smUserMapper.updateSmUser(data); + ServiceUtil.assertion(update != 1, "更新手机号失败"); + + int phoneCount = this.selectCountByPhone(mobile); + ServiceUtil.assertion(phoneCount > 1, "手机号重复,无法修改"); + + return update; + }); + + return result == null ? 0 : result; + } + + private int selectCountByPhone(String phone) { + return smUserMapper.selectCountByPhone(phone); + } + /** * 逻辑删除前校验 * @param userIds diff --git a/smart-switch-web/src/main/java/com/ruoyi/web/controller/app/AppUserController.java b/smart-switch-web/src/main/java/com/ruoyi/web/controller/app/AppUserController.java index e15a523e..ab52d0a7 100644 --- a/smart-switch-web/src/main/java/com/ruoyi/web/controller/app/AppUserController.java +++ b/smart-switch-web/src/main/java/com/ruoyi/web/controller/app/AppUserController.java @@ -12,6 +12,7 @@ import com.ruoyi.common.utils.StringUtils; import com.ruoyi.framework.web.service.SysPasswordService; import com.ruoyi.framework.web.service.TokenService; import com.ruoyi.ss.user.domain.SmUserVo; +import com.ruoyi.ss.user.domain.dto.UserChangeMobileDTO; import com.ruoyi.ss.user.domain.dto.UserRealNameDTO; import com.ruoyi.ss.user.domain.dto.UserUpdatePasswordDTO; import com.ruoyi.ss.user.service.ISmUserService; @@ -125,8 +126,20 @@ public class AppUserController extends BaseController { @ApiOperation("修改手机号") @PutMapping("/changeMobile") - public AjaxResult changeMobile() { - return success(); + public AjaxResult changeMobile(@RequestBody UserChangeMobileDTO dto) { + SmUserVo user = userService.selectSmUserByUserId(getUserId()); + ServiceUtil.assertion(user == null, "用户不存在"); + // 若有旧手机,则验证旧手机验证码 + if (user.getPhonenumber() != null) { + boolean oldCheck = verificationCodeService.checkCode(user.getPhonenumber(), dto.getOldCode(), CodeBusinessType.CHANGE_MOBILE_OLD, false); + ServiceUtil.assertion(!oldCheck, "旧手机验证码错误或已过期"); + } + + // 校验验证码 + boolean newCheck = verificationCodeService.checkCode(dto.getNewMobile(), dto.getNewCode(), CodeBusinessType.CHANGE_MOBILE_NEW, false); + ServiceUtil.assertion(!newCheck, "新手机验证码错误或已过期"); + + return success(userService.updateMobile(getUserId(), dto.getNewMobile())); } }