From 9cdb0568ccf30c27a3af81da8194e8bd7a1161b9 Mon Sep 17 00:00:00 2001 From: Gemini Wen Date: Sun, 25 Jan 2026 18:12:15 +0800 Subject: [PATCH] =?UTF-8?q?fix(subscription):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E8=AE=A2=E9=98=85=E8=B0=83=E6=95=B4=E9=80=BB=E8=BE=91,?= =?UTF-8?q?=E5=B7=B2=E8=BF=87=E6=9C=9F=E8=AE=A2=E9=98=85=E4=BB=8E=E5=BD=93?= =?UTF-8?q?=E5=89=8D=E6=97=B6=E9=97=B4=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 已过期订阅延长时,从当前时间开始增加天数 - 已过期订阅不允许负向调整(缩短) - 未过期订阅保持原逻辑,从原过期时间增减 Co-Authored-By: Claude Sonnet 4.5 --- .../internal/service/subscription_service.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/backend/internal/service/subscription_service.go b/backend/internal/service/subscription_service.go index 6b3ac8ab..3c42852e 100644 --- a/backend/internal/service/subscription_service.go +++ b/backend/internal/service/subscription_service.go @@ -324,14 +324,29 @@ func (s *SubscriptionService) ExtendSubscription(ctx context.Context, subscripti days = -MaxValidityDays } + now := time.Now() + isExpired := !sub.ExpiresAt.After(now) + + // 如果订阅已过期,不允许负向调整 + if isExpired && days < 0 { + return nil, infraerrors.BadRequest("CANNOT_SHORTEN_EXPIRED", "cannot shorten an expired subscription") + } + // 计算新的过期时间 - newExpiresAt := sub.ExpiresAt.AddDate(0, 0, days) + var newExpiresAt time.Time + if isExpired { + // 已过期:从当前时间开始增加天数 + newExpiresAt = now.AddDate(0, 0, days) + } else { + // 未过期:从原过期时间增加/减少天数 + newExpiresAt = sub.ExpiresAt.AddDate(0, 0, days) + } + if newExpiresAt.After(MaxExpiresAt) { newExpiresAt = MaxExpiresAt } // 检查新的过期时间必须大于当前时间 - now := time.Now() if !newExpiresAt.After(now) { return nil, ErrAdjustWouldExpire }