购买套餐添加count_min字段
This commit is contained in:
@@ -10,13 +10,13 @@ export default function Addr({channel}: {
|
||||
const expired = isBefore(channel.expired_at, new Date())
|
||||
|
||||
return (
|
||||
<div className={`${expired ? 'text-weak' : ''}`}>
|
||||
<>
|
||||
<span>{ip}:{port}</span>
|
||||
{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>
|
||||
)}
|
||||
</div>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -192,7 +192,7 @@ export default function ChannelsPage(props: ChannelsPageProps) {
|
||||
<span >白名单</span>
|
||||
<div className="flex flex-wrap gap-1 max-w-[200px]">
|
||||
{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()}
|
||||
</Badge >
|
||||
))}
|
||||
@@ -201,7 +201,7 @@ export default function ChannelsPage(props: ChannelsPageProps) {
|
||||
) : hasAuth ? (
|
||||
<div className="flex flex-col">
|
||||
<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}
|
||||
</Badge >
|
||||
</div>
|
||||
|
||||
@@ -30,6 +30,7 @@ export default function Purchase() {
|
||||
const res = profile
|
||||
? await listProduct({})
|
||||
: await listProductHome({})
|
||||
console.log(res, 'res')
|
||||
|
||||
if (res.success) {
|
||||
setProductList(res.data)
|
||||
|
||||
@@ -3,18 +3,18 @@ import {FormField} from '@/components/ui/form'
|
||||
import {RadioGroup} from '@/components/ui/radio-group'
|
||||
import FormOption from '@/components/composites/purchase/option'
|
||||
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 {Card} from '@/components/ui/card'
|
||||
import {BillingMethodField} from '../shared/billing-method-field'
|
||||
import {FeatureList} from '../shared/feature-list'
|
||||
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}: {
|
||||
skuData: PurchaseSkuData
|
||||
}) {
|
||||
const {setValue} = useFormContext<Schema>()
|
||||
const {setValue, getValues} = useFormContext<Schema>()
|
||||
const type = useWatch<Schema>({name: 'type'}) as Schema['type']
|
||||
const live = useWatch<Schema>({name: 'live'}) as Schema['live']
|
||||
const expire = useWatch<Schema>({name: 'expire'}) as Schema['expire']
|
||||
@@ -26,6 +26,27 @@ export default function Center({skuData}: {
|
||||
? 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(() => {
|
||||
const nextType = modeList.includes(type) ? type : modeList[0]
|
||||
|
||||
@@ -146,14 +167,14 @@ export default function Center({skuData}: {
|
||||
<NumberStepperField
|
||||
name="daily_limit"
|
||||
label="每日提取上限"
|
||||
min={100}
|
||||
min={currentCountMin || 100}
|
||||
step={100}
|
||||
/>
|
||||
) : (
|
||||
<NumberStepperField
|
||||
name="quota"
|
||||
label="IP 购买数量"
|
||||
min={500}
|
||||
min={currentCountMin || 500}
|
||||
step={100}
|
||||
/>
|
||||
)}
|
||||
|
||||
@@ -5,7 +5,7 @@ import {Form} from '@/components/ui/form'
|
||||
import * as z from 'zod'
|
||||
import {zodResolver} from '@hookform/resolvers/zod'
|
||||
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'
|
||||
|
||||
const schema = z.object({
|
||||
@@ -25,6 +25,11 @@ export default function LongForm({skuList}: {skuList: ProductItem['skus']}) {
|
||||
const defaultExpire = defaultMode === '1'
|
||||
? getAvailablePurchaseExpires(skuData, {mode: defaultMode, live: defaultLive})[0] || '0'
|
||||
: '0'
|
||||
const defaultCountMin = getPurchaseSkuCountMin(skuData, {
|
||||
mode: defaultMode,
|
||||
live: defaultLive,
|
||||
expire: defaultExpire,
|
||||
})
|
||||
|
||||
const form = useForm<Schema>({
|
||||
resolver: zodResolver(schema),
|
||||
@@ -32,8 +37,8 @@ export default function LongForm({skuList}: {skuList: ProductItem['skus']}) {
|
||||
type: defaultMode,
|
||||
live: defaultLive,
|
||||
expire: defaultExpire,
|
||||
quota: 500,
|
||||
daily_limit: 100,
|
||||
quota: defaultMode === '2' ? Math.max(defaultCountMin, 500) : 500,
|
||||
daily_limit: defaultMode === '1' ? Math.max(defaultCountMin, 100) : 100,
|
||||
pay_type: 'balance', // 余额支付
|
||||
},
|
||||
})
|
||||
|
||||
@@ -49,6 +49,9 @@ export function NumberStepperField(props: NumberStepperFieldProps) {
|
||||
className="w-40 h-10 border border-gray-200 rounded-sm text-center"
|
||||
min={props.min}
|
||||
step={props.step}
|
||||
onInvalid={(e) => {
|
||||
e.preventDefault()
|
||||
}}
|
||||
onBlur={(event) => {
|
||||
field.onBlur()
|
||||
const nextValue = Number(event.target.value)
|
||||
|
||||
@@ -7,11 +7,13 @@ export type PurchaseSkuItem = {
|
||||
live: string
|
||||
expire: string
|
||||
price: string
|
||||
count_min: number
|
||||
}
|
||||
|
||||
export type PurchaseSkuData = {
|
||||
items: PurchaseSkuItem[]
|
||||
priceMap: Map<string, string>
|
||||
countMinMap: Map<string, number>
|
||||
modeList: PurchaseMode[]
|
||||
liveList: string[]
|
||||
expireList: string[]
|
||||
@@ -24,6 +26,7 @@ export function parsePurchaseSkuList(kind: PurchaseKind, skuList: ProductItem['s
|
||||
|
||||
const items: PurchaseSkuItem[] = []
|
||||
const priceMap = new Map<string, string>()
|
||||
const countMinMap = new Map<string, number>()
|
||||
const modeSet = new Set<PurchaseMode>()
|
||||
const liveSet = new Set<number>()
|
||||
const expireSet = new Set<number>()
|
||||
@@ -45,6 +48,8 @@ export function parsePurchaseSkuList(kind: PurchaseKind, skuList: ProductItem['s
|
||||
live: liveValue,
|
||||
expire: expireValue,
|
||||
})
|
||||
const countMin = typeof sku.count_min === 'number' ? sku.count_min : Number(sku.count_min) || 0
|
||||
countMinMap.set(code, countMin)
|
||||
|
||||
items.push({
|
||||
code,
|
||||
@@ -52,6 +57,7 @@ export function parsePurchaseSkuList(kind: PurchaseKind, skuList: ProductItem['s
|
||||
live: liveValue,
|
||||
expire: expireValue,
|
||||
price: sku.price,
|
||||
count_min: countMin,
|
||||
})
|
||||
priceMap.set(code, sku.price)
|
||||
modeSet.add(mode)
|
||||
@@ -75,6 +81,7 @@ export function parsePurchaseSkuList(kind: PurchaseKind, skuList: ProductItem['s
|
||||
return {
|
||||
items,
|
||||
priceMap,
|
||||
countMinMap,
|
||||
modeList: (['2', '1'] as const).filter(mode => modeSet.has(mode)),
|
||||
liveList: sortNumericValues(liveSet),
|
||||
expireList: sortNumericValues(expireSet),
|
||||
@@ -163,3 +170,11 @@ export function formatPurchaseLiveLabel(live: string, kind: PurchaseKind) {
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@@ -2,21 +2,22 @@
|
||||
import {FormField} from '@/components/ui/form'
|
||||
import {RadioGroup} from '@/components/ui/radio-group'
|
||||
import FormOption from '@/components/composites/purchase/option'
|
||||
import {useEffect} from 'react'
|
||||
import {useEffect, useMemo} from 'react'
|
||||
import {useFormContext, useWatch} from 'react-hook-form'
|
||||
import {Schema} from '@/components/composites/purchase/short/form'
|
||||
import {Card} from '@/components/ui/card'
|
||||
import {BillingMethodField} from '../shared/billing-method-field'
|
||||
import {FeatureList} from '../shared/feature-list'
|
||||
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,
|
||||
}: {
|
||||
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 live = useWatch<Schema>({name: 'live'}) as Schema['live']
|
||||
const expire = useWatch<Schema>({name: 'expire'}) as Schema['expire']
|
||||
@@ -28,6 +29,25 @@ export default function Center({
|
||||
? 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(() => {
|
||||
const nextType = modeList.includes(type) ? type : modeList[0]
|
||||
|
||||
@@ -151,14 +171,14 @@ export default function Center({
|
||||
<NumberStepperField
|
||||
name="daily_limit"
|
||||
label="每日提取上限"
|
||||
min={2000}
|
||||
min={currentCountMin || 2000}
|
||||
step={1000}
|
||||
/>
|
||||
) : (
|
||||
<NumberStepperField
|
||||
name="quota"
|
||||
label="IP 购买数量"
|
||||
min={10000}
|
||||
min={currentCountMin || 10000}
|
||||
step={5000}
|
||||
/>
|
||||
)}
|
||||
|
||||
@@ -5,7 +5,7 @@ import {Form} from '@/components/ui/form'
|
||||
import * as z from 'zod'
|
||||
import {zodResolver} from '@hookform/resolvers/zod'
|
||||
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'
|
||||
|
||||
const schema = z.object({
|
||||
@@ -25,15 +25,19 @@ export default function ShortForm({skuList}: {skuList: ProductItem['skus']}) {
|
||||
const defaultExpire = defaultMode === '1'
|
||||
? getAvailablePurchaseExpires(skuData, {mode: defaultMode, live: defaultLive})[0] || '0'
|
||||
: '0'
|
||||
|
||||
const defaultCountMin = getPurchaseSkuCountMin(skuData, {
|
||||
mode: defaultMode,
|
||||
live: defaultLive,
|
||||
expire: defaultExpire,
|
||||
})
|
||||
const form = useForm<Schema>({
|
||||
resolver: zodResolver(schema),
|
||||
defaultValues: {
|
||||
type: defaultMode,
|
||||
live: defaultLive,
|
||||
expire: defaultExpire,
|
||||
quota: 10_000, // >= 10000,
|
||||
daily_limit: 2_000, // >= 2000
|
||||
quota: defaultMode === '2' ? defaultCountMin || 10000 : 10000,
|
||||
daily_limit: defaultMode === '1' ? defaultCountMin || 2000 : 2000,
|
||||
pay_type: 'balance', // 余额支付
|
||||
},
|
||||
})
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
export type ProductSku = {
|
||||
id: number
|
||||
code: string
|
||||
count_min: number
|
||||
name: string
|
||||
price: string
|
||||
price_min: string
|
||||
|
||||
Reference in New Issue
Block a user