From b06fa5efe88a1b7d8c163b6b3c5c9776a4944b2d Mon Sep 17 00:00:00 2001 From: shaw Date: Tue, 12 Aug 2025 14:21:01 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DOpenAI=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E8=B4=A6=E5=8F=B7=E6=95=B0=E6=8D=AE=E7=BB=9F=E8=AE=A1?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复API Key请求次数、费用、token统计无法记录的问题 - 修复OpenAI账户今日使用统计显示空白的问题 - 修复模型使用详情记录缺失的问题 - 统一OpenAI账户统计数据格式与其他平台保持一致 主要修改: 1. openaiRoutes.js: 将req.apiKeyData改为req.apiKey,与中间件保持一致 2. admin.js: 为OpenAI账户列表接口添加使用统计获取逻辑 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- src/routes/admin.js | 53 +++++++++++++++++++++++++++++++++++--- src/routes/openaiRoutes.js | 2 +- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/routes/admin.js b/src/routes/admin.js index a72479c7..611e2807 100644 --- a/src/routes/admin.js +++ b/src/routes/admin.js @@ -4582,13 +4582,60 @@ router.post('/openai-accounts/exchange-code', authenticateAdmin, async (req, res // 获取所有 OpenAI 账户 router.get('/openai-accounts', authenticateAdmin, async (req, res) => { try { - const accounts = await openaiAccountService.getAllAccounts() + const { platform, groupId } = req.query + let accounts = await openaiAccountService.getAllAccounts() - logger.info(`获取 OpenAI 账户列表: ${accounts.length} 个账户`) + // 根据查询参数进行筛选 + if (platform && platform !== 'all' && platform !== 'openai') { + // 如果指定了其他平台,返回空数组 + accounts = [] + } + + // 如果指定了分组筛选 + if (groupId && groupId !== 'all') { + if (groupId === 'ungrouped') { + // 筛选未分组账户 + accounts = accounts.filter((account) => !account.groupInfo) + } else { + // 筛选特定分组的账户 + accounts = accounts.filter( + (account) => account.groupInfo && account.groupInfo.id === groupId + ) + } + } + + // 为每个账户添加使用统计信息 + const accountsWithStats = await Promise.all( + accounts.map(async (account) => { + try { + const usageStats = await redis.getAccountUsageStats(account.id) + return { + ...account, + usage: { + daily: usageStats.daily, + total: usageStats.total, + monthly: usageStats.monthly + } + } + } catch (error) { + logger.debug(`Failed to get usage stats for OpenAI account ${account.id}:`, error) + return { + ...account, + usage: { + daily: { requests: 0, tokens: 0, allTokens: 0 }, + total: { requests: 0, tokens: 0, allTokens: 0 }, + monthly: { requests: 0, tokens: 0, allTokens: 0 } + } + } + } + }) + ) + + logger.info(`获取 OpenAI 账户列表: ${accountsWithStats.length} 个账户`) return res.json({ success: true, - data: accounts + data: accountsWithStats }) } catch (error) { logger.error('获取 OpenAI 账户列表失败:', error) diff --git a/src/routes/openaiRoutes.js b/src/routes/openaiRoutes.js index 1696031e..9b2fba15 100644 --- a/src/routes/openaiRoutes.js +++ b/src/routes/openaiRoutes.js @@ -56,7 +56,7 @@ router.post('/responses', authenticateApiKey, async (req, res) => { let upstream = null try { // 从中间件获取 API Key 数据 - const apiKeyData = req.apiKeyData || {} + const apiKeyData = req.apiKey || {} // 从请求头或请求体中提取会话 ID const sessionId =