客户照片上传功能
This commit is contained in:
@@ -4,18 +4,22 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.rj.common.AudioManagementConstants;
|
||||
import com.rj.common.DataEnrichmentUtil;
|
||||
import com.rj.dto.CustomerPhotoPreviewResult;
|
||||
import com.rj.dto.CustomerPhotoUploadResult;
|
||||
import com.rj.entity.AudioManagement;
|
||||
import com.rj.entity.CustomerManagement;
|
||||
import com.rj.entity.SalesManagement;
|
||||
import com.rj.service.IAudioManagementService;
|
||||
import com.rj.service.ICustomerManagementService;
|
||||
import com.rj.service.ISalesManagementService;
|
||||
import com.rj.service.MinIOService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.InputStreamResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@@ -35,7 +39,6 @@ import java.util.UUID;
|
||||
* @author 李中华 ,spllzh
|
||||
* @since 2025-08-07
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/api/customerManagement")
|
||||
@Tag(name = "客户管理", description = "客户管理相关接口")
|
||||
@@ -50,9 +53,6 @@ public class CustomerManagementController {
|
||||
@Autowired
|
||||
private ISalesManagementService salesManagementService;
|
||||
|
||||
@Autowired
|
||||
private MinIOService minIOService;
|
||||
|
||||
/**
|
||||
* 新增客户
|
||||
*/
|
||||
@@ -395,46 +395,46 @@ public class CustomerManagementController {
|
||||
@PostMapping("/uploadPhoto")
|
||||
@Operation(summary = "上传客户照片", description = "根据客户ID上传照片到 MinIO,返回文件访问 URL")
|
||||
public ResponseEntity<Map<String, Object>> uploadCustomerPhoto(
|
||||
@Parameter(description = "客户ID", required = true)
|
||||
@Parameter(description = "客户ID")
|
||||
@RequestParam String id,
|
||||
@Parameter(description = "照片文件", required = true)
|
||||
@RequestParam("file") MultipartFile file) {
|
||||
CustomerPhotoUploadResult uploadResult = customerManagementService.uploadCustomerPhoto(id, file);
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
try {
|
||||
if (id == null || id.trim().isEmpty()) {
|
||||
result.put("success", false);
|
||||
result.put("message", "客户ID不能为空");
|
||||
return ResponseEntity.badRequest().body(result);
|
||||
}
|
||||
if (file == null || file.isEmpty()) {
|
||||
result.put("success", false);
|
||||
result.put("message", "请选择要上传的文件");
|
||||
return ResponseEntity.badRequest().body(result);
|
||||
}
|
||||
CustomerManagement customer = customerManagementService.getById(id.trim());
|
||||
if (customer == null) {
|
||||
result.put("success", false);
|
||||
result.put("message", "客户不存在");
|
||||
return ResponseEntity.badRequest().body(result);
|
||||
}
|
||||
String original = file.getOriginalFilename();
|
||||
String ext = "";
|
||||
if (original != null && original.contains(".")) {
|
||||
ext = original.substring(original.lastIndexOf("."));
|
||||
}
|
||||
String objectName = "customer-management/photo/" + id.trim() + "/" + UUID.randomUUID().toString().replace("-", "") + ext;
|
||||
String url = minIOService.uploadFileWithName(file, objectName);
|
||||
log.info("客户照片已上传 MinIO,customerId={}, url={}", id, url);
|
||||
result.put("success", true);
|
||||
result.put("message", "上传成功");
|
||||
result.put("url", url);
|
||||
return ResponseEntity.ok(result);
|
||||
} catch (Exception e) {
|
||||
log.error("客户照片上传失败: {}", e.getMessage(), e);
|
||||
result.put("success", false);
|
||||
result.put("message", "上传失败:" + e.getMessage());
|
||||
return ResponseEntity.internalServerError().body(result);
|
||||
result.put("success", uploadResult.isSuccess());
|
||||
result.put("message", uploadResult.getMessage());
|
||||
if (uploadResult.getUrl() != null) {
|
||||
result.put("url", uploadResult.getUrl());
|
||||
}
|
||||
return ResponseEntity.status(uploadResult.getHttpStatus()).body(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 客户照片预览:从 MinIO 拉流返回图片。
|
||||
* 方式一:传 {@code url}(与上传接口返回的 MinIO 访问 URL 一致)。
|
||||
* 方式二:传 {@code id} + {@code photoType}(front / side / life),读取客户表中对应照片 URL。
|
||||
*/
|
||||
@GetMapping("/previewPhoto")
|
||||
@Operation(summary = "预览客户照片", description = "根据 MinIO URL 或客户ID+照片类型从 MinIO 读取并返回图片流,便于 img 标签 src 引用")
|
||||
public ResponseEntity<Resource> previewCustomerPhoto(
|
||||
@Parameter(description = "MinIO 完整访问 URL(与 uploadPhoto 返回的 url 一致)")
|
||||
@RequestParam(required = false) String url,
|
||||
@Parameter(description = "客户ID,与 photoType 联用")
|
||||
@RequestParam(required = false) String id,
|
||||
@Parameter(description = "照片类型:front 正面 / side 侧面 / life 生活照", example = "front")
|
||||
@RequestParam(required = false) String photoType) {
|
||||
CustomerPhotoPreviewResult preview = customerManagementService.openCustomerPhotoPreview(url, id, photoType);
|
||||
if (preview.getStatus() == CustomerPhotoPreviewResult.Status.NOT_FOUND) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
if (preview.getStatus() == CustomerPhotoPreviewResult.Status.BAD_REQUEST) {
|
||||
return ResponseEntity.badRequest().build();
|
||||
}
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.parseMediaType(preview.getContentType()));
|
||||
headers.setCacheControl("private, max-age=3600");
|
||||
Resource resource = new InputStreamResource(preview.getInputStream());
|
||||
return ResponseEntity.ok().headers(headers).body(resource);
|
||||
}
|
||||
|
||||
private AudioManagement buildAudioRecordFromCustomer(CustomerManagement customer, LocalDateTime now) {
|
||||
|
||||
Reference in New Issue
Block a user