修改手机号

This commit is contained in:
墨大叔 2024-10-11 17:25:58 +08:00
parent 7550ad1ac7
commit bac38cf247
6 changed files with 88 additions and 2 deletions

View File

@ -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;
}

View File

@ -117,4 +117,9 @@ public interface SmUserMapper
* @return
*/
int addRiskCount(@Param("userId") Long userId,@Param("count") int count);
/**
* 根据用户手机号查询数量
*/
int selectCountByPhone(@Param("phonenumber") String phone);
}

View File

@ -348,4 +348,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<include refid="searchCondition"/>
</where>
</select>
<select id="selectCountByPhone" resultType="java.lang.Integer">
select count(su.user_id)
from sm_user su
where su.phonenumber = #{phonenumber} and su.del_flag != '2'
</select>
</mapper>

View File

@ -249,4 +249,9 @@ public interface ISmUserService
* 获取用户修改手机号的旧手机短信验证码
*/
boolean getChangeMobileCodeOld(Long userId);
/**
* 更新手机号
*/
int updateMobile(Long userId, String mobile);
}

View File

@ -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

View File

@ -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()));
}
}