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
+4 -1
View File
@@ -15,7 +15,10 @@ from pydantic import BaseModel, Field
class PatchBasicArgs(BaseModel):
"""只填要改的字段;未出现的字段保持页面原值。"""
goodsName: str | None = Field(default=None, description="商品标题,60 字内")
goodsName: str | None = Field(
default=None,
description="商品标题:汉字算 2、英文数字算 1,合计最多 60(约 30 汉字)",
)
goodsDesc: str | None = Field(default=None, description="商品描述;空字符串表示清空")
marketPriceYuan: float | None = Field(default=None, description="参考价,元")
twoPiecesDiscount: float | None = Field(
+1
View File
@@ -36,6 +36,7 @@ _PUBLISH_SYSTEM = (
"假一赔十/7天无理由/预售/二手、运费模板)、ui_set_spec_axes(规格轴)、"
"ui_patch_skus(按 outSkuSn 改单买价、拼单价、库存、重量)、ui_patch_props(类目属性)。"
"每次只传要改的字段,未传的保持原样。"
"商品标题字数:汉字算 2、英文数字算 1,合计最多 60(约 30 汉字);生成或改写标题时自行压缩到上限内,不要写超再指望页面截断。"
"运费模板 ID 必须来自快照 costTemplates;发货时限只能用快照里的秒数;"
"类目属性枚举用 goodsProperties 里的 vid 或文案,多选英文逗号分隔。"
"不能改店铺、叶子类目、轮播图、详情图、SKU 缩略图;用户要求这些时用一句话说明对话里改不了。"
@@ -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://");
@@ -50,6 +50,11 @@ import { usePublishDraft } from './composables/use-publish-draft';
import { usePublishImages } from './composables/use-publish-images';
import { usePublishSku } from './composables/use-publish-sku';
import { usePublishSpecs } from './composables/use-publish-specs';
import {
PDD_GOODS_NAME_MAX_UNITS,
pddTitleCharUnits,
truncatePddTitle,
} from './utils/pdd-title-length';
defineOptions({ name: 'PddPublishEditor' });
@@ -363,6 +368,15 @@ function onSpecAxesListRef(el: HTMLElement | null) {
function onSkuTableWrapRef(el: HTMLElement | null) {
sku.skuTableWrapRef.value = el;
}
/** 标题:汉字算 2、ASCII 算 1,上限 60 */
function onGoodsNameChange(value: string) {
form.goodsName = truncatePddTitle(value);
}
function goodsNameCountFormatter(info: { value: string }) {
return `${pddTitleCharUnits(info.value)} / ${PDD_GOODS_NAME_MAX_UNITS}`;
}
</script>
<template>
@@ -461,7 +475,12 @@ function onSkuTableWrapRef(el: HTMLElement | null) {
:class="{ 'agent-typing-field': agentTypingField === 'goodsName' }"
>
<Form.Item label="商品标题" required>
<Input v-model:value="form.goodsName" :maxlength="60" show-count />
<Input
:value="form.goodsName"
:show-count="{ formatter: goodsNameCountFormatter }"
placeholder="最多约 30 个汉字(60 字符)"
@update:value="onGoodsNameChange"
/>
</Form.Item>
</div>
<div
@@ -16,6 +16,7 @@ import {
effectiveValueUnits,
resolveCatPropControlKind,
} from '../utils/cat-property';
import { truncatePddTitle } from '../utils/pdd-title-length';
import { typewriterReplace } from '../utils/typewriter-text';
/** 智能体正在打字的表单焦点(用于高亮输入区) */
@@ -235,8 +236,8 @@ export function usePublishAgent(options: {
): Promise<ClientToolResult> {
const name = pickArgString(args, 'goodsName', 'goods_name');
if (!name) return { ok: false, message: '标题为空' };
// 与输入框 maxlength 对齐,避免受控 Input 截断后看似「没写上」
const next = name.slice(0, 60);
// 与拼多多标题计字对齐:汉字 2、ASCII 1,上限 60
const next = truncatePddTitle(name);
if (form.goodsName === next) {
return { ok: true, message: `标题未变:${next}` };
}
@@ -33,6 +33,11 @@ import {
seedDefaultPropUnits,
validateCatPropValue,
} from '../utils/cat-property';
import {
PDD_GOODS_NAME_MAX_UNITS,
pddTitleCharUnits,
truncatePddTitle,
} from '../utils/pdd-title-length';
type EmitDraft = {
(e: 'draft-saved', draftId: number): void;
@@ -163,6 +168,10 @@ export function usePublishDraft(options: {
message.warning('请填写商品标题');
return false;
}
if (pddTitleCharUnits(form.goodsName) > PDD_GOODS_NAME_MAX_UNITS) {
message.warning('商品标题超长(汉字约 30 个 / 60 字符)');
return false;
}
if (!form.catId) {
message.warning('请选择叶子类目');
return false;
@@ -401,10 +410,11 @@ export function usePublishDraft(options: {
loadSkuCandidates(),
]);
form.goodsName =
form.goodsName = truncatePddTitle(
(typeof es.goodsName === 'string' && es.goodsName) ||
draft.title ||
'';
draft.title ||
'',
);
if (typeof es.goodsDesc === 'string') form.goodsDesc = es.goodsDesc;
const catId =
@@ -0,0 +1,36 @@
/**
* 拼多多商品标题字数:汉字/非 ASCII 算 2,ASCII 算 1,上限 60(约 30 汉字)。
*/
export const PDD_GOODS_NAME_MAX_UNITS = 60;
/** 单个码点占用的平台字符单位 */
function charUnit(ch: string): number {
const cp = ch.codePointAt(0) ?? 0;
return cp <= 0xff ? 1 : 2;
}
/** 标题已占用的字符单位 */
export function pddTitleCharUnits(text: string): number {
let n = 0;
for (const ch of text ?? '') {
n += charUnit(ch);
}
return n;
}
/** 按单位上限截断,保证不超过 maxUnits */
export function truncatePddTitle(
text: string,
maxUnits: number = PDD_GOODS_NAME_MAX_UNITS,
): string {
let n = 0;
let out = '';
for (const ch of text ?? '') {
const w = charUnit(ch);
if (n + w > maxUnits) break;
out += ch;
n += w;
}
return out;
}