feat(profile): 新增C端个人资料与设备相册功能
- 新增个人资料模块,支持获取、修改昵称和个性签名 - 实现头像上传与校验,支持JPEG/PNG格式及尺寸限制 - 添加app_user表signature字段的可重复执行SQL脚本 - 新增设备相册控制器,支持读取、收藏和删除相册图片 - 编写AppProfileService和DeviceAlbumController单元测试
This commit is contained in:
1 parent
8d61c63c8b
commit
4dd7d98ce9
9 files changed
+382
No files matched your search
@@ -0,0 +1,39 @@
|
||||
package com.youlai.boot.app.profile.controller;
|
||||
|
||||
import com.youlai.boot.app.profile.model.AppProfileForm;
|
||||
import com.youlai.boot.app.profile.model.AppProfileVO;
|
||||
import com.youlai.boot.app.profile.service.AppProfileService;
|
||||
import com.youlai.boot.common.result.Result;
|
||||
import com.youlai.boot.framework.security.util.SecurityUtils;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/client/account/profile")
|
||||
@PreAuthorize("isAuthenticated()")
|
||||
@RequiredArgsConstructor
|
||||
public class AppProfileController {
|
||||
private final AppProfileService service;
|
||||
|
||||
@GetMapping
|
||||
@Operation(summary = "获取当前C端用户个人资料")
|
||||
public Result<AppProfileVO> get() {
|
||||
return Result.success(service.get(SecurityUtils.getUserId()));
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
@Operation(summary = "修改当前C端用户昵称和个性签名")
|
||||
public Result<AppProfileVO> update(@Valid @RequestBody AppProfileForm form) {
|
||||
return Result.success(service.update(SecurityUtils.getUserId(), form));
|
||||
}
|
||||
|
||||
@PostMapping("/avatar")
|
||||
@Operation(summary = "上传并保存当前C端用户头像")
|
||||
public Result<AppProfileVO> avatar(@RequestPart("file") MultipartFile file) {
|
||||
return Result.success(service.uploadAvatar(SecurityUtils.getUserId(), file));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.youlai.boot.app.profile.mapper;
|
||||
|
||||
import com.youlai.boot.app.profile.model.AppProfileVO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
import org.apache.ibatis.annotations.Update;
|
||||
|
||||
@Mapper
|
||||
public interface AppProfileMapper {
|
||||
@Select("SELECT id, username, nickname, avatar, signature FROM app_user "
|
||||
+ "WHERE id = #{id} AND is_deleted = 0 AND status = 1")
|
||||
AppProfileVO findCurrent(@Param("id") Long id);
|
||||
|
||||
@Update("UPDATE app_user SET nickname = #{nickname}, signature = #{signature}, "
|
||||
+ "update_time = CURRENT_TIMESTAMP "
|
||||
+ "WHERE id = #{id} AND is_deleted = 0 AND status = 1")
|
||||
int updateProfile(@Param("id") Long id, @Param("nickname") String nickname,
|
||||
@Param("signature") String signature);
|
||||
|
||||
@Update("UPDATE app_user SET avatar = #{avatar}, update_time = CURRENT_TIMESTAMP "
|
||||
+ "WHERE id = #{id} AND is_deleted = 0 AND status = 1")
|
||||
int updateAvatar(@Param("id") Long id, @Param("avatar") String avatar);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.youlai.boot.app.profile.model;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.Size;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class AppProfileForm {
|
||||
@NotBlank(message = "昵称不能为空")
|
||||
@Size(max = 64, message = "昵称不能超过64个字符")
|
||||
private String nickname;
|
||||
|
||||
@Size(max = 200, message = "个性签名不能超过200个字符")
|
||||
private String signature;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.youlai.boot.app.profile.model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class AppProfileVO {
|
||||
private Long id;
|
||||
private String username;
|
||||
private String nickname;
|
||||
private String avatar;
|
||||
private String signature;
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package com.youlai.boot.app.profile.service;
|
||||
|
||||
import com.youlai.boot.app.profile.mapper.AppProfileMapper;
|
||||
import com.youlai.boot.app.profile.model.AppProfileForm;
|
||||
import com.youlai.boot.app.profile.model.AppProfileVO;
|
||||
import com.youlai.boot.common.config.StorageDir;
|
||||
import com.youlai.boot.common.exception.BusinessException;
|
||||
import com.youlai.boot.support.file.model.FileInfo;
|
||||
import com.youlai.boot.support.file.service.FileService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.imageio.ImageReader;
|
||||
import javax.imageio.stream.ImageInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.util.Iterator;
|
||||
import java.util.Locale;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class AppProfileService {
|
||||
private final AppProfileMapper mapper;
|
||||
private final FileService fileService;
|
||||
|
||||
public AppProfileVO get(Long userId) {
|
||||
AppProfileVO profile = mapper.findCurrent(userId);
|
||||
if (profile == null) throw new BusinessException("用户不存在或已停用");
|
||||
return profile;
|
||||
}
|
||||
|
||||
public AppProfileVO update(Long userId, AppProfileForm form) {
|
||||
get(userId);
|
||||
String nickname = form.getNickname().strip();
|
||||
if (nickname.isEmpty() || nickname.length() > 64) {
|
||||
throw new BusinessException("昵称须为1至64个字符");
|
||||
}
|
||||
String signature = form.getSignature() == null ? "" : form.getSignature().strip();
|
||||
mapper.updateProfile(userId, nickname, signature);
|
||||
return get(userId);
|
||||
}
|
||||
|
||||
public AppProfileVO uploadAvatar(Long userId, MultipartFile file) {
|
||||
get(userId);
|
||||
validateAvatar(file);
|
||||
FileInfo uploaded = fileService.uploadFile(file, StorageDir.AVATAR);
|
||||
if (uploaded == null || !StringUtils.hasText(uploaded.getUrl())) {
|
||||
throw new BusinessException("头像上传失败");
|
||||
}
|
||||
try {
|
||||
if (uploaded.getUrl().length() > 255) {
|
||||
throw new BusinessException("头像地址过长");
|
||||
}
|
||||
if (mapper.updateAvatar(userId, uploaded.getUrl()) != 1) {
|
||||
throw new BusinessException("头像保存失败,请重试");
|
||||
}
|
||||
} catch (RuntimeException error) {
|
||||
try {
|
||||
fileService.deleteFile(uploaded.getUrl());
|
||||
} catch (Exception cleanupError) {
|
||||
log.warn("清理未保存的头像失败, userId={}", userId);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
return get(userId);
|
||||
}
|
||||
|
||||
private void validateAvatar(MultipartFile file) {
|
||||
if (file.isEmpty() || file.getSize() > 5 * 1024 * 1024) {
|
||||
throw new BusinessException("请选择不超过5MB的头像图片");
|
||||
}
|
||||
String name = file.getOriginalFilename();
|
||||
if (name == null) throw new BusinessException("头像文件名不能为空");
|
||||
String lowerName = name.toLowerCase(Locale.ROOT);
|
||||
boolean png = lowerName.endsWith(".png");
|
||||
boolean jpeg = lowerName.endsWith(".jpg") || lowerName.endsWith(".jpeg");
|
||||
if ((!png && !jpeg) || !(png ? "image/png" : "image/jpeg").equals(file.getContentType())) {
|
||||
throw new BusinessException("头像仅支持JPEG或PNG图片");
|
||||
}
|
||||
try (InputStream stream = file.getInputStream();
|
||||
ImageInputStream input = ImageIO.createImageInputStream(stream)) {
|
||||
if (input == null) throw new BusinessException("头像图片无效");
|
||||
Iterator<ImageReader> readers = ImageIO.getImageReaders(input);
|
||||
if (!readers.hasNext()) throw new BusinessException("头像图片无效");
|
||||
ImageReader reader = readers.next();
|
||||
try {
|
||||
reader.setInput(input);
|
||||
String format = reader.getFormatName();
|
||||
if (!(png ? "png" : "JPEG").equalsIgnoreCase(format)
|
||||
|| reader.getWidth(0) > 4096 || reader.getHeight(0) > 4096) {
|
||||
throw new BusinessException("头像格式或尺寸不支持");
|
||||
}
|
||||
var image = reader.read(0);
|
||||
if (image == null) throw new BusinessException("头像图片无效");
|
||||
image.flush();
|
||||
} finally {
|
||||
reader.dispose();
|
||||
}
|
||||
} catch (BusinessException error) {
|
||||
throw error;
|
||||
} catch (Exception error) {
|
||||
throw new BusinessException("头像图片无效");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.youlai.boot.device.controller;
|
||||
|
||||
import com.youlai.boot.common.result.Result;
|
||||
import com.youlai.boot.device.model.vo.DevicePictureVO;
|
||||
import com.youlai.boot.device.service.DevicePictureService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.validation.constraints.Max;
|
||||
import jakarta.validation.constraints.Min;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/** DeviceSignatureVerifier validates the device headers for every /api/v1/sn request. */
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/sn/pictures")
|
||||
@RequiredArgsConstructor
|
||||
@Validated
|
||||
@Tag(name = "设备相册")
|
||||
public class DeviceAlbumController {
|
||||
private final DevicePictureService pictures;
|
||||
|
||||
@GetMapping
|
||||
@Operation(summary = "设备读取家属端上传的相册图片及收藏状态")
|
||||
public Result<List<DevicePictureVO>> list(@RequestHeader("X-Device-SN") @NotBlank String sn) {
|
||||
return Result.success(pictures.listPictures(sn));
|
||||
}
|
||||
|
||||
@PostMapping("/favorite")
|
||||
@Operation(summary = "设备设置相册图片收藏状态")
|
||||
public Result<Void> favorite(
|
||||
@RequestHeader("X-Device-SN") @NotBlank String sn,
|
||||
@RequestParam("url") @NotBlank String url,
|
||||
@RequestParam("favorite") @NotNull @Min(0) @Max(1) Integer favorite) {
|
||||
if (!pictures.updateFavorite(sn, url, favorite == 1)) {
|
||||
return Result.failed("图片不存在或已删除");
|
||||
}
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
@DeleteMapping
|
||||
@Operation(summary = "设备删除家属端上传的相册图片记录")
|
||||
public Result<Void> delete(
|
||||
@RequestHeader("X-Device-SN") @NotBlank String sn,
|
||||
@RequestParam("url") @NotBlank String url) {
|
||||
pictures.deletePicture(sn, url);
|
||||
return Result.success();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user