This commit is contained in:
2026-10-20 17:59:06 +08:00
parent c38a7384aa
commit 99922bbc34
8 changed files with 9 additions and 153 deletions
@@ -1,48 +0,0 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import { readFileSync } from 'node:fs'
import ts from 'typescript'
import { ref } from 'vue'
const flush = async () => { for (let i = 0; i < 8; i++) await Promise.resolve() }
function harness(api, props = { orderId: 'one', canConfirm: true }) {
let changed, disposed
const events = []
const source = readFileSync(new URL('./ChargeableWeightDialog.vue', import.meta.url), 'utf8').split('<script setup lang="ts">')[1].split('</script>')[0]
const code = ts.transpileModule(source + '\nexports.state = { rows, loading, saving, error, confirm, load };', {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 },
}).outputText
const exports = {}
new Function('require', 'exports', 'defineProps', 'defineEmits', code)(name => {
if (name === 'vue') return { ref, watch: (_, callback) => { changed = callback; callback() }, onBeforeUnmount: callback => { disposed = callback } }
if (name === 'element-plus') return { ElMessage: { success() {} } }
return api
}, exports, () => props, () => name => events.push(name))
return { ...exports.state, props, change: () => changed(), dispose: () => disposed(), events }
}
test('确认结果未知时重试原命令,成功后回查并通知工作台刷新', async () => {
const row = { inboundId: '100', fingerprint: 'weight-v1', chargeableWeightKg: 50, confirmed: false }
const commands = []
let fail = true
const h = harness({ getChargeableWeights: async () => [row], confirmChargeableWeight: async (_, key) => {
commands.push(key); if (fail) throw new Error('响应丢失'); row.confirmed = true
} })
await flush(); await h.confirm(row)
assert.equal(h.error.value, '响应丢失'); assert.equal(h.saving.value, '')
fail = false; await h.confirm(row)
assert.equal(commands[0], commands[1]); assert.deepEqual(h.events, ['confirmed'])
assert.equal(h.rows.value[0].confirmed, true); assert.equal(h.saving.value, '')
})
test('切换订单或关闭后丢弃迟到的读取结果,无权限不能确认', async () => {
let resolveOld
const h = harness({ getChargeableWeights: order => order === 'one' ? new Promise(resolve => { resolveOld = resolve }) : Promise.resolve([]),
confirmChargeableWeight: () => { throw new Error('无权限不得写入') } }, { orderId: 'one', canConfirm: false })
h.props.orderId = 'two'; h.change(); await flush()
resolveOld([{ inboundId: 'old' }]); await flush()
assert.deepEqual(h.rows.value, [])
await h.confirm({ inboundId: 'old', fingerprint: 'old' })
assert.deepEqual(h.events, [])
h.dispose()
})
@@ -1,55 +0,0 @@
<script setup lang="ts">
import { onBeforeUnmount, ref, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { getChargeableWeights, confirmChargeableWeight, type ChargeableWeight } from '../api/chargeableWeights'
const props = defineProps<{ orderId: string; canConfirm: boolean }>()
const emit = defineEmits<{ close: []; confirmed: [] }>()
const rows = ref<ChargeableWeight[]>([])
const loading = ref(false)
const saving = ref('')
const error = ref('')
const commands = new Map<string, string>()
let sequence = 0
let confirmationSequence = 0
async function load() {
const ticket = ++sequence
loading.value = true; error.value = ''; rows.value = []
try { const data = await getChargeableWeights(props.orderId); if (ticket === sequence) rows.value = data }
catch (e) { if (ticket === sequence) error.value = e instanceof Error ? e.message : '计费重加载失败' }
finally { if (ticket === sequence) loading.value = false }
}
async function confirm(row: ChargeableWeight) {
if (saving.value || !props.canConfirm) return
const ticket = sequence
const attempt = ++confirmationSequence
const fingerprint = `${row.inboundId}:${row.fingerprint}`
const key = commands.get(fingerprint) ?? `weight-${crypto.randomUUID()}`
commands.set(fingerprint, key); saving.value = row.inboundId; error.value = ''
try {
await confirmChargeableWeight(row, key)
if (ticket !== sequence) return
ElMessage.success('计费重已确认'); emit('confirmed'); await load()
} catch (e) { if (ticket === sequence) error.value = e instanceof Error ? e.message : '确认失败,请刷新后重试' }
finally { if (attempt === confirmationSequence) saving.value = '' }
}
watch(() => props.orderId, () => { confirmationSequence++; saving.value = ''; commands.clear(); void load() }, { immediate: true })
onBeforeUnmount(() => { sequence++; confirmationSequence++ })
</script>
<template>
<el-dialog :model-value="true" title="确认计费重" width="560px" :close-on-click-modal="false" @close="emit('close')">
<el-alert v-if="error" :title="error" type="error" :closable="false" />
<el-button v-if="error" :disabled="!!saving" @click="load">重新加载</el-button>
<div v-loading="loading">
<el-empty v-if="!loading && !error && !rows.length" description="暂无已审核的计费重" />
<div v-for="row in rows" :key="row.inboundId" class="weight-row">
<span>来源入仓单:{{ row.inboundId }}<br>计费重:{{ row.chargeableWeightKg }} kg</span>
<el-tag v-if="row.confirmed" type="success">已确认</el-tag>
<el-button v-else-if="canConfirm" type="primary" :disabled="!!saving" :loading="saving === row.inboundId" @click="confirm(row)">确认计费重</el-button>
<span v-else>待确认</span>
</div>
</div>
</el-dialog>
</template>
<style scoped>
.weight-row { display: flex; justify-content: space-between; align-items: center; gap: 16px; padding: 12px 0; }
</style>
@@ -30,7 +30,7 @@ test('shows the published merchant inbound, measurement details and customer con
}
assert.match(source, /formatInboundMoney\(props\.data\.latestFee\.totalAmount/)
assert.match(source, /formatConversionPrecision\(/)
assert.match(source, /确认费用/)
assert.match(source, /title="确认计费重"/)
assert.match(source, /确认后当前费用版本将锁定/)
assert.match(source, /customerConfirmationStatus === 'CONFIRMED'/)
assert.match(source, /confirmMerchantInboundFee/)
@@ -77,7 +77,7 @@ async function confirmFee() {
<el-tag :type="props.data.latestFee.customerConfirmationStatus === 'CONFIRMED' ? 'success' : 'warning'" effect="light">
{{ props.data.latestFee.customerConfirmationStatus === 'CONFIRMED' ? '客户已确认 · 费用已锁定' : '费用已发布 · 待确认' }}
</el-tag>
<el-button v-if="props.data.latestFee.customerConfirmationStatus !== 'CONFIRMED' && props.canConfirm" type="primary" @click="feeDialogOpen = true">确认费用</el-button>
<el-button v-if="props.data.latestFee.customerConfirmationStatus !== 'CONFIRMED' && props.canConfirm" type="primary" @click="feeDialogOpen = true">确认计费重</el-button>
<el-button v-else plain @click="feeDialogOpen = true">查看费用</el-button>
</div>
</header>
@@ -198,7 +198,7 @@ async function confirmFee() {
</section>
<el-dialog v-model="feeDialogOpen" class="billing-confirm-dialog" title="确认费用" width="min(1180px, calc(100vw - 40px))" append-to-body @closed="emit('closed')">
<el-dialog v-model="feeDialogOpen" class="billing-confirm-dialog" title="确认计费重" width="min(1180px, calc(100vw - 40px))" append-to-body @closed="emit('closed')">
<div v-if="props.loading" class="inbound-billing-loading" aria-live="polite" aria-busy="true">
<el-skeleton :rows="4" animated />
<span class="sr-only">正在加载费用</span>