orderDisplay.helpers.js
2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import NP from 'number-precision'
const hasDisplayValue = value =>
value !== null &&
value !== undefined &&
(typeof value !== 'string' || value.trim() !== '')
const preferCustomerValue = (customerValue, orderValue) =>
hasDisplayValue(customerValue) ? customerValue : orderValue
const toFiniteMoney = value => {
if (!hasDisplayValue(value)) return null
const amount = Number(value)
return Number.isFinite(amount) ? amount : null
}
/**
* 将订单详情接口的嵌套客户、支付信息展开为页面展示对象。
* 客户对象中的手机号和车牌号是权威值,避免被订单顶层空值覆盖。
*/
export function buildOrderDisplayInfo(order = {}) {
const customer = order.customer || {}
return {
...customer,
...(order.payOrder || {}),
...order,
customerPhone: preferCustomerValue(
customer.customerPhone,
order.customerPhone
),
plateNumber: preferCustomerValue(
customer.plateNumber,
order.plateNumber
)
}
}
export function resolveConstructionButton(confirmFlag) {
if (Number(confirmFlag) === 1) {
return {
showBtn: '确认订单是否施工',
btnName: 'confirmConstruction'
}
}
return { showBtn: false }
}
export function resolveOrderGoodsUnitPrice(goods = {}) {
if (Number(goods.discountType) === 4) return 0
const finalPrice = toFiniteMoney(goods.finalPrice)
const number = Number(goods.number)
if (finalPrice !== null && Number.isFinite(number) && number > 0) {
const finalPriceInCents = Math.round(NP.times(finalPrice, 100))
return Math.round(finalPriceInCents / number) / 100
}
const fallbackPrice = toFiniteMoney(goods.upgradePrice)
return fallbackPrice === null ? 0 : NP.round(fallbackPrice, 2)
}
export function formatOrderGoodsUnitPrice(goods = {}) {
return resolveOrderGoodsUnitPrice(goods).toFixed(2)
}
export function sumOrderMoney(values = []) {
const total = values.reduce((sum, value) => {
const amount = toFiniteMoney(value)
return amount === null ? sum : NP.plus(sum, amount)
}, 0)
return NP.round(total, 2)
}
export function resolveOrderPackageFinalPrice(goodsList = []) {
return sumOrderMoney(
goodsList
.filter(goods => Number(goods.discountType) !== 4)
.map(goods => goods.finalPrice)
)
}