fix: 拼多多商品标题按汉字2/ASCII1合计60字符限制

This commit is contained in:
2026-10-20 11:39:13 +08:00
parent cf08b393b7
commit 9bbb01fdee
8 changed files with 93 additions and 7 deletions
@@ -24,6 +24,7 @@ public class PddGoodsPublishDTO {
*/
private Long systemDraftId;
/** 拼多多标题:汉字算 2、ASCII 算 1,合计不超过 60(约 30 汉字) */
@NotBlank(message = "商品标题不能为空")
private String goodsName;
@@ -436,6 +436,10 @@ public class PddGoodsPublishServiceImpl implements PddGoodsPublishService {
if (strict && !StringUtils.hasText(goodsName)) {
throw new BusinessException("商品标题不能为空");
}
// 拼多多:汉字算 2、ASCII 算 1,合计不超过 60(约 30 汉字)
if (strict && goodsName != null && pddTitleCharUnits(goodsName) > 60) {
throw new BusinessException("商品标题超长(汉字约30个/60字符)");
}
return new PddGoodsPublishPopApiClient.GoodsAddCommand(
goodsName,
@@ -517,6 +521,17 @@ public class PddGoodsPublishServiceImpl implements PddGoodsPublishService {
return imageTransfer.transfer(companyId, ctx.mallId(), ctx.accessToken(), fileId);
}
/** 拼多多标题字符单位:code point ≤ 0xFF 算 1,否则算 2。 */
private static int pddTitleCharUnits(String text) {
int n = 0;
for (int i = 0; i < text.length(); ) {
int cp = text.codePointAt(i);
n += cp <= 0xff ? 1 : 2;
i += Character.charCount(cp);
}
return n;
}
private static boolean looksLikeUrl(String s) {
String lower = s.toLowerCase();
return lower.startsWith("http://") || lower.startsWith("https://");