修复 sku 缺失显示问题
This commit is contained in:
@@ -1,8 +1,18 @@
|
||||
import {ProductItem} from '@/actions/product'
|
||||
import {PurchaseKind, PurchaseMode} from './resource'
|
||||
|
||||
export type PurchaseSkuItem = {
|
||||
code: string
|
||||
mode: PurchaseMode
|
||||
live: string
|
||||
expire: string
|
||||
price: string
|
||||
}
|
||||
|
||||
export type PurchaseSkuData = {
|
||||
items: PurchaseSkuItem[]
|
||||
priceMap: Map<string, string>
|
||||
modeList: PurchaseMode[]
|
||||
liveList: string[]
|
||||
expireList: string[]
|
||||
}
|
||||
@@ -12,40 +22,82 @@ export function parsePurchaseSkuList(kind: PurchaseKind, skuList: ProductItem['s
|
||||
throw new Error('没有套餐数据')
|
||||
}
|
||||
|
||||
const items: PurchaseSkuItem[] = []
|
||||
const priceMap = new Map<string, string>()
|
||||
const modeSet = new Set<PurchaseMode>()
|
||||
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 mode = parsePurchaseSkuMode(params.get('mode'))
|
||||
const live = Number(params.get('live') || '0')
|
||||
const expire = Number(params.get('expire') || '0')
|
||||
|
||||
if (live > 0) {
|
||||
liveSet.add(live)
|
||||
if (!mode || live <= 0) {
|
||||
continue
|
||||
}
|
||||
|
||||
const liveValue = String(live)
|
||||
const expireValue = mode === '1' ? String(expire || '0') : '0'
|
||||
const code = getPurchaseSkuKey({
|
||||
mode,
|
||||
live: liveValue,
|
||||
expire: expireValue,
|
||||
})
|
||||
|
||||
items.push({
|
||||
code,
|
||||
mode,
|
||||
live: liveValue,
|
||||
expire: expireValue,
|
||||
price: sku.price,
|
||||
})
|
||||
priceMap.set(code, sku.price)
|
||||
modeSet.add(mode)
|
||||
|
||||
liveSet.add(live)
|
||||
|
||||
if (kind === 'short') {
|
||||
if (mode === 'time' && expire > 0) {
|
||||
if (mode === '1' && expire > 0) {
|
||||
expireSet.add(expire)
|
||||
}
|
||||
}
|
||||
else if (expire > 0) {
|
||||
expireSet.add(expire)
|
||||
}
|
||||
}
|
||||
|
||||
priceMap.set(sku.code, sku.price)
|
||||
if (items.length === 0) {
|
||||
throw new Error('没有可用的套餐数据')
|
||||
}
|
||||
|
||||
return {
|
||||
items,
|
||||
priceMap,
|
||||
liveList: Array.from(liveSet).sort((a, b) => a - b).map(String),
|
||||
expireList: Array.from(expireSet).sort((a, b) => a - b).map(String),
|
||||
modeList: (['2', '1'] as const).filter(mode => modeSet.has(mode)),
|
||||
liveList: sortNumericValues(liveSet),
|
||||
expireList: sortNumericValues(expireSet),
|
||||
}
|
||||
}
|
||||
|
||||
export function getPurchaseSkuPrice(priceMap: Map<string, string>, props: {
|
||||
function parsePurchaseSkuMode(mode: string | null): PurchaseMode | null {
|
||||
if (mode === 'time') {
|
||||
return '1'
|
||||
}
|
||||
|
||||
if (mode === 'quota') {
|
||||
return '2'
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
function sortNumericValues(values: Iterable<number>) {
|
||||
return Array.from(values).sort((a, b) => a - b).map(String)
|
||||
}
|
||||
|
||||
export function getPurchaseSkuKey(props: {
|
||||
mode: PurchaseMode
|
||||
live: string
|
||||
expire: string
|
||||
@@ -54,7 +106,48 @@ export function getPurchaseSkuPrice(priceMap: Map<string, string>, props: {
|
||||
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())
|
||||
return params.toString()
|
||||
}
|
||||
|
||||
export function getAvailablePurchaseLives(skuData: PurchaseSkuData, props: {
|
||||
mode: PurchaseMode
|
||||
expire?: string
|
||||
}) {
|
||||
return sortNumericValues(new Set(
|
||||
skuData.items
|
||||
.filter(item => item.mode === props.mode)
|
||||
.filter(item => !props.expire || item.expire === props.expire)
|
||||
.map(item => Number(item.live)),
|
||||
))
|
||||
}
|
||||
|
||||
export function getAvailablePurchaseExpires(skuData: PurchaseSkuData, props: {
|
||||
mode: PurchaseMode
|
||||
live?: string
|
||||
}) {
|
||||
return sortNumericValues(new Set(
|
||||
skuData.items
|
||||
.filter(item => item.mode === props.mode)
|
||||
.filter(item => !props.live || item.live === props.live)
|
||||
.filter(item => item.expire !== '0')
|
||||
.map(item => Number(item.expire)),
|
||||
))
|
||||
}
|
||||
|
||||
export function hasPurchaseSku(skuData: PurchaseSkuData, props: {
|
||||
mode: PurchaseMode
|
||||
live: string
|
||||
expire: string
|
||||
}) {
|
||||
return skuData.priceMap.has(getPurchaseSkuKey(props))
|
||||
}
|
||||
|
||||
export function getPurchaseSkuPrice(priceMap: Map<string, string>, props: {
|
||||
mode: PurchaseMode
|
||||
live: string
|
||||
expire: string
|
||||
}) {
|
||||
return priceMap.get(getPurchaseSkuKey(props))
|
||||
}
|
||||
|
||||
export function formatPurchaseLiveLabel(live: string, kind: PurchaseKind) {
|
||||
|
||||
Reference in New Issue
Block a user