『博客开发日记-后台』之驳回用户头像接口的实现

本文最后更新于 2026年5月21日 晚上

驳回用户头像接口的实现


驳回用户头像接口的需求

检查用户是否存在

没有头像时无需驳回

驳回时清空用户头像

在oss中将正式文件移动到 deleted/

如果数据库中存在对应文件记录,则删除文件元数据

驳回后给用户发送邮件


代码实现

在 AdminUserController 中

1
2
3
4
5
6
7
8
9
@DeleteMapping("/{id}/dismiss/avatar")
@PreAuthorize("@ps.hasPermission('sys:user:dismiss-avatar')")
@SystemLog(businessName = "驳回用户头像(实际是逻辑删除该头像的源文件)")
@ApiOperation(value = "驳回用户头像", notes = "驳回用户头像(实际是逻辑删除该头像的源文件)", response = String.class)
@ApiImplicitParam(name = "id", value = "用户id", dataType = "Long", paramType = "path", required = true)
public ResponseResult dismissUserAvatar(@PathVariable Long id)
{
return adminUserService.dismissUserAvatar(id);
}

EmailServiceImpl

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
//发送用户头像驳回通知邮件
@Async("asyncExecutor")
public void sendUserAvatarDismissedNotification(String toEmail, String nickname, String username)
{
try {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");

helper.setFrom(from);
helper.setTo(toEmail);
helper.setSubject("【云梦泽的个人博客】您的头像已被驳回");

String htmlContent = buildUserAvatarDismissedNotificationHtml(nickname, username);
helper.setText(htmlContent, true);

mailSender.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}

//用户头像驳回通知邮件
private String buildUserAvatarDismissedNotificationHtml(String nickname, String username)
{
return "<!DOCTYPE html>" +
"<html>" +
"<head>" +
" <meta charset=\"UTF-8\">" +
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">" +
"</head>" +
"<body style=\"margin: 0; padding: 0; background: linear-gradient(135deg, #a5dff9 0%, #008c9e 100%); font-family: 'Segoe UI', Arial, sans-serif;\">" +
" <table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" style=\"padding: 40px 20px;\">" +
" <tr>" +
" <td align=\"center\">" +
" <table width=\"600\" cellpadding=\"0\" cellspacing=\"0\" style=\"background-color: #f5fffa; border-radius: 12px; box-shadow: 0 12px 15px 0 rgba(0,0,0,0.24), 0 17px 50px 0 rgba(0,0,0,0.19); overflow: hidden;\">" +
" <tr>" +
" <td style=\"background: rgb(125, 182, 191); padding: 40px 30px; text-align: center;\">" +
" <h1 style=\"color: #2c3e50; margin: 0; font-size: 28px; font-weight: 600; letter-spacing: 1px;\">云梦泽的个人博客</h1>" +
" <p style=\"color: #546e7a; margin: 10px 0 0 0; font-size: 14px;\">头像驳回通知</p>" +
" </td>" +
" </tr>" +
" <tr>" +
" <td style=\"padding: 50px 40px;\">" +
" <h2 style=\"color: #3c4858; margin: 0 0 20px 0; font-size: 22px; font-weight: 600;\">您好," + nickname + "!</h2>" +
" <p style=\"color: #718096; line-height: 1.8; margin: 0 0 30px 0; font-size: 15px;\">" +
" ( ̄ε(# ̄)☆╰╮o( ̄▽ ̄///)" +
" </p>" +
" <p style=\"color: #718096; line-height: 1.8; margin: 0 0 30px 0; font-size: 15px;\">" +
" 由于您的头像太过于敏感,当前头像已被驳回并移除。" +
" </p>" +
" <div style=\"background-color: #fff8e1; border-left: 4px solid #ff9800; border-radius: 8px; padding: 20px; margin: 25px 0;\">" +
" <p style=\"color: #3c4858; line-height: 1.8; margin: 0; font-size: 14px;\">" +
" 您可以重新上传符合要求的头像。<br>" +
" 如有疑问,请联系管理员。" +
" </p>" +
" </div>" +
" </td>" +
" </tr>" +
" <tr>" +
" <td style=\"background: linear-gradient(135deg, #f8f9fa 0%, #eaecef 100%); padding: 30px; text-align: center; border-top: 1px solid #eaecef;\">" +
" <p style=\"color: #718096; margin: 0 0 10px 0; font-size: 13px;\">" +
" 此邮件由系统自动发送,请勿直接回复" +
" </p>" +
" <p style=\"color: #a7a9ad; margin: 0; font-size: 12px;\">" +
" © " + LocalDate.now().getYear() + " 云梦泽的个人博客 · All rights reserved" +
" </p>" +
" </td>" +
" </tr>" +
" </table>" +
" </td>" +
" </tr>" +
" </table>" +
"</body>" +
"</html>";
}

在 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
 //驳回用户头像
@Override
@Transactional(rollbackFor = Exception.class)
public ResponseResult dismissUserAvatar(Long id)
{
//检查用户是否存在
SysUser user = adminUserService.getById(id);
if (user == null) {
return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "该用户不存在!");
}

//没有头像时无需驳回
if (!StringUtils.hasText(user.getAvatar())) {
return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "该用户没有头像可驳回!");
}

//先保存原头像地址
String avatarUrl = user.getAvatar();

//清空用户头像
LambdaUpdateWrapper<SysUser> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(SysUser::getId, id)
.set(SysUser::getAvatar, null);
boolean update = adminUserService.update(updateWrapper);
if (!update) {
return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "驳回头像失败!");
}

//发送头像驳回邮件
if (StringUtils.hasText(user.getEmail())) {
emailService.sendUserAvatarDismissedNotification(user.getEmail(), user.getNickname(), user.getUsername());
}

//将正式文件移动到 deleted/
try {
ossFileService.moveFileToDeleted(avatarUrl);
} catch (Exception e) {
log.warn("驳回用户头像时移动文件到删除区失败, userId={}, avatarUrl={}", id, avatarUrl, e);
}

//如果数据库中存在对应文件记录,则删除文件元数据
SysFile file = sysFileService.getOne(
new LambdaQueryWrapper<SysFile>()
.eq(SysFile::getUrl, avatarUrl)
.last("limit 1")
);
if (file != null) {
sysFileService.removeById(file.getId());
}

return ResponseResult.okResult();
}




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

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


预告

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

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

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


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


『博客开发日记-后台』之驳回用户头像接口的实现
http://example.com/2026/05/21/『博客开发日记-后台』之驳回用户头像接口的实现/
作者
云梦泽
发布于
2026年5月21日
更新于
2026年5月21日
许可协议