From 79b8bc3ac112a7eb55adaf9e2796ff025eec3d4c Mon Sep 17 00:00:00 2001 From: yml Date: Sun, 15 Nov 2026 15:22:40 +0800 Subject: [PATCH] =?UTF-8?q?fix(checkout):=20=E4=BF=AE=E5=A4=8D=E8=A7=92?= =?UTF-8?q?=E8=89=B2=E4=BB=B7=E6=A0=BC=E5=BF=AB=E7=85=A7=E4=B8=8B=E5=88=B0?= =?UTF-8?q?=E6=89=8B=E8=AF=95=E7=AE=97=E4=B8=8D=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../__tests__/useCheckoutSimulator.spec.ts | 70 +++++++++++++++++++ .../composables/useCheckoutSimulator.ts | 23 ++++-- 2 files changed, 88 insertions(+), 5 deletions(-) diff --git a/frontend/src/features/orders/composables/__tests__/useCheckoutSimulator.spec.ts b/frontend/src/features/orders/composables/__tests__/useCheckoutSimulator.spec.ts index 46e99282..3d09b5f6 100644 --- a/frontend/src/features/orders/composables/__tests__/useCheckoutSimulator.spec.ts +++ b/frontend/src/features/orders/composables/__tests__/useCheckoutSimulator.spec.ts @@ -70,3 +70,73 @@ describe('useCheckoutSimulator insurance fee', () => { expect(simulator.value.lossCompensationYuan).toBe(50) }) }) + +describe('useCheckoutSimulator role-sanitized snapshot', () => { + it('号主修改实际消耗后实时更新试算金额', () => { + const order = orderFixture() + order.account_snapshot = { + haf_coin_amount: 100_000_000, + asset_summary: { + publish_ratio: 40, + display_ratio: 40, + display_coin_base_price: 200, + consumable_price: 50, + }, + } + const form = ref({ + consumableAmountYuan: 50, + coin_consumed_m: 0, + depositDeductAmountYuan: 0, + reason: '', + evidenceText: '', + }) + const simulator = useCheckoutSimulator({ + order: ref(order), + isOwner: ref(true), + isRenter: ref(false), + form, + }) + + expect(simulator.value?.totalIncomeYuan).toBe(50) + + form.value.coin_consumed_m = 50 + + if (!simulator.value || simulator.value.role !== 'owner') { + throw new Error('owner simulation missing') + } + expect(simulator.value.totalIncomeYuan).toBe(165) + expect(simulator.value.insuranceFeeYuan).toBe(10) + }) + + it('租客侧同样按摊平后的比例和币价计算', () => { + const order = orderFixture() + order.account_snapshot = { + haf_coin_amount: 100_000_000, + asset_summary: { + publish_ratio: 40, + display_ratio: 40, + display_coin_base_price: 250, + consumable_price: 0, + }, + } + const form = ref({ + consumableAmountYuan: 0, + coin_consumed_m: 50, + depositDeductAmountYuan: 0, + reason: '', + evidenceText: '', + }) + const simulator = useCheckoutSimulator({ + order: ref(order), + isOwner: ref(false), + isRenter: ref(true), + form, + }) + + if (!simulator.value || simulator.value.role !== 'renter') { + throw new Error('renter simulation missing') + } + expect(simulator.value.actualCostYuan).toBe(125) + expect(simulator.value.totalRefundYuan).toBe(275) + }) +}) diff --git a/frontend/src/features/orders/composables/useCheckoutSimulator.ts b/frontend/src/features/orders/composables/useCheckoutSimulator.ts index 46628c92..e89d9b8a 100644 --- a/frontend/src/features/orders/composables/useCheckoutSimulator.ts +++ b/frontend/src/features/orders/composables/useCheckoutSimulator.ts @@ -55,11 +55,24 @@ export function useCheckoutSimulator(options: { | undefined) || {} const totalCoinM = roundQuantity(readNumber(snapshot?.haf_coin_amount) / 1000000) - const buyerRatio = readNumber(priceBreakdown['buyer_ratio']) - const sellerRatio = readNumber(priceBreakdown['seller_ratio']) - const buyerCoinBaseYuan = readNumber(priceBreakdown['buyer_coin_base_price']) - const sellerCoinBaseYuan = readNumber(priceBreakdown['seller_coin_base_price']) - const prepaidConsumableYuan = readNumber(priceBreakdown['consumable_price']) + + // 订单详情接口会按角色删除 price_breakdown,并把当前角色可见的价格字段摊平。 + // 同时兼容原始快照,便于管理端/历史数据复用同一套试算。 + const roleRatio = readNumber(assetSummary?.publish_ratio ?? assetSummary?.display_ratio) + const roleCoinBaseYuan = readNumber(assetSummary?.display_coin_base_price) + const summaryConsumableYuan = readNumber(assetSummary?.consumable_price) + const buyerRatio = + readNumber(priceBreakdown['buyer_ratio']) || (options.isRenter.value ? roleRatio : 0) + const sellerRatio = + readNumber(priceBreakdown['seller_ratio']) || (options.isOwner.value ? roleRatio : 0) + const buyerCoinBaseYuan = + readNumber(priceBreakdown['buyer_coin_base_price']) || + (options.isRenter.value ? roleCoinBaseYuan : 0) + const sellerCoinBaseYuan = + readNumber(priceBreakdown['seller_coin_base_price']) || + (options.isOwner.value ? roleCoinBaseYuan : 0) + const prepaidConsumableYuan = + readNumber(priceBreakdown['consumable_price']) || summaryConsumableYuan const coinUseRatio = totalCoinM > 0 ? roundQuantity(consumedM / totalCoinM) : 1 const isCoinOvershoot = totalCoinM > 0 && consumedM > totalCoinM