chore(points): align types/api with final backend contract - apps/web/src/pages/subscription/hooks/useSubscription.ts

This commit is contained in:
2026-09-16 10:19:22 +08:00
parent 7e9a6ff358
commit 875b54df7f
@@ -1,3 +1,8 @@
/**
* 订阅管理 Hook
* 封装订阅信息查询、套餐变更、自动续费切换、取消订阅等逻辑
* 对齐最终契约(plan_id=free/monthly/quarterly/yearly + billing_cycle=monthly/yearly)
*/
import { useState, useEffect, useCallback } from "react"
import { message } from "antd"
import {
@@ -5,20 +10,14 @@ import {
changePlan,
toggleAutoRenew,
cancelSubscription,
type SubscriptionInfo,
type PlanType,
type BillingCycle,
} from "@/api/subscription"
import type { SubscriptionInfo, PlanId, BillingCycle } from "@/api/subscription/types"
/**
* 订阅管理 Hook
* 封装订阅信息查询、套餐变更、自动续费切换、取消订阅等逻辑
*/
export function useSubscription() {
const [subscription, setSubscription] = useState<SubscriptionInfo | null>(null)
const [loading, setLoading] = useState(true)
const [submitting, setSubmitting] = useState(false)
const [selectedPlan, setSelectedPlan] = useState<PlanType>("standard")
const [selectedPlan, setSelectedPlan] = useState<PlanId>("monthly")
const [billingCycle, setBillingCycle] = useState<BillingCycle>("monthly")
const loadSubscription = useCallback(async () => {
@@ -26,6 +25,7 @@ export function useSubscription() {
const data = await getCurrentSubscription()
setSubscription(data)
setSelectedPlan(data.plan_id)
setBillingCycle(data.billing_cycle)
} catch (err: unknown) {
if (!(err as { __msgShown?: boolean })?.__msgShown) message.error("获取订阅信息失败")
} finally {
@@ -37,45 +37,41 @@ export function useSubscription() {
loadSubscription()
}, [loadSubscription])
const handleUpgrade = useCallback(async () => {
if (!subscription) return
const executeChangePlan = useCallback(async () => {
if (!subscription) return false
if (selectedPlan === subscription.plan_id && billingCycle === subscription.billing_cycle) {
message.info("当前已是该套餐")
return
return false
}
// 由调用方决定是否弹确认框
}, [subscription, selectedPlan, billingCycle])
const executeChangePlan = useCallback(async () => {
try {
setSubmitting(true)
const res = await changePlan({
target_plan_id: selectedPlan,
billing_cycle: billingCycle,
})
const res = await changePlan({ target_plan_id: selectedPlan, billing_cycle: billingCycle })
if (res.success) {
message.success(res.message)
setSubscription(res.new_subscription ?? null)
return true
} else {
message.error(res.message)
return false
}
} catch (err: unknown) {
if (!(err as { __msgShown?: boolean })?.__msgShown) message.error("套餐变更失败,请重试")
return false
} finally {
setSubmitting(false)
}
}, [selectedPlan, billingCycle])
}, [subscription, selectedPlan, billingCycle])
const handleToggleAutoRenew = useCallback(
async (enabled: boolean) => {
try {
const res = await toggleAutoRenew(enabled)
const res = await toggleAutoRenew({ enabled })
message.success(res.message)
if (subscription) {
setSubscription({ ...subscription, auto_renew: enabled })
}
if (subscription) setSubscription({ ...subscription, auto_renew: enabled })
return true
} catch (err: unknown) {
if (!(err as { __msgShown?: boolean })?.__msgShown) message.error("操作失败")
return false
}
},
[subscription],
@@ -93,7 +89,6 @@ export function useSubscription() {
}, [])
return {
// 状态
subscription,
loading,
submitting,
@@ -101,9 +96,7 @@ export function useSubscription() {
billingCycle,
setSelectedPlan,
setBillingCycle,
// 操作
loadSubscription,
handleUpgrade,
executeChangePlan,
handleToggleAutoRenew,
handleCancel,