feat: 完善拼多多商品发布

This commit is contained in:
2026-09-09 14:53:09 +08:00
parent f94d9b2db4
commit bd40064755
7 changed files with 77 additions and 10 deletions
@@ -1,6 +1,8 @@
package top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.model.dto.publish;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
@@ -33,7 +35,7 @@ public class PddGoodsPublishDTO {
private Long costTemplateId;
/** 参考价,单位分 */
@NotNull(message = "市场价不能为空")
@NotNull(message = "参考价不能为空")
private Long marketPriceFen;
/** 承诺发货时间(秒),如 86400 / 172800 */
@@ -52,6 +54,14 @@ public class PddGoodsPublishDTO {
@NotNull(message = "是否二手不能为空")
private Boolean secondHand;
/**
* 满2件折扣:0-100,0 取消,95=95折;默认 95。
*/
@NotNull(message = "满两件折扣不能为空")
@Min(value = 0, message = "满两件折扣范围为0-100")
@Max(value = 100, message = "满两件折扣范围为0-100")
private Integer twoPiecesDiscount = 95;
/** 轮播图:本系统 fileId,或已是拼多多 CDN 的 URL */
@NotEmpty(message = "轮播图不能为空")
private List<String> carouselImages;
@@ -26,8 +26,8 @@ public class PddGoodsPublishSkuDTO {
@NotNull(message = "单买价不能为空")
private Long priceFen;
/** 团购价,分 */
@NotNull(message = "团购价不能为空")
/** 拼单价,分 */
@NotNull(message = "拼单价不能为空")
private Long multiPriceFen;
@NotNull(message = "库存不能为空")
@@ -322,6 +322,7 @@ public class PddGoodsPublishServiceImpl implements PddGoodsPublishService {
dto.getIsPreSale(),
dto.getIsRefundable(),
dto.getSecondHand(),
dto.getTwoPiecesDiscount() == null ? 95 : dto.getTwoPiecesDiscount(),
carousel,
detail,
primary.getProductCode().trim(),
@@ -19,7 +19,9 @@ import java.util.Objects;
import java.util.Set;
/**
* 本系统 fileId → 读字节 → Base64 → 拼多多商品图上传。
* 本系统 fileId → 读字节 → 校验魔数 → data URI Base64 → 拼多多商品图上传。
* <p>拼多多 image 参数需带 {@code data:image/jpeg;base64,} / {@code data:image/png;base64,} 前缀,
* 仅传裸 Base64 常报「图片格式错误」。
*/
@Slf4j
@Component
@@ -64,8 +66,37 @@ public class PddGoodsImageTransfer {
if (bytes.length > MAX_BYTES) {
throw new BusinessException("图片超过1MB限制,无法上传到拼多多");
}
String base64 = Base64.getEncoder().encodeToString(bytes);
return publishPopApiClient.uploadImageBase64(mallId, accessToken, base64);
String dataUri = toPddImageDataUri(bytes);
return publishPopApiClient.uploadImageBase64(mallId, accessToken, dataUri);
}
/**
* 按文件魔数识别 JPEG/PNG,并拼成拼多多要求的 data URI Base64。
*/
static String toPddImageDataUri(byte[] bytes) {
String mime = detectImageMime(bytes);
if (mime == null) {
throw new BusinessException("图片内容不是合法的 JPG/PNG(扩展名可能与实际格式不符,如 WebP/HEIC)");
}
return "data:" + mime + ";base64," + Base64.getEncoder().encodeToString(bytes);
}
/** JPEG: FF D8 …;PNG: 89 50 4E 47 */
static String detectImageMime(byte[] bytes) {
if (bytes == null || bytes.length < 3) {
return null;
}
if ((bytes[0] & 0xFF) == 0xFF && (bytes[1] & 0xFF) == 0xD8) {
return "image/jpeg";
}
if (bytes.length >= 8
&& (bytes[0] & 0xFF) == 0x89
&& bytes[1] == 0x50
&& bytes[2] == 0x4E
&& bytes[3] == 0x47) {
return "image/png";
}
return null;
}
private void assertImageAllowed(SysFileVO file) {
@@ -134,7 +134,7 @@ public class PddGoodsPublishPopApiClient {
});
}
/** 上传商品图(入参已是 base64)。 */
/** 上传商品图(入参为带 data URI 前缀的 base64,如 data:image/jpeg;base64,...)。 */
public String uploadImageBase64(Long mallId, String accessToken, String imageBase64) throws Exception {
if (!StringUtils.hasText(imageBase64)) {
throw new BusinessException("图片内容不能为空");
@@ -187,6 +187,7 @@ public class PddGoodsPublishPopApiClient {
request.setDetailGallery(cmd.detailGallery());
request.setOutGoodsId(cmd.outGoodsId());
request.setImageUrl(cmd.imageUrl());
request.setTwoPiecesDiscount(cmd.twoPiecesDiscount() == null ? 95 : cmd.twoPiecesDiscount());
if (cmd.goodsProperties() != null && !cmd.goodsProperties().isEmpty()) {
List<PddGoodsAddRequest.GoodsPropertiesItem> props = new ArrayList<>();
for (GoodsPropertyItem p : cmd.goodsProperties()) {
@@ -498,6 +499,7 @@ public class PddGoodsPublishPopApiClient {
Boolean isPreSale,
Boolean isRefundable,
Boolean secondHand,
Integer twoPiecesDiscount,
List<String> carouselGallery,
List<String> detailGallery,
String outGoodsId,