import {ProductItem} from '@/actions/product' import {PurchaseKind, PurchaseMode} from './resource' export type PurchaseSkuData = { priceMap: Map liveList: string[] expireList: string[] } export function parsePurchaseSkuList(kind: PurchaseKind, skuList: ProductItem['skus']): PurchaseSkuData { if (!skuList?.length) { throw new Error('没有套餐数据') } const priceMap = new Map() const liveSet = new Set() const expireSet = new Set() 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, 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} 分钟` }