fix: 规格做法生成SKU时叠加附加价

This commit is contained in:
2026-02-24 09:54:30 +08:00
parent 4c7b6e98da
commit fb6b9945c8

View File

@@ -25,6 +25,31 @@ export function createProductDetailSkuActions(
) { ) {
const { form, skuBatch, specTemplateOptions } = options; const { form, skuBatch, specTemplateOptions } = options;
function normalizeMoney(value: number) {
return Number(Number(value).toFixed(2));
}
function resolveSpecOptionExtraPrice(templateId: string, optionId: string) {
const template = specTemplateOptions.value.find(
(item) => item.id === templateId,
);
if (!template) return 0;
const option = template.values.find((item) => item.id === optionId);
if (!option) return 0;
const extraPrice = Number(option.extraPrice || 0);
return Number.isFinite(extraPrice) ? extraPrice : 0;
}
function resolveSkuAttrsExtraPrice(
attrs: ProductDetailSkuRowState['attributes'],
) {
return attrs.reduce(
(sum, item) =>
sum + resolveSpecOptionExtraPrice(item.templateId, item.optionId),
0,
);
}
function getTemplateName(templateId: string) { function getTemplateName(templateId: string) {
return ( return (
specTemplateOptions.value.find((item) => item.id === templateId)?.name || specTemplateOptions.value.find((item) => item.id === templateId)?.name ||
@@ -97,15 +122,23 @@ export function createProductDetailSkuActions(
const key = buildSkuKey(attrs); const key = buildSkuKey(attrs);
const cached = previousMap.get(key); const cached = previousMap.get(key);
const skuIndex = index + 1; const skuIndex = index + 1;
const totalExtraPrice = resolveSkuAttrsExtraPrice(attrs);
const defaultPrice = normalizeMoney(
Math.max(0, Number(form.price || 0) + totalExtraPrice),
);
const defaultOriginalPrice =
form.originalPrice !== null &&
form.originalPrice !== undefined &&
form.originalPrice > 0
? normalizeMoney(
Math.max(0, Number(form.originalPrice) + totalExtraPrice),
)
: null;
return { return {
id: cached?.id || '', id: cached?.id || '',
skuCode: cached?.skuCode || buildLocalSkuCode(skuIndex), skuCode: cached?.skuCode || buildLocalSkuCode(skuIndex),
price: cached?.price ?? Number(form.price || 0), price: cached?.price ?? defaultPrice,
originalPrice: originalPrice: cached?.originalPrice ?? defaultOriginalPrice,
cached?.originalPrice ??
(form.originalPrice && form.originalPrice > 0
? form.originalPrice
: null),
stock: stock:
cached?.stock ?? Math.max(0, Math.floor(Number(form.stock || 0))), cached?.stock ?? Math.max(0, Math.floor(Number(form.stock || 0))),
isEnabled: cached?.isEnabled ?? true, isEnabled: cached?.isEnabled ?? true,