『博客开发日记-后台』之解绑邮箱接口的实现

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

解绑邮箱接口的实现


解绑邮箱接口的需求

需求和解绑手机号接口差不多 都一样的 就换个参

需要注意的是复制粘贴过来一些验证和参数一定要注意


代码实现

在 AdminUserController 中

1
2
3
4
5
6
7
8
@DeleteMapping("/email")
@SystemLog(businessName = "解绑邮箱接口")
@ApiOperation(value = "解绑邮箱接口", notes = "用户解绑邮箱", response = String.class)
@ApiImplicitParam(name = "password", value = "当前密码", dataType = "String", paramType = "query", required = true)
public ResponseResult unbindEmail(@Valid @RequestBody UnbindPhoneOrEmailDto unbindPhoneOrEmailDto)
{
return adminUserService.unbindEmail(unbindPhoneOrEmailDto);
}

创建 UnbindPhoneOrEmailDto

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* 解绑手机号或邮箱操作请求DTO
*/
@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 unbindEmail(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.getEmail())) {
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::getEmail, null);

boolean success = adminUserService.update(unbindPhoneWrapper);
if (!success) {
return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "解绑邮箱失败");
}

return ResponseResult.okResult();
}




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

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


预告

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

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

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


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


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