购买套餐添加count_min字段

This commit is contained in:
Eamon-meng
2026-04-20 15:36:36 +08:00
parent 8f8def3a87
commit 74d53c619d
10 changed files with 92 additions and 22 deletions

View File

@@ -10,13 +10,13 @@ export default function Addr({channel}: {
const expired = isBefore(channel.expired_at, new Date()) const expired = isBefore(channel.expired_at, new Date())
return ( return (
<div className={`${expired ? 'text-weak' : ''}`}> <>
<span>{ip}:{port}</span> <span>{ip}:{port}</span>
{expired && ( {expired && (
<Badge variant="secondary"> <Badge className="ml-2 bg-orange-100 text-orange-700 hover:bg-orange-100 dark:bg-orange-900/30 dark:text-orange-400">
</Badge> </Badge>
)} )}
</div> </>
) )
} }

View File

@@ -192,7 +192,7 @@ export default function ChannelsPage(props: ChannelsPageProps) {
<span ></span> <span ></span>
<div className="flex flex-wrap gap-1 max-w-[200px]"> <div className="flex flex-wrap gap-1 max-w-[200px]">
{channel.whitelists.split(',').map((ip, index) => ( {channel.whitelists.split(',').map((ip, index) => (
<Badge key={index} variant="secondary"> <Badge key={index} className="bg-green-100 text-green-700 hover:bg-green-100 dark:bg-green-900/30 dark:text-green-400">
{ip.trim()} {ip.trim()}
</Badge > </Badge >
))} ))}
@@ -201,7 +201,7 @@ export default function ChannelsPage(props: ChannelsPageProps) {
) : hasAuth ? ( ) : hasAuth ? (
<div className="flex flex-col"> <div className="flex flex-col">
<span></span> <span></span>
<Badge variant="secondary"> <Badge className="bg-blue-100 text-blue-700 hover:bg-blue-100 dark:bg-blue-900/30 dark:text-blue-400">
{channel.username}:{channel.password} {channel.username}:{channel.password}
</Badge > </Badge >
</div> </div>

View File

@@ -30,6 +30,7 @@ export default function Purchase() {
const res = profile const res = profile
? await listProduct({}) ? await listProduct({})
: await listProductHome({}) : await listProductHome({})
console.log(res, 'res')
if (res.success) { if (res.success) {
setProductList(res.data) setProductList(res.data)

View File

@@ -3,18 +3,18 @@ import {FormField} from '@/components/ui/form'
import {RadioGroup} from '@/components/ui/radio-group' import {RadioGroup} from '@/components/ui/radio-group'
import FormOption from '@/components/composites/purchase/option' import FormOption from '@/components/composites/purchase/option'
import {Schema} from '@/components/composites/purchase/long/form' import {Schema} from '@/components/composites/purchase/long/form'
import {useEffect} from 'react' import {useEffect, useMemo} from 'react'
import {useFormContext, useWatch} from 'react-hook-form' import {useFormContext, useWatch} from 'react-hook-form'
import {Card} from '@/components/ui/card' import {Card} from '@/components/ui/card'
import {BillingMethodField} from '../shared/billing-method-field' import {BillingMethodField} from '../shared/billing-method-field'
import {FeatureList} from '../shared/feature-list' import {FeatureList} from '../shared/feature-list'
import {NumberStepperField} from '../shared/number-stepper-field' import {NumberStepperField} from '../shared/number-stepper-field'
import {getAvailablePurchaseExpires, getAvailablePurchaseLives, getPurchaseSkuPrice, hasPurchaseSku, PurchaseSkuData} from '../shared/sku' import {getAvailablePurchaseExpires, getAvailablePurchaseLives, getPurchaseSkuCountMin, getPurchaseSkuPrice, hasPurchaseSku, PurchaseSkuData} from '../shared/sku'
export default function Center({skuData}: { export default function Center({skuData}: {
skuData: PurchaseSkuData skuData: PurchaseSkuData
}) { }) {
const {setValue} = useFormContext<Schema>() const {setValue, getValues} = useFormContext<Schema>()
const type = useWatch<Schema>({name: 'type'}) as Schema['type'] const type = useWatch<Schema>({name: 'type'}) as Schema['type']
const live = useWatch<Schema>({name: 'live'}) as Schema['live'] const live = useWatch<Schema>({name: 'live'}) as Schema['live']
const expire = useWatch<Schema>({name: 'expire'}) as Schema['expire'] const expire = useWatch<Schema>({name: 'expire'}) as Schema['expire']
@@ -26,6 +26,27 @@ export default function Center({skuData}: {
? getAvailablePurchaseExpires(skuData, {mode: type, live}) ? getAvailablePurchaseExpires(skuData, {mode: type, live})
: [] : []
const currentCountMin = useMemo(() => {
if (!type || !live) return 0
const expireValue = type === '1' ? expire : '0'
return getPurchaseSkuCountMin(skuData, {mode: type, live, expire: expireValue})
}, [type, live, expire, skuData])
useEffect(() => {
if (type === '1') {
const current = getValues('daily_limit')
if (current < currentCountMin) {
setValue('daily_limit', currentCountMin)
}
}
else {
const current = getValues('quota')
if (current < currentCountMin) {
setValue('quota', currentCountMin)
}
}
}, [currentCountMin, type, setValue, getValues])
useEffect(() => { useEffect(() => {
const nextType = modeList.includes(type) ? type : modeList[0] const nextType = modeList.includes(type) ? type : modeList[0]
@@ -146,14 +167,14 @@ export default function Center({skuData}: {
<NumberStepperField <NumberStepperField
name="daily_limit" name="daily_limit"
label="每日提取上限" label="每日提取上限"
min={100} min={currentCountMin || 100}
step={100} step={100}
/> />
) : ( ) : (
<NumberStepperField <NumberStepperField
name="quota" name="quota"
label="IP 购买数量" label="IP 购买数量"
min={500} min={currentCountMin || 500}
step={100} step={100}
/> />
)} )}

View File

@@ -5,7 +5,7 @@ import {Form} from '@/components/ui/form'
import * as z from 'zod' import * as z from 'zod'
import {zodResolver} from '@hookform/resolvers/zod' import {zodResolver} from '@hookform/resolvers/zod'
import {ProductItem} from '@/actions/product' import {ProductItem} from '@/actions/product'
import {getAvailablePurchaseExpires, getAvailablePurchaseLives, parsePurchaseSkuList} from '../shared/sku' import {getAvailablePurchaseExpires, getAvailablePurchaseLives, getPurchaseSkuCountMin, parsePurchaseSkuList} from '../shared/sku'
import {PurchaseSidePanel} from '../shared/side-panel' import {PurchaseSidePanel} from '../shared/side-panel'
const schema = z.object({ const schema = z.object({
@@ -25,6 +25,11 @@ export default function LongForm({skuList}: {skuList: ProductItem['skus']}) {
const defaultExpire = defaultMode === '1' const defaultExpire = defaultMode === '1'
? getAvailablePurchaseExpires(skuData, {mode: defaultMode, live: defaultLive})[0] || '0' ? getAvailablePurchaseExpires(skuData, {mode: defaultMode, live: defaultLive})[0] || '0'
: '0' : '0'
const defaultCountMin = getPurchaseSkuCountMin(skuData, {
mode: defaultMode,
live: defaultLive,
expire: defaultExpire,
})
const form = useForm<Schema>({ const form = useForm<Schema>({
resolver: zodResolver(schema), resolver: zodResolver(schema),
@@ -32,8 +37,8 @@ export default function LongForm({skuList}: {skuList: ProductItem['skus']}) {
type: defaultMode, type: defaultMode,
live: defaultLive, live: defaultLive,
expire: defaultExpire, expire: defaultExpire,
quota: 500, quota: defaultMode === '2' ? Math.max(defaultCountMin, 500) : 500,
daily_limit: 100, daily_limit: defaultMode === '1' ? Math.max(defaultCountMin, 100) : 100,
pay_type: 'balance', // 余额支付 pay_type: 'balance', // 余额支付
}, },
}) })

View File

@@ -49,6 +49,9 @@ export function NumberStepperField(props: NumberStepperFieldProps) {
className="w-40 h-10 border border-gray-200 rounded-sm text-center" className="w-40 h-10 border border-gray-200 rounded-sm text-center"
min={props.min} min={props.min}
step={props.step} step={props.step}
onInvalid={(e) => {
e.preventDefault()
}}
onBlur={(event) => { onBlur={(event) => {
field.onBlur() field.onBlur()
const nextValue = Number(event.target.value) const nextValue = Number(event.target.value)

View File

@@ -7,11 +7,13 @@ export type PurchaseSkuItem = {
live: string live: string
expire: string expire: string
price: string price: string
count_min: number
} }
export type PurchaseSkuData = { export type PurchaseSkuData = {
items: PurchaseSkuItem[] items: PurchaseSkuItem[]
priceMap: Map<string, string> priceMap: Map<string, string>
countMinMap: Map<string, number>
modeList: PurchaseMode[] modeList: PurchaseMode[]
liveList: string[] liveList: string[]
expireList: string[] expireList: string[]
@@ -24,6 +26,7 @@ export function parsePurchaseSkuList(kind: PurchaseKind, skuList: ProductItem['s
const items: PurchaseSkuItem[] = [] const items: PurchaseSkuItem[] = []
const priceMap = new Map<string, string>() const priceMap = new Map<string, string>()
const countMinMap = new Map<string, number>()
const modeSet = new Set<PurchaseMode>() const modeSet = new Set<PurchaseMode>()
const liveSet = new Set<number>() const liveSet = new Set<number>()
const expireSet = new Set<number>() const expireSet = new Set<number>()
@@ -45,6 +48,8 @@ export function parsePurchaseSkuList(kind: PurchaseKind, skuList: ProductItem['s
live: liveValue, live: liveValue,
expire: expireValue, expire: expireValue,
}) })
const countMin = typeof sku.count_min === 'number' ? sku.count_min : Number(sku.count_min) || 0
countMinMap.set(code, countMin)
items.push({ items.push({
code, code,
@@ -52,6 +57,7 @@ export function parsePurchaseSkuList(kind: PurchaseKind, skuList: ProductItem['s
live: liveValue, live: liveValue,
expire: expireValue, expire: expireValue,
price: sku.price, price: sku.price,
count_min: countMin,
}) })
priceMap.set(code, sku.price) priceMap.set(code, sku.price)
modeSet.add(mode) modeSet.add(mode)
@@ -75,6 +81,7 @@ export function parsePurchaseSkuList(kind: PurchaseKind, skuList: ProductItem['s
return { return {
items, items,
priceMap, priceMap,
countMinMap,
modeList: (['2', '1'] as const).filter(mode => modeSet.has(mode)), modeList: (['2', '1'] as const).filter(mode => modeSet.has(mode)),
liveList: sortNumericValues(liveSet), liveList: sortNumericValues(liveSet),
expireList: sortNumericValues(expireSet), expireList: sortNumericValues(expireSet),
@@ -163,3 +170,11 @@ export function formatPurchaseLiveLabel(live: string, kind: PurchaseKind) {
return `${minutes} 分钟` return `${minutes} 分钟`
} }
export function getPurchaseSkuCountMin(
skuData: PurchaseSkuData,
props: {mode: PurchaseMode, live: string, expire: string},
): number {
const key = getPurchaseSkuKey(props)
return skuData.countMinMap.get(key) ?? 0
}

View File

@@ -2,21 +2,22 @@
import {FormField} from '@/components/ui/form' import {FormField} from '@/components/ui/form'
import {RadioGroup} from '@/components/ui/radio-group' import {RadioGroup} from '@/components/ui/radio-group'
import FormOption from '@/components/composites/purchase/option' import FormOption from '@/components/composites/purchase/option'
import {useEffect} from 'react' import {useEffect, useMemo} from 'react'
import {useFormContext, useWatch} from 'react-hook-form' import {useFormContext, useWatch} from 'react-hook-form'
import {Schema} from '@/components/composites/purchase/short/form' import {Schema} from '@/components/composites/purchase/short/form'
import {Card} from '@/components/ui/card' import {Card} from '@/components/ui/card'
import {BillingMethodField} from '../shared/billing-method-field' import {BillingMethodField} from '../shared/billing-method-field'
import {FeatureList} from '../shared/feature-list' import {FeatureList} from '../shared/feature-list'
import {NumberStepperField} from '../shared/number-stepper-field' import {NumberStepperField} from '../shared/number-stepper-field'
import {getAvailablePurchaseExpires, getAvailablePurchaseLives, getPurchaseSkuPrice, hasPurchaseSku, PurchaseSkuData} from '../shared/sku' import {getAvailablePurchaseExpires, getAvailablePurchaseLives, getPurchaseSkuCountMin, getPurchaseSkuPrice, hasPurchaseSku, PurchaseSkuData} from '../shared/sku'
export default function Center({ export default function Center({
skuData, skuData,
}: { }: {
skuData: PurchaseSkuData skuData: PurchaseSkuData
}) { }) {
const {setValue} = useFormContext<Schema>() // const {setValue} = useFormContext<Schema>()
const {setValue, getValues} = useFormContext<Schema>()
const type = useWatch<Schema>({name: 'type'}) as Schema['type'] const type = useWatch<Schema>({name: 'type'}) as Schema['type']
const live = useWatch<Schema>({name: 'live'}) as Schema['live'] const live = useWatch<Schema>({name: 'live'}) as Schema['live']
const expire = useWatch<Schema>({name: 'expire'}) as Schema['expire'] const expire = useWatch<Schema>({name: 'expire'}) as Schema['expire']
@@ -28,6 +29,25 @@ export default function Center({
? getAvailablePurchaseExpires(skuData, {mode: type, live}) ? getAvailablePurchaseExpires(skuData, {mode: type, live})
: [] : []
const currentCountMin = useMemo(() => {
if (!type || !live) return 0
const expireValue = type === '1' ? expire : '0'
return getPurchaseSkuCountMin(skuData, {mode: type, live, expire: expireValue})
}, [type, live, expire, skuData])
useEffect(() => {
if (type === '1') {
const current = getValues('daily_limit')
if (current < currentCountMin) {
setValue('daily_limit', currentCountMin)
}
}
else {
const current = getValues('quota')
if (current < currentCountMin) {
setValue('quota', currentCountMin)
}
}
}, [currentCountMin, type, setValue, getValues])
useEffect(() => { useEffect(() => {
const nextType = modeList.includes(type) ? type : modeList[0] const nextType = modeList.includes(type) ? type : modeList[0]
@@ -151,14 +171,14 @@ export default function Center({
<NumberStepperField <NumberStepperField
name="daily_limit" name="daily_limit"
label="每日提取上限" label="每日提取上限"
min={2000} min={currentCountMin || 2000}
step={1000} step={1000}
/> />
) : ( ) : (
<NumberStepperField <NumberStepperField
name="quota" name="quota"
label="IP 购买数量" label="IP 购买数量"
min={10000} min={currentCountMin || 10000}
step={5000} step={5000}
/> />
)} )}

View File

@@ -5,7 +5,7 @@ import {Form} from '@/components/ui/form'
import * as z from 'zod' import * as z from 'zod'
import {zodResolver} from '@hookform/resolvers/zod' import {zodResolver} from '@hookform/resolvers/zod'
import {ProductItem} from '@/actions/product' import {ProductItem} from '@/actions/product'
import {getAvailablePurchaseExpires, getAvailablePurchaseLives, parsePurchaseSkuList} from '../shared/sku' import {getAvailablePurchaseExpires, getAvailablePurchaseLives, getPurchaseSkuCountMin, parsePurchaseSkuList} from '../shared/sku'
import {PurchaseSidePanel} from '../shared/side-panel' import {PurchaseSidePanel} from '../shared/side-panel'
const schema = z.object({ const schema = z.object({
@@ -25,15 +25,19 @@ export default function ShortForm({skuList}: {skuList: ProductItem['skus']}) {
const defaultExpire = defaultMode === '1' const defaultExpire = defaultMode === '1'
? getAvailablePurchaseExpires(skuData, {mode: defaultMode, live: defaultLive})[0] || '0' ? getAvailablePurchaseExpires(skuData, {mode: defaultMode, live: defaultLive})[0] || '0'
: '0' : '0'
const defaultCountMin = getPurchaseSkuCountMin(skuData, {
mode: defaultMode,
live: defaultLive,
expire: defaultExpire,
})
const form = useForm<Schema>({ const form = useForm<Schema>({
resolver: zodResolver(schema), resolver: zodResolver(schema),
defaultValues: { defaultValues: {
type: defaultMode, type: defaultMode,
live: defaultLive, live: defaultLive,
expire: defaultExpire, expire: defaultExpire,
quota: 10_000, // >= 10000, quota: defaultMode === '2' ? defaultCountMin || 10000 : 10000,
daily_limit: 2_000, // >= 2000 daily_limit: defaultMode === '1' ? defaultCountMin || 2000 : 2000,
pay_type: 'balance', // 余额支付 pay_type: 'balance', // 余额支付
}, },
}) })

View File

@@ -1,6 +1,7 @@
export type ProductSku = { export type ProductSku = {
id: number id: number
code: string code: string
count_min: number
name: string name: string
price: string price: string
price_min: string price_min: string