feat: 网页配置顺丰运费时改为月结并填写顺丰单号
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
afcb4e9ebe
commit
9561f94074
7 files changed
+312
-98
No files matched your search
@@ -0,0 +1,48 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
import {
|
||||
buildFreightHeaderPayload,
|
||||
buildSettlePayload,
|
||||
freightAmountText,
|
||||
validateFreightHeader
|
||||
} from './sf-express-freight.ts'
|
||||
|
||||
test('sf express hides amount and requires waybill', () => {
|
||||
assert.equal(
|
||||
validateFreightHeader({ transportMode: 'sfkd', freightAmount: null, waybillNo: ' SF1 ' }),
|
||||
''
|
||||
)
|
||||
assert.match(
|
||||
validateFreightHeader({ transportMode: 'sfkd', freightAmount: null, waybillNo: ' ' }),
|
||||
/请填写顺丰单号/
|
||||
)
|
||||
assert.deepEqual(
|
||||
buildFreightHeaderPayload({ transportMode: 'sfkd', freightAmount: 8, waybillNo: ' SF1 ' }),
|
||||
{ transportMode: 'sfkd', waybillNo: 'SF1', freightAmount: null }
|
||||
)
|
||||
assert.equal(freightAmountText('sfkd', null), '月结')
|
||||
assert.equal(freightAmountText('sfkd', 8), '8.00')
|
||||
})
|
||||
|
||||
test('other transport still requires amount', () => {
|
||||
assert.match(
|
||||
validateFreightHeader({ transportMode: 'zxc', freightAmount: null, waybillNo: '' }),
|
||||
/请填写运费/
|
||||
)
|
||||
assert.equal(
|
||||
buildFreightHeaderPayload({ transportMode: 'zxc', freightAmount: 12.3, waybillNo: 'SF1' })
|
||||
.freightAmount,
|
||||
12.3
|
||||
)
|
||||
assert.equal(
|
||||
buildFreightHeaderPayload({ transportMode: 'zxc', freightAmount: 12.3, waybillNo: 'SF1' })
|
||||
.waybillNo,
|
||||
''
|
||||
)
|
||||
})
|
||||
|
||||
test('blank settle omits amount and batch settle never sends amount', () => {
|
||||
assert.deepEqual(buildSettlePayload(['a'], ' '), { ids: ['a'] })
|
||||
assert.deepEqual(buildSettlePayload(['a'], '6.5'), { ids: ['a'], freightAmount: 6.5 })
|
||||
assert.deepEqual(buildSettlePayload(['a', 'b'], '6.5'), { ids: ['a', 'b'] })
|
||||
})
|
||||
@@ -0,0 +1,59 @@
|
||||
export const SF_TRANSPORT_MODE = 'sfkd'
|
||||
|
||||
export function isSfExpress(mode: string | null | undefined) {
|
||||
return String(mode || '').trim() === SF_TRANSPORT_MODE
|
||||
}
|
||||
|
||||
export function freightAmountText(
|
||||
mode: string | null | undefined,
|
||||
amount: number | string | null | undefined
|
||||
) {
|
||||
if (isSfExpress(mode) && (amount === null || amount === undefined || amount === '')) return '月结'
|
||||
if (amount === null || amount === undefined || amount === '') return ''
|
||||
const value = Number(amount)
|
||||
if (!Number.isFinite(value)) return ''
|
||||
return value.toFixed(2)
|
||||
}
|
||||
|
||||
export function validateFreightHeader(input: {
|
||||
transportMode: string
|
||||
freightAmount: number | null
|
||||
waybillNo: string
|
||||
}) {
|
||||
if (!String(input.transportMode || '').trim()) return '请选择运输方式'
|
||||
if (isSfExpress(input.transportMode)) {
|
||||
const waybill = String(input.waybillNo || '').trim()
|
||||
if (!waybill) return '请填写顺丰单号'
|
||||
if (waybill.length > 64) return '顺丰单号不能超过 64 个字符'
|
||||
return ''
|
||||
}
|
||||
if (
|
||||
input.freightAmount == null ||
|
||||
!Number.isFinite(Number(input.freightAmount)) ||
|
||||
Number(input.freightAmount) < 0
|
||||
) {
|
||||
return '请填写运费'
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
export function buildFreightHeaderPayload(input: {
|
||||
transportMode: string
|
||||
freightAmount: number | null
|
||||
waybillNo: string
|
||||
}) {
|
||||
const sf = isSfExpress(input.transportMode)
|
||||
return {
|
||||
transportMode: String(input.transportMode || '').trim(),
|
||||
waybillNo: sf ? String(input.waybillNo || '').trim() : '',
|
||||
freightAmount: sf ? null : Number(input.freightAmount)
|
||||
}
|
||||
}
|
||||
|
||||
export function buildSettlePayload(ids: string[], freightAmount: string) {
|
||||
const payload: { ids: string[]; freightAmount?: number } = { ids }
|
||||
if (ids.length === 1 && String(freightAmount || '').trim()) {
|
||||
payload.freightAmount = Number(String(freightAmount).trim())
|
||||
}
|
||||
return payload
|
||||
}
|
||||
Reference in new issue
Block a user