73 lines
1.8 KiB
TypeScript
73 lines
1.8 KiB
TypeScript
|
|
import {ProductItem} from '@/actions/product'
|
||
|
|
import {PurchaseKind, PurchaseMode} from './resource'
|
||
|
|
|
||
|
|
export type PurchaseSkuData = {
|
||
|
|
priceMap: Map<string, string>
|
||
|
|
liveList: string[]
|
||
|
|
expireList: string[]
|
||
|
|
}
|
||
|
|
|
||
|
|
export function parsePurchaseSkuList(kind: PurchaseKind, skuList: ProductItem['skus']): PurchaseSkuData {
|
||
|
|
if (!skuList?.length) {
|
||
|
|
throw new Error('没有套餐数据')
|
||
|
|
}
|
||
|
|
|
||
|
|
const priceMap = new Map<string, string>()
|
||
|
|
const liveSet = new Set<number>()
|
||
|
|
const expireSet = new Set<number>()
|
||
|
|
|
||
|
|
for (const sku of skuList) {
|
||
|
|
const params = new URLSearchParams(sku.code)
|
||
|
|
const mode = params.get('mode')
|
||
|
|
const live = Number(params.get('live') || '0')
|
||
|
|
const expire = Number(params.get('expire') || '0')
|
||
|
|
|
||
|
|
if (live > 0) {
|
||
|
|
liveSet.add(live)
|
||
|
|
}
|
||
|
|
|
||
|
|
if (kind === 'short') {
|
||
|
|
if (mode === 'time' && expire > 0) {
|
||
|
|
expireSet.add(expire)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else if (expire > 0) {
|
||
|
|
expireSet.add(expire)
|
||
|
|
}
|
||
|
|
|
||
|
|
priceMap.set(sku.code, sku.price)
|
||
|
|
}
|
||
|
|
|
||
|
|
return {
|
||
|
|
priceMap,
|
||
|
|
liveList: Array.from(liveSet).sort((a, b) => a - b).map(String),
|
||
|
|
expireList: Array.from(expireSet).sort((a, b) => a - b).map(String),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export function getPurchaseSkuPrice(priceMap: Map<string, string>, props: {
|
||
|
|
mode: PurchaseMode
|
||
|
|
live: string
|
||
|
|
expire: string
|
||
|
|
}) {
|
||
|
|
const params = new URLSearchParams()
|
||
|
|
params.set('mode', props.mode === '1' ? 'time' : 'quota')
|
||
|
|
params.set('live', props.live || '0')
|
||
|
|
params.set('expire', props.mode === '1' ? props.expire || '0' : '0')
|
||
|
|
return priceMap.get(params.toString())
|
||
|
|
}
|
||
|
|
|
||
|
|
export function formatPurchaseLiveLabel(live: string, kind: PurchaseKind) {
|
||
|
|
const minutes = Number(live)
|
||
|
|
|
||
|
|
if (kind === 'long') {
|
||
|
|
return `${minutes / 60} 小时`
|
||
|
|
}
|
||
|
|
|
||
|
|
if (minutes % 60 === 0) {
|
||
|
|
return `${minutes / 60} 小时`
|
||
|
|
}
|
||
|
|
|
||
|
|
return `${minutes} 分钟`
|
||
|
|
}
|