feat(openai): 显示订阅到期时间

从 /backend-api/accounts/check 的 entitlement.expires_at 提取订阅
到期日期,每次 token 刷新时更新并存入 credentials,前端账号列表
的订阅类型和隐私下方以灰色小字显示(仅非 Free 账号)。

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
QTom
2026-04-02 20:44:28 +08:00
parent cf70fb1b4e
commit 00947d6492
6 changed files with 100 additions and 51 deletions

View File

@@ -88,8 +88,9 @@ func disableOpenAITraining(ctx context.Context, clientFactory PrivacyClientFacto
// ChatGPTAccountInfo 从 chatgpt.com/backend-api/accounts/check 获取的账号信息
type ChatGPTAccountInfo struct {
PlanType string
Email string
PlanType string
Email string
SubscriptionExpiresAt string // entitlement.expires_at (RFC3339)
}
const chatGPTAccountsCheckURL = "https://chatgpt.com/backend-api/accounts/check/v4-2023-04-27"
@@ -142,14 +143,20 @@ func fetchChatGPTAccountInfo(ctx context.Context, clientFactory PrivacyClientFac
// 优先匹配 orgID 对应的账号access_token JWT 中的 poid
if orgID != "" {
if matched := extractPlanFromAccount(accounts, orgID); matched != "" {
info.PlanType = matched
if acctRaw, ok := accounts[orgID]; ok {
if acct, ok := acctRaw.(map[string]any); ok {
fillAccountInfo(info, acct)
}
}
}
// 未匹配到时,遍历所有账号:优先 is_default次选非 free
if info.PlanType == "" {
var defaultPlan, paidPlan, anyPlan string
type candidate struct {
planType string
expiresAt string
}
var defaultC, paidC, anyC candidate
for _, acctRaw := range accounts {
acct, ok := acctRaw.(map[string]any)
if !ok {
@@ -159,26 +166,27 @@ func fetchChatGPTAccountInfo(ctx context.Context, clientFactory PrivacyClientFac
if planType == "" {
continue
}
if anyPlan == "" {
anyPlan = planType
ea := extractEntitlementExpiresAt(acct)
if anyC.planType == "" {
anyC = candidate{planType, ea}
}
if account, ok := acct["account"].(map[string]any); ok {
if isDefault, _ := account["is_default"].(bool); isDefault {
defaultPlan = planType
defaultC = candidate{planType, ea}
}
}
if !strings.EqualFold(planType, "free") && paidPlan == "" {
paidPlan = planType
if !strings.EqualFold(planType, "free") && paidC.planType == "" {
paidC = candidate{planType, ea}
}
}
// 优先级default > 非 free > 任意
switch {
case defaultPlan != "":
info.PlanType = defaultPlan
case paidPlan != "":
info.PlanType = paidPlan
case defaultC.planType != "":
info.PlanType, info.SubscriptionExpiresAt = defaultC.planType, defaultC.expiresAt
case paidC.planType != "":
info.PlanType, info.SubscriptionExpiresAt = paidC.planType, paidC.expiresAt
default:
info.PlanType = anyPlan
info.PlanType, info.SubscriptionExpiresAt = anyC.planType, anyC.expiresAt
}
}
@@ -187,21 +195,14 @@ func fetchChatGPTAccountInfo(ctx context.Context, clientFactory PrivacyClientFac
return nil
}
slog.Info("chatgpt_account_check_success", "plan_type", info.PlanType, "org_id", orgID)
slog.Info("chatgpt_account_check_success", "plan_type", info.PlanType, "subscription_expires_at", info.SubscriptionExpiresAt, "org_id", orgID)
return info
}
// extractPlanFromAccount 从 accounts map 中按 keyaccount_id精确匹配并提取 plan_type
func extractPlanFromAccount(accounts map[string]any, accountKey string) string {
acctRaw, ok := accounts[accountKey]
if !ok {
return ""
}
acct, ok := acctRaw.(map[string]any)
if !ok {
return ""
}
return extractPlanType(acct)
// fillAccountInfo 从单个 account 对象中提取 plan_type 和 subscription_expires_at
func fillAccountInfo(info *ChatGPTAccountInfo, acct map[string]any) {
info.PlanType = extractPlanType(acct)
info.SubscriptionExpiresAt = extractEntitlementExpiresAt(acct)
}
// extractPlanType 从单个 account 对象中提取 plan_type
@@ -219,6 +220,17 @@ func extractPlanType(acct map[string]any) string {
return ""
}
// extractEntitlementExpiresAt 从 entitlement 中提取 expires_at。
// 预期为 RFC3339 字符串格式,如 "2026-05-02T20:32:12+00:00"。
func extractEntitlementExpiresAt(acct map[string]any) string {
entitlement, ok := acct["entitlement"].(map[string]any)
if !ok {
return ""
}
ea, _ := entitlement["expires_at"].(string)
return ea
}
func truncate(s string, n int) string {
if len(s) <= n {
return s