Skip to content

Commit

Permalink
feat: 用户头像改为Base64存储
Browse files Browse the repository at this point in the history
  • Loading branch information
jskils authored and Charles7c committed May 30, 2024
1 parent 448f9a0 commit 969216d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import top.continew.starter.data.mybatis.plus.service.IService;
import top.continew.starter.extension.crud.service.BaseService;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

Expand Down Expand Up @@ -70,7 +71,7 @@ public interface UserService extends BaseService<UserResp, UserDetailResp, UserQ
* @param id ID
* @return 新头像路径
*/
String uploadAvatar(MultipartFile avatar, Long id);
String updateAvatar(MultipartFile avatar, Long id) throws IOException;

/**
* 修改基础信息
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
Expand All @@ -29,8 +30,6 @@
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -60,11 +59,9 @@
import top.continew.starter.extension.crud.service.CommonUserService;
import top.continew.starter.extension.crud.service.impl.BaseServiceImpl;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;

import static top.continew.admin.system.enums.PasswordPolicyEnum.*;
Expand All @@ -81,8 +78,6 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserRes

private final OnlineUserService onlineUserService;
private final UserRoleService userRoleService;
private final FileService fileService;
private final FileStorageService fileStorageService;
private final PasswordEncoder passwordEncoder;
private final OptionService optionService;
private final UserPasswordHistoryService userPasswordHistoryService;
Expand Down Expand Up @@ -175,22 +170,15 @@ public void updateRole(UserRoleUpdateReq updateReq, Long id) {
}

@Override
public String uploadAvatar(MultipartFile avatarFile, Long id) {
public String updateAvatar(MultipartFile avatarFile, Long id) throws IOException {
String avatarImageType = FileNameUtil.extName(avatarFile.getOriginalFilename());
CheckUtils.throwIf(!StrUtil.equalsAnyIgnoreCase(avatarImageType, avatarSupportSuffix), "头像仅支持 {} 格式的图片", String
.join(StringConstants.CHINESE_COMMA, avatarSupportSuffix));
// 上传新头像
UserDO user = super.getById(id);
FileInfo fileInfo = fileService.upload(avatarFile);
// 更新用户头像
String newAvatar = fileInfo.getUrl();
baseMapper.lambdaUpdate().set(UserDO::getAvatar, newAvatar).eq(UserDO::getId, id).update();
// 删除原头像
String oldAvatar = user.getAvatar();
if (StrUtil.isNotBlank(oldAvatar)) {
fileStorageService.delete(oldAvatar);
}
return newAvatar;
String base64 = ImgUtil.toBase64DataUri(ImgUtil.scale(ImgUtil.toImage(avatarFile
.getBytes()), 100, 100, null), avatarImageType);
baseMapper.lambdaUpdate().set(UserDO::getAvatar, base64).eq(UserDO::getId, id).update();
return base64;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import top.continew.starter.core.util.validate.ValidationUtils;
import top.continew.starter.web.model.R;

import java.io.IOException;
import java.util.List;

/**
Expand All @@ -69,12 +70,12 @@ public class UserCenterController {
private final UserSocialService userSocialService;
private final AuthRequestFactory authRequestFactory;

@Operation(summary = "上传头像", description = "用户上传个人头像")
@Operation(summary = "修改头像", description = "用户修改个人头像")
@PostMapping("/avatar")
public R<AvatarResp> uploadAvatar(@NotNull(message = "头像不能为空") MultipartFile avatarFile) {
public R<AvatarResp> updateAvatar(@NotNull(message = "头像不能为空") MultipartFile avatarFile) throws IOException {
ValidationUtils.throwIf(avatarFile::isEmpty, "头像不能为空");
String newAvatar = userService.uploadAvatar(avatarFile, LoginHelper.getUserId());
return R.ok("上传成功", AvatarResp.builder().avatar(newAvatar).build());
String newAvatar = userService.updateAvatar(avatarFile, LoginHelper.getUserId());
return R.ok("修改成功", AvatarResp.builder().avatar(newAvatar).build());
}

@Operation(summary = "修改基础信息", description = "修改用户基础信息")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ CREATE TABLE IF NOT EXISTS `sys_user` (
`gender` tinyint(1) UNSIGNED NOT NULL DEFAULT 0 COMMENT '性别(0:未知;1:男;2:女)',
`email` varchar(255) DEFAULT NULL COMMENT '邮箱',
`phone` varchar(255) DEFAULT NULL COMMENT '手机号码',
`avatar` varchar(255) DEFAULT NULL COMMENT '头像地址',
`avatar` longtext DEFAULT NULL COMMENT '头像',
`description` varchar(200) DEFAULT NULL COMMENT '描述',
`status` tinyint(1) UNSIGNED NOT NULL DEFAULT 1 COMMENT '状态(1:启用;2:禁用)',
`is_system` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否为系统内置数据',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ CREATE TABLE IF NOT EXISTS "sys_user" (
"gender" int2 NOT NULL DEFAULT 0,
"email" varchar(255) DEFAULT NULL,
"phone" varchar(255) DEFAULT NULL,
"avatar" varchar(255) DEFAULT NULL,
"avatar" text DEFAULT NULL,
"description" varchar(200) DEFAULT NULL,
"status" int2 NOT NULL DEFAULT 1,
"is_system" bool NOT NULL DEFAULT false,
Expand All @@ -146,7 +146,7 @@ COMMENT ON COLUMN "sys_user"."password" IS '密码';
COMMENT ON COLUMN "sys_user"."gender" IS '性别(0:未知;1:男;2:女)';
COMMENT ON COLUMN "sys_user"."email" IS '邮箱';
COMMENT ON COLUMN "sys_user"."phone" IS '手机号码';
COMMENT ON COLUMN "sys_user"."avatar" IS '头像地址';
COMMENT ON COLUMN "sys_user"."avatar" IS '头像';
COMMENT ON COLUMN "sys_user"."description" IS '描述';
COMMENT ON COLUMN "sys_user"."status" IS '状态(1:启用;2:禁用)';
COMMENT ON COLUMN "sys_user"."is_system" IS '是否为系统内置数据';
Expand Down

0 comments on commit 969216d

Please sign in to comment.