商品管理的图片编辑和用户管理的图片编辑功能优化,配置文件的优化

This commit is contained in:
xiaowang
2026-02-26 10:21:02 +08:00
parent 0d5619441c
commit 501aeb8ea3
11 changed files with 325 additions and 33 deletions
@@ -64,6 +64,7 @@ public class AuthInterceptor implements HandlerInterceptor {
path.contains("/api/user/refresh-token") ||
path.contains("/api/shop/list/simple") ||
path.contains("/api/admin/auth/login") ||
path.contains("/api/admin/object-storage-file/upload") ||
path.contains("/health");
}
}
@@ -21,6 +21,7 @@ public class WebMvcConfig implements WebMvcConfigurer {
"/api/user/refresh-token",
"/api/shop/list/simple",
"/api/admin/auth/login",
"/api/admin/object-storage-file/upload",
"/error",
"/health/**"
);
@@ -2,12 +2,15 @@ package com.xiaowang.yuminailcore.controller;
import com.xiaowang.yuminailcore.common.Result;
import com.xiaowang.yuminailcore.dto.ObjectStorageFileDTO;
import com.xiaowang.yuminailcore.vo.ObjectStorageFileVO;
import com.xiaowang.yuminailcore.service.MinioService;
import com.xiaowang.yuminailcore.service.ObjectStorageFileService;
import com.xiaowang.yuminailcore.vo.ObjectStorageFileVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.UUID;
/**
* 对象存储文件信息表 Controller
@@ -17,11 +20,74 @@ import java.util.List;
* @date 2026-02-13
*/
@RestController
@RequestMapping("/api/object-storage-file")
@RequestMapping("/api/admin/object-storage-file")
@RequiredArgsConstructor
public class ObjectStorageFileController {
private final ObjectStorageFileService objectStorageFileService;
private final MinioService minioService;
/**
* 上传文件
*
* @param file 文件
* @return 文件ID和URL
*/
@PostMapping("/upload")
public Result<ObjectStorageFileVO> uploadFile(@RequestParam("file") MultipartFile file) {
try {
String originalFilename = file.getOriginalFilename();
String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
String objectName = UUID.randomUUID().toString() + fileExtension;
String fileUrl = minioService.uploadFile(file, objectName);
ObjectStorageFileDTO dto = new ObjectStorageFileDTO();
dto.setFileName(originalFilename);
dto.setFileUrl(fileUrl);
dto.setStatus(1);
Long fileId = objectStorageFileService.createObjectStorageFile(dto);
ObjectStorageFileVO fileVO = objectStorageFileService.getObjectStorageFileById(fileId);
return Result.success(fileVO);
} catch (Exception e) {
e.printStackTrace();
return Result.error("文件上传失败: " + e.getMessage());
}
}
/**
* 获取带签名的图片链接
*
* @param fileId 文件ID
* @param expiryTimeInMinutes 有效期(分钟),默认60分钟
* @return 带签名的临时访问链接
*/
@GetMapping("/get-signed-url/{fileId}")
public Result<String> getSignedUrl(@PathVariable Long fileId, @RequestParam(defaultValue = "60") Integer expiryTimeInMinutes) {
try {
ObjectStorageFileVO fileVO = objectStorageFileService.getObjectStorageFileById(fileId);
if (fileVO == null) {
return Result.error("文件不存在");
}
// 从fileUrl中提取objectName
String fileUrl = fileVO.getFileUrl();
// 提取最后一个/后面的部分作为objectName
int lastSlashIndex = fileUrl.lastIndexOf("/");
if (lastSlashIndex == -1) {
return Result.error("文件URL格式错误");
}
String objectName = fileUrl.substring(lastSlashIndex + 1);
String signedUrl = minioService.getPresignedUrl(objectName, expiryTimeInMinutes);
return Result.success(signedUrl);
} catch (Exception e) {
e.printStackTrace();
return Result.error("获取签名链接失败: " + e.getMessage());
}
}
/**
* 创建文件
@@ -94,4 +160,4 @@ public class ObjectStorageFileController {
List<ObjectStorageFileVO> objectStorageFileVOs = objectStorageFileService.listObjectStorageFilesByStatus(status);
return Result.success(objectStorageFileVOs);
}
}
}
@@ -9,6 +9,7 @@ import com.xiaowang.yuminailcore.dto.RegisterRequestDTO;
import com.xiaowang.yuminailcore.dto.UserDTO;
import com.xiaowang.yuminailcore.vo.LoginResponseVO;
import com.xiaowang.yuminailcore.vo.UserVO;
import com.xiaowang.yuminailcore.service.MinioService;
import com.xiaowang.yuminailcore.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
@@ -31,6 +32,7 @@ import java.util.UUID;
public class UserController {
private final UserService userService;
private final MinioService minioService;
/**
* 创建用户
@@ -234,10 +236,20 @@ public class UserController {
*/
@PostMapping("/upload-avatar")
public Result<String> uploadAvatar(HttpServletRequest request, @RequestParam("file") MultipartFile file) {
Long userId = (Long) request.getAttribute("userId");
String fileName = UUID.randomUUID().toString() + "_" + file.getOriginalFilename();
String avatarUrl = "https://api.dicebear.com/7.x/avataaars/svg?seed=" + fileName;
return Result.success(avatarUrl);
try {
Long userId = (Long) request.getAttribute("userId");
String originalFilename = file.getOriginalFilename();
String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
String objectName = "avatar/" + userId + "/" + UUID.randomUUID().toString() + fileExtension;
String avatarUrl = minioService.uploadFile(file, objectName);
userService.updateUserAvatar(userId, avatarUrl);
return Result.success(avatarUrl);
} catch (Exception e) {
e.printStackTrace();
return Result.error("头像上传失败: " + e.getMessage());
}
}
/**
@@ -52,4 +52,6 @@ public interface UserService extends IService<UserDO> {
PageResultVO<UserVO> pageUsers(Integer page, Integer size, String keyword, Long shopId);
boolean updateUserStatus(Long id, Integer status);
boolean updateUserAvatar(Long userId, String avatarUrl);
}
@@ -7,8 +7,10 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaowang.yuminailcore.dto.ProductDTO;
import com.xiaowang.yuminailcore.entity.ProductDO;
import com.xiaowang.yuminailcore.mapper.ProductMapper;
import com.xiaowang.yuminailcore.service.ObjectStorageFileService;
import com.xiaowang.yuminailcore.service.ProductService;
import com.xiaowang.yuminailcore.service.TagService;
import com.xiaowang.yuminailcore.vo.ObjectStorageFileVO;
import com.xiaowang.yuminailcore.vo.PageResultVO;
import com.xiaowang.yuminailcore.vo.ProductVO;
import com.xiaowang.yuminailcore.vo.TagInfo;
@@ -17,6 +19,7 @@ import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@@ -33,6 +36,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im
private final ProductMapper productMapper;
private final TagService tagService;
private final ObjectStorageFileService objectStorageFileService;
@Override
public Long createProduct(ProductDTO productDTO) {
@@ -180,6 +184,24 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> im
productVO.setTagList(tagNames);
productVO.setTagInfos(tagInfos);
// 转换图片ID为图片URL列表
List<String> imageUrls = new ArrayList<>();
if (productDO.getImageFileIds() != null && !productDO.getImageFileIds().isEmpty()) {
String[] fileIds = productDO.getImageFileIds().split(",");
for (String fileIdStr : fileIds) {
try {
Long fileId = Long.parseLong(fileIdStr.trim());
ObjectStorageFileVO fileVO = objectStorageFileService.getObjectStorageFileById(fileId);
if (fileVO != null && fileVO.getFileUrl() != null) {
imageUrls.add(fileVO.getFileUrl());
}
} catch (NumberFormatException e) {
// 忽略无效的文件ID
}
}
}
productVO.setImageUrls(imageUrls);
return productVO;
}
@@ -465,4 +465,15 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
userDO.setUpdateTime(LocalDateTime.now());
return userMapper.updateById(userDO) > 0;
}
@Override
public boolean updateUserAvatar(Long userId, String avatarUrl) {
UserDO userDO = userMapper.selectById(userId);
if (userDO == null) {
throw new RuntimeException("用户不存在");
}
userDO.setAvatar(avatarUrl);
userDO.setUpdateTime(LocalDateTime.now());
return userMapper.updateById(userDO) > 0;
}
}
@@ -1,17 +0,0 @@
# 数据库连接编码配置
spring.datasource.url=jdbc:mysql://localhost:3306/yumi_nail?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
# 日志编码配置
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
# 服务器编码配置
server.servlet.encoding.charset=UTF-8
server.servlet.encoding.force=true
server.servlet.encoding.enabled=true
# MyBatis编码配置
mybatis.configuration.map-underscore-to-camel-case=true
# 时区配置
spring.jackson.time-zone=Asia/Shanghai
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
server.timezone=Asia/Shanghai
@@ -0,0 +1,22 @@
spring:
datasource:
url: jdbc:mysql://localhost:3306/yumi_nail?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
jackson:
time-zone: Asia/Shanghai
date-format: yyyy-MM-dd HH:mm:ss
logging:
pattern:
console: "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"
server:
servlet:
encoding:
charset: UTF-8
force: true
enabled: true
timezone: Asia/Shanghai
mybatis:
configuration:
map-underscore-to-camel-case: true