修复用户未/授权购买套餐价格计算 & 发布v1.6.0版本

This commit is contained in:
Eamon-meng
2026-04-16 17:46:11 +08:00
parent 5607217625
commit 8b65a1745c
7 changed files with 97 additions and 47 deletions

View File

@@ -1,12 +1,13 @@
'use client'
import {ReactNode, useEffect, useState} from 'react'
import {ReactNode, use, useEffect, useState} from 'react'
import {merge} from '@/lib/utils'
import {Tabs, TabsContent, TabsList, TabsTrigger} from '@/components/ui/tabs'
import LongForm from '@/components/composites/purchase/long/form'
import ShortForm from '@/components/composites/purchase/short/form'
import {usePathname, useRouter, useSearchParams} from 'next/navigation'
import SelfDesc from '@/components/features/self-desc'
import {listProduct, ProductItem} from '@/actions/product'
import {listProduct, listProductHome, ProductItem} from '@/actions/product'
import {useProfileStore} from '@/components/stores/profile'
export type TabType = 'short' | 'long' | 'fixed' | 'custom'
export default function Purchase() {
@@ -22,19 +23,21 @@ export default function Purchase() {
newParams.set('type', tab)
router.push(`${path}?${newParams.toString()}`)
}
const profile = use(useProfileStore(store => store.profile))
useEffect(() => {
const fetchProducts = async () => {
const res = await listProduct({})
const res = profile
? await listProduct({})
: await listProductHome({})
if (res.success) {
setProductList(res.data)
}
}
fetchProducts()
}, [])
}, [profile])
const currentProduct = productList.find(item => item.code === tab)
const currentSkuList = currentProduct?.skus || []
const componentMap: Record<string, React.FC<{skuList: ProductItem['skus']}>> = {
short: ShortForm,
long: LongForm,

View File

@@ -8,7 +8,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} from '@/actions/resource'
import {getPrice, getPriceHome} from '@/actions/resource'
import {ExtraResp} from '@/lib/api'
import {FieldPayment} from '../shared/field-payment'
@@ -25,19 +25,29 @@ export default function Right() {
actual: '0.00',
discounted: '0.00',
})
const profile = use(useProfileStore(store => store.profile))
useEffect(() => {
const price = async () => {
try {
const resp = await getPrice({
type: 2,
long: {
live: Number(live),
mode: Number(mode),
quota: mode === '1' ? Number(dailyLimit) : Number(quota),
expire: mode === '1' ? Number(expire) : undefined,
},
})
const resp = profile
? await getPrice({
type: 2,
long: {
live: Number(live),
mode: Number(mode),
quota: mode === '1' ? Number(dailyLimit) : Number(quota),
expire: mode === '1' ? Number(expire) : undefined,
},
}) : await getPriceHome({
type: 1,
short: {
live: Number(live),
mode: Number(mode),
quota: mode === '1' ? Number(dailyLimit) : Number(quota),
expire: mode === '1' ? Number(expire) : undefined,
},
})
if (!resp.success) {
throw new Error('获取价格失败')
}
@@ -57,7 +67,7 @@ export default function Right() {
}
}
price()
}, [dailyLimit, expire, live, quota, mode])
}, [dailyLimit, expire, live, quota, mode, profile])
const {price, actual: discountedPrice = ''} = priceData
// 计算总折扣价(原价 - 实付价格)

View File

@@ -59,7 +59,7 @@ export default function ShortForm({skuList}: {skuList: ProductItem['skus']}) {
return (
<Form form={form} className="flex flex-col lg:flex-row gap-4">
<Center {...{priceMap, liveList, expireList}}/>
<Right {...{skuList, priceMap}}/>
<Right/>
</Form>
)
}

View File

@@ -8,12 +8,12 @@ import {merge} from '@/lib/utils'
import Pay from '@/components/composites/purchase/pay'
import {useFormContext, useWatch} from 'react-hook-form'
import {Card} from '@/components/ui/card'
import {getPrice} from '@/actions/resource'
import {getPrice, getPriceHome} from '@/actions/resource'
import {ExtraResp} from '@/lib/api'
import {FieldPayment} from '../shared/field-payment'
import {ProductItem} from '@/actions/product'
export default function Right({skuList}: {skuList: ProductItem['skus']}) {
export default function Right() {
const {control} = useFormContext<Schema>()
const method = useWatch({control, name: 'pay_type'})
const live = useWatch({control, name: 'live'})
@@ -26,19 +26,31 @@ export default function Right({skuList}: {skuList: ProductItem['skus']}) {
actual: '0.00',
discounted: '0.00',
})
const profile = use(useProfileStore(store => store.profile))
useEffect(() => {
const price = async () => {
try {
const priceResponse = await getPrice({
type: 1,
short: {
live: Number(live),
mode: Number(mode),
quota: mode === '1' ? Number(dailyLimit) : Number(quota),
expire: mode === '1' ? Number(expire) : undefined,
},
})
const priceResponse = profile
? await getPrice({
type: 1,
short: {
live: Number(live),
mode: Number(mode),
quota: mode === '1' ? Number(dailyLimit) : Number(quota),
expire: mode === '1' ? Number(expire) : undefined,
},
})
: await getPriceHome({
type: 1,
short: {
live: Number(live),
mode: Number(mode),
quota: mode === '1' ? Number(dailyLimit) : Number(quota),
expire: mode === '1' ? Number(expire) : undefined,
},
})
if (!priceResponse.success) {
throw new Error('获取价格失败')
}
@@ -60,7 +72,7 @@ export default function Right({skuList}: {skuList: ProductItem['skus']}) {
}
}
price()
}, [expire, live, quota, mode, dailyLimit])
}, [expire, live, quota, mode, dailyLimit, profile])
const {price, actual: discountedPrice = ''} = priceData