🔧 chore: Unify subscription plan status toggle with PATCH endpoint

Replace separate enable/disable flows with a single PATCH API that updates the enabled flag.
Update frontend hooks and table actions to call the unified endpoint and keep UI behavior consistent.
Introduce a minimal admin controller handler and route for the status update.
This commit is contained in:
t0ng7u
2026-01-31 14:27:01 +08:00
parent 28c5feb570
commit 2297af731c
11 changed files with 293 additions and 133 deletions

View File

@@ -106,6 +106,16 @@ func GetJsonString(data any) string {
return string(b)
}
// NormalizeBillingPreference clamps the billing preference to valid values.
func NormalizeBillingPreference(pref string) string {
switch strings.TrimSpace(pref) {
case "subscription_first", "wallet_first", "subscription_only", "wallet_only":
return strings.TrimSpace(pref)
default:
return "subscription_first"
}
}
// MaskEmail masks a user email to prevent PII leakage in logs
// Returns "***masked***" if email is empty, otherwise shows only the domain part
func MaskEmail(email string) string {

View File

@@ -1,7 +1,6 @@
package controller
import (
"encoding/json"
"errors"
"strconv"
"strings"
@@ -23,27 +22,6 @@ type BillingPreferenceRequest struct {
BillingPreference string `json:"billing_preference"`
}
func normalizeBillingPreference(pref string) string {
switch strings.TrimSpace(pref) {
case "subscription_first", "wallet_first", "subscription_only", "wallet_only":
return strings.TrimSpace(pref)
default:
return "subscription_first"
}
}
func normalizeQuotaResetPeriod(period string) string {
switch strings.TrimSpace(period) {
case model.SubscriptionResetDaily,
model.SubscriptionResetWeekly,
model.SubscriptionResetMonthly,
model.SubscriptionResetCustom:
return strings.TrimSpace(period)
default:
return model.SubscriptionResetNever
}
}
// ---- User APIs ----
func GetSubscriptionPlans(c *gin.Context) {
@@ -66,7 +44,7 @@ func GetSubscriptionPlans(c *gin.Context) {
func GetSubscriptionSelf(c *gin.Context) {
userId := c.GetInt("id")
settingMap, _ := model.GetUserSetting(userId, false)
pref := normalizeBillingPreference(settingMap.BillingPreference)
pref := common.NormalizeBillingPreference(settingMap.BillingPreference)
// Get all subscriptions (including expired)
allSubscriptions, err := model.GetAllUserSubscriptions(userId)
@@ -94,7 +72,7 @@ func UpdateSubscriptionPreference(c *gin.Context) {
common.ApiErrorMsg(c, "参数错误")
return
}
pref := normalizeBillingPreference(req.BillingPreference)
pref := common.NormalizeBillingPreference(req.BillingPreference)
user, err := model.GetUserById(userId, true)
if err != nil {
@@ -156,7 +134,7 @@ func AdminCreateSubscriptionPlan(c *gin.Context) {
if req.Plan.DurationValue <= 0 && req.Plan.DurationUnit != model.SubscriptionDurationCustom {
req.Plan.DurationValue = 1
}
req.Plan.QuotaResetPeriod = normalizeQuotaResetPeriod(req.Plan.QuotaResetPeriod)
req.Plan.QuotaResetPeriod = model.NormalizeResetPeriod(req.Plan.QuotaResetPeriod)
if req.Plan.QuotaResetPeriod == model.SubscriptionResetCustom && req.Plan.QuotaResetCustomSeconds <= 0 {
common.ApiErrorMsg(c, "自定义重置周期需大于0秒")
return
@@ -223,7 +201,7 @@ func AdminUpdateSubscriptionPlan(c *gin.Context) {
if req.Plan.DurationValue <= 0 && req.Plan.DurationUnit != model.SubscriptionDurationCustom {
req.Plan.DurationValue = 1
}
req.Plan.QuotaResetPeriod = normalizeQuotaResetPeriod(req.Plan.QuotaResetPeriod)
req.Plan.QuotaResetPeriod = model.NormalizeResetPeriod(req.Plan.QuotaResetPeriod)
if req.Plan.QuotaResetPeriod == model.SubscriptionResetCustom && req.Plan.QuotaResetCustomSeconds <= 0 {
common.ApiErrorMsg(c, "自定义重置周期需大于0秒")
return
@@ -282,14 +260,22 @@ func AdminUpdateSubscriptionPlan(c *gin.Context) {
common.ApiSuccess(c, nil)
}
func AdminDeleteSubscriptionPlan(c *gin.Context) {
type AdminUpdateSubscriptionPlanStatusRequest struct {
Enabled *bool `json:"enabled"`
}
func AdminUpdateSubscriptionPlanStatus(c *gin.Context) {
id, _ := strconv.Atoi(c.Param("id"))
if id <= 0 {
common.ApiErrorMsg(c, "无效的ID")
return
}
// best practice: disable instead of hard delete to avoid breaking past orders
if err := model.DB.Model(&model.SubscriptionPlan{}).Where("id = ?", id).Update("enabled", false).Error; err != nil {
var req AdminUpdateSubscriptionPlanStatusRequest
if err := c.ShouldBindJSON(&req); err != nil || req.Enabled == nil {
common.ApiErrorMsg(c, "参数错误")
return
}
if err := model.DB.Model(&model.SubscriptionPlan{}).Where("id = ?", id).Update("enabled", *req.Enabled).Error; err != nil {
common.ApiError(c, err)
return
}
@@ -323,7 +309,7 @@ func AdminListUserSubscriptions(c *gin.Context) {
common.ApiErrorMsg(c, "无效的用户ID")
return
}
subs, err := model.AdminListUserSubscriptions(userId)
subs, err := model.GetAllUserSubscriptions(userId)
if err != nil {
common.ApiError(c, err)
return
@@ -381,10 +367,3 @@ func AdminDeleteUserSubscription(c *gin.Context) {
}
common.ApiSuccess(c, nil)
}
// ---- Helper: serialize provider payload safely ----
func jsonString(v any) string {
b, _ := json.Marshal(v)
return string(b)
}

View File

@@ -118,7 +118,7 @@ func SubscriptionEpayNotify(c *gin.Context) {
LockOrder(verifyInfo.ServiceTradeNo)
defer UnlockOrder(verifyInfo.ServiceTradeNo)
if err := model.CompleteSubscriptionOrder(verifyInfo.ServiceTradeNo, jsonString(verifyInfo)); err != nil {
if err := model.CompleteSubscriptionOrder(verifyInfo.ServiceTradeNo, common.GetJsonString(verifyInfo)); err != nil {
// do not fail webhook response after signature verified
return
}
@@ -145,7 +145,7 @@ func SubscriptionEpayReturn(c *gin.Context) {
if verifyInfo.TradeStatus == epay.StatusTradeSuccess {
LockOrder(verifyInfo.ServiceTradeNo)
defer UnlockOrder(verifyInfo.ServiceTradeNo)
_ = model.CompleteSubscriptionOrder(verifyInfo.ServiceTradeNo, jsonString(verifyInfo))
_ = model.CompleteSubscriptionOrder(verifyInfo.ServiceTradeNo, common.GetJsonString(verifyInfo))
c.Redirect(http.StatusFound, system_setting.ServerAddress+"/console/topup?pay=success")
return
}

View File

@@ -302,7 +302,7 @@ func handleCheckoutCompleted(c *gin.Context, event *CreemWebhookEvent) {
// Try complete subscription order first
LockOrder(referenceId)
defer UnlockOrder(referenceId)
if err := model.CompleteSubscriptionOrder(referenceId, jsonString(event)); err == nil {
if err := model.CompleteSubscriptionOrder(referenceId, common.GetJsonString(event)); err == nil {
c.Status(http.StatusOK)
return
} else if err != nil && !errors.Is(err, model.ErrSubscriptionOrderNotFound) {

View File

@@ -176,7 +176,7 @@ func sessionCompleted(event stripe.Event) {
"currency": strings.ToUpper(event.GetObjectValue("currency")),
"event_type": string(event.Type),
}
if err := model.CompleteSubscriptionOrder(referenceId, jsonString(payload)); err == nil {
if err := model.CompleteSubscriptionOrder(referenceId, common.GetJsonString(payload)); err == nil {
return
} else if err != nil && !errors.Is(err, model.ErrSubscriptionOrderNotFound) {
log.Println("complete subscription order failed:", err.Error(), referenceId)

View File

@@ -344,7 +344,7 @@ func calcPlanEndTime(start time.Time, plan *SubscriptionPlan) (int64, error) {
}
}
func normalizeResetPeriod(period string) string {
func NormalizeResetPeriod(period string) string {
switch strings.TrimSpace(period) {
case SubscriptionResetDaily, SubscriptionResetWeekly, SubscriptionResetMonthly, SubscriptionResetCustom:
return strings.TrimSpace(period)
@@ -357,7 +357,7 @@ func calcNextResetTime(base time.Time, plan *SubscriptionPlan, endUnix int64) in
if plan == nil {
return 0
}
period := normalizeResetPeriod(plan.QuotaResetPeriod)
period := NormalizeResetPeriod(plan.QuotaResetPeriod)
if period == SubscriptionResetNever {
return 0
}
@@ -689,13 +689,6 @@ func buildSubscriptionSummaries(subs []UserSubscription) ([]SubscriptionSummary,
return result, nil
}
// ---- Admin helpers for managing user subscriptions ----
// AdminListUserSubscriptions lists all subscriptions (including expired) for a user.
func AdminListUserSubscriptions(userId int) ([]SubscriptionSummary, error) {
return GetAllUserSubscriptions(userId)
}
// AdminInvalidateUserSubscription marks a user subscription as cancelled and ends it immediately.
func AdminInvalidateUserSubscription(userSubscriptionId int) error {
if userSubscriptionId <= 0 {
@@ -786,7 +779,7 @@ func maybeResetSubscriptionItemWithPlanTx(tx *gorm.DB, item *UserSubscriptionIte
if item.NextResetTime > 0 && item.NextResetTime > now {
return nil
}
if normalizeResetPeriod(plan.QuotaResetPeriod) == SubscriptionResetNever {
if NormalizeResetPeriod(plan.QuotaResetPeriod) == SubscriptionResetNever {
return nil
}

View File

@@ -137,7 +137,7 @@ func SetApiRouter(router *gin.Engine) {
subscriptionAdminRoute.GET("/plans", controller.AdminListSubscriptionPlans)
subscriptionAdminRoute.POST("/plans", controller.AdminCreateSubscriptionPlan)
subscriptionAdminRoute.PUT("/plans/:id", controller.AdminUpdateSubscriptionPlan)
subscriptionAdminRoute.DELETE("/plans/:id", controller.AdminDeleteSubscriptionPlan)
subscriptionAdminRoute.PATCH("/plans/:id", controller.AdminUpdateSubscriptionPlanStatus)
subscriptionAdminRoute.POST("/bind", controller.AdminBindSubscription)
// User subscription management (admin)

View File

@@ -4,6 +4,7 @@ import (
"fmt"
"net/http"
"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/logger"
"github.com/QuantumNous/new-api/model"
relaycommon "github.com/QuantumNous/new-api/relay/common"
@@ -16,15 +17,6 @@ const (
BillingSourceSubscription = "subscription"
)
func normalizeBillingPreference(pref string) string {
switch pref {
case "subscription_first", "wallet_first", "subscription_only", "wallet_only":
return pref
default:
return "subscription_first"
}
}
// PreConsumeBilling decides whether to pre-consume from subscription or wallet based on user preference.
// It also always pre-consumes token quota in quota units (same as legacy flow).
func PreConsumeBilling(c *gin.Context, preConsumedQuota int, relayInfo *relaycommon.RelayInfo) *types.NewAPIError {
@@ -32,7 +24,7 @@ func PreConsumeBilling(c *gin.Context, preConsumedQuota int, relayInfo *relaycom
return types.NewError(fmt.Errorf("relayInfo is nil"), types.ErrorCodeInvalidRequest, types.ErrOptionWithSkipRetry())
}
pref := normalizeBillingPreference(relayInfo.UserSetting.BillingPreference)
pref := common.NormalizeBillingPreference(relayInfo.UserSetting.BillingPreference)
trySubscription := func() *types.NewAPIError {
quotaTypes := model.GetModelQuotaTypes(relayInfo.OriginModelName)
quotaType := 0

View File

@@ -18,8 +18,19 @@ For commercial licensing, please contact support@quantumnous.com
*/
import React from 'react';
import { Button, Modal, Space, Tag } from '@douyinfe/semi-ui';
import { convertUSDToCurrency } from '../../helpers/render';
import {
Button,
Modal,
Space,
Tag,
Typography,
Popover,
Divider,
} from '@douyinfe/semi-ui';
import { IconEdit, IconStop, IconPlay } from '@douyinfe/semi-icons';
import { convertUSDToCurrency } from '../../../helpers/render';
const { Text } = Typography;
const quotaTypeLabel = (quotaType) => (quotaType === 1 ? '按次' : '按量');
@@ -38,33 +49,92 @@ function formatDuration(plan, t) {
return `${plan.duration_value || 0}${unitMap[u] || u}`;
}
const renderPlanTitle = (text, record) => {
return (
<div>
<div className='font-medium'>{text}</div>
{record?.plan?.subtitle ? (
<div className='text-xs text-gray-500'>{record.plan.subtitle}</div>
) : null}
function formatResetPeriod(plan, t) {
const period = plan?.quota_reset_period || 'never';
if (period === 'daily') return t('每天');
if (period === 'weekly') return t('每周');
if (period === 'monthly') return t('每月');
if (period === 'custom') {
const seconds = Number(plan?.quota_reset_custom_seconds || 0);
if (seconds >= 86400) return `${Math.floor(seconds / 86400)} ${t('天')}`;
if (seconds >= 3600) return `${Math.floor(seconds / 3600)} ${t('小时')}`;
if (seconds >= 60) return `${Math.floor(seconds / 60)} ${t('分钟')}`;
return `${seconds} ${t('秒')}`;
}
return t('不重置');
}
const renderPlanTitle = (text, record, t) => {
const subtitle = record?.plan?.subtitle;
const plan = record?.plan;
const items = record?.items || [];
const popoverContent = (
<div style={{ width: 260 }}>
<Text strong>{text}</Text>
{subtitle && (
<Text type='tertiary' style={{ display: 'block', marginTop: 4 }}>
{subtitle}
</Text>
)}
<Divider margin={12} />
<div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 8 }}>
<Text type='tertiary'>{t('价格')}</Text>
<Text strong style={{ color: 'var(--semi-color-success)' }}>
{convertUSDToCurrency(Number(plan?.price_amount || 0), 2)}
</Text>
<Text type='tertiary'>{t('有效期')}</Text>
<Text>{formatDuration(plan, t)}</Text>
<Text type='tertiary'>{t('重置')}</Text>
<Text>{formatResetPeriod(plan, t)}</Text>
<Text type='tertiary'>{t('模型')}</Text>
<Text>
{items.length} {t('个')}
</Text>
</div>
</div>
);
return (
<Popover content={popoverContent} position='rightTop' showArrow>
<div style={{ cursor: 'pointer', maxWidth: 180 }}>
<Text strong ellipsis={{ showTooltip: false }}>
{text}
</Text>
{subtitle && (
<Text
type='tertiary'
ellipsis={{ showTooltip: false }}
style={{ display: 'block' }}
>
{subtitle}
</Text>
)}
</div>
</Popover>
);
};
const renderPrice = (text) => {
return convertUSDToCurrency(Number(text || 0), 2);
return (
<Text strong style={{ color: 'var(--semi-color-success)' }}>
{convertUSDToCurrency(Number(text || 0), 2)}
</Text>
);
};
const renderDuration = (text, record, t) => {
return formatDuration(record?.plan, t);
return <Text type='secondary'>{formatDuration(record?.plan, t)}</Text>;
};
const renderEnabled = (text, record) => {
const renderEnabled = (text, record, t) => {
return text ? (
<Tag color='green' shape='circle'>
启用
{t('启用')}
</Tag>
) : (
<Tag color='grey' shape='circle'>
禁用
{t('禁用')}
</Tag>
);
};
@@ -72,93 +142,203 @@ const renderEnabled = (text, record) => {
const renderModels = (text, record, t) => {
const items = record?.items || [];
if (items.length === 0) {
return <div className='text-xs text-gray-500'>{t('无模型')}</div>;
return <Text type='tertiary'></Text>;
}
return (
<div className='text-xs space-y-1'>
{items.slice(0, 3).map((it, idx) => (
<div key={idx}>
{it.model_name} ({quotaTypeLabel(it.quota_type)}: {it.amount_total})
</div>
))}
{items.length > 3 && (
<div className='text-gray-500'>
...{t('共')} {items.length} {t('个模型')}
</div>
)}
const popoverContent = (
<div style={{ maxWidth: 320, maxHeight: 260, overflowY: 'auto' }}>
<Text strong>
{t('模型权益')} ({items.length})
</Text>
<Divider margin={8} />
<Space vertical align='start' spacing={6}>
{items.map((it, idx) => (
<div
key={idx}
style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between',
width: '100%',
gap: 12,
}}
>
<Text ellipsis={{ showTooltip: true }} style={{ maxWidth: 180 }}>
{it.model_name}
</Text>
<Space spacing={8}>
<Tag
color={it.quota_type === 1 ? 'amber' : 'teal'}
shape='circle'
>
{quotaTypeLabel(it.quota_type)}
</Tag>
<Text type='secondary'>{it.amount_total}</Text>
</Space>
</div>
))}
</Space>
</div>
);
return (
<Popover content={popoverContent} position='leftTop' showArrow>
<Tag color='blue' shape='circle' style={{ cursor: 'pointer' }}>
{items.length} {t('个模型')}
</Tag>
</Popover>
);
};
const renderOperations = (text, record, { openEdit, disablePlan, t }) => {
const handleDisable = () => {
Modal.confirm({
title: t('确认禁用'),
content: t('禁用后用户端不再展示,但历史订单不受影响。是否继续?'),
centered: true,
onOk: () => disablePlan(record?.plan?.id),
});
};
const renderResetPeriod = (text, record, t) => {
const period = record?.plan?.quota_reset_period || 'never';
const isNever = period === 'never';
return (
<Text type={isNever ? 'tertiary' : 'secondary'}>
{formatResetPeriod(record?.plan, t)}
</Text>
);
};
const renderPaymentConfig = (text, record, t) => {
const hasStripe = !!record?.plan?.stripe_price_id;
const hasCreem = !!record?.plan?.creem_product_id;
return (
<Space>
<Button
type='tertiary'
size='small'
onClick={() => {
openEdit(record);
}}
>
{t('编辑')}
</Button>
<Button type='danger' size='small' onClick={handleDisable}>
{t('禁用')}
</Button>
<Space spacing={4}>
{hasStripe && (
<Tag color='violet' shape='circle'>
Stripe
</Tag>
)}
{hasCreem && (
<Tag color='cyan' shape='circle'>
Creem
</Tag>
)}
<Tag color='light-green' shape='circle'>
{t('易支付')}
</Tag>
</Space>
);
};
export const getSubscriptionsColumns = ({ t, openEdit, disablePlan }) => {
const renderOperations = (text, record, { openEdit, setPlanEnabled, t }) => {
const isEnabled = record?.plan?.enabled;
const handleToggle = () => {
if (isEnabled) {
Modal.confirm({
title: t('确认禁用'),
content: t('禁用后用户端不再展示,但历史订单不受影响。是否继续?'),
centered: true,
onOk: () => setPlanEnabled(record, false),
});
} else {
Modal.confirm({
title: t('确认启用'),
content: t('启用后套餐将在用户端展示。是否继续?'),
centered: true,
onOk: () => setPlanEnabled(record, true),
});
}
};
return (
<Space spacing={8}>
<Button
theme='borderless'
type='tertiary'
size='small'
icon={<IconEdit />}
onClick={() => openEdit(record)}
>
{t('编辑')}
</Button>
{isEnabled ? (
<Button
theme='borderless'
type='danger'
size='small'
icon={<IconStop />}
onClick={handleToggle}
>
{t('禁用')}
</Button>
) : (
<Button
theme='borderless'
type='primary'
size='small'
icon={<IconPlay />}
onClick={handleToggle}
>
{t('启用')}
</Button>
)}
</Space>
);
};
export const getSubscriptionsColumns = ({ t, openEdit, setPlanEnabled }) => {
return [
{
title: 'ID',
dataIndex: ['plan', 'id'],
width: 80,
width: 60,
render: (text) => <Text type='tertiary'>#{text}</Text>,
},
{
title: t('标题'),
title: t('套餐'),
dataIndex: ['plan', 'title'],
render: (text, record) => renderPlanTitle(text, record),
width: 200,
render: (text, record) => renderPlanTitle(text, record, t),
},
{
title: t('价格'),
dataIndex: ['plan', 'price_amount'],
width: 140,
render: (text, record) => renderPrice(text, record),
width: 100,
render: (text) => renderPrice(text),
},
{
title: t('优先级'),
dataIndex: ['plan', 'sort_order'],
width: 80,
render: (text) => <Text type='tertiary'>{Number(text || 0)}</Text>,
},
{
title: t('有效期'),
width: 140,
width: 80,
render: (text, record) => renderDuration(text, record, t),
},
{
title: t('重置'),
width: 80,
render: (text, record) => renderResetPeriod(text, record, t),
},
{
title: t('状态'),
dataIndex: ['plan', 'enabled'],
width: 90,
render: (text, record) => renderEnabled(text, record),
width: 80,
render: (text, record) => renderEnabled(text, record, t),
},
{
title: t('模型权益'),
width: 200,
title: t('支付渠道'),
width: 180,
render: (text, record) => renderPaymentConfig(text, record, t),
},
{
title: t('模型'),
width: 100,
render: (text, record) => renderModels(text, record, t),
},
{
title: '',
title: t('操作'),
dataIndex: 'operate',
fixed: 'right',
width: 180,
width: 160,
render: (text, record) =>
renderOperations(text, record, { openEdit, disablePlan, t }),
renderOperations(text, record, { openEdit, setPlanEnabled, t }),
},
];
};

View File

@@ -27,16 +27,16 @@ import {
import { getSubscriptionsColumns } from './SubscriptionsColumnDefs';
const SubscriptionsTable = (subscriptionsData) => {
const { plans, loading, compactMode, openEdit, disablePlan, t } =
const { plans, loading, compactMode, openEdit, setPlanEnabled, t } =
subscriptionsData;
const columns = useMemo(() => {
return getSubscriptionsColumns({
t,
openEdit,
disablePlan,
setPlanEnabled,
});
}, [t, openEdit, disablePlan]);
}, [t, openEdit, setPlanEnabled]);
const tableColumns = useMemo(() => {
return compactMode

View File

@@ -88,14 +88,20 @@ export const useSubscriptionsData = () => {
setActivePage(1);
};
// Disable plan
const disablePlan = async (planId) => {
// Update plan enabled status (single endpoint)
const setPlanEnabled = async (planRecordOrId, enabled) => {
const planId =
typeof planRecordOrId === 'number'
? planRecordOrId
: planRecordOrId?.plan?.id;
if (!planId) return;
setLoading(true);
try {
const res = await API.delete(`/api/subscription/admin/plans/${planId}`);
const res = await API.patch(`/api/subscription/admin/plans/${planId}`, {
enabled: !!enabled,
});
if (res.data?.success) {
showSuccess(t('已禁用'));
showSuccess(enabled ? t('已启用') : t('已禁用'));
await loadPlans();
} else {
showError(res.data?.message || t('操作失败'));
@@ -163,7 +169,7 @@ export const useSubscriptionsData = () => {
// Actions
loadPlans,
disablePlan,
setPlanEnabled,
refresh,
closeEdit,
openCreate,