修改购买套餐调用接口后端计算价格 & 重置包时/包量初始化数量
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
'use client'
|
||||
import {Suspense, use, useContext, useMemo} from 'react'
|
||||
import {Suspense, use, useContext, useEffect, useMemo, useState} from 'react'
|
||||
import {PurchaseFormContext} from '@/components/composites/purchase/short/form'
|
||||
import {RadioGroup} from '@/components/ui/radio-group'
|
||||
import {FormField} from '@/components/ui/form'
|
||||
@@ -17,6 +17,7 @@ import {merge} from '@/lib/utils'
|
||||
import {useFormContext, useWatch} from 'react-hook-form'
|
||||
import {Schema} from '@/components/composites/purchase/long/form'
|
||||
import {Card} from '@/components/ui/card'
|
||||
import {getPrice, CreateResourceReq} from '@/actions/resource'
|
||||
|
||||
export default function Right() {
|
||||
const {control} = useFormContext<Schema>()
|
||||
@@ -27,21 +28,37 @@ export default function Right() {
|
||||
const expire = useWatch({control, name: 'expire'})
|
||||
const dailyLimit = useWatch({control, name: 'daily_limit'})
|
||||
|
||||
const price = useMemo(() => {
|
||||
const base = {
|
||||
1: 30,
|
||||
4: 80,
|
||||
8: 120,
|
||||
12: 180,
|
||||
24: 350,
|
||||
}[live]
|
||||
const factor = {
|
||||
1: Number(expire) * dailyLimit,
|
||||
2: quota,
|
||||
}[mode]
|
||||
return (base * factor / 100).toFixed(2)
|
||||
}, [dailyLimit, expire, live, quota, mode])
|
||||
const [price, setPrice] = useState<string>('0.00')
|
||||
|
||||
useEffect(() => {
|
||||
const price = async () => {
|
||||
const params: CreateResourceReq = {
|
||||
type: 2,
|
||||
long: {
|
||||
live: Number(live),
|
||||
mode: Number(mode),
|
||||
quota: quota,
|
||||
expire: Number(mode) === 1 ? Number(expire) : undefined,
|
||||
},
|
||||
}
|
||||
console.log(params, 'params')
|
||||
|
||||
try {
|
||||
const priceValue = await getPrice(params)
|
||||
if (priceValue.success && priceValue.data?.price) {
|
||||
setPrice(priceValue.data.price)
|
||||
}
|
||||
else {
|
||||
setPrice('0.00')
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
console.error('获取价格失败:', error)
|
||||
setPrice('0.00')
|
||||
}
|
||||
}
|
||||
price()
|
||||
}, [dailyLimit, expire, live, quota, mode])
|
||||
return (
|
||||
<Card className={merge(
|
||||
`flex-none basis-90 p-6 flex flex-col gap-6 relative`,
|
||||
|
||||
Reference in New Issue
Block a user