mirror of
https://github.com/Wei-Shaw/claude-relay-service.git
synced 2026-01-23 09:21:24 +00:00
fix: 修复tokenExpiresAt混淆问题
This commit is contained in:
@@ -32,6 +32,18 @@ const ProxyHelper = require('../utils/proxyHelper')
|
|||||||
|
|
||||||
const router = express.Router()
|
const router = express.Router()
|
||||||
|
|
||||||
|
// 🛠️ 工具函数:处理可为空的时间字段
|
||||||
|
function normalizeNullableDate(value) {
|
||||||
|
if (value === undefined || value === null) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
if (typeof value === 'string') {
|
||||||
|
const trimmed = value.trim()
|
||||||
|
return trimmed === '' ? null : trimmed
|
||||||
|
}
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
// 🛠️ 工具函数:映射前端字段名到后端字段名
|
// 🛠️ 工具函数:映射前端字段名到后端字段名
|
||||||
/**
|
/**
|
||||||
* 映射前端的 expiresAt 字段到后端的 subscriptionExpiresAt 字段
|
* 映射前端的 expiresAt 字段到后端的 subscriptionExpiresAt 字段
|
||||||
@@ -64,17 +76,22 @@ function formatAccountExpiry(account) {
|
|||||||
return account
|
return account
|
||||||
}
|
}
|
||||||
|
|
||||||
// 保存原始的 OAuth token 过期时间
|
const rawSubscription = Object.prototype.hasOwnProperty.call(account, 'subscriptionExpiresAt')
|
||||||
const tokenExpiresAt = account.expiresAt || null
|
? account.subscriptionExpiresAt
|
||||||
|
: null
|
||||||
|
|
||||||
// 将订阅过期时间映射到 expiresAt(前端使用)
|
const rawToken = Object.prototype.hasOwnProperty.call(account, 'tokenExpiresAt')
|
||||||
const subscriptionExpiresAt = account.subscriptionExpiresAt || null
|
? account.tokenExpiresAt
|
||||||
|
: account.expiresAt
|
||||||
|
|
||||||
|
const subscriptionExpiresAt = normalizeNullableDate(rawSubscription)
|
||||||
|
const tokenExpiresAt = normalizeNullableDate(rawToken)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...account,
|
...account,
|
||||||
expiresAt: subscriptionExpiresAt, // 前端显示订阅过期时间
|
subscriptionExpiresAt,
|
||||||
tokenExpiresAt, // 保留 OAuth token 过期时间
|
tokenExpiresAt,
|
||||||
subscriptionExpiresAt // 保留原始字段
|
expiresAt: subscriptionExpiresAt
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -654,6 +654,12 @@ async function getAllAccounts() {
|
|||||||
// 转换 schedulable 字符串为布尔值(与 getAccount 保持一致)
|
// 转换 schedulable 字符串为布尔值(与 getAccount 保持一致)
|
||||||
accountData.schedulable = accountData.schedulable !== 'false' // 默认为true,只有明确设置为'false'才为false
|
accountData.schedulable = accountData.schedulable !== 'false' // 默认为true,只有明确设置为'false'才为false
|
||||||
|
|
||||||
|
const tokenExpiresAt = accountData.expiresAt || null
|
||||||
|
const subscriptionExpiresAt =
|
||||||
|
accountData.subscriptionExpiresAt && accountData.subscriptionExpiresAt !== ''
|
||||||
|
? accountData.subscriptionExpiresAt
|
||||||
|
: null
|
||||||
|
|
||||||
// 不解密敏感字段,只返回基本信息
|
// 不解密敏感字段,只返回基本信息
|
||||||
accounts.push({
|
accounts.push({
|
||||||
...accountData,
|
...accountData,
|
||||||
@@ -663,7 +669,9 @@ async function getAllAccounts() {
|
|||||||
|
|
||||||
// ✅ 前端显示订阅过期时间(业务字段)
|
// ✅ 前端显示订阅过期时间(业务字段)
|
||||||
// 注意:前端看到的 expiresAt 实际上是 subscriptionExpiresAt
|
// 注意:前端看到的 expiresAt 实际上是 subscriptionExpiresAt
|
||||||
expiresAt: accountData.subscriptionExpiresAt || null,
|
tokenExpiresAt,
|
||||||
|
subscriptionExpiresAt,
|
||||||
|
expiresAt: subscriptionExpiresAt,
|
||||||
|
|
||||||
// 添加 scopes 字段用于判断认证方式
|
// 添加 scopes 字段用于判断认证方式
|
||||||
// 处理空字符串和默认值的情况
|
// 处理空字符串和默认值的情况
|
||||||
|
|||||||
@@ -793,6 +793,12 @@ async function getAllAccounts() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const tokenExpiresAt = accountData.expiresAt || null
|
||||||
|
const subscriptionExpiresAt =
|
||||||
|
accountData.subscriptionExpiresAt && accountData.subscriptionExpiresAt !== ''
|
||||||
|
? accountData.subscriptionExpiresAt
|
||||||
|
: null
|
||||||
|
|
||||||
// 不解密敏感字段,只返回基本信息
|
// 不解密敏感字段,只返回基本信息
|
||||||
accounts.push({
|
accounts.push({
|
||||||
...accountData,
|
...accountData,
|
||||||
@@ -803,7 +809,9 @@ async function getAllAccounts() {
|
|||||||
refreshToken: maskedRefreshToken,
|
refreshToken: maskedRefreshToken,
|
||||||
|
|
||||||
// ✅ 前端显示订阅过期时间(业务字段)
|
// ✅ 前端显示订阅过期时间(业务字段)
|
||||||
expiresAt: accountData.subscriptionExpiresAt || null,
|
tokenExpiresAt,
|
||||||
|
subscriptionExpiresAt,
|
||||||
|
expiresAt: subscriptionExpiresAt,
|
||||||
|
|
||||||
// 添加 scopes 字段用于判断认证方式
|
// 添加 scopes 字段用于判断认证方式
|
||||||
// 处理空字符串的情况
|
// 处理空字符串的情况
|
||||||
|
|||||||
Reference in New Issue
Block a user