本文最后更新于 2026年5月22日 下午
解绑手机号接口的实现
解绑手机号接口的需求
检测用户是否登录
验证当前用户是否存在
检测当前手机号是否被绑定
密码不能为空
校验当前密码
校验验证码
前面校验都通过后对手机号进行解绑
代码实现
在 AdminUserController 中
1 2 3 4 5 6 7 8
| @DeleteMapping("/mobile") @SystemLog(businessName = "解绑手机号接口") @ApiOperation(value = "解绑手机号接口", notes = "用户解绑手机号", response = String.class) @ApiImplicitParam(name = "password", value = "当前密码", dataType = "String", paramType = "query", required = true) public ResponseResult unbindPhone(@Valid @RequestBody UnbindPhoneOrEmailDto unbindPhoneOrEmailDto) { return adminUserService.unbindPhone(unbindPhoneOrEmailDto); }
|
创建 UnbindPhoneOrEmailDto
1 2 3 4 5 6 7 8 9 10 11 12 13
|
@Data @AllArgsConstructor @NoArgsConstructor @ApiModel(description = "解绑手机号或邮箱操作请求对象") public class UnbindPhoneOrEmailDto { @NotBlank(message = "密码不能为空") @ApiModelProperty(value = "密码", 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
| @Override public ResponseResult unbindPhone(UnbindPhoneOrEmailDto unbindPhoneOrEmailDto) { Long currentUserId = SecurityUtils.getUserId(); if (currentUserId == null) { return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN); }
SysUser currentUser = adminUserService.getById(currentUserId); if (currentUser == null) { return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "用户不存在"); }
if (!StringUtils.hasText(currentUser.getPhone())) { return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "当前用户未绑定手机号"); }
if (!StringUtils.hasText(unbindPhoneOrEmailDto.getPassword())) { return ResponseResult.errorResult(AppHttpCodeEnum.PASSWORD_NOT_NULL, "密码不能为空"); }
if (!passwordEncoder.matches(unbindPhoneOrEmailDto.getPassword(), currentUser.getPassword())) { return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "密码错误"); }
LambdaUpdateWrapper<SysUser> unbindPhoneWrapper = new LambdaUpdateWrapper<>(); unbindPhoneWrapper.eq(SysUser::getId, currentUser.getId()) .set(SysUser::getPhone, null);
boolean success = adminUserService.update(unbindPhoneWrapper); if (!success) { return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "解绑手机号失败"); }
return ResponseResult.okResult(); }
|
PS:该系列只做为作者学习开发项目做的笔记用
不一定符合读者来学习,仅供参考
预告
后续会记录博客的开发过程
每次学习会做一份笔记来进行发表
“一花一世界,一叶一菩提”
版权所有 © 2026 云梦泽
欢迎访问我的个人网站:https://hgt12.github.io/