『博客开发日记-后台』之绑定或更换手机号接口的实现

本文最后更新于 2026年5月22日 下午

绑定或更换手机号接口的实现


绑定或更换手机号接口的需求

检测用户是否登录

校验手机号格式

验证码不能为空

密码不能为空

验证当前用户是否存在

校验验证码

手机号不能被其他用户占用

更新手机号

删除验证码缓存


代码实现

在 AdminUserController 中

1
2
3
4
5
6
7
@PutMapping("/mobile")
@SystemLog(businessName = "绑定或更换手机号接口")
@ApiOperation(value = "绑定或更换手机号接口", notes = "个人中心绑定或更换手机号", response = String.class)
public ResponseResult updateOrBindPhone(@Valid @RequestBody UpdateOrBindPhoneDto updateOrBindPhoneDto)
{
return adminUserService.updateOrBindPhone(updateOrBindPhoneDto);
}

创建 UpdateOrBindPhoneDto

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* 绑定或更换手机号请求DTO
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(description = "绑定或更换手机号请求对象")
public class UpdateOrBindPhoneDto
{
@NotBlank(message = "手机号不能为空")
@Pattern(regexp = "^$|^1[3-9]\\d{9}$", message = "手机号格式不正确")
@ApiModelProperty(value = "手机号", required = true, example = "123456788433")
private String mobile;

@NotBlank(message = "验证码不能为空")
@Pattern(regexp = "^\\d{6}$", message = "验证码必须为6位数字")
@ApiModelProperty(value = "验证码", required = true, example = "123555")
private String code;

@NotBlank(message = "密码不能为空")
@ApiModelProperty(value = "当前密码", required = true, example = "password123456")
private String password;
}

在 AdminUserServiceImpl 中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
 //绑定或更换手机号
@Override
@Transactional(rollbackFor = Exception.class)
public ResponseResult updateOrBindPhone(UpdateOrBindPhoneDto updateOrBindPhoneDto)
{
//检测用户是否登录
Long currentUserId = SecurityUtils.getUserId();
if (currentUserId == null) {
return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
}

//校验手机号格式
String mobile = updateOrBindPhoneDto.getMobile() == null ? null : updateOrBindPhoneDto.getMobile().trim();
if (!StringUtils.hasText(mobile)) {
return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "手机号不能为空");
}
if (!mobile.matches("^1[3-9]\\d{9}$")) {
return ResponseResult.errorResult(AppHttpCodeEnum.PHONE_FORMAT_ERROR);
}

//验证码不能为空
String code = updateOrBindPhoneDto.getCode() == null ? null : updateOrBindPhoneDto.getCode().trim();
if (!StringUtils.hasText(code)) {
return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "验证码不能为空");
}

//密码不能为空
String password = updateOrBindPhoneDto.getPassword() == null ? null : updateOrBindPhoneDto.getPassword().trim();
if (!StringUtils.hasText(password)) {
return ResponseResult.errorResult(AppHttpCodeEnum.PASSWORD_NOT_NULL, "密码不能为空");
}

//验证当前用户是否存在
SysUser currentUser = adminUserService.getById(currentUserId);
if (currentUser == null) {
return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "用户不存在");
}

//校验验证码
String redisKey = String.format("admin:code:%s", mobile);
String cacheCode = redisCache.getCacheObject(redisKey);
if (!StringUtils.hasText(cacheCode)) {
return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "验证码已过期,请重新获取");
}
if (!Objects.equals(cacheCode, code)) {
return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "验证码错误");
}

//校验当前密码
if (!passwordEncoder.matches(password, currentUser.getPassword())) {
return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "密码错误");
}

//手机号不能被其他用户占用
LambdaQueryWrapper<SysUser> phoneWrapper = new LambdaQueryWrapper<>();
phoneWrapper.eq(SysUser::getPhone, mobile)
.ne(SysUser::getId, currentUserId);
SysUser existUser = adminUserService.getOne(phoneWrapper);
if (existUser != null) {
return ResponseResult.errorResult(AppHttpCodeEnum.PHONENUMBER_EXIST, "该手机号已被其他用户绑定");
}

//更新手机号
LambdaUpdateWrapper<SysUser> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(SysUser::getId, currentUserId)
.set(SysUser::getPhone, mobile);
boolean update = adminUserService.update(updateWrapper);
if (!update) {
return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "绑定手机号失败");
}

//删除验证码缓存
redisCache.deleteObject(redisKey);
redisCache.deleteObject(String.format("admin:send:time:%s", mobile));

return ResponseResult.okResult();
}





PS:该系列只做为作者学习开发项目做的笔记用

不一定符合读者来学习,仅供参考


预告

后续会记录博客的开发过程

每次学习会做一份笔记来进行发表

“一花一世界,一叶一菩提”


版权所有 © 2026 云梦泽
欢迎访问我的个人网站:https://hgt12.github.io/


『博客开发日记-后台』之绑定或更换手机号接口的实现
http://example.com/2026/05/22/『博客开发日记-后台』之绑定或更换手机号接口的实现/
作者
云梦泽
发布于
2026年5月22日
更新于
2026年5月22日
许可协议