cmc-web/packages/common/components/sku-table/utils.ts

33 lines
918 B
TypeScript
Raw Normal View History

2024-08-21 01:17:14 +00:00
export function modeUnitFilter(value: string) {
2024-08-20 12:11:31 +00:00
const map: any = {
HOUR: '小时',
MONTH: '月',
YEAR: '年'
}
return map[value]
}
2024-08-21 01:17:14 +00:00
export function getPrice(sku: any, mode: string) {
const { spec, billPolicy, billable } = sku
2024-08-20 12:11:31 +00:00
// 不开启计费
2024-08-21 01:17:14 +00:00
if (!billable) return 0
2024-08-20 12:11:31 +00:00
const key = `${mode.toLowerCase()}Price`
// 规格计费
2024-08-21 01:17:14 +00:00
if (billPolicy !== 'agility') return sku[key]
const basicPrice = JSON.parse(sku.basicPrice)
2024-08-20 12:11:31 +00:00
// 没有specName说明没有规格设置
2024-08-21 01:17:14 +00:00
const flag = spec.every((item: any) => !item.specName || item.specValue)
2024-08-20 12:11:31 +00:00
// 无值直接返回
2024-08-21 01:17:14 +00:00
if (!flag) return '/'
2024-08-20 12:11:31 +00:00
// 获取数值map
2024-08-21 01:17:14 +00:00
const countMap: any = {}
spec.forEach((item: any) => {
countMap[item.specName] = item.specValue
2024-08-20 12:11:31 +00:00
})
2024-08-21 01:17:14 +00:00
let totalPrice = 0
basicPrice.forEach((item: any) => {
const { specName } = item
2024-08-20 12:11:31 +00:00
totalPrice += item[key] * (countMap[specName] || 1)
2024-08-21 01:17:14 +00:00
})
return totalPrice
2024-08-20 12:11:31 +00:00
}