feat(edit): 优化图片导入ozon问题
This commit is contained in:
@@ -36,6 +36,11 @@ type Job = {
|
||||
poll_count: number;
|
||||
};
|
||||
type ErrorInfo = { code: string; field?: string; message: string };
|
||||
const imageErrorMessages: Record<string, string> = {
|
||||
primary_image_load_failed: 'Ozon 下载主图失败,已用附加图片替代,请检查图片交付域名并重新上传。',
|
||||
pics_http_error: 'Ozon 无法下载图片,请检查下载域名的海外连通性及访问限制。',
|
||||
some_image_failed: '部分图片未被 Ozon 下载,请检查图片并重新上传。',
|
||||
};
|
||||
export type PublishDependencies = {
|
||||
request: typeof requestOzon;
|
||||
images: (
|
||||
@@ -116,7 +121,7 @@ export function safePublishErrors(input: unknown): ErrorInfo[] {
|
||||
? { field: clean(item.field, 200) }
|
||||
: {}),
|
||||
message: clean(
|
||||
item.message ??
|
||||
imageErrorMessages[String(item.code)] ?? item.message ??
|
||||
item.description ??
|
||||
'Ozon 拒绝该 SKU,请核对商品资料',
|
||||
800,
|
||||
@@ -312,6 +317,45 @@ export async function runProductPublish(
|
||||
credentials.secret_ciphertext,
|
||||
credentials.secret_nonce,
|
||||
);
|
||||
const completeImported = async (item: Item, productId: string, importErrors: unknown = []) => {
|
||||
const errors = safePublishErrors(importErrors);
|
||||
try {
|
||||
const pictures = await dependencies.request(
|
||||
credentials.platform_client_id, apiKey, '/v2/product/pictures/info',
|
||||
{ product_id: [productId] },
|
||||
) as { items?: Array<{
|
||||
product_id: number | string; primary_photo?: string[]; photo?: string[];
|
||||
errors?: Array<{ message?: string }>;
|
||||
}> };
|
||||
if (!Array.isArray(pictures.items)) throw new Error('Ozon 图片状态响应格式异常');
|
||||
const picture = pictures.items.find((entry) => String(entry.product_id) === productId);
|
||||
const count = new Set([...(picture?.primary_photo ?? []), ...(picture?.photo ?? [])]).size;
|
||||
const pictureErrors = picture?.errors ?? [];
|
||||
// 商品导入与图片抓取异步完成,图片尚未返回时继续轮询,不能提前报全量成功。
|
||||
if (count < item.snapshot.media.length && !pictureErrors.length
|
||||
&& !errors.some((entry) => entry.code in imageErrorMessages)
|
||||
&& job.poll_count <= 120) return;
|
||||
if (count < item.snapshot.media.length) errors.push({
|
||||
code: 'OZON_IMAGES_INCOMPLETE', field: 'images',
|
||||
message: `商品已导入,但 Ozon 当前仅接收 ${count}/${item.snapshot.media.length} 张图片,请检查图片交付域名后重新上传。`,
|
||||
});
|
||||
errors.push(...safePublishErrors(pictureErrors.map((entry) => ({
|
||||
code: 'OZON_IMAGE_DOWNLOAD_FAILED', field: 'images',
|
||||
message: entry.message?.includes('host unreachable')
|
||||
? 'Ozon 无法连接图片下载域名,请检查海外或全球加速配置后重新上传图片。'
|
||||
: entry.message || 'Ozon 图片下载失败,请检查图片直链并重新上传。',
|
||||
}))));
|
||||
} catch (error) {
|
||||
if (job.poll_count <= 120) throw error;
|
||||
errors.push({
|
||||
code: 'OZON_IMAGE_STATUS_UNCONFIRMED', field: 'images',
|
||||
message: '商品已导入,但暂未确认图片是否全部接收,请在 Ozon 后台核对图片。',
|
||||
});
|
||||
}
|
||||
const unique = errors.filter((entry, index) => errors.findIndex((other) =>
|
||||
other.code === entry.code && other.field === entry.field && other.message === entry.message) === index);
|
||||
await complete(item, 'imported', productId, unique.slice(0, 30));
|
||||
};
|
||||
const remote = async () =>
|
||||
parseInfoProducts(
|
||||
await dependencies.request(
|
||||
@@ -349,11 +393,10 @@ export async function runProductPublish(
|
||||
outcome.product_id &&
|
||||
String(outcome.product_id) !== '0'
|
||||
)
|
||||
await complete(
|
||||
await completeImported(
|
||||
item,
|
||||
'imported',
|
||||
String(outcome.product_id),
|
||||
[],
|
||||
outcome.errors,
|
||||
);
|
||||
else if (outcome.status === 'failed')
|
||||
await complete(
|
||||
@@ -374,7 +417,7 @@ export async function runProductPublish(
|
||||
(r) => r.offer_id === item.offer_id,
|
||||
);
|
||||
if (found)
|
||||
await complete(item, 'imported', found.id, [
|
||||
await completeImported(item, found.id, [
|
||||
{
|
||||
code: 'OZON_IMPORT_RECONCILED',
|
||||
message:
|
||||
@@ -397,7 +440,7 @@ export async function runProductPublish(
|
||||
for (const item of items) {
|
||||
const match = found.find((r) => r.offer_id === item.offer_id);
|
||||
if (match)
|
||||
await complete(item, 'imported', match.id, [
|
||||
await completeImported(item, match.id, [
|
||||
{
|
||||
code: 'OZON_IMPORT_RECONCILED',
|
||||
message:
|
||||
|
||||
Reference in New Issue
Block a user