feat: 商品详情大批量SKU保存改为后台非阻塞
All checks were successful
Build and Deploy TenantUI / build-and-deploy (push) Successful in 53s
All checks were successful
Build and Deploy TenantUI / build-and-deploy (push) Successful in 53s
This commit is contained in:
@@ -81,6 +81,7 @@ export function createProductDetailDataActions(
|
|||||||
specTemplateOptions,
|
specTemplateOptions,
|
||||||
storeId,
|
storeId,
|
||||||
} = options;
|
} = options;
|
||||||
|
let activeSkuSaveJobId = '';
|
||||||
|
|
||||||
function resetForm() {
|
function resetForm() {
|
||||||
Object.assign(form, {
|
Object.assign(form, {
|
||||||
@@ -360,6 +361,73 @@ export function createProductDetailDataActions(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function watchSkuSaveJobInBackground(
|
||||||
|
storeIdValue: string,
|
||||||
|
productIdValue: string,
|
||||||
|
jobId: string,
|
||||||
|
) {
|
||||||
|
if (activeSkuSaveJobId && activeSkuSaveJobId !== jobId) {
|
||||||
|
message.destroy(`product-sku-save-job-${activeSkuSaveJobId}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
activeSkuSaveJobId = jobId;
|
||||||
|
const toastKey = `product-sku-save-job-${jobId}`;
|
||||||
|
message.loading({
|
||||||
|
content: 'SKU 正在后台保存,可继续操作',
|
||||||
|
duration: 0,
|
||||||
|
key: toastKey,
|
||||||
|
});
|
||||||
|
|
||||||
|
const jobResult = await waitForSkuSaveJob(storeIdValue, jobId);
|
||||||
|
if (activeSkuSaveJobId !== jobId) {
|
||||||
|
message.destroy(toastKey);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (jobResult.status === 'succeeded') {
|
||||||
|
if (storeId.value === storeIdValue && form.id === productIdValue) {
|
||||||
|
try {
|
||||||
|
const latestDetail = await getProductDetailApi({
|
||||||
|
storeId: storeIdValue,
|
||||||
|
productId: productIdValue,
|
||||||
|
});
|
||||||
|
detail.value = latestDetail;
|
||||||
|
patchForm(latestDetail);
|
||||||
|
buildSkuRows();
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
message.success({
|
||||||
|
content: 'SKU 后台保存完成',
|
||||||
|
key: toastKey,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (jobResult.status === 'failed' || jobResult.status === 'canceled') {
|
||||||
|
message.error({
|
||||||
|
content: jobResult.errorMessage || 'SKU 异步保存失败',
|
||||||
|
key: toastKey,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (jobResult.timedOut) {
|
||||||
|
message.warning({
|
||||||
|
content: 'SKU 仍在后台处理中,请稍后刷新查看',
|
||||||
|
key: toastKey,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
message.warning({
|
||||||
|
content: 'SKU 保存状态未知,请稍后刷新查看',
|
||||||
|
key: toastKey,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async function saveDetail() {
|
async function saveDetail() {
|
||||||
if (!storeId.value || !form.id) return;
|
if (!storeId.value || !form.id) return;
|
||||||
if (!form.name.trim()) {
|
if (!form.name.trim()) {
|
||||||
@@ -485,43 +553,33 @@ export function createProductDetailDataActions(
|
|||||||
tags: [],
|
tags: [],
|
||||||
});
|
});
|
||||||
|
|
||||||
let latestDetail = saved;
|
|
||||||
if (shouldSaveSkuAsync) {
|
if (shouldSaveSkuAsync) {
|
||||||
|
try {
|
||||||
const createdJob = await createProductSkuSaveJobApi({
|
const createdJob = await createProductSkuSaveJobApi({
|
||||||
storeId: storeId.value,
|
storeId: storeId.value,
|
||||||
productId: saved.id,
|
productId: saved.id,
|
||||||
specTemplateIds: [...form.specTemplateIds],
|
specTemplateIds: [...form.specTemplateIds],
|
||||||
skus: normalizedSkus,
|
skus: normalizedSkus,
|
||||||
});
|
});
|
||||||
const jobResult = await waitForSkuSaveJob(
|
message.success(
|
||||||
|
`商品基础信息已保存,${normalizedSkus.length} 条 SKU 正在后台处理`,
|
||||||
|
);
|
||||||
|
detail.value = saved;
|
||||||
|
void watchSkuSaveJobInBackground(
|
||||||
storeId.value,
|
storeId.value,
|
||||||
|
saved.id,
|
||||||
createdJob.jobId,
|
createdJob.jobId,
|
||||||
);
|
);
|
||||||
if (jobResult.status === 'succeeded') {
|
} catch (error) {
|
||||||
latestDetail = await getProductDetailApi({
|
console.error(error);
|
||||||
storeId: storeId.value,
|
message.error('商品基础信息已保存,但 SKU 异步任务创建失败');
|
||||||
productId: saved.id,
|
|
||||||
});
|
|
||||||
message.success('商品详情已保存,SKU 已异步更新');
|
|
||||||
} else if (
|
|
||||||
jobResult.status === 'failed' ||
|
|
||||||
jobResult.status === 'canceled'
|
|
||||||
) {
|
|
||||||
message.error(
|
|
||||||
jobResult.errorMessage || '商品基础信息已保存,但 SKU 异步保存失败',
|
|
||||||
);
|
|
||||||
} else if (jobResult.timedOut) {
|
|
||||||
message.warning('商品基础信息已保存,SKU 正在后台处理');
|
|
||||||
} else {
|
|
||||||
message.warning('商品基础信息已保存,SKU 保存状态请稍后刷新查看');
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
message.success('商品详情已保存');
|
message.success('商品详情已保存');
|
||||||
}
|
detail.value = saved;
|
||||||
|
patchForm(saved);
|
||||||
detail.value = latestDetail;
|
|
||||||
patchForm(latestDetail);
|
|
||||||
buildSkuRows();
|
buildSkuRows();
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
Reference in New Issue
Block a user