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,
@@ -13,6 +13,8 @@ export namespace PddGoodsPublishDTO {
isPreSale: boolean;
isRefundable: boolean;
secondHand: boolean;
/** 满2件折扣 0-100,默认 95 */
twoPiecesDiscount: number;
/** fileId 字符串或已转传的 URL */
carouselImages: string[];
detailImages: string[];
@@ -208,6 +208,7 @@ const form = reactive({
isPreSale: false,
isRefundable: true,
secondHand: false,
twoPiecesDiscount: 95,
goodsPropValues: {} as Record<string, string>,
});
@@ -284,7 +285,7 @@ const skuTableColumns = computed(() => [
})),
{ title: '商家编码', key: 'code', width: 300 },
{ title: '单买价', key: 'price', width: 120 },
{ title: '团购价', key: 'multi', width: 120 },
{ title: '拼单价', key: 'multi', width: 120 },
{ title: '库存', key: 'qty', width: 100 },
{ title: '重量g', key: 'weight', width: 100 },
{ title: '缩略图', key: 'thumb', width: 100 },
@@ -1314,7 +1315,15 @@ function validateForm(): boolean {
return false;
}
if (form.marketPriceYuan == null) {
message.warning('请填写市场价');
message.warning('请填写参考价');
return false;
}
if (
form.twoPiecesDiscount == null ||
form.twoPiecesDiscount < 0 ||
form.twoPiecesDiscount > 100
) {
message.warning('请填写满两件折扣(0-100,95表示95折)');
return false;
}
for (const p of nonSaleProps.value) {
@@ -1489,6 +1498,7 @@ async function buildPublishPayload(
isPreSale: form.isPreSale,
isRefundable: form.isRefundable,
secondHand: form.secondHand,
twoPiecesDiscount: form.twoPiecesDiscount,
carouselImages: carouselFileIds.map(String),
detailImages: detailFileIds.map(String),
goodsProperties,
@@ -1550,6 +1560,7 @@ function resetAll() {
form.isPreSale = false;
form.isRefundable = true;
form.secondHand = false;
form.twoPiecesDiscount = 95;
clearLocalImages();
form.goodsPropValues = {};
clearSkuRows();
@@ -1785,7 +1796,7 @@ onBeforeUnmount(() => {
</Card>
<div class="grid grid-cols-2 gap-3">
<Form.Item label="市场价(元)" required>
<Form.Item label="参考价(元)" required>
<InputNumber
v-model:value="form.marketPriceYuan"
class="w-full"
@@ -1803,6 +1814,16 @@ onBeforeUnmount(() => {
]"
/>
</Form.Item>
<Form.Item label="满两件折扣" required>
<InputNumber
v-model:value="form.twoPiecesDiscount"
class="w-full"
:min="0"
:max="100"
:precision="0"
placeholder="95 表示 95 折,0 取消"
/>
</Form.Item>
</div>
<div class="flex flex-wrap gap-6">