mirror of
https://github.com/Wei-Shaw/claude-relay-service.git
synced 2026-01-22 16:43:35 +00:00
Merge branch 'dev' of https://github.com/Wei-Shaw/claude-relay-service into dev
This commit is contained in:
@@ -96,4 +96,5 @@ LDAP_USER_ATTR_LAST_NAME=sn
|
||||
USER_MANAGEMENT_ENABLED=false
|
||||
DEFAULT_USER_ROLE=user
|
||||
USER_SESSION_TIMEOUT=86400000
|
||||
MAX_API_KEYS_PER_USER=5
|
||||
MAX_API_KEYS_PER_USER=1
|
||||
ALLOW_USER_DELETE_API_KEYS=false
|
||||
|
||||
93
README.md
93
README.md
@@ -474,42 +474,101 @@ claude
|
||||
gemini # 或其他 Gemini CLI 命令
|
||||
```
|
||||
|
||||
**Codex 设置环境变量:**
|
||||
**Codex 配置:**
|
||||
|
||||
```bash
|
||||
export OPENAI_BASE_URL="http://127.0.0.1:3000/openai" # 根据实际填写你服务器的ip地址或者域名
|
||||
export OPENAI_API_KEY="后台创建的API密钥" # 使用后台创建的API密钥
|
||||
在 `~/.codex/config.toml` 文件中添加以下配置:
|
||||
|
||||
```toml
|
||||
model_provider = "crs"
|
||||
model = "gpt-5"
|
||||
model_reasoning_effort = "high"
|
||||
disable_response_storage = true
|
||||
|
||||
[model_providers.crs]
|
||||
name = "crs"
|
||||
base_url = "http://127.0.0.1:3000/openai" # 根据实际填写你服务器的ip地址或者域名
|
||||
wire_api = "responses"
|
||||
```
|
||||
|
||||
在 `~/.codex/auth.json` 文件中配置API密钥:
|
||||
|
||||
```json
|
||||
{
|
||||
"OPENAI_API_KEY": "你的后台创建的API密钥"
|
||||
}
|
||||
```
|
||||
|
||||
### 5. 第三方工具API接入
|
||||
|
||||
本服务支持多种API端点格式,方便接入不同的第三方工具(如Cherry Studio等):
|
||||
本服务支持多种API端点格式,方便接入不同的第三方工具(如Cherry Studio等)。
|
||||
|
||||
**Claude标准格式:**
|
||||
#### Cherry Studio 接入示例
|
||||
|
||||
Cherry Studio支持多种AI服务的接入,下面是不同账号类型的详细配置:
|
||||
|
||||
**1. Claude账号接入:**
|
||||
|
||||
```
|
||||
# 如果工具支持Claude标准格式,请使用该接口
|
||||
# API地址
|
||||
http://你的服务器:3000/claude/
|
||||
|
||||
# 模型ID示例
|
||||
claude-sonnet-4-20250514 # Claude Sonnet 4
|
||||
claude-opus-4-20250514 # Claude Opus 4
|
||||
```
|
||||
|
||||
**OpenAI兼容格式:**
|
||||
配置步骤:
|
||||
- 供应商类型选择"Anthropic"
|
||||
- API地址填入:`http://你的服务器:3000/claude/`
|
||||
- API Key填入:后台创建的API密钥(cr_开头)
|
||||
|
||||
**2. Gemini账号接入:**
|
||||
|
||||
```
|
||||
# 适用于需要OpenAI格式的第三方工具
|
||||
http://你的服务器:3000/openai/claude/v1/
|
||||
# API地址
|
||||
http://你的服务器:3000/gemini/
|
||||
|
||||
# 模型ID示例
|
||||
gemini-2.5-pro # Gemini 2.5 Pro
|
||||
```
|
||||
|
||||
**接入示例:**
|
||||
配置步骤:
|
||||
- 供应商类型选择"Gemini"
|
||||
- API地址填入:`http://你的服务器:3000/gemini/`
|
||||
- API Key填入:后台创建的API密钥(cr_开头)
|
||||
|
||||
- **Cherry Studio**: 使用OpenAI格式 `http://你的服务器:3000/openai/claude/v1/` 使用Codex cli API `http://你的服务器:3000/openai/responses`
|
||||
- **其他支持自定义API的工具**: 根据工具要求选择合适的格式
|
||||
**3. Codex接入:**
|
||||
|
||||
```
|
||||
# API地址
|
||||
http://你的服务器:3000/openai/
|
||||
|
||||
# 模型ID(固定)
|
||||
gpt-5 # Codex使用固定模型ID
|
||||
```
|
||||
|
||||
配置步骤:
|
||||
- 供应商类型选择"Openai-Response"
|
||||
- API地址填入:`http://你的服务器:3000/openai/`
|
||||
- API Key填入:后台创建的API密钥(cr_开头)
|
||||
- **重要**:Codex只支持Openai-Response标准
|
||||
|
||||
#### 其他第三方工具接入
|
||||
|
||||
**接入要点:**
|
||||
|
||||
- 所有账号类型都使用相同的API密钥(在后台统一创建)
|
||||
- 根据不同的路由前缀自动识别账号类型
|
||||
- `/claude/` - 使用Claude账号池
|
||||
- `/gemini/` - 使用Gemini账号池
|
||||
- `/openai/` - 使用Codex账号(只支持Openai-Response格式)
|
||||
- 支持所有标准API端点(messages、models等)
|
||||
|
||||
**重要说明:**
|
||||
|
||||
- 所有格式都支持相同的功能,仅是路径不同
|
||||
- `/api/v1/messages` = `/claude/v1/messages` = `/openai/claude/v1/messages`
|
||||
- 选择适合你使用工具的格式即可
|
||||
- 支持所有Claude API端点(messages、models等)
|
||||
- 确保在后台已添加对应类型的账号(Claude/Gemini/Codex)
|
||||
- API密钥可以通用,系统会根据路由自动选择账号类型
|
||||
- 建议为不同用户创建不同的API密钥便于使用统计
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -175,7 +175,8 @@ const config = {
|
||||
enabled: process.env.USER_MANAGEMENT_ENABLED === 'true',
|
||||
defaultUserRole: process.env.DEFAULT_USER_ROLE || 'user',
|
||||
userSessionTimeout: parseInt(process.env.USER_SESSION_TIMEOUT) || 86400000, // 24小时
|
||||
maxApiKeysPerUser: parseInt(process.env.MAX_API_KEYS_PER_USER) || 5
|
||||
maxApiKeysPerUser: parseInt(process.env.MAX_API_KEYS_PER_USER) || 1,
|
||||
allowUserDeleteApiKeys: process.env.ALLOW_USER_DELETE_API_KEYS === 'true' // 默认不允许用户删除自己的API Keys
|
||||
},
|
||||
|
||||
// 📢 Webhook通知配置
|
||||
|
||||
@@ -86,6 +86,33 @@ function decryptGeminiData(encryptedData) {
|
||||
}
|
||||
}
|
||||
|
||||
// API Key 哈希函数(与apiKeyService保持一致)
|
||||
function hashApiKey(apiKey) {
|
||||
if (!apiKey || !config.security.encryptionKey) {
|
||||
return apiKey
|
||||
}
|
||||
|
||||
return crypto
|
||||
.createHash('sha256')
|
||||
.update(apiKey + config.security.encryptionKey)
|
||||
.digest('hex')
|
||||
}
|
||||
|
||||
// 检查是否为明文API Key(通过格式判断,不依赖前缀)
|
||||
function isPlaintextApiKey(apiKey) {
|
||||
if (!apiKey || typeof apiKey !== 'string') {
|
||||
return false
|
||||
}
|
||||
|
||||
// SHA256哈希值固定为64个十六进制字符,如果是哈希值则返回false
|
||||
if (apiKey.length === 64 && /^[a-f0-9]+$/i.test(apiKey)) {
|
||||
return false // 已经是哈希值
|
||||
}
|
||||
|
||||
// 其他情况都认为是明文API Key(包括sk-ant-、cr_、自定义前缀等)
|
||||
return true
|
||||
}
|
||||
|
||||
// 数据加密函数(用于导入)
|
||||
function encryptClaudeData(data) {
|
||||
if (!data || !config.security.encryptionKey) {
|
||||
@@ -651,6 +678,13 @@ Important Notes:
|
||||
- If importing decrypted data, it will be re-encrypted automatically
|
||||
- If importing encrypted data, it will be stored as-is
|
||||
- Sanitized exports cannot be properly imported (missing sensitive data)
|
||||
- Automatic handling of plaintext API Keys
|
||||
* Uses your configured API_KEY_PREFIX from config (sk-, cr_, etc.)
|
||||
* Automatically detects plaintext vs hashed API Keys by format
|
||||
* Plaintext API Keys are automatically hashed during import
|
||||
* Hash mappings are created correctly for plaintext keys
|
||||
* Supports custom prefixes and legacy format detection
|
||||
* No manual conversion needed - just import your backup file
|
||||
|
||||
Examples:
|
||||
# Export all data with decryption (for migration)
|
||||
@@ -659,7 +693,7 @@ Examples:
|
||||
# Export without decrypting (for backup)
|
||||
node scripts/data-transfer-enhanced.js export --decrypt=false
|
||||
|
||||
# Import data (auto-handles encryption)
|
||||
# Import data (auto-handles encryption and plaintext API keys)
|
||||
node scripts/data-transfer-enhanced.js import --input=backup.json
|
||||
|
||||
# Import with force overwrite
|
||||
@@ -773,6 +807,26 @@ async function importData() {
|
||||
const apiKeyData = { ...apiKey }
|
||||
delete apiKeyData.usageStats
|
||||
|
||||
// 检查并处理API Key哈希
|
||||
let plainTextApiKey = null
|
||||
let hashedApiKey = null
|
||||
|
||||
if (apiKeyData.apiKey && isPlaintextApiKey(apiKeyData.apiKey)) {
|
||||
// 如果是明文API Key,保存明文并计算哈希
|
||||
plainTextApiKey = apiKeyData.apiKey
|
||||
hashedApiKey = hashApiKey(plainTextApiKey)
|
||||
logger.info(`🔐 Detected plaintext API Key for: ${apiKey.name} (${apiKey.id})`)
|
||||
} else if (apiKeyData.apiKey) {
|
||||
// 如果已经是哈希值,直接使用
|
||||
hashedApiKey = apiKeyData.apiKey
|
||||
logger.info(`🔍 Using existing hashed API Key for: ${apiKey.name} (${apiKey.id})`)
|
||||
}
|
||||
|
||||
// API Key字段始终存储哈希值
|
||||
if (hashedApiKey) {
|
||||
apiKeyData.apiKey = hashedApiKey
|
||||
}
|
||||
|
||||
// 使用 hset 存储到哈希表
|
||||
const pipeline = redis.client.pipeline()
|
||||
for (const [field, value] of Object.entries(apiKeyData)) {
|
||||
@@ -780,9 +834,12 @@ async function importData() {
|
||||
}
|
||||
await pipeline.exec()
|
||||
|
||||
// 更新哈希映射
|
||||
if (apiKey.apiKey && !importDataObj.metadata.sanitized) {
|
||||
await redis.client.hset('apikey:hash_map', apiKey.apiKey, apiKey.id)
|
||||
// 更新哈希映射:hash_map的key必须是哈希值
|
||||
if (!importDataObj.metadata.sanitized && hashedApiKey) {
|
||||
await redis.client.hset('apikey:hash_map', hashedApiKey, apiKey.id)
|
||||
logger.info(
|
||||
`📝 Updated hash mapping: ${hashedApiKey.substring(0, 8)}... -> ${apiKey.id}`
|
||||
)
|
||||
}
|
||||
|
||||
// 导入使用统计数据
|
||||
|
||||
@@ -185,7 +185,7 @@ class ServiceManager {
|
||||
|
||||
restart(daemon = false) {
|
||||
console.log('🔄 重启服务...')
|
||||
|
||||
this.stop()
|
||||
// 等待停止完成
|
||||
setTimeout(() => {
|
||||
this.start(daemon)
|
||||
|
||||
@@ -24,6 +24,68 @@ const ProxyHelper = require('../utils/proxyHelper')
|
||||
|
||||
const router = express.Router()
|
||||
|
||||
// 👥 用户管理
|
||||
|
||||
// 获取所有用户列表(用于API Key分配)
|
||||
router.get('/users', authenticateAdmin, async (req, res) => {
|
||||
try {
|
||||
const userService = require('../services/userService')
|
||||
|
||||
// Extract query parameters for filtering
|
||||
const { role, isActive } = req.query
|
||||
const options = { limit: 1000 }
|
||||
|
||||
// Apply role filter if provided
|
||||
if (role) {
|
||||
options.role = role
|
||||
}
|
||||
|
||||
// Apply isActive filter if provided, otherwise default to active users only
|
||||
if (isActive !== undefined) {
|
||||
options.isActive = isActive === 'true'
|
||||
} else {
|
||||
options.isActive = true // Default to active users for backwards compatibility
|
||||
}
|
||||
|
||||
const result = await userService.getAllUsers(options)
|
||||
|
||||
// Extract users array from the paginated result
|
||||
const allUsers = result.users || []
|
||||
|
||||
// Map to the format needed for the dropdown
|
||||
const activeUsers = allUsers.map((user) => ({
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
displayName: user.displayName || user.username,
|
||||
email: user.email,
|
||||
role: user.role
|
||||
}))
|
||||
|
||||
// 添加Admin选项作为第一个
|
||||
const usersWithAdmin = [
|
||||
{
|
||||
id: 'admin',
|
||||
username: 'admin',
|
||||
displayName: 'Admin',
|
||||
email: '',
|
||||
role: 'admin'
|
||||
},
|
||||
...activeUsers
|
||||
]
|
||||
|
||||
return res.json({
|
||||
success: true,
|
||||
data: usersWithAdmin
|
||||
})
|
||||
} catch (error) {
|
||||
logger.error('❌ Failed to get users list:', error)
|
||||
return res.status(500).json({
|
||||
error: 'Failed to get users list',
|
||||
message: error.message
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
// 🔑 API Keys 管理
|
||||
|
||||
// 调试:获取API Key费用详情
|
||||
@@ -63,6 +125,9 @@ router.get('/api-keys', authenticateAdmin, async (req, res) => {
|
||||
const { timeRange = 'all' } = req.query // all, 7days, monthly
|
||||
const apiKeys = await apiKeyService.getAllApiKeys()
|
||||
|
||||
// 获取用户服务来补充owner信息
|
||||
const userService = require('../services/userService')
|
||||
|
||||
// 根据时间范围计算查询模式
|
||||
const now = new Date()
|
||||
const searchPatterns = []
|
||||
@@ -313,6 +378,28 @@ router.get('/api-keys', authenticateAdmin, async (req, res) => {
|
||||
}
|
||||
}
|
||||
|
||||
// 为每个API Key添加owner的displayName
|
||||
for (const apiKey of apiKeys) {
|
||||
// 如果API Key有关联的用户ID,获取用户信息
|
||||
if (apiKey.userId) {
|
||||
try {
|
||||
const user = await userService.getUserById(apiKey.userId, false)
|
||||
if (user) {
|
||||
apiKey.ownerDisplayName = user.displayName || user.username || 'Unknown User'
|
||||
} else {
|
||||
apiKey.ownerDisplayName = 'Unknown User'
|
||||
}
|
||||
} catch (error) {
|
||||
logger.debug(`无法获取用户 ${apiKey.userId} 的信息:`, error)
|
||||
apiKey.ownerDisplayName = 'Unknown User'
|
||||
}
|
||||
} else {
|
||||
// 如果没有userId,使用createdBy字段或默认为Admin
|
||||
apiKey.ownerDisplayName =
|
||||
apiKey.createdBy === 'admin' ? 'Admin' : apiKey.createdBy || 'Admin'
|
||||
}
|
||||
}
|
||||
|
||||
return res.json({ success: true, data: apiKeys })
|
||||
} catch (error) {
|
||||
logger.error('❌ Failed to get API keys:', error)
|
||||
@@ -404,7 +491,9 @@ router.post('/api-keys', authenticateAdmin, async (req, res) => {
|
||||
allowedClients,
|
||||
dailyCostLimit,
|
||||
weeklyOpusCostLimit,
|
||||
tags
|
||||
tags,
|
||||
activationDays, // 新增:激活后有效天数
|
||||
expirationMode // 新增:过期模式
|
||||
} = req.body
|
||||
|
||||
// 输入验证
|
||||
@@ -482,6 +571,31 @@ router.post('/api-keys', authenticateAdmin, async (req, res) => {
|
||||
return res.status(400).json({ error: 'All tags must be non-empty strings' })
|
||||
}
|
||||
|
||||
// 验证激活相关字段
|
||||
if (expirationMode && !['fixed', 'activation'].includes(expirationMode)) {
|
||||
return res
|
||||
.status(400)
|
||||
.json({ error: 'Expiration mode must be either "fixed" or "activation"' })
|
||||
}
|
||||
|
||||
if (expirationMode === 'activation') {
|
||||
if (
|
||||
!activationDays ||
|
||||
!Number.isInteger(Number(activationDays)) ||
|
||||
Number(activationDays) < 1
|
||||
) {
|
||||
return res
|
||||
.status(400)
|
||||
.json({ error: 'Activation days must be a positive integer when using activation mode' })
|
||||
}
|
||||
// 激活模式下不应该设置固定过期时间
|
||||
if (expiresAt) {
|
||||
return res
|
||||
.status(400)
|
||||
.json({ error: 'Cannot set fixed expiration date when using activation mode' })
|
||||
}
|
||||
}
|
||||
|
||||
const newKey = await apiKeyService.generateApiKey({
|
||||
name,
|
||||
description,
|
||||
@@ -503,7 +617,9 @@ router.post('/api-keys', authenticateAdmin, async (req, res) => {
|
||||
allowedClients,
|
||||
dailyCostLimit,
|
||||
weeklyOpusCostLimit,
|
||||
tags
|
||||
tags,
|
||||
activationDays,
|
||||
expirationMode
|
||||
})
|
||||
|
||||
logger.success(`🔑 Admin created new API key: ${name}`)
|
||||
@@ -537,7 +653,9 @@ router.post('/api-keys/batch', authenticateAdmin, async (req, res) => {
|
||||
allowedClients,
|
||||
dailyCostLimit,
|
||||
weeklyOpusCostLimit,
|
||||
tags
|
||||
tags,
|
||||
activationDays,
|
||||
expirationMode
|
||||
} = req.body
|
||||
|
||||
// 输入验证
|
||||
@@ -581,7 +699,9 @@ router.post('/api-keys/batch', authenticateAdmin, async (req, res) => {
|
||||
allowedClients,
|
||||
dailyCostLimit,
|
||||
weeklyOpusCostLimit,
|
||||
tags
|
||||
tags,
|
||||
activationDays,
|
||||
expirationMode
|
||||
})
|
||||
|
||||
// 保留原始 API Key 供返回
|
||||
@@ -679,6 +799,9 @@ router.put('/api-keys/batch', authenticateAdmin, async (req, res) => {
|
||||
if (updates.tokenLimit !== undefined) {
|
||||
finalUpdates.tokenLimit = updates.tokenLimit
|
||||
}
|
||||
if (updates.rateLimitCost !== undefined) {
|
||||
finalUpdates.rateLimitCost = updates.rateLimitCost
|
||||
}
|
||||
if (updates.concurrencyLimit !== undefined) {
|
||||
finalUpdates.concurrencyLimit = updates.concurrencyLimit
|
||||
}
|
||||
@@ -800,6 +923,7 @@ router.put('/api-keys/:keyId', authenticateAdmin, async (req, res) => {
|
||||
try {
|
||||
const { keyId } = req.params
|
||||
const {
|
||||
name, // 添加名称字段
|
||||
tokenLimit,
|
||||
concurrencyLimit,
|
||||
rateLimitWindow,
|
||||
@@ -819,12 +943,25 @@ router.put('/api-keys/:keyId', authenticateAdmin, async (req, res) => {
|
||||
expiresAt,
|
||||
dailyCostLimit,
|
||||
weeklyOpusCostLimit,
|
||||
tags
|
||||
tags,
|
||||
ownerId // 新增:所有者ID字段
|
||||
} = req.body
|
||||
|
||||
// 只允许更新指定字段
|
||||
const updates = {}
|
||||
|
||||
// 处理名称字段
|
||||
if (name !== undefined && name !== null && name !== '') {
|
||||
const trimmedName = name.toString().trim()
|
||||
if (trimmedName.length === 0) {
|
||||
return res.status(400).json({ error: 'API Key name cannot be empty' })
|
||||
}
|
||||
if (trimmedName.length > 100) {
|
||||
return res.status(400).json({ error: 'API Key name must be less than 100 characters' })
|
||||
}
|
||||
updates.name = trimmedName
|
||||
}
|
||||
|
||||
if (tokenLimit !== undefined && tokenLimit !== null && tokenLimit !== '') {
|
||||
if (!Number.isInteger(Number(tokenLimit)) || Number(tokenLimit) < 0) {
|
||||
return res.status(400).json({ error: 'Token limit must be a non-negative integer' })
|
||||
@@ -989,6 +1126,45 @@ router.put('/api-keys/:keyId', authenticateAdmin, async (req, res) => {
|
||||
updates.isActive = isActive
|
||||
}
|
||||
|
||||
// 处理所有者变更
|
||||
if (ownerId !== undefined) {
|
||||
const userService = require('../services/userService')
|
||||
|
||||
if (ownerId === 'admin') {
|
||||
// 分配给Admin
|
||||
updates.userId = ''
|
||||
updates.userUsername = ''
|
||||
updates.createdBy = 'admin'
|
||||
} else if (ownerId) {
|
||||
// 分配给用户
|
||||
try {
|
||||
const user = await userService.getUserById(ownerId, false)
|
||||
if (!user) {
|
||||
return res.status(400).json({ error: 'Invalid owner: User not found' })
|
||||
}
|
||||
if (!user.isActive) {
|
||||
return res.status(400).json({ error: 'Cannot assign to inactive user' })
|
||||
}
|
||||
|
||||
// 设置新的所有者信息
|
||||
updates.userId = ownerId
|
||||
updates.userUsername = user.username
|
||||
updates.createdBy = user.username
|
||||
|
||||
// 管理员重新分配时,不检查用户的API Key数量限制
|
||||
logger.info(`🔄 Admin reassigning API key ${keyId} to user ${user.username}`)
|
||||
} catch (error) {
|
||||
logger.error('Error fetching user for owner reassignment:', error)
|
||||
return res.status(400).json({ error: 'Invalid owner ID' })
|
||||
}
|
||||
} else {
|
||||
// 清空所有者(分配给Admin)
|
||||
updates.userId = ''
|
||||
updates.userUsername = ''
|
||||
updates.createdBy = 'admin'
|
||||
}
|
||||
}
|
||||
|
||||
await apiKeyService.updateApiKey(keyId, updates)
|
||||
|
||||
logger.success(`📝 Admin updated API key: ${keyId}`)
|
||||
@@ -999,6 +1175,85 @@ router.put('/api-keys/:keyId', authenticateAdmin, async (req, res) => {
|
||||
}
|
||||
})
|
||||
|
||||
// 修改API Key过期时间(包括手动激活功能)
|
||||
router.patch('/api-keys/:keyId/expiration', authenticateAdmin, async (req, res) => {
|
||||
try {
|
||||
const { keyId } = req.params
|
||||
const { expiresAt, activateNow } = req.body
|
||||
|
||||
// 获取当前API Key信息
|
||||
const keyData = await redis.getApiKey(keyId)
|
||||
if (!keyData || Object.keys(keyData).length === 0) {
|
||||
return res.status(404).json({ error: 'API key not found' })
|
||||
}
|
||||
|
||||
const updates = {}
|
||||
|
||||
// 如果是激活操作(用于未激活的key)
|
||||
if (activateNow === true) {
|
||||
if (keyData.expirationMode === 'activation' && keyData.isActivated !== 'true') {
|
||||
const now = new Date()
|
||||
const activationDays = parseInt(keyData.activationDays || 30)
|
||||
const newExpiresAt = new Date(now.getTime() + activationDays * 24 * 60 * 60 * 1000)
|
||||
|
||||
updates.isActivated = 'true'
|
||||
updates.activatedAt = now.toISOString()
|
||||
updates.expiresAt = newExpiresAt.toISOString()
|
||||
|
||||
logger.success(
|
||||
`🔓 API key manually activated by admin: ${keyId} (${keyData.name}), expires at ${newExpiresAt.toISOString()}`
|
||||
)
|
||||
} else {
|
||||
return res.status(400).json({
|
||||
error: 'Cannot activate',
|
||||
message: 'Key is either already activated or not in activation mode'
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 如果提供了新的过期时间(但不是激活操作)
|
||||
if (expiresAt !== undefined && activateNow !== true) {
|
||||
// 验证过期时间格式
|
||||
if (expiresAt && isNaN(Date.parse(expiresAt))) {
|
||||
return res.status(400).json({ error: 'Invalid expiration date format' })
|
||||
}
|
||||
|
||||
// 如果设置了过期时间,确保key是激活状态
|
||||
if (expiresAt) {
|
||||
updates.expiresAt = new Date(expiresAt).toISOString()
|
||||
// 如果之前是未激活状态,现在激活它
|
||||
if (keyData.isActivated !== 'true') {
|
||||
updates.isActivated = 'true'
|
||||
updates.activatedAt = new Date().toISOString()
|
||||
}
|
||||
} else {
|
||||
// 清除过期时间(永不过期)
|
||||
updates.expiresAt = ''
|
||||
}
|
||||
}
|
||||
|
||||
if (Object.keys(updates).length === 0) {
|
||||
return res.status(400).json({ error: 'No valid updates provided' })
|
||||
}
|
||||
|
||||
// 更新API Key
|
||||
await apiKeyService.updateApiKey(keyId, updates)
|
||||
|
||||
logger.success(`📝 Updated API key expiration: ${keyId} (${keyData.name})`)
|
||||
return res.json({
|
||||
success: true,
|
||||
message: 'API key expiration updated successfully',
|
||||
updates
|
||||
})
|
||||
} catch (error) {
|
||||
logger.error('❌ Failed to update API key expiration:', error)
|
||||
return res.status(500).json({
|
||||
error: 'Failed to update API key expiration',
|
||||
message: error.message
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
// 批量删除API Keys(必须在 :keyId 路由之前定义)
|
||||
router.delete('/api-keys/batch', authenticateAdmin, async (req, res) => {
|
||||
try {
|
||||
@@ -1125,7 +1380,7 @@ router.get('/api-keys/deleted', authenticateAdmin, async (req, res) => {
|
||||
deletedAt: key.deletedAt,
|
||||
deletedBy: key.deletedBy,
|
||||
deletedByType: key.deletedByType,
|
||||
canRestore: false // Deleted keys cannot be restored per requirement
|
||||
canRestore: true // 已删除的API Key可以恢复
|
||||
}))
|
||||
|
||||
logger.success(`📋 Admin retrieved ${enrichedKeys.length} deleted API keys`)
|
||||
@@ -1138,6 +1393,123 @@ router.get('/api-keys/deleted', authenticateAdmin, async (req, res) => {
|
||||
}
|
||||
})
|
||||
|
||||
// 🔄 恢复已删除的API Key
|
||||
router.post('/api-keys/:keyId/restore', authenticateAdmin, async (req, res) => {
|
||||
try {
|
||||
const { keyId } = req.params
|
||||
const adminUsername = req.session?.admin?.username || 'unknown'
|
||||
|
||||
// 调用服务层的恢复方法
|
||||
const result = await apiKeyService.restoreApiKey(keyId, adminUsername, 'admin')
|
||||
|
||||
if (result.success) {
|
||||
logger.success(`✅ Admin ${adminUsername} restored API key: ${keyId}`)
|
||||
return res.json({
|
||||
success: true,
|
||||
message: 'API Key 已成功恢复',
|
||||
apiKey: result.apiKey
|
||||
})
|
||||
} else {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
error: 'Failed to restore API key'
|
||||
})
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error('❌ Failed to restore API key:', error)
|
||||
|
||||
// 根据错误类型返回适当的响应
|
||||
if (error.message === 'API key not found') {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
error: 'API Key 不存在'
|
||||
})
|
||||
} else if (error.message === 'API key is not deleted') {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
error: '该 API Key 未被删除,无需恢复'
|
||||
})
|
||||
}
|
||||
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
error: '恢复 API Key 失败',
|
||||
message: error.message
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
// 🗑️ 彻底删除API Key(物理删除)
|
||||
router.delete('/api-keys/:keyId/permanent', authenticateAdmin, async (req, res) => {
|
||||
try {
|
||||
const { keyId } = req.params
|
||||
const adminUsername = req.session?.admin?.username || 'unknown'
|
||||
|
||||
// 调用服务层的彻底删除方法
|
||||
const result = await apiKeyService.permanentDeleteApiKey(keyId)
|
||||
|
||||
if (result.success) {
|
||||
logger.success(`🗑️ Admin ${adminUsername} permanently deleted API key: ${keyId}`)
|
||||
return res.json({
|
||||
success: true,
|
||||
message: 'API Key 已彻底删除'
|
||||
})
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error('❌ Failed to permanently delete API key:', error)
|
||||
|
||||
if (error.message === 'API key not found') {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
error: 'API Key 不存在'
|
||||
})
|
||||
} else if (error.message === '只能彻底删除已经删除的API Key') {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
error: '只能彻底删除已经删除的API Key'
|
||||
})
|
||||
}
|
||||
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
error: '彻底删除 API Key 失败',
|
||||
message: error.message
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
// 🧹 清空所有已删除的API Keys
|
||||
router.delete('/api-keys/deleted/clear-all', authenticateAdmin, async (req, res) => {
|
||||
try {
|
||||
const adminUsername = req.session?.admin?.username || 'unknown'
|
||||
|
||||
// 调用服务层的清空方法
|
||||
const result = await apiKeyService.clearAllDeletedApiKeys()
|
||||
|
||||
logger.success(
|
||||
`🧹 Admin ${adminUsername} cleared deleted API keys: ${result.successCount}/${result.total}`
|
||||
)
|
||||
|
||||
return res.json({
|
||||
success: true,
|
||||
message: `成功清空 ${result.successCount} 个已删除的 API Keys`,
|
||||
details: {
|
||||
total: result.total,
|
||||
successCount: result.successCount,
|
||||
failedCount: result.failedCount,
|
||||
errors: result.errors
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
logger.error('❌ Failed to clear all deleted API keys:', error)
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
error: '清空已删除的 API Keys 失败',
|
||||
message: error.message
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
// 👥 账户分组管理
|
||||
|
||||
// 创建账户分组
|
||||
@@ -1642,7 +2014,8 @@ router.post('/claude-accounts', authenticateAdmin, async (req, res) => {
|
||||
priority,
|
||||
groupId,
|
||||
groupIds,
|
||||
autoStopOnWarning
|
||||
autoStopOnWarning,
|
||||
useUnifiedUserAgent
|
||||
} = req.body
|
||||
|
||||
if (!name) {
|
||||
@@ -1682,7 +2055,8 @@ router.post('/claude-accounts', authenticateAdmin, async (req, res) => {
|
||||
accountType: accountType || 'shared', // 默认为共享类型
|
||||
platform,
|
||||
priority: priority || 50, // 默认优先级为50
|
||||
autoStopOnWarning: autoStopOnWarning === true // 默认为false
|
||||
autoStopOnWarning: autoStopOnWarning === true, // 默认为false
|
||||
useUnifiedUserAgent: useUnifiedUserAgent === true // 默认为false
|
||||
})
|
||||
|
||||
// 如果是分组类型,将账户添加到分组
|
||||
@@ -2032,7 +2406,9 @@ router.post('/claude-console-accounts', authenticateAdmin, async (req, res) => {
|
||||
rateLimitDuration,
|
||||
proxy,
|
||||
accountType,
|
||||
groupId
|
||||
groupId,
|
||||
dailyQuota,
|
||||
quotaResetTime
|
||||
} = req.body
|
||||
|
||||
if (!name || !apiUrl || !apiKey) {
|
||||
@@ -2067,7 +2443,9 @@ router.post('/claude-console-accounts', authenticateAdmin, async (req, res) => {
|
||||
rateLimitDuration:
|
||||
rateLimitDuration !== undefined && rateLimitDuration !== null ? rateLimitDuration : 60,
|
||||
proxy,
|
||||
accountType: accountType || 'shared'
|
||||
accountType: accountType || 'shared',
|
||||
dailyQuota: dailyQuota || 0,
|
||||
quotaResetTime: quotaResetTime || '00:00'
|
||||
})
|
||||
|
||||
// 如果是分组类型,将账户添加到分组
|
||||
@@ -2246,6 +2624,56 @@ router.put(
|
||||
}
|
||||
)
|
||||
|
||||
// 获取Claude Console账户的使用统计
|
||||
router.get('/claude-console-accounts/:accountId/usage', authenticateAdmin, async (req, res) => {
|
||||
try {
|
||||
const { accountId } = req.params
|
||||
const usageStats = await claudeConsoleAccountService.getAccountUsageStats(accountId)
|
||||
|
||||
if (!usageStats) {
|
||||
return res.status(404).json({ error: 'Account not found' })
|
||||
}
|
||||
|
||||
return res.json(usageStats)
|
||||
} catch (error) {
|
||||
logger.error('❌ Failed to get Claude Console account usage stats:', error)
|
||||
return res.status(500).json({ error: 'Failed to get usage stats', message: error.message })
|
||||
}
|
||||
})
|
||||
|
||||
// 手动重置Claude Console账户的每日使用量
|
||||
router.post(
|
||||
'/claude-console-accounts/:accountId/reset-usage',
|
||||
authenticateAdmin,
|
||||
async (req, res) => {
|
||||
try {
|
||||
const { accountId } = req.params
|
||||
await claudeConsoleAccountService.resetDailyUsage(accountId)
|
||||
|
||||
logger.success(`✅ Admin manually reset daily usage for Claude Console account: ${accountId}`)
|
||||
return res.json({ success: true, message: 'Daily usage reset successfully' })
|
||||
} catch (error) {
|
||||
logger.error('❌ Failed to reset Claude Console account daily usage:', error)
|
||||
return res.status(500).json({ error: 'Failed to reset daily usage', message: error.message })
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
// 手动重置所有Claude Console账户的每日使用量
|
||||
router.post('/claude-console-accounts/reset-all-usage', authenticateAdmin, async (req, res) => {
|
||||
try {
|
||||
await claudeConsoleAccountService.resetAllDailyUsage()
|
||||
|
||||
logger.success('✅ Admin manually reset daily usage for all Claude Console accounts')
|
||||
return res.json({ success: true, message: 'All daily usage reset successfully' })
|
||||
} catch (error) {
|
||||
logger.error('❌ Failed to reset all Claude Console accounts daily usage:', error)
|
||||
return res
|
||||
.status(500)
|
||||
.json({ error: 'Failed to reset all daily usage', message: error.message })
|
||||
}
|
||||
})
|
||||
|
||||
// ☁️ Bedrock 账户管理
|
||||
|
||||
// 获取所有Bedrock账户
|
||||
@@ -5317,7 +5745,9 @@ router.post('/openai-accounts', authenticateAdmin, async (req, res) => {
|
||||
accountType,
|
||||
groupId,
|
||||
rateLimitDuration,
|
||||
priority
|
||||
priority,
|
||||
needsImmediateRefresh, // 是否需要立即刷新
|
||||
requireRefreshSuccess // 是否必须刷新成功才能创建
|
||||
} = req.body
|
||||
|
||||
if (!name) {
|
||||
@@ -5326,7 +5756,8 @@ router.post('/openai-accounts', authenticateAdmin, async (req, res) => {
|
||||
message: '账户名称不能为空'
|
||||
})
|
||||
}
|
||||
// 创建账户数据
|
||||
|
||||
// 准备账户数据
|
||||
const accountData = {
|
||||
name,
|
||||
description: description || '',
|
||||
@@ -5341,7 +5772,83 @@ router.post('/openai-accounts', authenticateAdmin, async (req, res) => {
|
||||
schedulable: true
|
||||
}
|
||||
|
||||
// 创建账户
|
||||
// 如果需要立即刷新且必须成功(OpenAI 手动模式)
|
||||
if (needsImmediateRefresh && requireRefreshSuccess) {
|
||||
// 先创建临时账户以测试刷新
|
||||
const tempAccount = await openaiAccountService.createAccount(accountData)
|
||||
|
||||
try {
|
||||
logger.info(`🔄 测试刷新 OpenAI 账户以获取完整 token 信息`)
|
||||
|
||||
// 尝试刷新 token(会自动使用账户配置的代理)
|
||||
await openaiAccountService.refreshAccountToken(tempAccount.id)
|
||||
|
||||
// 刷新成功,获取更新后的账户信息
|
||||
const refreshedAccount = await openaiAccountService.getAccount(tempAccount.id)
|
||||
|
||||
// 检查是否获取到了 ID Token
|
||||
if (!refreshedAccount.idToken || refreshedAccount.idToken === '') {
|
||||
// 没有获取到 ID Token,删除账户
|
||||
await openaiAccountService.deleteAccount(tempAccount.id)
|
||||
throw new Error('无法获取 ID Token,请检查 Refresh Token 是否有效')
|
||||
}
|
||||
|
||||
// 如果是分组类型,添加到分组
|
||||
if (accountType === 'group' && groupId) {
|
||||
await accountGroupService.addAccountToGroup(tempAccount.id, groupId, 'openai')
|
||||
}
|
||||
|
||||
// 清除敏感信息后返回
|
||||
delete refreshedAccount.idToken
|
||||
delete refreshedAccount.accessToken
|
||||
delete refreshedAccount.refreshToken
|
||||
|
||||
logger.success(`✅ 创建并验证 OpenAI 账户成功: ${name} (ID: ${tempAccount.id})`)
|
||||
|
||||
return res.json({
|
||||
success: true,
|
||||
data: refreshedAccount,
|
||||
message: '账户创建成功,并已获取完整 token 信息'
|
||||
})
|
||||
} catch (refreshError) {
|
||||
// 刷新失败,删除临时创建的账户
|
||||
logger.warn(`❌ 刷新失败,删除临时账户: ${refreshError.message}`)
|
||||
await openaiAccountService.deleteAccount(tempAccount.id)
|
||||
|
||||
// 构建详细的错误信息
|
||||
const errorResponse = {
|
||||
success: false,
|
||||
message: '账户创建失败',
|
||||
error: refreshError.message
|
||||
}
|
||||
|
||||
// 添加更详细的错误信息
|
||||
if (refreshError.status) {
|
||||
errorResponse.errorCode = refreshError.status
|
||||
}
|
||||
if (refreshError.details) {
|
||||
errorResponse.errorDetails = refreshError.details
|
||||
}
|
||||
if (refreshError.code) {
|
||||
errorResponse.networkError = refreshError.code
|
||||
}
|
||||
|
||||
// 提供更友好的错误提示
|
||||
if (refreshError.message.includes('Refresh Token 无效')) {
|
||||
errorResponse.suggestion = '请检查 Refresh Token 是否正确,或重新通过 OAuth 授权获取'
|
||||
} else if (refreshError.message.includes('代理')) {
|
||||
errorResponse.suggestion = '请检查代理配置是否正确,包括地址、端口和认证信息'
|
||||
} else if (refreshError.message.includes('过于频繁')) {
|
||||
errorResponse.suggestion = '请稍后再试,或更换代理 IP'
|
||||
} else if (refreshError.message.includes('连接')) {
|
||||
errorResponse.suggestion = '请检查网络连接和代理设置'
|
||||
}
|
||||
|
||||
return res.status(400).json(errorResponse)
|
||||
}
|
||||
}
|
||||
|
||||
// 不需要强制刷新的情况(OAuth 模式或其他平台)
|
||||
const createdAccount = await openaiAccountService.createAccount(accountData)
|
||||
|
||||
// 如果是分组类型,添加到分组
|
||||
@@ -5349,6 +5856,17 @@ router.post('/openai-accounts', authenticateAdmin, async (req, res) => {
|
||||
await accountGroupService.addAccountToGroup(createdAccount.id, groupId, 'openai')
|
||||
}
|
||||
|
||||
// 如果需要刷新但不强制成功(OAuth 模式可能已有完整信息)
|
||||
if (needsImmediateRefresh && !requireRefreshSuccess) {
|
||||
try {
|
||||
logger.info(`🔄 尝试刷新 OpenAI 账户 ${createdAccount.id}`)
|
||||
await openaiAccountService.refreshAccountToken(createdAccount.id)
|
||||
logger.info(`✅ 刷新成功`)
|
||||
} catch (refreshError) {
|
||||
logger.warn(`⚠️ 刷新失败,但账户已创建: ${refreshError.message}`)
|
||||
}
|
||||
}
|
||||
|
||||
logger.success(`✅ 创建 OpenAI 账户成功: ${name} (ID: ${createdAccount.id})`)
|
||||
|
||||
return res.json({
|
||||
@@ -5370,6 +5888,7 @@ router.put('/openai-accounts/:id', authenticateAdmin, async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params
|
||||
const updates = req.body
|
||||
const { needsImmediateRefresh, requireRefreshSuccess } = updates
|
||||
|
||||
// 验证accountType的有效性
|
||||
if (updates.accountType && !['shared', 'dedicated', 'group'].includes(updates.accountType)) {
|
||||
@@ -5389,6 +5908,93 @@ router.put('/openai-accounts/:id', authenticateAdmin, async (req, res) => {
|
||||
return res.status(404).json({ error: 'Account not found' })
|
||||
}
|
||||
|
||||
// 如果更新了 Refresh Token,需要验证其有效性
|
||||
if (updates.openaiOauth?.refreshToken && needsImmediateRefresh && requireRefreshSuccess) {
|
||||
// 先更新 token 信息
|
||||
const tempUpdateData = {}
|
||||
if (updates.openaiOauth.refreshToken) {
|
||||
tempUpdateData.refreshToken = updates.openaiOauth.refreshToken
|
||||
}
|
||||
if (updates.openaiOauth.accessToken) {
|
||||
tempUpdateData.accessToken = updates.openaiOauth.accessToken
|
||||
}
|
||||
// 更新代理配置(如果有)
|
||||
if (updates.proxy !== undefined) {
|
||||
tempUpdateData.proxy = updates.proxy
|
||||
}
|
||||
|
||||
// 临时更新账户以测试新的 token
|
||||
await openaiAccountService.updateAccount(id, tempUpdateData)
|
||||
|
||||
try {
|
||||
logger.info(`🔄 验证更新的 OpenAI token (账户: ${id})`)
|
||||
|
||||
// 尝试刷新 token(会使用账户配置的代理)
|
||||
await openaiAccountService.refreshAccountToken(id)
|
||||
|
||||
// 获取刷新后的账户信息
|
||||
const refreshedAccount = await openaiAccountService.getAccount(id)
|
||||
|
||||
// 检查是否获取到了 ID Token
|
||||
if (!refreshedAccount.idToken || refreshedAccount.idToken === '') {
|
||||
// 恢复原始 token
|
||||
await openaiAccountService.updateAccount(id, {
|
||||
refreshToken: currentAccount.refreshToken,
|
||||
accessToken: currentAccount.accessToken,
|
||||
idToken: currentAccount.idToken
|
||||
})
|
||||
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: '无法获取 ID Token,请检查 Refresh Token 是否有效',
|
||||
error: 'Invalid refresh token'
|
||||
})
|
||||
}
|
||||
|
||||
logger.success(`✅ Token 验证成功,继续更新账户信息`)
|
||||
} catch (refreshError) {
|
||||
// 刷新失败,恢复原始 token
|
||||
logger.warn(`❌ Token 验证失败,恢复原始配置: ${refreshError.message}`)
|
||||
await openaiAccountService.updateAccount(id, {
|
||||
refreshToken: currentAccount.refreshToken,
|
||||
accessToken: currentAccount.accessToken,
|
||||
idToken: currentAccount.idToken,
|
||||
proxy: currentAccount.proxy
|
||||
})
|
||||
|
||||
// 构建详细的错误信息
|
||||
const errorResponse = {
|
||||
success: false,
|
||||
message: '更新失败',
|
||||
error: refreshError.message
|
||||
}
|
||||
|
||||
// 添加更详细的错误信息
|
||||
if (refreshError.status) {
|
||||
errorResponse.errorCode = refreshError.status
|
||||
}
|
||||
if (refreshError.details) {
|
||||
errorResponse.errorDetails = refreshError.details
|
||||
}
|
||||
if (refreshError.code) {
|
||||
errorResponse.networkError = refreshError.code
|
||||
}
|
||||
|
||||
// 提供更友好的错误提示
|
||||
if (refreshError.message.includes('Refresh Token 无效')) {
|
||||
errorResponse.suggestion = '请检查 Refresh Token 是否正确,或重新通过 OAuth 授权获取'
|
||||
} else if (refreshError.message.includes('代理')) {
|
||||
errorResponse.suggestion = '请检查代理配置是否正确,包括地址、端口和认证信息'
|
||||
} else if (refreshError.message.includes('过于频繁')) {
|
||||
errorResponse.suggestion = '请稍后再试,或更换代理 IP'
|
||||
} else if (refreshError.message.includes('连接')) {
|
||||
errorResponse.suggestion = '请检查网络连接和代理设置'
|
||||
}
|
||||
|
||||
return res.status(400).json(errorResponse)
|
||||
}
|
||||
}
|
||||
|
||||
// 处理分组的变更
|
||||
if (updates.accountType !== undefined) {
|
||||
// 如果之前是分组类型,需要从原分组中移除
|
||||
@@ -5410,9 +6016,7 @@ router.put('/openai-accounts/:id', authenticateAdmin, async (req, res) => {
|
||||
// 处理敏感数据加密
|
||||
if (updates.openaiOauth) {
|
||||
updateData.openaiOauth = updates.openaiOauth
|
||||
if (updates.openaiOauth.idToken) {
|
||||
updateData.idToken = updates.openaiOauth.idToken
|
||||
}
|
||||
// 编辑时不允许直接输入 ID Token,只能通过刷新获取
|
||||
if (updates.openaiOauth.accessToken) {
|
||||
updateData.accessToken = updates.openaiOauth.accessToken
|
||||
}
|
||||
@@ -5446,6 +6050,17 @@ router.put('/openai-accounts/:id', authenticateAdmin, async (req, res) => {
|
||||
|
||||
const updatedAccount = await openaiAccountService.updateAccount(id, updateData)
|
||||
|
||||
// 如果需要刷新但不强制成功(非关键更新)
|
||||
if (needsImmediateRefresh && !requireRefreshSuccess) {
|
||||
try {
|
||||
logger.info(`🔄 尝试刷新 OpenAI 账户 ${id}`)
|
||||
await openaiAccountService.refreshAccountToken(id)
|
||||
logger.info(`✅ 刷新成功`)
|
||||
} catch (refreshError) {
|
||||
logger.warn(`⚠️ 刷新失败,但账户信息已更新: ${refreshError.message}`)
|
||||
}
|
||||
}
|
||||
|
||||
logger.success(`📝 Admin updated OpenAI account: ${id}`)
|
||||
return res.json({ success: true, data: updatedAccount })
|
||||
} catch (error) {
|
||||
|
||||
@@ -407,6 +407,317 @@ router.post('/api/user-stats', async (req, res) => {
|
||||
}
|
||||
})
|
||||
|
||||
// 📊 批量查询统计数据接口
|
||||
router.post('/api/batch-stats', async (req, res) => {
|
||||
try {
|
||||
const { apiIds } = req.body
|
||||
|
||||
// 验证输入
|
||||
if (!apiIds || !Array.isArray(apiIds) || apiIds.length === 0) {
|
||||
return res.status(400).json({
|
||||
error: 'Invalid input',
|
||||
message: 'API IDs array is required'
|
||||
})
|
||||
}
|
||||
|
||||
// 限制最多查询 30 个
|
||||
if (apiIds.length > 30) {
|
||||
return res.status(400).json({
|
||||
error: 'Too many keys',
|
||||
message: 'Maximum 30 API keys can be queried at once'
|
||||
})
|
||||
}
|
||||
|
||||
// 验证所有 ID 格式
|
||||
const uuidRegex = /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i
|
||||
const invalidIds = apiIds.filter((id) => !uuidRegex.test(id))
|
||||
if (invalidIds.length > 0) {
|
||||
return res.status(400).json({
|
||||
error: 'Invalid API ID format',
|
||||
message: `Invalid API IDs: ${invalidIds.join(', ')}`
|
||||
})
|
||||
}
|
||||
|
||||
const individualStats = []
|
||||
const aggregated = {
|
||||
totalKeys: apiIds.length,
|
||||
activeKeys: 0,
|
||||
usage: {
|
||||
requests: 0,
|
||||
inputTokens: 0,
|
||||
outputTokens: 0,
|
||||
cacheCreateTokens: 0,
|
||||
cacheReadTokens: 0,
|
||||
allTokens: 0,
|
||||
cost: 0,
|
||||
formattedCost: '$0.000000'
|
||||
},
|
||||
dailyUsage: {
|
||||
requests: 0,
|
||||
inputTokens: 0,
|
||||
outputTokens: 0,
|
||||
cacheCreateTokens: 0,
|
||||
cacheReadTokens: 0,
|
||||
allTokens: 0,
|
||||
cost: 0,
|
||||
formattedCost: '$0.000000'
|
||||
},
|
||||
monthlyUsage: {
|
||||
requests: 0,
|
||||
inputTokens: 0,
|
||||
outputTokens: 0,
|
||||
cacheCreateTokens: 0,
|
||||
cacheReadTokens: 0,
|
||||
allTokens: 0,
|
||||
cost: 0,
|
||||
formattedCost: '$0.000000'
|
||||
}
|
||||
}
|
||||
|
||||
// 并行查询所有 API Key 数据(复用单key查询逻辑)
|
||||
const results = await Promise.allSettled(
|
||||
apiIds.map(async (apiId) => {
|
||||
const keyData = await redis.getApiKey(apiId)
|
||||
|
||||
if (!keyData || Object.keys(keyData).length === 0) {
|
||||
return { error: 'Not found', apiId }
|
||||
}
|
||||
|
||||
// 检查是否激活
|
||||
if (keyData.isActive !== 'true') {
|
||||
return { error: 'Disabled', apiId }
|
||||
}
|
||||
|
||||
// 检查是否过期
|
||||
if (keyData.expiresAt && new Date() > new Date(keyData.expiresAt)) {
|
||||
return { error: 'Expired', apiId }
|
||||
}
|
||||
|
||||
// 复用单key查询的逻辑:获取使用统计
|
||||
const usage = await redis.getUsageStats(apiId)
|
||||
|
||||
// 获取费用统计(与单key查询一致)
|
||||
const costStats = await redis.getCostStats(apiId)
|
||||
|
||||
return {
|
||||
apiId,
|
||||
name: keyData.name,
|
||||
description: keyData.description || '',
|
||||
isActive: true,
|
||||
createdAt: keyData.createdAt,
|
||||
usage: usage.total || {},
|
||||
dailyStats: {
|
||||
...usage.daily,
|
||||
cost: costStats.daily
|
||||
},
|
||||
monthlyStats: {
|
||||
...usage.monthly,
|
||||
cost: costStats.monthly
|
||||
},
|
||||
totalCost: costStats.total
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
// 处理结果并聚合
|
||||
results.forEach((result) => {
|
||||
if (result.status === 'fulfilled' && result.value && !result.value.error) {
|
||||
const stats = result.value
|
||||
aggregated.activeKeys++
|
||||
|
||||
// 聚合总使用量
|
||||
if (stats.usage) {
|
||||
aggregated.usage.requests += stats.usage.requests || 0
|
||||
aggregated.usage.inputTokens += stats.usage.inputTokens || 0
|
||||
aggregated.usage.outputTokens += stats.usage.outputTokens || 0
|
||||
aggregated.usage.cacheCreateTokens += stats.usage.cacheCreateTokens || 0
|
||||
aggregated.usage.cacheReadTokens += stats.usage.cacheReadTokens || 0
|
||||
aggregated.usage.allTokens += stats.usage.allTokens || 0
|
||||
}
|
||||
|
||||
// 聚合总费用
|
||||
aggregated.usage.cost += stats.totalCost || 0
|
||||
|
||||
// 聚合今日使用量
|
||||
aggregated.dailyUsage.requests += stats.dailyStats.requests || 0
|
||||
aggregated.dailyUsage.inputTokens += stats.dailyStats.inputTokens || 0
|
||||
aggregated.dailyUsage.outputTokens += stats.dailyStats.outputTokens || 0
|
||||
aggregated.dailyUsage.cacheCreateTokens += stats.dailyStats.cacheCreateTokens || 0
|
||||
aggregated.dailyUsage.cacheReadTokens += stats.dailyStats.cacheReadTokens || 0
|
||||
aggregated.dailyUsage.allTokens += stats.dailyStats.allTokens || 0
|
||||
aggregated.dailyUsage.cost += stats.dailyStats.cost || 0
|
||||
|
||||
// 聚合本月使用量
|
||||
aggregated.monthlyUsage.requests += stats.monthlyStats.requests || 0
|
||||
aggregated.monthlyUsage.inputTokens += stats.monthlyStats.inputTokens || 0
|
||||
aggregated.monthlyUsage.outputTokens += stats.monthlyStats.outputTokens || 0
|
||||
aggregated.monthlyUsage.cacheCreateTokens += stats.monthlyStats.cacheCreateTokens || 0
|
||||
aggregated.monthlyUsage.cacheReadTokens += stats.monthlyStats.cacheReadTokens || 0
|
||||
aggregated.monthlyUsage.allTokens += stats.monthlyStats.allTokens || 0
|
||||
aggregated.monthlyUsage.cost += stats.monthlyStats.cost || 0
|
||||
|
||||
// 添加到个体统计
|
||||
individualStats.push({
|
||||
apiId: stats.apiId,
|
||||
name: stats.name,
|
||||
isActive: true,
|
||||
usage: stats.usage,
|
||||
dailyUsage: {
|
||||
...stats.dailyStats,
|
||||
formattedCost: CostCalculator.formatCost(stats.dailyStats.cost || 0)
|
||||
},
|
||||
monthlyUsage: {
|
||||
...stats.monthlyStats,
|
||||
formattedCost: CostCalculator.formatCost(stats.monthlyStats.cost || 0)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
// 格式化费用显示
|
||||
aggregated.usage.formattedCost = CostCalculator.formatCost(aggregated.usage.cost)
|
||||
aggregated.dailyUsage.formattedCost = CostCalculator.formatCost(aggregated.dailyUsage.cost)
|
||||
aggregated.monthlyUsage.formattedCost = CostCalculator.formatCost(aggregated.monthlyUsage.cost)
|
||||
|
||||
logger.api(`📊 Batch stats query for ${apiIds.length} keys from ${req.ip || 'unknown'}`)
|
||||
|
||||
return res.json({
|
||||
success: true,
|
||||
data: {
|
||||
aggregated,
|
||||
individual: individualStats
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
logger.error('❌ Failed to process batch stats query:', error)
|
||||
return res.status(500).json({
|
||||
error: 'Internal server error',
|
||||
message: 'Failed to retrieve batch statistics'
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
// 📊 批量模型统计查询接口
|
||||
router.post('/api/batch-model-stats', async (req, res) => {
|
||||
try {
|
||||
const { apiIds, period = 'daily' } = req.body
|
||||
|
||||
// 验证输入
|
||||
if (!apiIds || !Array.isArray(apiIds) || apiIds.length === 0) {
|
||||
return res.status(400).json({
|
||||
error: 'Invalid input',
|
||||
message: 'API IDs array is required'
|
||||
})
|
||||
}
|
||||
|
||||
// 限制最多查询 30 个
|
||||
if (apiIds.length > 30) {
|
||||
return res.status(400).json({
|
||||
error: 'Too many keys',
|
||||
message: 'Maximum 30 API keys can be queried at once'
|
||||
})
|
||||
}
|
||||
|
||||
const client = redis.getClientSafe()
|
||||
const tzDate = redis.getDateInTimezone()
|
||||
const today = redis.getDateStringInTimezone()
|
||||
const currentMonth = `${tzDate.getFullYear()}-${String(tzDate.getMonth() + 1).padStart(2, '0')}`
|
||||
|
||||
const modelUsageMap = new Map()
|
||||
|
||||
// 并行查询所有 API Key 的模型统计
|
||||
await Promise.all(
|
||||
apiIds.map(async (apiId) => {
|
||||
const pattern =
|
||||
period === 'daily'
|
||||
? `usage:${apiId}:model:daily:*:${today}`
|
||||
: `usage:${apiId}:model:monthly:*:${currentMonth}`
|
||||
|
||||
const keys = await client.keys(pattern)
|
||||
|
||||
for (const key of keys) {
|
||||
const match = key.match(
|
||||
period === 'daily'
|
||||
? /usage:.+:model:daily:(.+):\d{4}-\d{2}-\d{2}$/
|
||||
: /usage:.+:model:monthly:(.+):\d{4}-\d{2}$/
|
||||
)
|
||||
|
||||
if (!match) {
|
||||
continue
|
||||
}
|
||||
|
||||
const model = match[1]
|
||||
const data = await client.hgetall(key)
|
||||
|
||||
if (data && Object.keys(data).length > 0) {
|
||||
if (!modelUsageMap.has(model)) {
|
||||
modelUsageMap.set(model, {
|
||||
requests: 0,
|
||||
inputTokens: 0,
|
||||
outputTokens: 0,
|
||||
cacheCreateTokens: 0,
|
||||
cacheReadTokens: 0,
|
||||
allTokens: 0
|
||||
})
|
||||
}
|
||||
|
||||
const modelUsage = modelUsageMap.get(model)
|
||||
modelUsage.requests += parseInt(data.requests) || 0
|
||||
modelUsage.inputTokens += parseInt(data.inputTokens) || 0
|
||||
modelUsage.outputTokens += parseInt(data.outputTokens) || 0
|
||||
modelUsage.cacheCreateTokens += parseInt(data.cacheCreateTokens) || 0
|
||||
modelUsage.cacheReadTokens += parseInt(data.cacheReadTokens) || 0
|
||||
modelUsage.allTokens += parseInt(data.allTokens) || 0
|
||||
}
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
// 转换为数组并计算费用
|
||||
const modelStats = []
|
||||
for (const [model, usage] of modelUsageMap) {
|
||||
const usageData = {
|
||||
input_tokens: usage.inputTokens,
|
||||
output_tokens: usage.outputTokens,
|
||||
cache_creation_input_tokens: usage.cacheCreateTokens,
|
||||
cache_read_input_tokens: usage.cacheReadTokens
|
||||
}
|
||||
|
||||
const costData = CostCalculator.calculateCost(usageData, model)
|
||||
|
||||
modelStats.push({
|
||||
model,
|
||||
requests: usage.requests,
|
||||
inputTokens: usage.inputTokens,
|
||||
outputTokens: usage.outputTokens,
|
||||
cacheCreateTokens: usage.cacheCreateTokens,
|
||||
cacheReadTokens: usage.cacheReadTokens,
|
||||
allTokens: usage.allTokens,
|
||||
costs: costData.costs,
|
||||
formatted: costData.formatted,
|
||||
pricing: costData.pricing
|
||||
})
|
||||
}
|
||||
|
||||
// 按总 token 数降序排列
|
||||
modelStats.sort((a, b) => b.allTokens - a.allTokens)
|
||||
|
||||
logger.api(`📊 Batch model stats query for ${apiIds.length} keys, period: ${period}`)
|
||||
|
||||
return res.json({
|
||||
success: true,
|
||||
data: modelStats,
|
||||
period
|
||||
})
|
||||
} catch (error) {
|
||||
logger.error('❌ Failed to process batch model stats query:', error)
|
||||
return res.status(500).json({
|
||||
error: 'Internal server error',
|
||||
message: 'Failed to retrieve batch model statistics'
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
// 📊 用户模型统计查询接口 - 安全的自查询接口
|
||||
router.post('/api/user-model-stats', async (req, res) => {
|
||||
try {
|
||||
|
||||
@@ -343,20 +343,22 @@ async function handleLoadCodeAssist(req, res) {
|
||||
|
||||
const client = await geminiAccountService.getOauthClient(accessToken, refreshToken, proxyConfig)
|
||||
|
||||
// 根据账户配置决定项目ID:
|
||||
// 1. 如果账户有项目ID -> 使用账户的项目ID(强制覆盖)
|
||||
// 2. 如果账户没有项目ID -> 传递 null(移除项目ID)
|
||||
let effectiveProjectId = null
|
||||
// 智能处理项目ID:
|
||||
// 1. 如果账户配置了项目ID -> 使用账户的项目ID(覆盖请求中的)
|
||||
// 2. 如果账户没有项目ID -> 使用请求中的cloudaicompanionProject
|
||||
// 3. 都没有 -> 传null
|
||||
const effectiveProjectId = projectId || cloudaicompanionProject || null
|
||||
|
||||
if (projectId) {
|
||||
// 账户配置了项目ID,强制使用它
|
||||
effectiveProjectId = projectId
|
||||
logger.info('Using account project ID for loadCodeAssist:', effectiveProjectId)
|
||||
} else {
|
||||
// 账户没有配置项目ID,确保不传递项目ID
|
||||
effectiveProjectId = null
|
||||
logger.info('No project ID in account for loadCodeAssist, removing project parameter')
|
||||
}
|
||||
logger.info('📋 loadCodeAssist项目ID处理逻辑', {
|
||||
accountProjectId: projectId,
|
||||
requestProjectId: cloudaicompanionProject,
|
||||
effectiveProjectId,
|
||||
decision: projectId
|
||||
? '使用账户配置'
|
||||
: cloudaicompanionProject
|
||||
? '使用请求参数'
|
||||
: '不使用项目ID'
|
||||
})
|
||||
|
||||
const response = await geminiAccountService.loadCodeAssist(
|
||||
client,
|
||||
@@ -413,20 +415,22 @@ async function handleOnboardUser(req, res) {
|
||||
|
||||
const client = await geminiAccountService.getOauthClient(accessToken, refreshToken, proxyConfig)
|
||||
|
||||
// 根据账户配置决定项目ID:
|
||||
// 1. 如果账户有项目ID -> 使用账户的项目ID(强制覆盖)
|
||||
// 2. 如果账户没有项目ID -> 传递 null(移除项目ID)
|
||||
let effectiveProjectId = null
|
||||
// 智能处理项目ID:
|
||||
// 1. 如果账户配置了项目ID -> 使用账户的项目ID(覆盖请求中的)
|
||||
// 2. 如果账户没有项目ID -> 使用请求中的cloudaicompanionProject
|
||||
// 3. 都没有 -> 传null
|
||||
const effectiveProjectId = projectId || cloudaicompanionProject || null
|
||||
|
||||
if (projectId) {
|
||||
// 账户配置了项目ID,强制使用它
|
||||
effectiveProjectId = projectId
|
||||
logger.info('Using account project ID:', effectiveProjectId)
|
||||
} else {
|
||||
// 账户没有配置项目ID,确保不传递项目ID(即使客户端传了也要移除)
|
||||
effectiveProjectId = null
|
||||
logger.info('No project ID in account, removing project parameter')
|
||||
}
|
||||
logger.info('📋 onboardUser项目ID处理逻辑', {
|
||||
accountProjectId: projectId,
|
||||
requestProjectId: cloudaicompanionProject,
|
||||
effectiveProjectId,
|
||||
decision: projectId
|
||||
? '使用账户配置'
|
||||
: cloudaicompanionProject
|
||||
? '使用请求参数'
|
||||
: '不使用项目ID'
|
||||
})
|
||||
|
||||
// 如果提供了 tierId,直接调用 onboardUser
|
||||
if (tierId) {
|
||||
@@ -593,11 +597,24 @@ async function handleGenerateContent(req, res) {
|
||||
|
||||
const client = await geminiAccountService.getOauthClient(accessToken, refreshToken, proxyConfig)
|
||||
|
||||
// 智能处理项目ID:
|
||||
// 1. 如果账户配置了项目ID -> 使用账户的项目ID(覆盖请求中的)
|
||||
// 2. 如果账户没有项目ID -> 使用请求中的项目ID(如果有的话)
|
||||
// 3. 都没有 -> 传null
|
||||
const effectiveProjectId = account.projectId || project || null
|
||||
|
||||
logger.info('📋 项目ID处理逻辑', {
|
||||
accountProjectId: account.projectId,
|
||||
requestProjectId: project,
|
||||
effectiveProjectId,
|
||||
decision: account.projectId ? '使用账户配置' : project ? '使用请求参数' : '不使用项目ID'
|
||||
})
|
||||
|
||||
const response = await geminiAccountService.generateContent(
|
||||
client,
|
||||
{ model, request: actualRequestData },
|
||||
user_prompt_id,
|
||||
account.projectId, // 始终使用账户配置的项目ID,忽略请求中的project
|
||||
effectiveProjectId, // 使用智能决策的项目ID
|
||||
req.apiKey?.id, // 使用 API Key ID 作为 session ID
|
||||
proxyConfig // 传递代理配置
|
||||
)
|
||||
@@ -729,11 +746,24 @@ async function handleStreamGenerateContent(req, res) {
|
||||
|
||||
const client = await geminiAccountService.getOauthClient(accessToken, refreshToken, proxyConfig)
|
||||
|
||||
// 智能处理项目ID:
|
||||
// 1. 如果账户配置了项目ID -> 使用账户的项目ID(覆盖请求中的)
|
||||
// 2. 如果账户没有项目ID -> 使用请求中的项目ID(如果有的话)
|
||||
// 3. 都没有 -> 传null
|
||||
const effectiveProjectId = account.projectId || project || null
|
||||
|
||||
logger.info('📋 流式请求项目ID处理逻辑', {
|
||||
accountProjectId: account.projectId,
|
||||
requestProjectId: project,
|
||||
effectiveProjectId,
|
||||
decision: account.projectId ? '使用账户配置' : project ? '使用请求参数' : '不使用项目ID'
|
||||
})
|
||||
|
||||
const streamResponse = await geminiAccountService.generateContentStream(
|
||||
client,
|
||||
{ model, request: actualRequestData },
|
||||
user_prompt_id,
|
||||
account.projectId, // 始终使用账户配置的项目ID,忽略请求中的project
|
||||
effectiveProjectId, // 使用智能决策的项目ID
|
||||
req.apiKey?.id, // 使用 API Key ID 作为 session ID
|
||||
abortController.signal, // 传递中止信号
|
||||
proxyConfig // 传递代理配置
|
||||
|
||||
@@ -3,7 +3,6 @@ const axios = require('axios')
|
||||
const router = express.Router()
|
||||
const logger = require('../utils/logger')
|
||||
const { authenticateApiKey } = require('../middleware/auth')
|
||||
const claudeAccountService = require('../services/claudeAccountService')
|
||||
const unifiedOpenAIScheduler = require('../services/unifiedOpenAIScheduler')
|
||||
const openaiAccountService = require('../services/openaiAccountService')
|
||||
const apiKeyService = require('../services/apiKeyService')
|
||||
@@ -35,13 +34,31 @@ async function getOpenAIAuthToken(apiKeyData, sessionId = null, requestedModel =
|
||||
}
|
||||
|
||||
// 获取账户详情
|
||||
const account = await openaiAccountService.getAccount(result.accountId)
|
||||
let account = await openaiAccountService.getAccount(result.accountId)
|
||||
if (!account || !account.accessToken) {
|
||||
throw new Error(`OpenAI account ${result.accountId} has no valid accessToken`)
|
||||
}
|
||||
|
||||
// 解密 accessToken
|
||||
const accessToken = claudeAccountService._decryptSensitiveData(account.accessToken)
|
||||
// 检查 token 是否过期并自动刷新(双重保护)
|
||||
if (openaiAccountService.isTokenExpired(account)) {
|
||||
if (account.refreshToken) {
|
||||
logger.info(`🔄 Token expired, auto-refreshing for account ${account.name} (fallback)`)
|
||||
try {
|
||||
await openaiAccountService.refreshAccountToken(result.accountId)
|
||||
// 重新获取更新后的账户
|
||||
account = await openaiAccountService.getAccount(result.accountId)
|
||||
logger.info(`✅ Token refreshed successfully in route handler`)
|
||||
} catch (refreshError) {
|
||||
logger.error(`Failed to refresh token for ${account.name}:`, refreshError)
|
||||
throw new Error(`Token expired and refresh failed: ${refreshError.message}`)
|
||||
}
|
||||
} else {
|
||||
throw new Error(`Token expired and no refresh token available for account ${account.name}`)
|
||||
}
|
||||
}
|
||||
|
||||
// 解密 accessToken(account.accessToken 是加密的)
|
||||
const accessToken = openaiAccountService.decrypt(account.accessToken)
|
||||
if (!accessToken) {
|
||||
throw new Error('Failed to decrypt OpenAI accessToken')
|
||||
}
|
||||
@@ -161,7 +178,7 @@ router.post('/responses', authenticateApiKey, async (req, res) => {
|
||||
// 配置请求选项
|
||||
const axiosConfig = {
|
||||
headers,
|
||||
timeout: 60000,
|
||||
timeout: 60 * 1000 * 10,
|
||||
validateStatus: () => true
|
||||
}
|
||||
|
||||
|
||||
@@ -208,7 +208,8 @@ router.get('/profile', authenticateUser, async (req, res) => {
|
||||
totalUsage: user.totalUsage
|
||||
},
|
||||
config: {
|
||||
maxApiKeysPerUser: config.userManagement.maxApiKeysPerUser
|
||||
maxApiKeysPerUser: config.userManagement.maxApiKeysPerUser,
|
||||
allowUserDeleteApiKeys: config.userManagement.allowUserDeleteApiKeys
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
@@ -352,6 +353,15 @@ router.delete('/api-keys/:keyId', authenticateUser, async (req, res) => {
|
||||
try {
|
||||
const { keyId } = req.params
|
||||
|
||||
// 检查是否允许用户删除自己的API Keys
|
||||
if (!config.userManagement.allowUserDeleteApiKeys) {
|
||||
return res.status(403).json({
|
||||
error: 'Operation not allowed',
|
||||
message:
|
||||
'Users are not allowed to delete their own API keys. Please contact an administrator.'
|
||||
})
|
||||
}
|
||||
|
||||
// 检查API Key是否属于当前用户
|
||||
const existingKey = await apiKeyService.getApiKeyById(keyId)
|
||||
if (!existingKey || existingKey.userId !== req.user.id) {
|
||||
|
||||
@@ -34,7 +34,9 @@ class ApiKeyService {
|
||||
allowedClients = [],
|
||||
dailyCostLimit = 0,
|
||||
weeklyOpusCostLimit = 0,
|
||||
tags = []
|
||||
tags = [],
|
||||
activationDays = 0, // 新增:激活后有效天数(0表示不使用此功能)
|
||||
expirationMode = 'fixed' // 新增:过期模式 'fixed'(固定时间) 或 'activation'(首次使用后激活)
|
||||
} = options
|
||||
|
||||
// 生成简单的API Key (64字符十六进制)
|
||||
@@ -67,9 +69,13 @@ class ApiKeyService {
|
||||
dailyCostLimit: String(dailyCostLimit || 0),
|
||||
weeklyOpusCostLimit: String(weeklyOpusCostLimit || 0),
|
||||
tags: JSON.stringify(tags || []),
|
||||
activationDays: String(activationDays || 0), // 新增:激活后有效天数
|
||||
expirationMode: expirationMode || 'fixed', // 新增:过期模式
|
||||
isActivated: expirationMode === 'fixed' ? 'true' : 'false', // 根据模式决定激活状态
|
||||
activatedAt: expirationMode === 'fixed' ? new Date().toISOString() : '', // 激活时间
|
||||
createdAt: new Date().toISOString(),
|
||||
lastUsedAt: '',
|
||||
expiresAt: expiresAt || '',
|
||||
expiresAt: expirationMode === 'fixed' ? expiresAt || '' : '', // 固定模式才设置过期时间
|
||||
createdBy: options.createdBy || 'admin',
|
||||
userId: options.userId || '',
|
||||
userUsername: options.userUsername || ''
|
||||
@@ -105,6 +111,10 @@ class ApiKeyService {
|
||||
dailyCostLimit: parseFloat(keyData.dailyCostLimit || 0),
|
||||
weeklyOpusCostLimit: parseFloat(keyData.weeklyOpusCostLimit || 0),
|
||||
tags: JSON.parse(keyData.tags || '[]'),
|
||||
activationDays: parseInt(keyData.activationDays || 0),
|
||||
expirationMode: keyData.expirationMode || 'fixed',
|
||||
isActivated: keyData.isActivated === 'true',
|
||||
activatedAt: keyData.activatedAt,
|
||||
createdAt: keyData.createdAt,
|
||||
expiresAt: keyData.expiresAt,
|
||||
createdBy: keyData.createdBy
|
||||
@@ -133,6 +143,27 @@ class ApiKeyService {
|
||||
return { valid: false, error: 'API key is disabled' }
|
||||
}
|
||||
|
||||
// 处理激活逻辑(仅在 activation 模式下)
|
||||
if (keyData.expirationMode === 'activation' && keyData.isActivated !== 'true') {
|
||||
// 首次使用,需要激活
|
||||
const now = new Date()
|
||||
const activationDays = parseInt(keyData.activationDays || 30) // 默认30天
|
||||
const expiresAt = new Date(now.getTime() + activationDays * 24 * 60 * 60 * 1000)
|
||||
|
||||
// 更新激活状态和过期时间
|
||||
keyData.isActivated = 'true'
|
||||
keyData.activatedAt = now.toISOString()
|
||||
keyData.expiresAt = expiresAt.toISOString()
|
||||
keyData.lastUsedAt = now.toISOString()
|
||||
|
||||
// 保存到Redis
|
||||
await redis.setApiKey(keyData.id, keyData)
|
||||
|
||||
logger.success(
|
||||
`🔓 API key activated: ${keyData.id} (${keyData.name}), will expire in ${activationDays} days at ${expiresAt.toISOString()}`
|
||||
)
|
||||
}
|
||||
|
||||
// 检查是否过期
|
||||
if (keyData.expiresAt && new Date() > new Date(keyData.expiresAt)) {
|
||||
return { valid: false, error: 'API key has expired' }
|
||||
@@ -261,6 +292,10 @@ class ApiKeyService {
|
||||
key.weeklyOpusCostLimit = parseFloat(key.weeklyOpusCostLimit || 0)
|
||||
key.dailyCost = (await redis.getDailyCost(key.id)) || 0
|
||||
key.weeklyOpusCost = (await redis.getWeeklyOpusCost(key.id)) || 0
|
||||
key.activationDays = parseInt(key.activationDays || 0)
|
||||
key.expirationMode = key.expirationMode || 'fixed'
|
||||
key.isActivated = key.isActivated === 'true'
|
||||
key.activatedAt = key.activatedAt || null
|
||||
|
||||
// 获取当前时间窗口的请求次数、Token使用量和费用
|
||||
if (key.rateLimitWindow > 0) {
|
||||
@@ -362,13 +397,20 @@ class ApiKeyService {
|
||||
'bedrockAccountId', // 添加 Bedrock 账号ID
|
||||
'permissions',
|
||||
'expiresAt',
|
||||
'activationDays', // 新增:激活后有效天数
|
||||
'expirationMode', // 新增:过期模式
|
||||
'isActivated', // 新增:是否已激活
|
||||
'activatedAt', // 新增:激活时间
|
||||
'enableModelRestriction',
|
||||
'restrictedModels',
|
||||
'enableClientRestriction',
|
||||
'allowedClients',
|
||||
'dailyCostLimit',
|
||||
'weeklyOpusCostLimit',
|
||||
'tags'
|
||||
'tags',
|
||||
'userId', // 新增:用户ID(所有者变更)
|
||||
'userUsername', // 新增:用户名(所有者变更)
|
||||
'createdBy' // 新增:创建者(所有者变更)
|
||||
]
|
||||
const updatedData = { ...keyData }
|
||||
|
||||
@@ -377,9 +419,16 @@ class ApiKeyService {
|
||||
if (field === 'restrictedModels' || field === 'allowedClients' || field === 'tags') {
|
||||
// 特殊处理数组字段
|
||||
updatedData[field] = JSON.stringify(value || [])
|
||||
} else if (field === 'enableModelRestriction' || field === 'enableClientRestriction') {
|
||||
} else if (
|
||||
field === 'enableModelRestriction' ||
|
||||
field === 'enableClientRestriction' ||
|
||||
field === 'isActivated'
|
||||
) {
|
||||
// 布尔值转字符串
|
||||
updatedData[field] = String(value)
|
||||
} else if (field === 'expiresAt' || field === 'activatedAt') {
|
||||
// 日期字段保持原样,不要toString()
|
||||
updatedData[field] = value || ''
|
||||
} else {
|
||||
updatedData[field] = (value !== null && value !== undefined ? value : '').toString()
|
||||
}
|
||||
@@ -434,6 +483,139 @@ class ApiKeyService {
|
||||
}
|
||||
}
|
||||
|
||||
// 🔄 恢复已删除的API Key
|
||||
async restoreApiKey(keyId, restoredBy = 'system', restoredByType = 'system') {
|
||||
try {
|
||||
const keyData = await redis.getApiKey(keyId)
|
||||
if (!keyData || Object.keys(keyData).length === 0) {
|
||||
throw new Error('API key not found')
|
||||
}
|
||||
|
||||
// 检查是否确实是已删除的key
|
||||
if (keyData.isDeleted !== 'true') {
|
||||
throw new Error('API key is not deleted')
|
||||
}
|
||||
|
||||
// 准备更新的数据
|
||||
const updatedData = { ...keyData }
|
||||
updatedData.isActive = 'true'
|
||||
updatedData.restoredAt = new Date().toISOString()
|
||||
updatedData.restoredBy = restoredBy
|
||||
updatedData.restoredByType = restoredByType
|
||||
|
||||
// 从更新的数据中移除删除相关的字段
|
||||
delete updatedData.isDeleted
|
||||
delete updatedData.deletedAt
|
||||
delete updatedData.deletedBy
|
||||
delete updatedData.deletedByType
|
||||
|
||||
// 保存更新后的数据
|
||||
await redis.setApiKey(keyId, updatedData)
|
||||
|
||||
// 使用Redis的hdel命令删除不需要的字段
|
||||
const keyName = `apikey:${keyId}`
|
||||
await redis.client.hdel(keyName, 'isDeleted', 'deletedAt', 'deletedBy', 'deletedByType')
|
||||
|
||||
// 重新建立哈希映射(恢复API Key的使用能力)
|
||||
if (keyData.apiKey) {
|
||||
await redis.setApiKeyHash(keyData.apiKey, {
|
||||
id: keyId,
|
||||
name: keyData.name,
|
||||
isActive: 'true'
|
||||
})
|
||||
}
|
||||
|
||||
logger.success(`✅ Restored API key: ${keyId} by ${restoredBy} (${restoredByType})`)
|
||||
|
||||
return { success: true, apiKey: updatedData }
|
||||
} catch (error) {
|
||||
logger.error('❌ Failed to restore API key:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
// 🗑️ 彻底删除API Key(物理删除)
|
||||
async permanentDeleteApiKey(keyId) {
|
||||
try {
|
||||
const keyData = await redis.getApiKey(keyId)
|
||||
if (!keyData || Object.keys(keyData).length === 0) {
|
||||
throw new Error('API key not found')
|
||||
}
|
||||
|
||||
// 确保只能彻底删除已经软删除的key
|
||||
if (keyData.isDeleted !== 'true') {
|
||||
throw new Error('只能彻底删除已经删除的API Key')
|
||||
}
|
||||
|
||||
// 删除所有相关的使用统计数据
|
||||
const today = new Date().toISOString().split('T')[0]
|
||||
const yesterday = new Date(Date.now() - 86400000).toISOString().split('T')[0]
|
||||
|
||||
// 删除每日统计
|
||||
await redis.client.del(`usage:daily:${today}:${keyId}`)
|
||||
await redis.client.del(`usage:daily:${yesterday}:${keyId}`)
|
||||
|
||||
// 删除月度统计
|
||||
const currentMonth = today.substring(0, 7)
|
||||
await redis.client.del(`usage:monthly:${currentMonth}:${keyId}`)
|
||||
|
||||
// 删除所有相关的统计键(通过模式匹配)
|
||||
const usageKeys = await redis.client.keys(`usage:*:${keyId}*`)
|
||||
if (usageKeys.length > 0) {
|
||||
await redis.client.del(...usageKeys)
|
||||
}
|
||||
|
||||
// 删除API Key本身
|
||||
await redis.deleteApiKey(keyId)
|
||||
|
||||
logger.success(`🗑️ Permanently deleted API key: ${keyId}`)
|
||||
|
||||
return { success: true }
|
||||
} catch (error) {
|
||||
logger.error('❌ Failed to permanently delete API key:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
// 🧹 清空所有已删除的API Keys
|
||||
async clearAllDeletedApiKeys() {
|
||||
try {
|
||||
const allKeys = await this.getAllApiKeys(true)
|
||||
const deletedKeys = allKeys.filter((key) => key.isDeleted === 'true')
|
||||
|
||||
let successCount = 0
|
||||
let failedCount = 0
|
||||
const errors = []
|
||||
|
||||
for (const key of deletedKeys) {
|
||||
try {
|
||||
await this.permanentDeleteApiKey(key.id)
|
||||
successCount++
|
||||
} catch (error) {
|
||||
failedCount++
|
||||
errors.push({
|
||||
keyId: key.id,
|
||||
keyName: key.name,
|
||||
error: error.message
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
logger.success(`🧹 Cleared deleted API keys: ${successCount} success, ${failedCount} failed`)
|
||||
|
||||
return {
|
||||
success: true,
|
||||
total: deletedKeys.length,
|
||||
successCount,
|
||||
failedCount,
|
||||
errors
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error('❌ Failed to clear all deleted API keys:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
// 📊 记录使用情况(支持缓存token和账户级别统计)
|
||||
async recordUsage(
|
||||
keyId,
|
||||
|
||||
@@ -1695,9 +1695,31 @@ class ClaudeAccountService {
|
||||
}
|
||||
}
|
||||
|
||||
// 🚫 标记账户为未授权状态(401错误)
|
||||
async markAccountUnauthorized(accountId, sessionHash = null) {
|
||||
// 🚫 通用的账户错误标记方法
|
||||
async markAccountError(accountId, errorType, sessionHash = null) {
|
||||
const ERROR_CONFIG = {
|
||||
unauthorized: {
|
||||
status: 'unauthorized',
|
||||
errorMessage: 'Account unauthorized (401 errors detected)',
|
||||
timestampField: 'unauthorizedAt',
|
||||
errorCode: 'CLAUDE_OAUTH_UNAUTHORIZED',
|
||||
logMessage: 'unauthorized'
|
||||
},
|
||||
blocked: {
|
||||
status: 'blocked',
|
||||
errorMessage: 'Account blocked (403 error detected - account may be suspended by Claude)',
|
||||
timestampField: 'blockedAt',
|
||||
errorCode: 'CLAUDE_OAUTH_BLOCKED',
|
||||
logMessage: 'blocked'
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const errorConfig = ERROR_CONFIG[errorType]
|
||||
if (!errorConfig) {
|
||||
throw new Error(`Unsupported error type: ${errorType}`)
|
||||
}
|
||||
|
||||
const accountData = await redis.getClaudeAccount(accountId)
|
||||
if (!accountData || Object.keys(accountData).length === 0) {
|
||||
throw new Error('Account not found')
|
||||
@@ -1705,10 +1727,10 @@ class ClaudeAccountService {
|
||||
|
||||
// 更新账户状态
|
||||
const updatedAccountData = { ...accountData }
|
||||
updatedAccountData.status = 'unauthorized'
|
||||
updatedAccountData.status = errorConfig.status
|
||||
updatedAccountData.schedulable = 'false' // 设置为不可调度
|
||||
updatedAccountData.errorMessage = 'Account unauthorized (401 errors detected)'
|
||||
updatedAccountData.unauthorizedAt = new Date().toISOString()
|
||||
updatedAccountData.errorMessage = errorConfig.errorMessage
|
||||
updatedAccountData[errorConfig.timestampField] = new Date().toISOString()
|
||||
|
||||
// 保存更新后的账户数据
|
||||
await redis.setClaudeAccount(accountId, updatedAccountData)
|
||||
@@ -1720,7 +1742,7 @@ class ClaudeAccountService {
|
||||
}
|
||||
|
||||
logger.warn(
|
||||
`⚠️ Account ${accountData.name} (${accountId}) marked as unauthorized and disabled for scheduling`
|
||||
`⚠️ Account ${accountData.name} (${accountId}) marked as ${errorConfig.logMessage} and disabled for scheduling`
|
||||
)
|
||||
|
||||
// 发送Webhook通知
|
||||
@@ -1730,9 +1752,10 @@ class ClaudeAccountService {
|
||||
accountId,
|
||||
accountName: accountData.name,
|
||||
platform: 'claude-oauth',
|
||||
status: 'unauthorized',
|
||||
errorCode: 'CLAUDE_OAUTH_UNAUTHORIZED',
|
||||
reason: 'Account unauthorized (401 errors detected)'
|
||||
status: errorConfig.status,
|
||||
errorCode: errorConfig.errorCode,
|
||||
reason: errorConfig.errorMessage,
|
||||
timestamp: getISOStringWithTimezone(new Date())
|
||||
})
|
||||
} catch (webhookError) {
|
||||
logger.error('Failed to send webhook notification:', webhookError)
|
||||
@@ -1740,11 +1763,21 @@ class ClaudeAccountService {
|
||||
|
||||
return { success: true }
|
||||
} catch (error) {
|
||||
logger.error(`❌ Failed to mark account ${accountId} as unauthorized:`, error)
|
||||
logger.error(`❌ Failed to mark account ${accountId} as ${errorType}:`, error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
// 🚫 标记账户为未授权状态(401错误)
|
||||
async markAccountUnauthorized(accountId, sessionHash = null) {
|
||||
return this.markAccountError(accountId, 'unauthorized', sessionHash)
|
||||
}
|
||||
|
||||
// 🚫 标记账户为被封锁状态(403错误)
|
||||
async markAccountBlocked(accountId, sessionHash = null) {
|
||||
return this.markAccountError(accountId, 'blocked', sessionHash)
|
||||
}
|
||||
|
||||
// 🔄 重置账户所有异常状态
|
||||
async resetAccountStatus(accountId) {
|
||||
try {
|
||||
@@ -1769,6 +1802,7 @@ class ClaudeAccountService {
|
||||
// 清除错误相关字段
|
||||
delete updatedAccountData.errorMessage
|
||||
delete updatedAccountData.unauthorizedAt
|
||||
delete updatedAccountData.blockedAt
|
||||
delete updatedAccountData.rateLimitedAt
|
||||
delete updatedAccountData.rateLimitStatus
|
||||
delete updatedAccountData.rateLimitEndAt
|
||||
@@ -1779,6 +1813,20 @@ class ClaudeAccountService {
|
||||
// 保存更新后的账户数据
|
||||
await redis.setClaudeAccount(accountId, updatedAccountData)
|
||||
|
||||
// 显式从 Redis 中删除这些字段(因为 HSET 不会删除现有字段)
|
||||
const fieldsToDelete = [
|
||||
'errorMessage',
|
||||
'unauthorizedAt',
|
||||
'blockedAt',
|
||||
'rateLimitedAt',
|
||||
'rateLimitStatus',
|
||||
'rateLimitEndAt',
|
||||
'tempErrorAt',
|
||||
'sessionWindowStart',
|
||||
'sessionWindowEnd'
|
||||
]
|
||||
await redis.client.hdel(`claude:account:${accountId}`, ...fieldsToDelete)
|
||||
|
||||
// 清除401错误计数
|
||||
const errorKey = `claude_account:${accountId}:401_errors`
|
||||
await redis.client.del(errorKey)
|
||||
@@ -1830,6 +1878,10 @@ class ClaudeAccountService {
|
||||
delete account.errorMessage
|
||||
delete account.tempErrorAt
|
||||
await redis.setClaudeAccount(account.id, account)
|
||||
|
||||
// 显式从 Redis 中删除这些字段(因为 HSET 不会删除现有字段)
|
||||
await redis.client.hdel(`claude:account:${account.id}`, 'errorMessage', 'tempErrorAt')
|
||||
|
||||
// 同时清除500错误计数
|
||||
await this.clearInternalErrors(account.id)
|
||||
cleanedCount++
|
||||
@@ -1917,6 +1969,52 @@ class ClaudeAccountService {
|
||||
// 保存更新后的账户数据
|
||||
await redis.setClaudeAccount(accountId, updatedAccountData)
|
||||
|
||||
// 设置 5 分钟后自动恢复(一次性定时器)
|
||||
setTimeout(
|
||||
async () => {
|
||||
try {
|
||||
const account = await redis.getClaudeAccount(accountId)
|
||||
if (account && account.status === 'temp_error' && account.tempErrorAt) {
|
||||
// 验证是否确实过了 5 分钟(防止重复定时器)
|
||||
const tempErrorAt = new Date(account.tempErrorAt)
|
||||
const now = new Date()
|
||||
const minutesSince = (now - tempErrorAt) / (1000 * 60)
|
||||
|
||||
if (minutesSince >= 5) {
|
||||
// 恢复账户
|
||||
account.status = 'active'
|
||||
account.schedulable = 'true'
|
||||
delete account.errorMessage
|
||||
delete account.tempErrorAt
|
||||
|
||||
await redis.setClaudeAccount(accountId, account)
|
||||
|
||||
// 显式删除 Redis 字段
|
||||
await redis.client.hdel(
|
||||
`claude:account:${accountId}`,
|
||||
'errorMessage',
|
||||
'tempErrorAt'
|
||||
)
|
||||
|
||||
// 清除 500 错误计数
|
||||
await this.clearInternalErrors(accountId)
|
||||
|
||||
logger.success(
|
||||
`✅ Auto-recovered temp_error after 5 minutes: ${account.name} (${accountId})`
|
||||
)
|
||||
} else {
|
||||
logger.debug(
|
||||
`⏰ Temp error timer triggered but only ${minutesSince.toFixed(1)} minutes passed for ${account.name} (${accountId})`
|
||||
)
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error(`❌ Failed to auto-recover temp_error account ${accountId}:`, error)
|
||||
}
|
||||
},
|
||||
6 * 60 * 1000
|
||||
) // 6 分钟后执行,确保已过 5 分钟
|
||||
|
||||
// 如果有sessionHash,删除粘性会话映射
|
||||
if (sessionHash) {
|
||||
await redis.client.del(`sticky_session:${sessionHash}`)
|
||||
|
||||
@@ -50,7 +50,9 @@ class ClaudeConsoleAccountService {
|
||||
proxy = null,
|
||||
isActive = true,
|
||||
accountType = 'shared', // 'dedicated' or 'shared'
|
||||
schedulable = true // 是否可被调度
|
||||
schedulable = true, // 是否可被调度
|
||||
dailyQuota = 0, // 每日额度限制(美元),0表示不限制
|
||||
quotaResetTime = '00:00' // 额度重置时间(HH:mm格式)
|
||||
} = options
|
||||
|
||||
// 验证必填字段
|
||||
@@ -85,7 +87,14 @@ class ClaudeConsoleAccountService {
|
||||
rateLimitedAt: '',
|
||||
rateLimitStatus: '',
|
||||
// 调度控制
|
||||
schedulable: schedulable.toString()
|
||||
schedulable: schedulable.toString(),
|
||||
// 额度管理相关
|
||||
dailyQuota: dailyQuota.toString(), // 每日额度限制(美元)
|
||||
dailyUsage: '0', // 当日使用金额(美元)
|
||||
// 使用与统计一致的时区日期,避免边界问题
|
||||
lastResetDate: redis.getDateStringInTimezone(), // 最后重置日期(按配置时区)
|
||||
quotaResetTime, // 额度重置时间
|
||||
quotaStoppedAt: '' // 因额度停用的时间
|
||||
}
|
||||
|
||||
const client = redis.getClientSafe()
|
||||
@@ -116,7 +125,12 @@ class ClaudeConsoleAccountService {
|
||||
proxy,
|
||||
accountType,
|
||||
status: 'active',
|
||||
createdAt: accountData.createdAt
|
||||
createdAt: accountData.createdAt,
|
||||
dailyQuota,
|
||||
dailyUsage: 0,
|
||||
lastResetDate: accountData.lastResetDate,
|
||||
quotaResetTime,
|
||||
quotaStoppedAt: null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,12 +162,18 @@ class ClaudeConsoleAccountService {
|
||||
isActive: accountData.isActive === 'true',
|
||||
proxy: accountData.proxy ? JSON.parse(accountData.proxy) : null,
|
||||
accountType: accountData.accountType || 'shared',
|
||||
status: accountData.status,
|
||||
errorMessage: accountData.errorMessage,
|
||||
createdAt: accountData.createdAt,
|
||||
lastUsedAt: accountData.lastUsedAt,
|
||||
rateLimitStatus: rateLimitInfo,
|
||||
schedulable: accountData.schedulable !== 'false' // 默认为true,只有明确设置为false才不可调度
|
||||
status: accountData.status || 'active',
|
||||
errorMessage: accountData.errorMessage,
|
||||
rateLimitInfo,
|
||||
schedulable: accountData.schedulable !== 'false', // 默认为true,只有明确设置为false才不可调度
|
||||
// 额度管理相关
|
||||
dailyQuota: parseFloat(accountData.dailyQuota || '0'),
|
||||
dailyUsage: parseFloat(accountData.dailyUsage || '0'),
|
||||
lastResetDate: accountData.lastResetDate || '',
|
||||
quotaResetTime: accountData.quotaResetTime || '00:00',
|
||||
quotaStoppedAt: accountData.quotaStoppedAt || null
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -267,6 +287,23 @@ class ClaudeConsoleAccountService {
|
||||
updatedData.schedulable = updates.schedulable.toString()
|
||||
}
|
||||
|
||||
// 额度管理相关字段
|
||||
if (updates.dailyQuota !== undefined) {
|
||||
updatedData.dailyQuota = updates.dailyQuota.toString()
|
||||
}
|
||||
if (updates.quotaResetTime !== undefined) {
|
||||
updatedData.quotaResetTime = updates.quotaResetTime
|
||||
}
|
||||
if (updates.dailyUsage !== undefined) {
|
||||
updatedData.dailyUsage = updates.dailyUsage.toString()
|
||||
}
|
||||
if (updates.lastResetDate !== undefined) {
|
||||
updatedData.lastResetDate = updates.lastResetDate
|
||||
}
|
||||
if (updates.quotaStoppedAt !== undefined) {
|
||||
updatedData.quotaStoppedAt = updates.quotaStoppedAt
|
||||
}
|
||||
|
||||
// 处理账户类型变更
|
||||
if (updates.accountType && updates.accountType !== existingAccount.accountType) {
|
||||
updatedData.accountType = updates.accountType
|
||||
@@ -361,7 +398,16 @@ class ClaudeConsoleAccountService {
|
||||
|
||||
const updates = {
|
||||
rateLimitedAt: new Date().toISOString(),
|
||||
rateLimitStatus: 'limited'
|
||||
rateLimitStatus: 'limited',
|
||||
isActive: 'false', // 禁用账户
|
||||
errorMessage: `Rate limited at ${new Date().toISOString()}`
|
||||
}
|
||||
|
||||
// 只有当前状态不是quota_exceeded时才设置为rate_limited
|
||||
// 避免覆盖更重要的配额超限状态
|
||||
const currentStatus = await client.hget(`${this.ACCOUNT_KEY_PREFIX}${accountId}`, 'status')
|
||||
if (currentStatus !== 'quota_exceeded') {
|
||||
updates.status = 'rate_limited'
|
||||
}
|
||||
|
||||
await client.hset(`${this.ACCOUNT_KEY_PREFIX}${accountId}`, updates)
|
||||
@@ -376,7 +422,7 @@ class ClaudeConsoleAccountService {
|
||||
platform: 'claude-console',
|
||||
status: 'error',
|
||||
errorCode: 'CLAUDE_CONSOLE_RATE_LIMITED',
|
||||
reason: `Account rate limited (429 error). ${account.rateLimitDuration ? `Will be blocked for ${account.rateLimitDuration} hours` : 'Temporary rate limit'}`,
|
||||
reason: `Account rate limited (429 error) and has been disabled. ${account.rateLimitDuration ? `Will be automatically re-enabled after ${account.rateLimitDuration} minutes` : 'Manual intervention required to re-enable'}`,
|
||||
timestamp: getISOStringWithTimezone(new Date())
|
||||
})
|
||||
} catch (webhookError) {
|
||||
@@ -397,14 +443,40 @@ class ClaudeConsoleAccountService {
|
||||
async removeAccountRateLimit(accountId) {
|
||||
try {
|
||||
const client = redis.getClientSafe()
|
||||
const accountKey = `${this.ACCOUNT_KEY_PREFIX}${accountId}`
|
||||
|
||||
await client.hdel(
|
||||
`${this.ACCOUNT_KEY_PREFIX}${accountId}`,
|
||||
'rateLimitedAt',
|
||||
'rateLimitStatus'
|
||||
// 获取账户当前状态和额度信息
|
||||
const [currentStatus, quotaStoppedAt] = await client.hmget(
|
||||
accountKey,
|
||||
'status',
|
||||
'quotaStoppedAt'
|
||||
)
|
||||
|
||||
logger.success(`✅ Rate limit removed for Claude Console account: ${accountId}`)
|
||||
// 删除限流相关字段
|
||||
await client.hdel(accountKey, 'rateLimitedAt', 'rateLimitStatus')
|
||||
|
||||
// 根据不同情况决定是否恢复账户
|
||||
if (currentStatus === 'rate_limited') {
|
||||
if (quotaStoppedAt) {
|
||||
// 还有额度限制,改为quota_exceeded状态
|
||||
await client.hset(accountKey, {
|
||||
status: 'quota_exceeded'
|
||||
// isActive保持false
|
||||
})
|
||||
logger.info(`⚠️ Rate limit removed but quota exceeded remains for account: ${accountId}`)
|
||||
} else {
|
||||
// 没有额度限制,完全恢复
|
||||
await client.hset(accountKey, {
|
||||
isActive: 'true',
|
||||
status: 'active',
|
||||
errorMessage: ''
|
||||
})
|
||||
logger.success(`✅ Rate limit removed and account re-enabled: ${accountId}`)
|
||||
}
|
||||
} else {
|
||||
logger.success(`✅ Rate limit removed for Claude Console account: ${accountId}`)
|
||||
}
|
||||
|
||||
return { success: true }
|
||||
} catch (error) {
|
||||
logger.error(`❌ Failed to remove rate limit for Claude Console account: ${accountId}`, error)
|
||||
@@ -454,6 +526,64 @@ class ClaudeConsoleAccountService {
|
||||
}
|
||||
}
|
||||
|
||||
// 🔍 检查账号是否因额度超限而被停用(懒惰检查)
|
||||
async isAccountQuotaExceeded(accountId) {
|
||||
try {
|
||||
const account = await this.getAccount(accountId)
|
||||
if (!account) {
|
||||
return false
|
||||
}
|
||||
|
||||
// 如果没有设置额度限制,不会超额
|
||||
const dailyQuota = parseFloat(account.dailyQuota || '0')
|
||||
if (isNaN(dailyQuota) || dailyQuota <= 0) {
|
||||
return false
|
||||
}
|
||||
|
||||
// 如果账户没有被额度停用,检查当前使用情况
|
||||
if (!account.quotaStoppedAt) {
|
||||
return false
|
||||
}
|
||||
|
||||
// 检查是否应该重置额度(到了新的重置时间点)
|
||||
if (this._shouldResetQuota(account)) {
|
||||
await this.resetDailyUsage(accountId)
|
||||
return false
|
||||
}
|
||||
|
||||
// 仍在额度超限状态
|
||||
return true
|
||||
} catch (error) {
|
||||
logger.error(
|
||||
`❌ Failed to check quota exceeded status for Claude Console account: ${accountId}`,
|
||||
error
|
||||
)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// 🔍 判断是否应该重置账户额度
|
||||
_shouldResetQuota(account) {
|
||||
// 与 Redis 统计一致:按配置时区判断“今天”与时间点
|
||||
const tzNow = redis.getDateInTimezone(new Date())
|
||||
const today = redis.getDateStringInTimezone(tzNow)
|
||||
|
||||
// 如果已经是今天重置过的,不需要重置
|
||||
if (account.lastResetDate === today) {
|
||||
return false
|
||||
}
|
||||
|
||||
// 检查是否到了重置时间点(按配置时区的小时/分钟)
|
||||
const resetTime = account.quotaResetTime || '00:00'
|
||||
const [resetHour, resetMinute] = resetTime.split(':').map((n) => parseInt(n))
|
||||
|
||||
const currentHour = tzNow.getUTCHours()
|
||||
const currentMinute = tzNow.getUTCMinutes()
|
||||
|
||||
// 如果当前时间已过重置时间且不是同一天重置的,应该重置
|
||||
return currentHour > resetHour || (currentHour === resetHour && currentMinute >= resetMinute)
|
||||
}
|
||||
|
||||
// 🚫 标记账号为未授权状态(401错误)
|
||||
async markAccountUnauthorized(accountId) {
|
||||
try {
|
||||
@@ -820,6 +950,187 @@ class ClaudeConsoleAccountService {
|
||||
// 返回映射后的模型,如果不存在则返回原模型
|
||||
return modelMapping[requestedModel] || requestedModel
|
||||
}
|
||||
|
||||
// 💰 检查账户使用额度(基于实时统计数据)
|
||||
async checkQuotaUsage(accountId) {
|
||||
try {
|
||||
// 获取实时的使用统计(包含费用)
|
||||
const usageStats = await redis.getAccountUsageStats(accountId)
|
||||
const currentDailyCost = usageStats.daily.cost || 0
|
||||
|
||||
// 获取账户配置
|
||||
const accountData = await this.getAccount(accountId)
|
||||
if (!accountData) {
|
||||
logger.warn(`Account not found: ${accountId}`)
|
||||
return
|
||||
}
|
||||
|
||||
// 解析额度配置,确保数值有效
|
||||
const dailyQuota = parseFloat(accountData.dailyQuota || '0')
|
||||
if (isNaN(dailyQuota) || dailyQuota <= 0) {
|
||||
// 没有设置有效额度,无需检查
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否已经因额度停用(避免重复操作)
|
||||
if (!accountData.isActive && accountData.quotaStoppedAt) {
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否超过额度限制
|
||||
if (currentDailyCost >= dailyQuota) {
|
||||
// 使用原子操作避免竞态条件 - 再次检查是否已设置quotaStoppedAt
|
||||
const client = redis.getClientSafe()
|
||||
const accountKey = `${this.ACCOUNT_KEY_PREFIX}${accountId}`
|
||||
|
||||
// double-check locking pattern - 检查quotaStoppedAt而不是status
|
||||
const existingQuotaStop = await client.hget(accountKey, 'quotaStoppedAt')
|
||||
if (existingQuotaStop) {
|
||||
return // 已经被其他进程处理
|
||||
}
|
||||
|
||||
// 超过额度,停用账户
|
||||
const updates = {
|
||||
isActive: false,
|
||||
quotaStoppedAt: new Date().toISOString(),
|
||||
errorMessage: `Daily quota exceeded: $${currentDailyCost.toFixed(2)} / $${dailyQuota.toFixed(2)}`
|
||||
}
|
||||
|
||||
// 只有当前状态是active时才改为quota_exceeded
|
||||
// 如果是rate_limited等其他状态,保持原状态不变
|
||||
const currentStatus = await client.hget(accountKey, 'status')
|
||||
if (currentStatus === 'active') {
|
||||
updates.status = 'quota_exceeded'
|
||||
}
|
||||
|
||||
await this.updateAccount(accountId, updates)
|
||||
|
||||
logger.warn(
|
||||
`💰 Account ${accountId} exceeded daily quota: $${currentDailyCost.toFixed(2)} / $${dailyQuota.toFixed(2)}`
|
||||
)
|
||||
|
||||
// 发送webhook通知
|
||||
try {
|
||||
const webhookNotifier = require('../utils/webhookNotifier')
|
||||
await webhookNotifier.sendAccountAnomalyNotification({
|
||||
accountId,
|
||||
accountName: accountData.name || 'Unknown Account',
|
||||
platform: 'claude-console',
|
||||
status: 'quota_exceeded',
|
||||
errorCode: 'CLAUDE_CONSOLE_QUOTA_EXCEEDED',
|
||||
reason: `Daily quota exceeded: $${currentDailyCost.toFixed(2)} / $${dailyQuota.toFixed(2)}`
|
||||
})
|
||||
} catch (webhookError) {
|
||||
logger.error('Failed to send webhook notification for quota exceeded:', webhookError)
|
||||
}
|
||||
}
|
||||
|
||||
logger.debug(
|
||||
`💰 Quota check for account ${accountId}: $${currentDailyCost.toFixed(4)} / $${dailyQuota.toFixed(2)}`
|
||||
)
|
||||
} catch (error) {
|
||||
logger.error('Failed to check quota usage:', error)
|
||||
}
|
||||
}
|
||||
|
||||
// 🔄 重置账户每日使用量(恢复因额度停用的账户)
|
||||
async resetDailyUsage(accountId) {
|
||||
try {
|
||||
const accountData = await this.getAccount(accountId)
|
||||
if (!accountData) {
|
||||
return
|
||||
}
|
||||
|
||||
const today = redis.getDateStringInTimezone()
|
||||
const updates = {
|
||||
lastResetDate: today
|
||||
}
|
||||
|
||||
// 如果账户是因为超额被停用的,恢复账户
|
||||
// 注意:状态可能是 quota_exceeded 或 rate_limited(如果429错误时也超额了)
|
||||
if (
|
||||
accountData.quotaStoppedAt &&
|
||||
accountData.isActive === false &&
|
||||
(accountData.status === 'quota_exceeded' || accountData.status === 'rate_limited')
|
||||
) {
|
||||
updates.isActive = true
|
||||
updates.status = 'active'
|
||||
updates.errorMessage = ''
|
||||
updates.quotaStoppedAt = ''
|
||||
|
||||
// 如果是rate_limited状态,也清除限流相关字段
|
||||
if (accountData.status === 'rate_limited') {
|
||||
const client = redis.getClientSafe()
|
||||
const accountKey = `${this.ACCOUNT_KEY_PREFIX}${accountId}`
|
||||
await client.hdel(accountKey, 'rateLimitedAt', 'rateLimitStatus')
|
||||
}
|
||||
|
||||
logger.info(
|
||||
`✅ Restored account ${accountId} after daily reset (was ${accountData.status})`
|
||||
)
|
||||
}
|
||||
|
||||
await this.updateAccount(accountId, updates)
|
||||
|
||||
logger.debug(`🔄 Reset daily usage for account ${accountId}`)
|
||||
} catch (error) {
|
||||
logger.error('Failed to reset daily usage:', error)
|
||||
}
|
||||
}
|
||||
|
||||
// 🔄 重置所有账户的每日使用量
|
||||
async resetAllDailyUsage() {
|
||||
try {
|
||||
const accounts = await this.getAllAccounts()
|
||||
// 与统计一致使用配置时区日期
|
||||
const today = redis.getDateStringInTimezone()
|
||||
let resetCount = 0
|
||||
|
||||
for (const account of accounts) {
|
||||
// 只重置需要重置的账户
|
||||
if (account.lastResetDate !== today) {
|
||||
await this.resetDailyUsage(account.id)
|
||||
resetCount += 1
|
||||
}
|
||||
}
|
||||
|
||||
logger.success(`✅ Reset daily usage for ${resetCount} Claude Console accounts`)
|
||||
} catch (error) {
|
||||
logger.error('Failed to reset all daily usage:', error)
|
||||
}
|
||||
}
|
||||
|
||||
// 📊 获取账户使用统计(基于实时数据)
|
||||
async getAccountUsageStats(accountId) {
|
||||
try {
|
||||
// 获取实时的使用统计(包含费用)
|
||||
const usageStats = await redis.getAccountUsageStats(accountId)
|
||||
const currentDailyCost = usageStats.daily.cost || 0
|
||||
|
||||
// 获取账户配置
|
||||
const accountData = await this.getAccount(accountId)
|
||||
if (!accountData) {
|
||||
return null
|
||||
}
|
||||
|
||||
const dailyQuota = parseFloat(accountData.dailyQuota || '0')
|
||||
|
||||
return {
|
||||
dailyQuota,
|
||||
dailyUsage: currentDailyCost, // 使用实时计算的费用
|
||||
remainingQuota: dailyQuota > 0 ? Math.max(0, dailyQuota - currentDailyCost) : null,
|
||||
usagePercentage: dailyQuota > 0 ? (currentDailyCost / dailyQuota) * 100 : 0,
|
||||
lastResetDate: accountData.lastResetDate,
|
||||
quotaStoppedAt: accountData.quotaStoppedAt,
|
||||
isQuotaExceeded: dailyQuota > 0 && currentDailyCost >= dailyQuota,
|
||||
// 额外返回完整的使用统计
|
||||
fullUsageStats: usageStats
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error('Failed to get account usage stats:', error)
|
||||
return null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new ClaudeConsoleAccountService()
|
||||
|
||||
@@ -181,6 +181,11 @@ class ClaudeConsoleRelayService {
|
||||
await claudeConsoleAccountService.markAccountUnauthorized(accountId)
|
||||
} else if (response.status === 429) {
|
||||
logger.warn(`🚫 Rate limit detected for Claude Console account ${accountId}`)
|
||||
// 收到429先检查是否因为超过了手动配置的每日额度
|
||||
await claudeConsoleAccountService.checkQuotaUsage(accountId).catch((err) => {
|
||||
logger.error('❌ Failed to check quota after 429 error:', err)
|
||||
})
|
||||
|
||||
await claudeConsoleAccountService.markAccountRateLimited(accountId)
|
||||
} else if (response.status === 529) {
|
||||
logger.warn(`🚫 Overload error detected for Claude Console account ${accountId}`)
|
||||
@@ -377,6 +382,10 @@ class ClaudeConsoleRelayService {
|
||||
claudeConsoleAccountService.markAccountUnauthorized(accountId)
|
||||
} else if (response.status === 429) {
|
||||
claudeConsoleAccountService.markAccountRateLimited(accountId)
|
||||
// 检查是否因为超过每日额度
|
||||
claudeConsoleAccountService.checkQuotaUsage(accountId).catch((err) => {
|
||||
logger.error('❌ Failed to check quota after 429 error:', err)
|
||||
})
|
||||
} else if (response.status === 529) {
|
||||
claudeConsoleAccountService.markAccountOverloaded(accountId)
|
||||
}
|
||||
@@ -589,6 +598,10 @@ class ClaudeConsoleRelayService {
|
||||
claudeConsoleAccountService.markAccountUnauthorized(accountId)
|
||||
} else if (error.response.status === 429) {
|
||||
claudeConsoleAccountService.markAccountRateLimited(accountId)
|
||||
// 检查是否因为超过每日额度
|
||||
claudeConsoleAccountService.checkQuotaUsage(accountId).catch((err) => {
|
||||
logger.error('❌ Failed to check quota after 429 error:', err)
|
||||
})
|
||||
} else if (error.response.status === 529) {
|
||||
claudeConsoleAccountService.markAccountOverloaded(accountId)
|
||||
}
|
||||
|
||||
@@ -198,6 +198,13 @@ class ClaudeRelayService {
|
||||
)
|
||||
}
|
||||
}
|
||||
// 检查是否为403状态码(禁止访问)
|
||||
else if (response.statusCode === 403) {
|
||||
logger.error(
|
||||
`🚫 Forbidden error (403) detected for account ${accountId}, marking as blocked`
|
||||
)
|
||||
await unifiedClaudeScheduler.markAccountBlocked(accountId, accountType, sessionHash)
|
||||
}
|
||||
// 检查是否为5xx状态码
|
||||
else if (response.statusCode >= 500 && response.statusCode < 600) {
|
||||
logger.warn(`🔥 Server error (${response.statusCode}) detected for account ${accountId}`)
|
||||
@@ -664,7 +671,10 @@ class ClaudeRelayService {
|
||||
}
|
||||
|
||||
// 使用统一 User-Agent 或客户端提供的,最后使用默认值
|
||||
if (!options.headers['User-Agent'] && !options.headers['user-agent']) {
|
||||
if (
|
||||
(!options.headers['User-Agent'] && !options.headers['user-agent']) ||
|
||||
account.useUnifiedUserAgent === 'true'
|
||||
) {
|
||||
const userAgent =
|
||||
unifiedUA ||
|
||||
clientHeaders?.['user-agent'] ||
|
||||
@@ -673,8 +683,9 @@ class ClaudeRelayService {
|
||||
options.headers['User-Agent'] = userAgent
|
||||
}
|
||||
|
||||
logger.info(`🔗 指纹是这个: ${options.headers['User-Agent']}`)
|
||||
logger.info(`🔗 指纹是这个: ${options.headers['user-agent']}`)
|
||||
logger.info(
|
||||
`🔗 指纹是这个: ${options.headers['User-Agent'] || options.headers['user-agent']}`
|
||||
)
|
||||
|
||||
// 使用自定义的 betaHeader 或默认值
|
||||
const betaHeader =
|
||||
@@ -930,7 +941,10 @@ class ClaudeRelayService {
|
||||
}
|
||||
|
||||
// 使用统一 User-Agent 或客户端提供的,最后使用默认值
|
||||
if (!options.headers['User-Agent'] && !options.headers['user-agent']) {
|
||||
if (
|
||||
(!options.headers['User-Agent'] && !options.headers['user-agent']) ||
|
||||
account.useUnifiedUserAgent === 'true'
|
||||
) {
|
||||
const userAgent =
|
||||
unifiedUA ||
|
||||
clientHeaders?.['user-agent'] ||
|
||||
@@ -939,6 +953,9 @@ class ClaudeRelayService {
|
||||
options.headers['User-Agent'] = userAgent
|
||||
}
|
||||
|
||||
logger.info(
|
||||
`🔗 指纹是这个: ${options.headers['User-Agent'] || options.headers['user-agent']}`
|
||||
)
|
||||
// 使用自定义的 betaHeader 或默认值
|
||||
const betaHeader =
|
||||
requestOptions?.betaHeader !== undefined ? requestOptions.betaHeader : this.betaHeader
|
||||
@@ -953,8 +970,32 @@ class ClaudeRelayService {
|
||||
if (res.statusCode !== 200) {
|
||||
// 将错误处理逻辑封装在一个异步函数中
|
||||
const handleErrorResponse = async () => {
|
||||
// 增加对5xx错误的处理
|
||||
if (res.statusCode >= 500 && res.statusCode < 600) {
|
||||
if (res.statusCode === 401) {
|
||||
logger.warn(`🔐 [Stream] Unauthorized error (401) detected for account ${accountId}`)
|
||||
|
||||
await this.recordUnauthorizedError(accountId)
|
||||
|
||||
const errorCount = await this.getUnauthorizedErrorCount(accountId)
|
||||
logger.info(
|
||||
`🔐 [Stream] Account ${accountId} has ${errorCount} consecutive 401 errors in the last 5 minutes`
|
||||
)
|
||||
|
||||
if (errorCount >= 1) {
|
||||
logger.error(
|
||||
`❌ [Stream] Account ${accountId} encountered 401 error (${errorCount} errors), marking as unauthorized`
|
||||
)
|
||||
await unifiedClaudeScheduler.markAccountUnauthorized(
|
||||
accountId,
|
||||
accountType,
|
||||
sessionHash
|
||||
)
|
||||
}
|
||||
} else if (res.statusCode === 403) {
|
||||
logger.error(
|
||||
`🚫 [Stream] Forbidden error (403) detected for account ${accountId}, marking as blocked`
|
||||
)
|
||||
await unifiedClaudeScheduler.markAccountBlocked(accountId, accountType, sessionHash)
|
||||
} else if (res.statusCode >= 500 && res.statusCode < 600) {
|
||||
logger.warn(
|
||||
`🔥 [Stream] Server error (${res.statusCode}) detected for account ${accountId}`
|
||||
)
|
||||
|
||||
@@ -1022,15 +1022,23 @@ async function loadCodeAssist(client, projectId = null, proxyConfig = null) {
|
||||
const clientMetadata = {
|
||||
ideType: 'IDE_UNSPECIFIED',
|
||||
platform: 'PLATFORM_UNSPECIFIED',
|
||||
pluginType: 'GEMINI',
|
||||
duetProject: projectId
|
||||
pluginType: 'GEMINI'
|
||||
}
|
||||
|
||||
// 只有当projectId存在时才添加duetProject
|
||||
if (projectId) {
|
||||
clientMetadata.duetProject = projectId
|
||||
}
|
||||
|
||||
const request = {
|
||||
cloudaicompanionProject: projectId,
|
||||
metadata: clientMetadata
|
||||
}
|
||||
|
||||
// 只有当projectId存在时才添加cloudaicompanionProject
|
||||
if (projectId) {
|
||||
request.cloudaicompanionProject = projectId
|
||||
}
|
||||
|
||||
const axiosConfig = {
|
||||
url: `${CODE_ASSIST_ENDPOINT}/${CODE_ASSIST_API_VERSION}:loadCodeAssist`,
|
||||
method: 'POST',
|
||||
@@ -1096,10 +1104,14 @@ async function onboardUser(client, tierId, projectId, clientMetadata, proxyConfi
|
||||
|
||||
const onboardReq = {
|
||||
tierId,
|
||||
cloudaicompanionProject: projectId,
|
||||
metadata: clientMetadata
|
||||
}
|
||||
|
||||
// 只有当projectId存在时才添加cloudaicompanionProject
|
||||
if (projectId) {
|
||||
onboardReq.cloudaicompanionProject = projectId
|
||||
}
|
||||
|
||||
// 创建基础axios配置
|
||||
const baseAxiosConfig = {
|
||||
url: `${CODE_ASSIST_ENDPOINT}/${CODE_ASSIST_API_VERSION}:onboardUser`,
|
||||
@@ -1278,7 +1290,6 @@ async function generateContent(
|
||||
// 按照 gemini-cli 的转换格式构造请求
|
||||
const request = {
|
||||
model: requestData.model,
|
||||
project: projectId,
|
||||
user_prompt_id: userPromptId,
|
||||
request: {
|
||||
...requestData.request,
|
||||
@@ -1286,6 +1297,11 @@ async function generateContent(
|
||||
}
|
||||
}
|
||||
|
||||
// 只有当projectId存在时才添加project字段
|
||||
if (projectId) {
|
||||
request.project = projectId
|
||||
}
|
||||
|
||||
logger.info('🤖 generateContent API调用开始', {
|
||||
model: requestData.model,
|
||||
userPromptId,
|
||||
@@ -1340,7 +1356,6 @@ async function generateContentStream(
|
||||
// 按照 gemini-cli 的转换格式构造请求
|
||||
const request = {
|
||||
model: requestData.model,
|
||||
project: projectId,
|
||||
user_prompt_id: userPromptId,
|
||||
request: {
|
||||
...requestData.request,
|
||||
@@ -1348,6 +1363,11 @@ async function generateContentStream(
|
||||
}
|
||||
}
|
||||
|
||||
// 只有当projectId存在时才添加project字段
|
||||
if (projectId) {
|
||||
request.project = projectId
|
||||
}
|
||||
|
||||
logger.info('🌊 streamGenerateContent API调用开始', {
|
||||
model: requestData.model,
|
||||
userPromptId,
|
||||
|
||||
@@ -97,6 +97,38 @@ class LdapService {
|
||||
return null
|
||||
}
|
||||
|
||||
// 🌐 从DN中提取域名,用于Windows AD UPN格式认证
|
||||
extractDomainFromDN(dnString) {
|
||||
try {
|
||||
if (!dnString || typeof dnString !== 'string') {
|
||||
return null
|
||||
}
|
||||
|
||||
// 提取所有DC组件:DC=test,DC=demo,DC=com
|
||||
const dcMatches = dnString.match(/DC=([^,]+)/gi)
|
||||
if (!dcMatches || dcMatches.length === 0) {
|
||||
return null
|
||||
}
|
||||
|
||||
// 提取DC值并连接成域名
|
||||
const domainParts = dcMatches.map((match) => {
|
||||
const value = match.replace(/DC=/i, '').trim()
|
||||
return value
|
||||
})
|
||||
|
||||
if (domainParts.length > 0) {
|
||||
const domain = domainParts.join('.')
|
||||
logger.debug(`🌐 从DN提取域名: ${domain}`)
|
||||
return domain
|
||||
}
|
||||
|
||||
return null
|
||||
} catch (error) {
|
||||
logger.debug('⚠️ 域名提取失败:', error.message)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
// 🔗 创建LDAP客户端连接
|
||||
createClient() {
|
||||
try {
|
||||
@@ -336,6 +368,79 @@ class LdapService {
|
||||
})
|
||||
}
|
||||
|
||||
// 🔐 Windows AD兼容认证 - 在DN认证失败时尝试多种格式
|
||||
async tryWindowsADAuthentication(username, password) {
|
||||
if (!username || !password) {
|
||||
return false
|
||||
}
|
||||
|
||||
// 从searchBase提取域名
|
||||
const domain = this.extractDomainFromDN(this.config.server.searchBase)
|
||||
|
||||
const adFormats = []
|
||||
|
||||
if (domain) {
|
||||
// UPN格式(Windows AD标准)
|
||||
adFormats.push(`${username}@${domain}`)
|
||||
|
||||
// 如果域名有多个部分,也尝试简化版本
|
||||
const domainParts = domain.split('.')
|
||||
if (domainParts.length > 1) {
|
||||
adFormats.push(`${username}@${domainParts.slice(-2).join('.')}`) // 只取后两部分
|
||||
}
|
||||
|
||||
// 域\用户名格式
|
||||
const firstDomainPart = domainParts[0]
|
||||
if (firstDomainPart) {
|
||||
adFormats.push(`${firstDomainPart}\\${username}`)
|
||||
adFormats.push(`${firstDomainPart.toUpperCase()}\\${username}`)
|
||||
}
|
||||
}
|
||||
|
||||
// 纯用户名(最后尝试)
|
||||
adFormats.push(username)
|
||||
|
||||
logger.info(`🔄 尝试 ${adFormats.length} 种Windows AD认证格式...`)
|
||||
|
||||
for (const format of adFormats) {
|
||||
try {
|
||||
logger.info(`🔍 尝试格式: ${format}`)
|
||||
const result = await this.tryDirectBind(format, password)
|
||||
if (result) {
|
||||
logger.info(`✅ Windows AD认证成功: ${format}`)
|
||||
return true
|
||||
}
|
||||
logger.debug(`❌ 认证失败: ${format}`)
|
||||
} catch (error) {
|
||||
logger.debug(`认证异常 ${format}:`, error.message)
|
||||
}
|
||||
}
|
||||
|
||||
logger.info(`🚫 所有Windows AD格式认证都失败了`)
|
||||
return false
|
||||
}
|
||||
|
||||
// 🔐 直接尝试绑定认证的辅助方法
|
||||
async tryDirectBind(identifier, password) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const authClient = this.createClient()
|
||||
|
||||
authClient.bind(identifier, password, (err) => {
|
||||
authClient.unbind()
|
||||
|
||||
if (err) {
|
||||
if (err.name === 'InvalidCredentialsError') {
|
||||
resolve(false)
|
||||
} else {
|
||||
reject(err)
|
||||
}
|
||||
} else {
|
||||
resolve(true)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// 📝 提取用户信息
|
||||
extractUserInfo(ldapEntry, username) {
|
||||
try {
|
||||
@@ -478,10 +583,32 @@ class LdapService {
|
||||
return { success: false, message: 'Authentication service error' }
|
||||
}
|
||||
|
||||
// 4. 验证用户密码
|
||||
const isPasswordValid = await this.authenticateUser(userDN, password)
|
||||
// 4. 验证用户密码 - 支持传统LDAP和Windows AD
|
||||
let isPasswordValid = false
|
||||
|
||||
// 首先尝试传统的DN认证(保持原有LDAP逻辑)
|
||||
try {
|
||||
isPasswordValid = await this.authenticateUser(userDN, password)
|
||||
if (isPasswordValid) {
|
||||
logger.info(`✅ DN authentication successful for user: ${sanitizedUsername}`)
|
||||
}
|
||||
} catch (error) {
|
||||
logger.debug(
|
||||
`DN authentication failed for user: ${sanitizedUsername}, error: ${error.message}`
|
||||
)
|
||||
}
|
||||
|
||||
// 如果DN认证失败,尝试Windows AD多格式认证
|
||||
if (!isPasswordValid) {
|
||||
logger.info(`🚫 Invalid password for user: ${sanitizedUsername}`)
|
||||
logger.debug(`🔄 Trying Windows AD authentication formats for user: ${sanitizedUsername}`)
|
||||
isPasswordValid = await this.tryWindowsADAuthentication(sanitizedUsername, password)
|
||||
if (isPasswordValid) {
|
||||
logger.info(`✅ Windows AD authentication successful for user: ${sanitizedUsername}`)
|
||||
}
|
||||
}
|
||||
|
||||
if (!isPasswordValid) {
|
||||
logger.info(`🚫 All authentication methods failed for user: ${sanitizedUsername}`)
|
||||
return { success: false, message: 'Invalid username or password' }
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ const {
|
||||
logRefreshSkipped
|
||||
} = require('../utils/tokenRefreshLogger')
|
||||
const LRUCache = require('../utils/lruCache')
|
||||
// const tokenRefreshService = require('./tokenRefreshService')
|
||||
const tokenRefreshService = require('./tokenRefreshService')
|
||||
|
||||
// 加密相关常量
|
||||
const ALGORITHM = 'aes-256-cbc'
|
||||
@@ -57,7 +57,17 @@ function encrypt(text) {
|
||||
|
||||
// 解密函数
|
||||
function decrypt(text) {
|
||||
if (!text) {
|
||||
if (!text || text === '') {
|
||||
return ''
|
||||
}
|
||||
|
||||
// 检查是否是有效的加密格式(至少需要 32 个字符的 IV + 冒号 + 加密文本)
|
||||
if (text.length < 33 || text.charAt(32) !== ':') {
|
||||
logger.warn('Invalid encrypted text format, returning empty string', {
|
||||
textLength: text ? text.length : 0,
|
||||
char32: text && text.length > 32 ? text.charAt(32) : 'N/A',
|
||||
first50: text ? text.substring(0, 50) : 'N/A'
|
||||
})
|
||||
return ''
|
||||
}
|
||||
|
||||
@@ -135,6 +145,7 @@ async function refreshAccessToken(refreshToken, proxy = null) {
|
||||
const proxyAgent = ProxyHelper.createProxyAgent(proxy)
|
||||
if (proxyAgent) {
|
||||
requestOptions.httpsAgent = proxyAgent
|
||||
requestOptions.proxy = false // 重要:禁用 axios 的默认代理,强制使用我们的 httpsAgent
|
||||
logger.info(
|
||||
`🌐 Using proxy for OpenAI token refresh: ${ProxyHelper.getProxyDescription(proxy)}`
|
||||
)
|
||||
@@ -143,6 +154,7 @@ async function refreshAccessToken(refreshToken, proxy = null) {
|
||||
}
|
||||
|
||||
// 发送请求
|
||||
logger.info('🔍 发送 token 刷新请求,使用代理:', !!requestOptions.httpsAgent)
|
||||
const response = await axios(requestOptions)
|
||||
|
||||
if (response.status === 200 && response.data) {
|
||||
@@ -164,22 +176,73 @@ async function refreshAccessToken(refreshToken, proxy = null) {
|
||||
} catch (error) {
|
||||
if (error.response) {
|
||||
// 服务器响应了错误状态码
|
||||
const errorData = error.response.data || {}
|
||||
logger.error('OpenAI token refresh failed:', {
|
||||
status: error.response.status,
|
||||
data: error.response.data,
|
||||
data: errorData,
|
||||
headers: error.response.headers
|
||||
})
|
||||
throw new Error(
|
||||
`Token refresh failed: ${error.response.status} - ${JSON.stringify(error.response.data)}`
|
||||
)
|
||||
|
||||
// 构建详细的错误信息
|
||||
let errorMessage = `OpenAI 服务器返回错误 (${error.response.status})`
|
||||
|
||||
if (error.response.status === 400) {
|
||||
if (errorData.error === 'invalid_grant') {
|
||||
errorMessage = 'Refresh Token 无效或已过期,请重新授权'
|
||||
} else if (errorData.error === 'invalid_request') {
|
||||
errorMessage = `请求参数错误:${errorData.error_description || errorData.error}`
|
||||
} else {
|
||||
errorMessage = `请求错误:${errorData.error_description || errorData.error || '未知错误'}`
|
||||
}
|
||||
} else if (error.response.status === 401) {
|
||||
errorMessage = '认证失败:Refresh Token 无效'
|
||||
} else if (error.response.status === 403) {
|
||||
errorMessage = '访问被拒绝:可能是 IP 被封或账户被禁用'
|
||||
} else if (error.response.status === 429) {
|
||||
errorMessage = '请求过于频繁,请稍后重试'
|
||||
} else if (error.response.status >= 500) {
|
||||
errorMessage = 'OpenAI 服务器内部错误,请稍后重试'
|
||||
} else if (errorData.error_description) {
|
||||
errorMessage = errorData.error_description
|
||||
} else if (errorData.error) {
|
||||
errorMessage = errorData.error
|
||||
} else if (errorData.message) {
|
||||
errorMessage = errorData.message
|
||||
}
|
||||
|
||||
const fullError = new Error(errorMessage)
|
||||
fullError.status = error.response.status
|
||||
fullError.details = errorData
|
||||
throw fullError
|
||||
} else if (error.request) {
|
||||
// 请求已发出但没有收到响应
|
||||
logger.error('OpenAI token refresh no response:', error.message)
|
||||
throw new Error(`Token refresh failed: No response from server - ${error.message}`)
|
||||
|
||||
let errorMessage = '无法连接到 OpenAI 服务器'
|
||||
if (proxy) {
|
||||
errorMessage += `(代理: ${ProxyHelper.getProxyDescription(proxy)})`
|
||||
}
|
||||
if (error.code === 'ECONNREFUSED') {
|
||||
errorMessage += ' - 连接被拒绝'
|
||||
} else if (error.code === 'ETIMEDOUT') {
|
||||
errorMessage += ' - 连接超时'
|
||||
} else if (error.code === 'ENOTFOUND') {
|
||||
errorMessage += ' - 无法解析域名'
|
||||
} else if (error.code === 'EPROTO') {
|
||||
errorMessage += ' - 协议错误(可能是代理配置问题)'
|
||||
} else if (error.message) {
|
||||
errorMessage += ` - ${error.message}`
|
||||
}
|
||||
|
||||
const fullError = new Error(errorMessage)
|
||||
fullError.code = error.code
|
||||
throw fullError
|
||||
} else {
|
||||
// 设置请求时发生错误
|
||||
logger.error('OpenAI token refresh error:', error.message)
|
||||
throw new Error(`Token refresh failed: ${error.message}`)
|
||||
const fullError = new Error(`请求设置错误: ${error.message}`)
|
||||
fullError.originalError = error
|
||||
throw fullError
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -192,34 +255,71 @@ function isTokenExpired(account) {
|
||||
return new Date(account.expiresAt) <= new Date()
|
||||
}
|
||||
|
||||
// 刷新账户的 access token
|
||||
// 刷新账户的 access token(带分布式锁)
|
||||
async function refreshAccountToken(accountId) {
|
||||
const account = await getAccount(accountId)
|
||||
if (!account) {
|
||||
throw new Error('Account not found')
|
||||
}
|
||||
|
||||
const accountName = account.name || accountId
|
||||
logRefreshStart(accountId, accountName, 'openai')
|
||||
|
||||
// 检查是否有 refresh token
|
||||
const refreshToken = account.refreshToken ? decrypt(account.refreshToken) : null
|
||||
if (!refreshToken) {
|
||||
logRefreshSkipped(accountId, accountName, 'openai', 'No refresh token available')
|
||||
throw new Error('No refresh token available')
|
||||
}
|
||||
|
||||
// 获取代理配置
|
||||
let proxy = null
|
||||
if (account.proxy) {
|
||||
try {
|
||||
proxy = typeof account.proxy === 'string' ? JSON.parse(account.proxy) : account.proxy
|
||||
} catch (e) {
|
||||
logger.warn(`Failed to parse proxy config for account ${accountId}:`, e)
|
||||
}
|
||||
}
|
||||
let lockAcquired = false
|
||||
let account = null
|
||||
let accountName = accountId
|
||||
|
||||
try {
|
||||
account = await getAccount(accountId)
|
||||
if (!account) {
|
||||
throw new Error('Account not found')
|
||||
}
|
||||
|
||||
accountName = account.name || accountId
|
||||
|
||||
// 检查是否有 refresh token
|
||||
// account.refreshToken 在 getAccount 中已经被解密了,直接使用即可
|
||||
const refreshToken = account.refreshToken || null
|
||||
|
||||
if (!refreshToken) {
|
||||
logRefreshSkipped(accountId, accountName, 'openai', 'No refresh token available')
|
||||
throw new Error('No refresh token available')
|
||||
}
|
||||
|
||||
// 尝试获取分布式锁
|
||||
lockAcquired = await tokenRefreshService.acquireRefreshLock(accountId, 'openai')
|
||||
|
||||
if (!lockAcquired) {
|
||||
// 如果无法获取锁,说明另一个进程正在刷新
|
||||
logger.info(
|
||||
`🔒 Token refresh already in progress for OpenAI account: ${accountName} (${accountId})`
|
||||
)
|
||||
logRefreshSkipped(accountId, accountName, 'openai', 'already_locked')
|
||||
|
||||
// 等待一段时间后返回,期望其他进程已完成刷新
|
||||
await new Promise((resolve) => setTimeout(resolve, 2000))
|
||||
|
||||
// 重新获取账户数据(可能已被其他进程刷新)
|
||||
const updatedAccount = await getAccount(accountId)
|
||||
if (updatedAccount && !isTokenExpired(updatedAccount)) {
|
||||
return {
|
||||
access_token: decrypt(updatedAccount.accessToken),
|
||||
id_token: updatedAccount.idToken,
|
||||
refresh_token: updatedAccount.refreshToken,
|
||||
expires_in: 3600,
|
||||
expiry_date: new Date(updatedAccount.expiresAt).getTime()
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error('Token refresh in progress by another process')
|
||||
}
|
||||
|
||||
// 获取锁成功,开始刷新
|
||||
logRefreshStart(accountId, accountName, 'openai')
|
||||
logger.info(`🔄 Starting token refresh for OpenAI account: ${accountName} (${accountId})`)
|
||||
|
||||
// 获取代理配置
|
||||
let proxy = null
|
||||
if (account.proxy) {
|
||||
try {
|
||||
proxy = typeof account.proxy === 'string' ? JSON.parse(account.proxy) : account.proxy
|
||||
} catch (e) {
|
||||
logger.warn(`Failed to parse proxy config for account ${accountId}:`, e)
|
||||
}
|
||||
}
|
||||
|
||||
const newTokens = await refreshAccessToken(refreshToken, proxy)
|
||||
if (!newTokens) {
|
||||
throw new Error('Failed to refresh token')
|
||||
@@ -231,9 +331,51 @@ async function refreshAccountToken(accountId) {
|
||||
expiresAt: new Date(newTokens.expiry_date).toISOString()
|
||||
}
|
||||
|
||||
// 如果有新的 ID token,也更新它
|
||||
// 如果有新的 ID token,也更新它(这对于首次未提供 ID Token 的账户特别重要)
|
||||
if (newTokens.id_token) {
|
||||
updates.idToken = encrypt(newTokens.id_token)
|
||||
|
||||
// 如果之前没有 ID Token,尝试解析并更新用户信息
|
||||
if (!account.idToken || account.idToken === '') {
|
||||
try {
|
||||
const idTokenParts = newTokens.id_token.split('.')
|
||||
if (idTokenParts.length === 3) {
|
||||
const payload = JSON.parse(Buffer.from(idTokenParts[1], 'base64').toString())
|
||||
const authClaims = payload['https://api.openai.com/auth'] || {}
|
||||
|
||||
// 更新账户信息 - 使用正确的字段名
|
||||
// OpenAI ID Token中用户ID在chatgpt_account_id、chatgpt_user_id和user_id字段
|
||||
if (authClaims.chatgpt_account_id) {
|
||||
updates.accountId = authClaims.chatgpt_account_id
|
||||
}
|
||||
if (authClaims.chatgpt_user_id) {
|
||||
updates.chatgptUserId = authClaims.chatgpt_user_id
|
||||
} else if (authClaims.user_id) {
|
||||
// 有些情况下可能只有user_id字段
|
||||
updates.chatgptUserId = authClaims.user_id
|
||||
}
|
||||
if (authClaims.organizations?.[0]?.id) {
|
||||
updates.organizationId = authClaims.organizations[0].id
|
||||
}
|
||||
if (authClaims.organizations?.[0]?.role) {
|
||||
updates.organizationRole = authClaims.organizations[0].role
|
||||
}
|
||||
if (authClaims.organizations?.[0]?.title) {
|
||||
updates.organizationTitle = authClaims.organizations[0].title
|
||||
}
|
||||
if (payload.email) {
|
||||
updates.email = encrypt(payload.email)
|
||||
}
|
||||
if (payload.email_verified !== undefined) {
|
||||
updates.emailVerified = payload.email_verified
|
||||
}
|
||||
|
||||
logger.info(`Updated user info from ID Token for account ${accountId}`)
|
||||
}
|
||||
} catch (e) {
|
||||
logger.warn(`Failed to parse ID Token for account ${accountId}:`, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 如果返回了新的 refresh token,更新它
|
||||
@@ -248,8 +390,34 @@ async function refreshAccountToken(accountId) {
|
||||
logRefreshSuccess(accountId, accountName, 'openai', newTokens.expiry_date)
|
||||
return newTokens
|
||||
} catch (error) {
|
||||
logRefreshError(accountId, accountName, 'openai', error.message)
|
||||
logRefreshError(accountId, account?.name || accountName, 'openai', error.message)
|
||||
|
||||
// 发送 Webhook 通知(如果启用)
|
||||
try {
|
||||
const webhookNotifier = require('../utils/webhookNotifier')
|
||||
await webhookNotifier.sendAccountAnomalyNotification({
|
||||
accountId,
|
||||
accountName: account?.name || accountName,
|
||||
platform: 'openai',
|
||||
status: 'error',
|
||||
errorCode: 'OPENAI_TOKEN_REFRESH_FAILED',
|
||||
reason: `Token refresh failed: ${error.message}`,
|
||||
timestamp: new Date().toISOString()
|
||||
})
|
||||
logger.info(
|
||||
`📢 Webhook notification sent for OpenAI account ${account?.name || accountName} refresh failure`
|
||||
)
|
||||
} catch (webhookError) {
|
||||
logger.error('Failed to send webhook notification:', webhookError)
|
||||
}
|
||||
|
||||
throw error
|
||||
} finally {
|
||||
// 确保释放锁
|
||||
if (lockAcquired) {
|
||||
await tokenRefreshService.releaseRefreshLock(accountId, 'openai')
|
||||
logger.debug(`🔓 Released refresh lock for OpenAI account ${accountId}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -270,6 +438,10 @@ async function createAccount(accountData) {
|
||||
// 处理账户信息
|
||||
const accountInfo = accountData.accountInfo || {}
|
||||
|
||||
// 检查邮箱是否已经是加密格式(包含冒号分隔的32位十六进制字符)
|
||||
const isEmailEncrypted =
|
||||
accountInfo.email && accountInfo.email.length >= 33 && accountInfo.email.charAt(32) === ':'
|
||||
|
||||
const account = {
|
||||
id: accountId,
|
||||
name: accountData.name,
|
||||
@@ -282,19 +454,25 @@ async function createAccount(accountData) {
|
||||
? accountData.rateLimitDuration
|
||||
: 60,
|
||||
// OAuth相关字段(加密存储)
|
||||
idToken: encrypt(oauthData.idToken || ''),
|
||||
accessToken: encrypt(oauthData.accessToken || ''),
|
||||
refreshToken: encrypt(oauthData.refreshToken || ''),
|
||||
// ID Token 现在是可选的,如果没有提供会在首次刷新时自动获取
|
||||
idToken: oauthData.idToken && oauthData.idToken.trim() ? encrypt(oauthData.idToken) : '',
|
||||
accessToken:
|
||||
oauthData.accessToken && oauthData.accessToken.trim() ? encrypt(oauthData.accessToken) : '',
|
||||
refreshToken:
|
||||
oauthData.refreshToken && oauthData.refreshToken.trim()
|
||||
? encrypt(oauthData.refreshToken)
|
||||
: '',
|
||||
openaiOauth: encrypt(JSON.stringify(oauthData)),
|
||||
// 账户信息字段
|
||||
// 账户信息字段 - 确保所有字段都被保存,即使是空字符串
|
||||
accountId: accountInfo.accountId || '',
|
||||
chatgptUserId: accountInfo.chatgptUserId || '',
|
||||
organizationId: accountInfo.organizationId || '',
|
||||
organizationRole: accountInfo.organizationRole || '',
|
||||
organizationTitle: accountInfo.organizationTitle || '',
|
||||
planType: accountInfo.planType || '',
|
||||
email: encrypt(accountInfo.email || ''),
|
||||
emailVerified: accountInfo.emailVerified || false,
|
||||
// 邮箱字段:检查是否已经加密,避免双重加密
|
||||
email: isEmailEncrypted ? accountInfo.email : encrypt(accountInfo.email || ''),
|
||||
emailVerified: accountInfo.emailVerified === true ? 'true' : 'false',
|
||||
// 过期时间
|
||||
expiresAt: oauthData.expires_in
|
||||
? new Date(Date.now() + oauthData.expires_in * 1000).toISOString()
|
||||
@@ -339,9 +517,10 @@ async function getAccount(accountId) {
|
||||
if (accountData.idToken) {
|
||||
accountData.idToken = decrypt(accountData.idToken)
|
||||
}
|
||||
if (accountData.accessToken) {
|
||||
accountData.accessToken = decrypt(accountData.accessToken)
|
||||
}
|
||||
// 注意:accessToken 在 openaiRoutes.js 中会被单独解密,这里不解密
|
||||
// if (accountData.accessToken) {
|
||||
// accountData.accessToken = decrypt(accountData.accessToken)
|
||||
// }
|
||||
if (accountData.refreshToken) {
|
||||
accountData.refreshToken = decrypt(accountData.refreshToken)
|
||||
}
|
||||
@@ -391,7 +570,7 @@ async function updateAccount(accountId, updates) {
|
||||
if (updates.accessToken) {
|
||||
updates.accessToken = encrypt(updates.accessToken)
|
||||
}
|
||||
if (updates.refreshToken) {
|
||||
if (updates.refreshToken && updates.refreshToken.trim()) {
|
||||
updates.refreshToken = encrypt(updates.refreshToken)
|
||||
}
|
||||
if (updates.email) {
|
||||
@@ -476,6 +655,9 @@ async function getAllAccounts() {
|
||||
accountData.email = decrypt(accountData.email)
|
||||
}
|
||||
|
||||
// 先保存 refreshToken 是否存在的标记
|
||||
const hasRefreshTokenFlag = !!accountData.refreshToken
|
||||
|
||||
// 屏蔽敏感信息(token等不应该返回给前端)
|
||||
delete accountData.idToken
|
||||
delete accountData.accessToken
|
||||
@@ -512,7 +694,7 @@ async function getAllAccounts() {
|
||||
scopes:
|
||||
accountData.scopes && accountData.scopes.trim() ? accountData.scopes.split(' ') : [],
|
||||
// 添加 hasRefreshToken 标记
|
||||
hasRefreshToken: !!accountData.refreshToken,
|
||||
hasRefreshToken: hasRefreshTokenFlag,
|
||||
// 添加限流状态信息(统一格式)
|
||||
rateLimitStatus: rateLimitInfo
|
||||
? {
|
||||
@@ -640,6 +822,26 @@ async function setAccountRateLimited(accountId, isLimited) {
|
||||
|
||||
await updateAccount(accountId, updates)
|
||||
logger.info(`Set rate limit status for OpenAI account ${accountId}: ${updates.rateLimitStatus}`)
|
||||
|
||||
// 如果被限流,发送 Webhook 通知
|
||||
if (isLimited) {
|
||||
try {
|
||||
const account = await getAccount(accountId)
|
||||
const webhookNotifier = require('../utils/webhookNotifier')
|
||||
await webhookNotifier.sendAccountAnomalyNotification({
|
||||
accountId,
|
||||
accountName: account.name || accountId,
|
||||
platform: 'openai',
|
||||
status: 'blocked',
|
||||
errorCode: 'OPENAI_RATE_LIMITED',
|
||||
reason: 'Account rate limited (429 error). Estimated reset in 1 hour',
|
||||
timestamp: new Date().toISOString()
|
||||
})
|
||||
logger.info(`📢 Webhook notification sent for OpenAI account ${account.name} rate limit`)
|
||||
} catch (webhookError) {
|
||||
logger.error('Failed to send rate limit webhook notification:', webhookError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 切换账户调度状态
|
||||
|
||||
@@ -20,6 +20,77 @@ class UnifiedClaudeScheduler {
|
||||
return schedulable !== false && schedulable !== 'false'
|
||||
}
|
||||
|
||||
// 🔍 检查账户是否支持请求的模型
|
||||
_isModelSupportedByAccount(account, accountType, requestedModel, context = '') {
|
||||
if (!requestedModel) {
|
||||
return true // 没有指定模型时,默认支持
|
||||
}
|
||||
|
||||
// Claude OAuth 账户的 Opus 模型检查
|
||||
if (accountType === 'claude-official') {
|
||||
if (requestedModel.toLowerCase().includes('opus')) {
|
||||
if (account.subscriptionInfo) {
|
||||
try {
|
||||
const info =
|
||||
typeof account.subscriptionInfo === 'string'
|
||||
? JSON.parse(account.subscriptionInfo)
|
||||
: account.subscriptionInfo
|
||||
|
||||
// Pro 和 Free 账号不支持 Opus
|
||||
if (info.hasClaudePro === true && info.hasClaudeMax !== true) {
|
||||
logger.info(
|
||||
`🚫 Claude account ${account.name} (Pro) does not support Opus model${context ? ` ${context}` : ''}`
|
||||
)
|
||||
return false
|
||||
}
|
||||
if (info.accountType === 'claude_pro' || info.accountType === 'claude_free') {
|
||||
logger.info(
|
||||
`🚫 Claude account ${account.name} (${info.accountType}) does not support Opus model${context ? ` ${context}` : ''}`
|
||||
)
|
||||
return false
|
||||
}
|
||||
} catch (e) {
|
||||
// 解析失败,假设为旧数据,默认支持(兼容旧数据为 Max)
|
||||
logger.debug(
|
||||
`Account ${account.name} has invalid subscriptionInfo${context ? ` ${context}` : ''}, assuming Max`
|
||||
)
|
||||
}
|
||||
}
|
||||
// 没有订阅信息的账号,默认当作支持(兼容旧数据)
|
||||
}
|
||||
}
|
||||
|
||||
// Claude Console 账户的模型支持检查
|
||||
if (accountType === 'claude-console' && account.supportedModels) {
|
||||
// 兼容旧格式(数组)和新格式(对象)
|
||||
if (Array.isArray(account.supportedModels)) {
|
||||
// 旧格式:数组
|
||||
if (
|
||||
account.supportedModels.length > 0 &&
|
||||
!account.supportedModels.includes(requestedModel)
|
||||
) {
|
||||
logger.info(
|
||||
`🚫 Claude Console account ${account.name} does not support model ${requestedModel}${context ? ` ${context}` : ''}`
|
||||
)
|
||||
return false
|
||||
}
|
||||
} else if (typeof account.supportedModels === 'object') {
|
||||
// 新格式:映射表
|
||||
if (
|
||||
Object.keys(account.supportedModels).length > 0 &&
|
||||
!claudeConsoleAccountService.isModelSupported(account.supportedModels, requestedModel)
|
||||
) {
|
||||
logger.info(
|
||||
`🚫 Claude Console account ${account.name} does not support model ${requestedModel}${context ? ` ${context}` : ''}`
|
||||
)
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// 🎯 统一调度Claude账号(官方和Console)
|
||||
async selectAccountForApiKey(apiKeyData, sessionHash = null, requestedModel = null) {
|
||||
try {
|
||||
@@ -102,7 +173,8 @@ class UnifiedClaudeScheduler {
|
||||
// 验证映射的账户是否仍然可用
|
||||
const isAvailable = await this._isAccountAvailable(
|
||||
mappedAccount.accountId,
|
||||
mappedAccount.accountType
|
||||
mappedAccount.accountType,
|
||||
requestedModel
|
||||
)
|
||||
if (isAvailable) {
|
||||
logger.info(
|
||||
@@ -209,10 +281,25 @@ class UnifiedClaudeScheduler {
|
||||
boundConsoleAccount.isActive === true &&
|
||||
boundConsoleAccount.status === 'active'
|
||||
) {
|
||||
// 主动触发一次额度检查
|
||||
try {
|
||||
await claudeConsoleAccountService.checkQuotaUsage(boundConsoleAccount.id)
|
||||
} catch (e) {
|
||||
logger.warn(
|
||||
`Failed to check quota for bound Claude Console account ${boundConsoleAccount.name}: ${e.message}`
|
||||
)
|
||||
// 继续使用该账号
|
||||
}
|
||||
|
||||
// 检查限流状态和额度状态
|
||||
const isRateLimited = await claudeConsoleAccountService.isAccountRateLimited(
|
||||
boundConsoleAccount.id
|
||||
)
|
||||
if (!isRateLimited) {
|
||||
const isQuotaExceeded = await claudeConsoleAccountService.isAccountQuotaExceeded(
|
||||
boundConsoleAccount.id
|
||||
)
|
||||
|
||||
if (!isRateLimited && !isQuotaExceeded) {
|
||||
logger.info(
|
||||
`🎯 Using bound dedicated Claude Console account: ${boundConsoleAccount.name} (${apiKeyData.claudeConsoleAccountId})`
|
||||
)
|
||||
@@ -269,33 +356,9 @@ class UnifiedClaudeScheduler {
|
||||
) {
|
||||
// 检查是否可调度
|
||||
|
||||
// 检查模型支持(如果请求的是 Opus 模型)
|
||||
if (requestedModel && requestedModel.toLowerCase().includes('opus')) {
|
||||
// 检查账号的订阅信息
|
||||
if (account.subscriptionInfo) {
|
||||
try {
|
||||
const info =
|
||||
typeof account.subscriptionInfo === 'string'
|
||||
? JSON.parse(account.subscriptionInfo)
|
||||
: account.subscriptionInfo
|
||||
|
||||
// Pro 和 Free 账号不支持 Opus
|
||||
if (info.hasClaudePro === true && info.hasClaudeMax !== true) {
|
||||
logger.info(`🚫 Claude account ${account.name} (Pro) does not support Opus model`)
|
||||
continue // Claude Pro 不支持 Opus
|
||||
}
|
||||
if (info.accountType === 'claude_pro' || info.accountType === 'claude_free') {
|
||||
logger.info(
|
||||
`🚫 Claude account ${account.name} (${info.accountType}) does not support Opus model`
|
||||
)
|
||||
continue // 明确标记为 Pro 或 Free 的账号不支持
|
||||
}
|
||||
} catch (e) {
|
||||
// 解析失败,假设为旧数据,默认支持(兼容旧数据为 Max)
|
||||
logger.debug(`Account ${account.name} has invalid subscriptionInfo, assuming Max`)
|
||||
}
|
||||
}
|
||||
// 没有订阅信息的账号,默认当作支持(兼容旧数据)
|
||||
// 检查模型支持
|
||||
if (!this._isModelSupportedByAccount(account, 'claude-official', requestedModel)) {
|
||||
continue
|
||||
}
|
||||
|
||||
// 检查是否被限流
|
||||
@@ -330,37 +393,26 @@ class UnifiedClaudeScheduler {
|
||||
) {
|
||||
// 检查是否可调度
|
||||
|
||||
// 检查模型支持(如果有请求的模型)
|
||||
if (requestedModel && account.supportedModels) {
|
||||
// 兼容旧格式(数组)和新格式(对象)
|
||||
if (Array.isArray(account.supportedModels)) {
|
||||
// 旧格式:数组
|
||||
if (
|
||||
account.supportedModels.length > 0 &&
|
||||
!account.supportedModels.includes(requestedModel)
|
||||
) {
|
||||
logger.info(
|
||||
`🚫 Claude Console account ${account.name} does not support model ${requestedModel}`
|
||||
)
|
||||
continue
|
||||
}
|
||||
} else if (typeof account.supportedModels === 'object') {
|
||||
// 新格式:映射表
|
||||
if (
|
||||
Object.keys(account.supportedModels).length > 0 &&
|
||||
!claudeConsoleAccountService.isModelSupported(account.supportedModels, requestedModel)
|
||||
) {
|
||||
logger.info(
|
||||
`🚫 Claude Console account ${account.name} does not support model ${requestedModel}`
|
||||
)
|
||||
continue
|
||||
}
|
||||
}
|
||||
// 检查模型支持
|
||||
if (!this._isModelSupportedByAccount(account, 'claude-console', requestedModel)) {
|
||||
continue
|
||||
}
|
||||
|
||||
// 主动触发一次额度检查,确保状态即时生效
|
||||
try {
|
||||
await claudeConsoleAccountService.checkQuotaUsage(account.id)
|
||||
} catch (e) {
|
||||
logger.warn(
|
||||
`Failed to check quota for Claude Console account ${account.name}: ${e.message}`
|
||||
)
|
||||
// 继续处理该账号
|
||||
}
|
||||
|
||||
// 检查是否被限流
|
||||
const isRateLimited = await claudeConsoleAccountService.isAccountRateLimited(account.id)
|
||||
if (!isRateLimited) {
|
||||
const isQuotaExceeded = await claudeConsoleAccountService.isAccountQuotaExceeded(account.id)
|
||||
|
||||
if (!isRateLimited && !isQuotaExceeded) {
|
||||
availableAccounts.push({
|
||||
...account,
|
||||
accountId: account.id,
|
||||
@@ -372,7 +424,12 @@ class UnifiedClaudeScheduler {
|
||||
`✅ Added Claude Console account to available pool: ${account.name} (priority: ${account.priority})`
|
||||
)
|
||||
} else {
|
||||
logger.warn(`⚠️ Claude Console account ${account.name} is rate limited`)
|
||||
if (isRateLimited) {
|
||||
logger.warn(`⚠️ Claude Console account ${account.name} is rate limited`)
|
||||
}
|
||||
if (isQuotaExceeded) {
|
||||
logger.warn(`💰 Claude Console account ${account.name} quota exceeded`)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
logger.info(
|
||||
@@ -439,7 +496,7 @@ class UnifiedClaudeScheduler {
|
||||
}
|
||||
|
||||
// 🔍 检查账户是否可用
|
||||
async _isAccountAvailable(accountId, accountType) {
|
||||
async _isAccountAvailable(accountId, accountType, requestedModel = null) {
|
||||
try {
|
||||
if (accountType === 'claude-official') {
|
||||
const account = await redis.getClaudeAccount(accountId)
|
||||
@@ -456,6 +513,19 @@ class UnifiedClaudeScheduler {
|
||||
logger.info(`🚫 Account ${accountId} is not schedulable`)
|
||||
return false
|
||||
}
|
||||
|
||||
// 检查模型兼容性
|
||||
if (
|
||||
!this._isModelSupportedByAccount(
|
||||
account,
|
||||
'claude-official',
|
||||
requestedModel,
|
||||
'in session check'
|
||||
)
|
||||
) {
|
||||
return false
|
||||
}
|
||||
|
||||
return !(await claudeAccountService.isAccountRateLimited(accountId))
|
||||
} else if (accountType === 'claude-console') {
|
||||
const account = await claudeConsoleAccountService.getAccount(accountId)
|
||||
@@ -475,10 +545,32 @@ class UnifiedClaudeScheduler {
|
||||
logger.info(`🚫 Claude Console account ${accountId} is not schedulable`)
|
||||
return false
|
||||
}
|
||||
// 检查模型支持
|
||||
if (
|
||||
!this._isModelSupportedByAccount(
|
||||
account,
|
||||
'claude-console',
|
||||
requestedModel,
|
||||
'in session check'
|
||||
)
|
||||
) {
|
||||
return false
|
||||
}
|
||||
// 检查是否超额
|
||||
try {
|
||||
await claudeConsoleAccountService.checkQuotaUsage(accountId)
|
||||
} catch (e) {
|
||||
logger.warn(`Failed to check quota for Claude Console account ${accountId}: ${e.message}`)
|
||||
// 继续处理
|
||||
}
|
||||
|
||||
// 检查是否被限流
|
||||
if (await claudeConsoleAccountService.isAccountRateLimited(accountId)) {
|
||||
return false
|
||||
}
|
||||
if (await claudeConsoleAccountService.isAccountQuotaExceeded(accountId)) {
|
||||
return false
|
||||
}
|
||||
// 检查是否未授权(401错误)
|
||||
if (account.status === 'unauthorized') {
|
||||
return false
|
||||
@@ -636,6 +728,32 @@ class UnifiedClaudeScheduler {
|
||||
}
|
||||
}
|
||||
|
||||
// 🚫 标记账户为被封锁状态(403错误)
|
||||
async markAccountBlocked(accountId, accountType, sessionHash = null) {
|
||||
try {
|
||||
// 只处理claude-official类型的账户,不处理claude-console和gemini
|
||||
if (accountType === 'claude-official') {
|
||||
await claudeAccountService.markAccountBlocked(accountId, sessionHash)
|
||||
|
||||
// 删除会话映射
|
||||
if (sessionHash) {
|
||||
await this._deleteSessionMapping(sessionHash)
|
||||
}
|
||||
|
||||
logger.warn(`🚫 Account ${accountId} marked as blocked due to 403 error`)
|
||||
} else {
|
||||
logger.info(
|
||||
`ℹ️ Skipping blocked marking for non-Claude OAuth account: ${accountId} (${accountType})`
|
||||
)
|
||||
}
|
||||
|
||||
return { success: true }
|
||||
} catch (error) {
|
||||
logger.error(`❌ Failed to mark account as blocked: ${accountId} (${accountType})`, error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
// 🚫 标记Claude Console账户为封锁状态(模型不支持)
|
||||
async blockConsoleAccount(accountId, reason) {
|
||||
try {
|
||||
@@ -667,7 +785,8 @@ class UnifiedClaudeScheduler {
|
||||
if (memberIds.includes(mappedAccount.accountId)) {
|
||||
const isAvailable = await this._isAccountAvailable(
|
||||
mappedAccount.accountId,
|
||||
mappedAccount.accountType
|
||||
mappedAccount.accountType,
|
||||
requestedModel
|
||||
)
|
||||
if (isAvailable) {
|
||||
logger.info(
|
||||
@@ -730,19 +849,9 @@ class UnifiedClaudeScheduler {
|
||||
: account.status === 'active'
|
||||
|
||||
if (isActive && status && this._isSchedulable(account.schedulable)) {
|
||||
// 检查模型支持(Console账户)
|
||||
if (
|
||||
accountType === 'claude-console' &&
|
||||
requestedModel &&
|
||||
account.supportedModels &&
|
||||
account.supportedModels.length > 0
|
||||
) {
|
||||
if (!account.supportedModels.includes(requestedModel)) {
|
||||
logger.info(
|
||||
`🚫 Account ${account.name} in group does not support model ${requestedModel}`
|
||||
)
|
||||
continue
|
||||
}
|
||||
// 检查模型支持
|
||||
if (!this._isModelSupportedByAccount(account, accountType, requestedModel, 'in group')) {
|
||||
continue
|
||||
}
|
||||
|
||||
// 检查是否被限流
|
||||
|
||||
@@ -167,7 +167,7 @@ class UnifiedOpenAIScheduler {
|
||||
|
||||
// 获取所有OpenAI账户(共享池)
|
||||
const openaiAccounts = await openaiAccountService.getAllAccounts()
|
||||
for (const account of openaiAccounts) {
|
||||
for (let account of openaiAccounts) {
|
||||
if (
|
||||
account.isActive &&
|
||||
account.status !== 'error' &&
|
||||
@@ -176,13 +176,27 @@ class UnifiedOpenAIScheduler {
|
||||
) {
|
||||
// 检查是否可调度
|
||||
|
||||
// 检查token是否过期
|
||||
// 检查token是否过期并自动刷新
|
||||
const isExpired = openaiAccountService.isTokenExpired(account)
|
||||
if (isExpired && !account.refreshToken) {
|
||||
logger.warn(
|
||||
`⚠️ OpenAI account ${account.name} token expired and no refresh token available`
|
||||
)
|
||||
continue
|
||||
if (isExpired) {
|
||||
if (!account.refreshToken) {
|
||||
logger.warn(
|
||||
`⚠️ OpenAI account ${account.name} token expired and no refresh token available`
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
// 自动刷新过期的 token
|
||||
try {
|
||||
logger.info(`🔄 Auto-refreshing expired token for OpenAI account ${account.name}`)
|
||||
await openaiAccountService.refreshAccountToken(account.id)
|
||||
// 重新获取更新后的账户信息
|
||||
account = await openaiAccountService.getAccount(account.id)
|
||||
logger.info(`✅ Token refreshed successfully for ${account.name}`)
|
||||
} catch (refreshError) {
|
||||
logger.error(`❌ Failed to refresh token for ${account.name}:`, refreshError.message)
|
||||
continue // 刷新失败,跳过此账户
|
||||
}
|
||||
}
|
||||
|
||||
// 检查模型支持(仅在明确设置了supportedModels且不为空时才检查)
|
||||
|
||||
@@ -75,6 +75,11 @@ class UserService {
|
||||
await redis.set(`${this.userPrefix}${user.id}`, JSON.stringify(user))
|
||||
await redis.set(`${this.usernamePrefix}${username}`, user.id)
|
||||
|
||||
// 如果是新用户,尝试转移匹配的API Keys
|
||||
if (isNewUser) {
|
||||
await this.transferMatchingApiKeys(user)
|
||||
}
|
||||
|
||||
logger.info(`📝 ${isNewUser ? 'Created' : 'Updated'} user: ${username} (${user.id})`)
|
||||
return user
|
||||
} catch (error) {
|
||||
@@ -509,6 +514,80 @@ class UserService {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
// 🔄 转移匹配的API Keys给新用户
|
||||
async transferMatchingApiKeys(user) {
|
||||
try {
|
||||
const apiKeyService = require('./apiKeyService')
|
||||
const { displayName, username, email } = user
|
||||
|
||||
// 获取所有API Keys
|
||||
const allApiKeys = await apiKeyService.getAllApiKeys()
|
||||
|
||||
// 找到没有用户ID的API Keys(即由Admin创建的)
|
||||
const unownedApiKeys = allApiKeys.filter((key) => !key.userId || key.userId === '')
|
||||
|
||||
if (unownedApiKeys.length === 0) {
|
||||
logger.debug(`📝 No unowned API keys found for potential transfer to user: ${username}`)
|
||||
return
|
||||
}
|
||||
|
||||
// 构建匹配字符串数组(只考虑displayName、username、email,去除空值和重复值)
|
||||
const matchStrings = new Set()
|
||||
if (displayName) {
|
||||
matchStrings.add(displayName.toLowerCase().trim())
|
||||
}
|
||||
if (username) {
|
||||
matchStrings.add(username.toLowerCase().trim())
|
||||
}
|
||||
if (email) {
|
||||
matchStrings.add(email.toLowerCase().trim())
|
||||
}
|
||||
|
||||
const matchingKeys = []
|
||||
|
||||
// 查找名称匹配的API Keys(只进行完全匹配)
|
||||
for (const apiKey of unownedApiKeys) {
|
||||
const keyName = apiKey.name ? apiKey.name.toLowerCase().trim() : ''
|
||||
|
||||
// 检查API Key名称是否与用户信息完全匹配
|
||||
for (const matchString of matchStrings) {
|
||||
if (keyName === matchString) {
|
||||
matchingKeys.push(apiKey)
|
||||
break // 找到匹配后跳出内层循环
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 转移匹配的API Keys
|
||||
let transferredCount = 0
|
||||
for (const apiKey of matchingKeys) {
|
||||
try {
|
||||
await apiKeyService.updateApiKey(apiKey.id, {
|
||||
userId: user.id,
|
||||
userUsername: user.username,
|
||||
createdBy: user.username
|
||||
})
|
||||
|
||||
transferredCount++
|
||||
logger.info(`🔄 Transferred API key "${apiKey.name}" (${apiKey.id}) to user: ${username}`)
|
||||
} catch (error) {
|
||||
logger.error(`❌ Failed to transfer API key ${apiKey.id} to user ${username}:`, error)
|
||||
}
|
||||
}
|
||||
|
||||
if (transferredCount > 0) {
|
||||
logger.success(
|
||||
`🎉 Successfully transferred ${transferredCount} API key(s) to new user: ${username} (${displayName})`
|
||||
)
|
||||
} else if (matchingKeys.length === 0) {
|
||||
logger.debug(`📝 No matching API keys found for user: ${username} (${displayName})`)
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error('❌ Error transferring matching API keys:', error)
|
||||
// Don't throw error to prevent blocking user creation
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new UserService()
|
||||
|
||||
9
src/services/webhookService.js
Normal file → Executable file
9
src/services/webhookService.js
Normal file → Executable file
@@ -3,6 +3,7 @@ const crypto = require('crypto')
|
||||
const logger = require('../utils/logger')
|
||||
const webhookConfigService = require('./webhookConfigService')
|
||||
const { getISOStringWithTimezone } = require('../utils/dateHelper')
|
||||
const appConfig = require('../../config/config')
|
||||
|
||||
class WebhookService {
|
||||
constructor() {
|
||||
@@ -15,6 +16,7 @@ class WebhookService {
|
||||
custom: this.sendToCustom.bind(this),
|
||||
bark: this.sendToBark.bind(this)
|
||||
}
|
||||
this.timezone = appConfig.system.timezone || 'Asia/Shanghai'
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -309,11 +311,10 @@ class WebhookService {
|
||||
formatMessageForWechatWork(type, data) {
|
||||
const title = this.getNotificationTitle(type)
|
||||
const details = this.formatNotificationDetails(data)
|
||||
|
||||
return (
|
||||
`## ${title}\n\n` +
|
||||
`> **服务**: Claude Relay Service\n` +
|
||||
`> **时间**: ${new Date().toLocaleString('zh-CN')}\n\n${details}`
|
||||
`> **时间**: ${new Date().toLocaleString('zh-CN', { timeZone: this.timezone })}\n\n${details}`
|
||||
)
|
||||
}
|
||||
|
||||
@@ -325,7 +326,7 @@ class WebhookService {
|
||||
|
||||
return (
|
||||
`#### 服务: Claude Relay Service\n` +
|
||||
`#### 时间: ${new Date().toLocaleString('zh-CN')}\n\n${details}`
|
||||
`#### 时间: ${new Date().toLocaleString('zh-CN', { timeZone: this.timezone })}\n\n${details}`
|
||||
)
|
||||
}
|
||||
|
||||
@@ -450,7 +451,7 @@ class WebhookService {
|
||||
|
||||
// 添加服务标识和时间戳
|
||||
lines.push(`\n服务: Claude Relay Service`)
|
||||
lines.push(`时间: ${new Date().toLocaleString('zh-CN')}`)
|
||||
lines.push(`时间: ${new Date().toLocaleString('zh-CN', { timeZone: this.timezone })}`)
|
||||
|
||||
return lines.join('\n')
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
const winston = require('winston')
|
||||
const DailyRotateFile = require('winston-daily-rotate-file')
|
||||
const config = require('../../config/config')
|
||||
const { formatDateWithTimezone } = require('../utils/dateHelper')
|
||||
const path = require('path')
|
||||
const fs = require('fs')
|
||||
const os = require('os')
|
||||
@@ -95,7 +96,7 @@ const safeStringify = (obj, maxDepth = 3, fullDepth = false) => {
|
||||
// 📝 增强的日志格式
|
||||
const createLogFormat = (colorize = false) => {
|
||||
const formats = [
|
||||
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
|
||||
winston.format.timestamp({ format: () => formatDateWithTimezone(new Date(), false) }),
|
||||
winston.format.errors({ stack: true })
|
||||
// 移除 winston.format.metadata() 来避免自动包装
|
||||
]
|
||||
|
||||
@@ -4,7 +4,7 @@ const logger = require('./logger')
|
||||
class SessionHelper {
|
||||
/**
|
||||
* 生成会话哈希,用于sticky会话保持
|
||||
* 基于Anthropic的prompt caching机制,优先使用cacheable内容
|
||||
* 基于Anthropic的prompt caching机制,优先使用metadata中的session ID
|
||||
* @param {Object} requestBody - 请求体
|
||||
* @returns {string|null} - 32字符的会话哈希,如果无法生成则返回null
|
||||
*/
|
||||
@@ -13,11 +13,24 @@ class SessionHelper {
|
||||
return null
|
||||
}
|
||||
|
||||
// 1. 最高优先级:使用metadata中的session ID(直接使用,无需hash)
|
||||
if (requestBody.metadata && requestBody.metadata.user_id) {
|
||||
// 提取 session_xxx 部分
|
||||
const userIdString = requestBody.metadata.user_id
|
||||
const sessionMatch = userIdString.match(/session_([a-f0-9-]{36})/)
|
||||
if (sessionMatch && sessionMatch[1]) {
|
||||
const sessionId = sessionMatch[1]
|
||||
// 直接返回session ID
|
||||
logger.debug(`📋 Session ID extracted from metadata.user_id: ${sessionId}`)
|
||||
return sessionId
|
||||
}
|
||||
}
|
||||
|
||||
let cacheableContent = ''
|
||||
const system = requestBody.system || ''
|
||||
const messages = requestBody.messages || []
|
||||
|
||||
// 1. 优先提取带有cache_control: {"type": "ephemeral"}的内容
|
||||
// 2. 提取带有cache_control: {"type": "ephemeral"}的内容
|
||||
// 检查system中的cacheable内容
|
||||
if (Array.isArray(system)) {
|
||||
for (const part of system) {
|
||||
@@ -30,13 +43,13 @@ class SessionHelper {
|
||||
// 检查messages中的cacheable内容
|
||||
for (const msg of messages) {
|
||||
const content = msg.content || ''
|
||||
let hasCacheControl = false
|
||||
|
||||
if (Array.isArray(content)) {
|
||||
for (const part of content) {
|
||||
if (part && part.cache_control && part.cache_control.type === 'ephemeral') {
|
||||
if (part.type === 'text') {
|
||||
cacheableContent += part.text || ''
|
||||
}
|
||||
// 其他类型(如image)不参与hash计算
|
||||
hasCacheControl = true
|
||||
break
|
||||
}
|
||||
}
|
||||
} else if (
|
||||
@@ -44,12 +57,31 @@ class SessionHelper {
|
||||
msg.cache_control &&
|
||||
msg.cache_control.type === 'ephemeral'
|
||||
) {
|
||||
// 罕见情况,但需要检查
|
||||
cacheableContent += content
|
||||
hasCacheControl = true
|
||||
}
|
||||
|
||||
if (hasCacheControl) {
|
||||
for (const message of messages) {
|
||||
let messageText = ''
|
||||
if (typeof message.content === 'string') {
|
||||
messageText = message.content
|
||||
} else if (Array.isArray(message.content)) {
|
||||
messageText = message.content
|
||||
.filter((part) => part.type === 'text')
|
||||
.map((part) => part.text || '')
|
||||
.join('')
|
||||
}
|
||||
|
||||
if (messageText) {
|
||||
cacheableContent += messageText
|
||||
break
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 如果有cacheable内容,直接使用
|
||||
// 3. 如果有cacheable内容,直接使用
|
||||
if (cacheableContent) {
|
||||
const hash = crypto
|
||||
.createHash('sha256')
|
||||
@@ -60,7 +92,7 @@ class SessionHelper {
|
||||
return hash
|
||||
}
|
||||
|
||||
// 3. Fallback: 使用system内容
|
||||
// 4. Fallback: 使用system内容
|
||||
if (system) {
|
||||
let systemText = ''
|
||||
if (typeof system === 'string') {
|
||||
@@ -76,7 +108,7 @@ class SessionHelper {
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 最后fallback: 使用第一条消息内容
|
||||
// 5. 最后fallback: 使用第一条消息内容
|
||||
if (messages.length > 0) {
|
||||
const firstMessage = messages[0]
|
||||
let firstMessageText = ''
|
||||
|
||||
@@ -68,6 +68,7 @@ class WebhookNotifier {
|
||||
const errorCodes = {
|
||||
'claude-oauth': {
|
||||
unauthorized: 'CLAUDE_OAUTH_UNAUTHORIZED',
|
||||
blocked: 'CLAUDE_OAUTH_BLOCKED',
|
||||
error: 'CLAUDE_OAUTH_ERROR',
|
||||
disabled: 'CLAUDE_OAUTH_MANUALLY_DISABLED'
|
||||
},
|
||||
@@ -80,6 +81,12 @@ class WebhookNotifier {
|
||||
error: 'GEMINI_ERROR',
|
||||
unauthorized: 'GEMINI_UNAUTHORIZED',
|
||||
disabled: 'GEMINI_MANUALLY_DISABLED'
|
||||
},
|
||||
openai: {
|
||||
error: 'OPENAI_ERROR',
|
||||
unauthorized: 'OPENAI_UNAUTHORIZED',
|
||||
blocked: 'OPENAI_RATE_LIMITED',
|
||||
disabled: 'OPENAI_MANUALLY_DISABLED'
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
--bg-gradient-mid: #764ba2;
|
||||
--bg-gradient-end: #f093fb;
|
||||
--input-bg: rgba(255, 255, 255, 0.9);
|
||||
--input-border: rgba(255, 255, 255, 0.3);
|
||||
--input-border: rgba(209, 213, 219, 0.8);
|
||||
--modal-bg: rgba(0, 0, 0, 0.4);
|
||||
--table-bg: rgba(255, 255, 255, 0.95);
|
||||
--table-hover: rgba(102, 126, 234, 0.05);
|
||||
|
||||
@@ -176,7 +176,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="form.name"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
:class="{ 'border-red-500': errors.name }"
|
||||
placeholder="为账户设置一个易识别的名称"
|
||||
required
|
||||
@@ -193,7 +193,7 @@
|
||||
>
|
||||
<textarea
|
||||
v-model="form.description"
|
||||
class="form-input w-full resize-none dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full resize-none border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
placeholder="账户用途说明..."
|
||||
rows="3"
|
||||
/>
|
||||
@@ -300,7 +300,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="form.projectId"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
placeholder="例如:verdant-wares-464411-k9"
|
||||
type="text"
|
||||
/>
|
||||
@@ -351,7 +351,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="form.accessKeyId"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
:class="{ 'border-red-500': errors.accessKeyId }"
|
||||
placeholder="请输入 AWS Access Key ID"
|
||||
required
|
||||
@@ -368,7 +368,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="form.secretAccessKey"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
:class="{ 'border-red-500': errors.secretAccessKey }"
|
||||
placeholder="请输入 AWS Secret Access Key"
|
||||
required
|
||||
@@ -385,7 +385,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="form.region"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
:class="{ 'border-red-500': errors.region }"
|
||||
placeholder="例如:us-east-1"
|
||||
required
|
||||
@@ -419,7 +419,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="form.sessionToken"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
placeholder="如果使用临时凭证,请输入会话令牌"
|
||||
type="password"
|
||||
/>
|
||||
@@ -434,7 +434,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="form.defaultModel"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
placeholder="例如:us.anthropic.claude-sonnet-4-20250514-v1:0"
|
||||
type="text"
|
||||
/>
|
||||
@@ -463,7 +463,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="form.smallFastModel"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
placeholder="例如:us.anthropic.claude-3-5-haiku-20241022-v1:0"
|
||||
type="text"
|
||||
/>
|
||||
@@ -481,7 +481,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="form.azureEndpoint"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
:class="{ 'border-red-500': errors.azureEndpoint }"
|
||||
placeholder="https://your-resource.openai.azure.com"
|
||||
required
|
||||
@@ -501,7 +501,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="form.apiVersion"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
placeholder="2024-02-01"
|
||||
type="text"
|
||||
/>
|
||||
@@ -516,7 +516,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="form.deploymentName"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
:class="{ 'border-red-500': errors.deploymentName }"
|
||||
placeholder="gpt-4"
|
||||
required
|
||||
@@ -536,7 +536,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="form.apiKey"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
:class="{ 'border-red-500': errors.apiKey }"
|
||||
placeholder="请输入 Azure OpenAI API Key"
|
||||
required
|
||||
@@ -610,7 +610,7 @@
|
||||
>
|
||||
<input
|
||||
v-model.number="form.rateLimitDuration"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
min="1"
|
||||
placeholder="默认60分钟"
|
||||
type="number"
|
||||
@@ -630,7 +630,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="form.apiUrl"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
:class="{ 'border-red-500': errors.apiUrl }"
|
||||
placeholder="例如:https://api.example.com"
|
||||
required
|
||||
@@ -647,7 +647,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="form.apiKey"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
:class="{ 'border-red-500': errors.apiKey }"
|
||||
placeholder="请输入API Key"
|
||||
required
|
||||
@@ -658,6 +658,41 @@
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<!-- 额度管理字段 -->
|
||||
<div class="grid grid-cols-2 gap-4">
|
||||
<div>
|
||||
<label class="mb-3 block text-sm font-semibold text-gray-700 dark:text-gray-300">
|
||||
每日额度限制 ($)
|
||||
</label>
|
||||
<input
|
||||
v-model.number="form.dailyQuota"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200"
|
||||
min="0"
|
||||
placeholder="0 表示不限制"
|
||||
step="0.01"
|
||||
type="number"
|
||||
/>
|
||||
<p class="mt-1 text-xs text-gray-500 dark:text-gray-400">
|
||||
设置每日使用额度,0 表示不限制
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label class="mb-3 block text-sm font-semibold text-gray-700 dark:text-gray-300">
|
||||
额度重置时间
|
||||
</label>
|
||||
<input
|
||||
v-model="form.quotaResetTime"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200"
|
||||
placeholder="00:00"
|
||||
type="time"
|
||||
/>
|
||||
<p class="mt-1 text-xs text-gray-500 dark:text-gray-400">
|
||||
每日自动重置额度的时间
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label class="mb-3 block text-sm font-semibold text-gray-700 dark:text-gray-300"
|
||||
>模型映射表 (可选)</label
|
||||
@@ -678,14 +713,14 @@
|
||||
>
|
||||
<input
|
||||
v-model="mapping.from"
|
||||
class="form-input flex-1 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input flex-1 border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
placeholder="原始模型名称"
|
||||
type="text"
|
||||
/>
|
||||
<i class="fas fa-arrow-right text-gray-400 dark:text-gray-500" />
|
||||
<input
|
||||
v-model="mapping.to"
|
||||
class="form-input flex-1 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input flex-1 border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
placeholder="映射后的模型名称"
|
||||
type="text"
|
||||
/>
|
||||
@@ -759,7 +794,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="form.userAgent"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
placeholder="留空则透传客户端 User-Agent"
|
||||
type="text"
|
||||
/>
|
||||
@@ -792,7 +827,7 @@
|
||||
>
|
||||
<input
|
||||
v-model.number="form.rateLimitDuration"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
min="1"
|
||||
placeholder="默认60分钟"
|
||||
type="number"
|
||||
@@ -906,7 +941,7 @@
|
||||
>
|
||||
<input
|
||||
v-model.number="form.priority"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
max="100"
|
||||
min="1"
|
||||
placeholder="数字越小优先级越高,默认50"
|
||||
@@ -998,34 +1033,29 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- OpenAI 平台需要 ID Token -->
|
||||
<div v-if="form.platform === 'openai'">
|
||||
<label class="mb-3 block text-sm font-semibold text-gray-700 dark:text-gray-300"
|
||||
>ID Token *</label
|
||||
>Access Token (可选)</label
|
||||
>
|
||||
<textarea
|
||||
v-model="form.idToken"
|
||||
class="form-input w-full resize-none font-mono text-xs dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
:class="{ 'border-red-500': errors.idToken }"
|
||||
placeholder="请输入 ID Token (JWT 格式)..."
|
||||
required
|
||||
v-model="form.accessToken"
|
||||
class="form-input w-full resize-none border-gray-300 font-mono text-xs dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
placeholder="可选:如果不填写,系统会自动通过 Refresh Token 获取..."
|
||||
rows="4"
|
||||
/>
|
||||
<p v-if="errors.idToken" class="mt-1 text-xs text-red-500">
|
||||
{{ errors.idToken }}
|
||||
</p>
|
||||
<p class="mt-2 text-xs text-gray-500 dark:text-gray-400">
|
||||
ID Token 是 OpenAI OAuth 认证返回的 JWT token,包含用户信息和组织信息
|
||||
<i class="fas fa-info-circle mr-1" />
|
||||
Access Token 可选填。如果不提供,系统会通过 Refresh Token 自动获取。
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<div v-else>
|
||||
<label class="mb-3 block text-sm font-semibold text-gray-700 dark:text-gray-300"
|
||||
>Access Token *</label
|
||||
>
|
||||
<textarea
|
||||
v-model="form.accessToken"
|
||||
class="form-input w-full resize-none font-mono text-xs dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full resize-none border-gray-300 font-mono text-xs dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
:class="{ 'border-red-500': errors.accessToken }"
|
||||
placeholder="请输入 Access Token..."
|
||||
required
|
||||
@@ -1036,13 +1066,34 @@
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<div v-if="form.platform === 'openai'">
|
||||
<label class="mb-3 block text-sm font-semibold text-gray-700 dark:text-gray-300"
|
||||
>Refresh Token *</label
|
||||
>
|
||||
<textarea
|
||||
v-model="form.refreshToken"
|
||||
class="form-input w-full resize-none border-gray-300 font-mono text-xs dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
:class="{ 'border-red-500': errors.refreshToken }"
|
||||
placeholder="请输入 Refresh Token(必填)..."
|
||||
required
|
||||
rows="4"
|
||||
/>
|
||||
<p v-if="errors.refreshToken" class="mt-1 text-xs text-red-500">
|
||||
{{ errors.refreshToken }}
|
||||
</p>
|
||||
<p class="mt-2 text-xs text-gray-500 dark:text-gray-400">
|
||||
<i class="fas fa-info-circle mr-1" />
|
||||
系统将使用 Refresh Token 自动获取 Access Token 和用户信息
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div v-else>
|
||||
<label class="mb-3 block text-sm font-semibold text-gray-700 dark:text-gray-300"
|
||||
>Refresh Token (可选)</label
|
||||
>
|
||||
<textarea
|
||||
v-model="form.refreshToken"
|
||||
class="form-input w-full resize-none font-mono text-xs dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full resize-none border-gray-300 font-mono text-xs dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
placeholder="请输入 Refresh Token..."
|
||||
rows="4"
|
||||
/>
|
||||
@@ -1230,7 +1281,7 @@
|
||||
</label>
|
||||
<textarea
|
||||
v-model="setupTokenAuthCode"
|
||||
class="form-input w-full resize-none font-mono text-sm dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full resize-none border-gray-300 font-mono text-sm dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
placeholder="粘贴从Claude Code授权页面获取的Authorization Code..."
|
||||
rows="3"
|
||||
/>
|
||||
@@ -1278,7 +1329,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="form.name"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
placeholder="为账户设置一个易识别的名称"
|
||||
required
|
||||
type="text"
|
||||
@@ -1291,7 +1342,7 @@
|
||||
>
|
||||
<textarea
|
||||
v-model="form.description"
|
||||
class="form-input w-full resize-none dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full resize-none border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
placeholder="账户用途说明..."
|
||||
rows="3"
|
||||
/>
|
||||
@@ -1398,7 +1449,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="form.projectId"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
placeholder="例如:verdant-wares-464411-k9"
|
||||
type="text"
|
||||
/>
|
||||
@@ -1509,7 +1560,7 @@
|
||||
>
|
||||
<input
|
||||
v-model.number="form.priority"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
max="100"
|
||||
min="1"
|
||||
placeholder="数字越小优先级越高"
|
||||
@@ -1544,6 +1595,75 @@
|
||||
<p class="mt-1 text-xs text-gray-500">留空表示不更新 API Key</p>
|
||||
</div>
|
||||
|
||||
<!-- 额度管理字段 -->
|
||||
<div class="grid grid-cols-2 gap-4">
|
||||
<div>
|
||||
<label class="mb-3 block text-sm font-semibold text-gray-700 dark:text-gray-300">
|
||||
每日额度限制 ($)
|
||||
</label>
|
||||
<input
|
||||
v-model.number="form.dailyQuota"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200"
|
||||
min="0"
|
||||
placeholder="0 表示不限制"
|
||||
step="0.01"
|
||||
type="number"
|
||||
/>
|
||||
<p class="mt-1 text-xs text-gray-500 dark:text-gray-400">
|
||||
设置每日使用额度,0 表示不限制
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label class="mb-3 block text-sm font-semibold text-gray-700 dark:text-gray-300">
|
||||
额度重置时间
|
||||
</label>
|
||||
<input
|
||||
v-model="form.quotaResetTime"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200"
|
||||
placeholder="00:00"
|
||||
type="time"
|
||||
/>
|
||||
<p class="mt-1 text-xs text-gray-500 dark:text-gray-400">每日自动重置额度的时间</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 当前使用情况(仅编辑模式显示) -->
|
||||
<div
|
||||
v-if="isEdit && form.dailyQuota > 0"
|
||||
class="rounded-lg bg-gray-50 p-4 dark:bg-gray-800"
|
||||
>
|
||||
<div class="mb-2 flex items-center justify-between">
|
||||
<span class="text-sm font-semibold text-gray-700 dark:text-gray-300">
|
||||
今日使用情况
|
||||
</span>
|
||||
<span class="text-sm text-gray-500 dark:text-gray-400">
|
||||
${{ calculateCurrentUsage().toFixed(4) }} / ${{ form.dailyQuota.toFixed(2) }}
|
||||
</span>
|
||||
</div>
|
||||
<div class="relative h-2 w-full rounded-full bg-gray-200 dark:bg-gray-700">
|
||||
<div
|
||||
class="absolute left-0 top-0 h-full rounded-full transition-all"
|
||||
:class="
|
||||
usagePercentage >= 90
|
||||
? 'bg-red-500'
|
||||
: usagePercentage >= 70
|
||||
? 'bg-yellow-500'
|
||||
: 'bg-green-500'
|
||||
"
|
||||
:style="{ width: `${Math.min(usagePercentage, 100)}%` }"
|
||||
/>
|
||||
</div>
|
||||
<div class="mt-2 flex items-center justify-between text-xs">
|
||||
<span class="text-gray-500 dark:text-gray-400">
|
||||
剩余: ${{ Math.max(0, form.dailyQuota - calculateCurrentUsage()).toFixed(2) }}
|
||||
</span>
|
||||
<span class="text-gray-500 dark:text-gray-400">
|
||||
{{ usagePercentage.toFixed(1) }}% 已使用
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label class="mb-3 block text-sm font-semibold text-gray-700"
|
||||
>模型映射表 (可选)</label
|
||||
@@ -1806,7 +1926,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="form.azureEndpoint"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
:class="{ 'border-red-500': errors.azureEndpoint }"
|
||||
placeholder="https://your-resource.openai.azure.com"
|
||||
type="url"
|
||||
@@ -1822,7 +1942,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="form.apiVersion"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
placeholder="2024-02-01"
|
||||
type="text"
|
||||
/>
|
||||
@@ -1837,7 +1957,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="form.deploymentName"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
:class="{ 'border-red-500': errors.deploymentName }"
|
||||
placeholder="gpt-4"
|
||||
type="text"
|
||||
@@ -1853,7 +1973,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="form.apiKey"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
:class="{ 'border-red-500': errors.apiKey }"
|
||||
placeholder="留空表示不更新"
|
||||
type="password"
|
||||
@@ -1928,7 +2048,7 @@
|
||||
>
|
||||
<textarea
|
||||
v-model="form.accessToken"
|
||||
class="form-input w-full resize-none font-mono text-xs dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full resize-none border-gray-300 font-mono text-xs dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
placeholder="留空表示不更新..."
|
||||
rows="4"
|
||||
/>
|
||||
@@ -1940,7 +2060,7 @@
|
||||
>
|
||||
<textarea
|
||||
v-model="form.refreshToken"
|
||||
class="form-input w-full resize-none font-mono text-xs dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full resize-none border-gray-300 font-mono text-xs dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
placeholder="留空表示不更新..."
|
||||
rows="4"
|
||||
/>
|
||||
@@ -2076,7 +2196,6 @@ const form = ref({
|
||||
groupId: '',
|
||||
groupIds: [],
|
||||
projectId: props.account?.projectId || '',
|
||||
idToken: '',
|
||||
accessToken: '',
|
||||
refreshToken: '',
|
||||
proxy: initProxyConfig(),
|
||||
@@ -2100,6 +2219,10 @@ const form = ref({
|
||||
userAgent: props.account?.userAgent || '',
|
||||
enableRateLimit: props.account ? props.account.rateLimitDuration > 0 : true,
|
||||
rateLimitDuration: props.account?.rateLimitDuration || 60,
|
||||
// 额度管理字段
|
||||
dailyQuota: props.account?.dailyQuota || 0,
|
||||
dailyUsage: props.account?.dailyUsage || 0,
|
||||
quotaResetTime: props.account?.quotaResetTime || '00:00',
|
||||
// Bedrock 特定字段
|
||||
accessKeyId: props.account?.accessKeyId || '',
|
||||
secretAccessKey: props.account?.secretAccessKey || '',
|
||||
@@ -2141,7 +2264,7 @@ const initModelMappings = () => {
|
||||
// 表单验证错误
|
||||
const errors = ref({
|
||||
name: '',
|
||||
idToken: '',
|
||||
refreshToken: '',
|
||||
accessToken: '',
|
||||
apiUrl: '',
|
||||
apiKey: '',
|
||||
@@ -2162,6 +2285,45 @@ const canExchangeSetupToken = computed(() => {
|
||||
return setupTokenAuthUrl.value && setupTokenAuthCode.value.trim()
|
||||
})
|
||||
|
||||
// 获取当前使用量(实时)
|
||||
const calculateCurrentUsage = () => {
|
||||
// 如果不是编辑模式或没有账户ID,返回0
|
||||
if (!isEdit.value || !props.account?.id) {
|
||||
return 0
|
||||
}
|
||||
|
||||
// 如果已经加载了今日使用数据,直接使用
|
||||
if (typeof form.value.dailyUsage === 'number') {
|
||||
return form.value.dailyUsage
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
// 计算额度使用百分比
|
||||
const usagePercentage = computed(() => {
|
||||
if (!form.value.dailyQuota || form.value.dailyQuota <= 0) {
|
||||
return 0
|
||||
}
|
||||
const currentUsage = calculateCurrentUsage()
|
||||
return (currentUsage / form.value.dailyQuota) * 100
|
||||
})
|
||||
|
||||
// 加载账户今日使用情况
|
||||
const loadAccountUsage = async () => {
|
||||
if (!isEdit.value || !props.account?.id) return
|
||||
|
||||
try {
|
||||
const response = await apiClient.get(`/admin/claude-console-accounts/${props.account.id}/usage`)
|
||||
if (response) {
|
||||
// 更新表单中的使用量数据
|
||||
form.value.dailyUsage = response.dailyUsage || 0
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('Failed to load account usage:', error)
|
||||
}
|
||||
}
|
||||
|
||||
// // 计算是否可以创建
|
||||
// const canCreate = computed(() => {
|
||||
// if (form.value.addType === 'manual') {
|
||||
@@ -2383,7 +2545,35 @@ const handleOAuthSuccess = async (tokenInfo) => {
|
||||
|
||||
emit('success', result)
|
||||
} catch (error) {
|
||||
showToast(error.message || '账户创建失败', 'error')
|
||||
// 显示详细的错误信息
|
||||
const errorMessage = error.response?.data?.error || error.message || '账户创建失败'
|
||||
const suggestion = error.response?.data?.suggestion || ''
|
||||
const errorDetails = error.response?.data?.errorDetails || null
|
||||
|
||||
// 构建完整的错误提示
|
||||
let fullMessage = errorMessage
|
||||
if (suggestion) {
|
||||
fullMessage += `\n${suggestion}`
|
||||
}
|
||||
|
||||
// 如果有详细的 OAuth 错误信息,也显示出来
|
||||
if (errorDetails && errorDetails.error_description) {
|
||||
fullMessage += `\n详细信息: ${errorDetails.error_description}`
|
||||
} else if (errorDetails && errorDetails.error && errorDetails.error.message) {
|
||||
// 处理 OpenAI 格式的错误
|
||||
fullMessage += `\n详细信息: ${errorDetails.error.message}`
|
||||
}
|
||||
|
||||
showToast(fullMessage, 'error', '', 8000)
|
||||
|
||||
// 在控制台打印完整的错误信息以便调试
|
||||
console.error('账户创建失败:', {
|
||||
message: errorMessage,
|
||||
suggestion,
|
||||
errorDetails,
|
||||
errorCode: error.response?.data?.errorCode,
|
||||
networkError: error.response?.data?.networkError
|
||||
})
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
@@ -2444,17 +2634,19 @@ const createAccount = async () => {
|
||||
}
|
||||
} else if (form.value.addType === 'manual') {
|
||||
// 手动模式验证
|
||||
if (!form.value.accessToken || form.value.accessToken.trim() === '') {
|
||||
errors.value.accessToken = '请填写 Access Token'
|
||||
hasError = true
|
||||
}
|
||||
// OpenAI 平台需要验证 ID Token
|
||||
if (
|
||||
form.value.platform === 'openai' &&
|
||||
(!form.value.idToken || form.value.idToken.trim() === '')
|
||||
) {
|
||||
errors.value.idToken = '请填写 ID Token'
|
||||
hasError = true
|
||||
if (form.value.platform === 'openai') {
|
||||
// OpenAI 平台必须有 Refresh Token
|
||||
if (!form.value.refreshToken || form.value.refreshToken.trim() === '') {
|
||||
errors.value.refreshToken = '请填写 Refresh Token'
|
||||
hasError = true
|
||||
}
|
||||
// Access Token 可选,如果没有会通过 Refresh Token 获取
|
||||
} else {
|
||||
// 其他平台(Gemini)需要 Access Token
|
||||
if (!form.value.accessToken || form.value.accessToken.trim() === '') {
|
||||
errors.value.accessToken = '请填写 Access Token'
|
||||
hasError = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2548,14 +2740,14 @@ const createAccount = async () => {
|
||||
: 365 * 24 * 60 * 60 * 1000 // 1年
|
||||
|
||||
data.openaiOauth = {
|
||||
idToken: form.value.idToken, // 使用用户输入的 ID Token
|
||||
accessToken: form.value.accessToken,
|
||||
refreshToken: form.value.refreshToken || '',
|
||||
idToken: '', // 不再需要用户输入,系统会自动获取
|
||||
accessToken: form.value.accessToken || '', // Access Token 可选
|
||||
refreshToken: form.value.refreshToken, // Refresh Token 必填
|
||||
expires_in: Math.floor(expiresInMs / 1000) // 转换为秒
|
||||
}
|
||||
|
||||
// 手动模式下,尝试从 ID Token 解析用户信息
|
||||
let accountInfo = {
|
||||
// 账户信息将在首次刷新时自动获取
|
||||
data.accountInfo = {
|
||||
accountId: '',
|
||||
chatgptUserId: '',
|
||||
organizationId: '',
|
||||
@@ -2566,31 +2758,9 @@ const createAccount = async () => {
|
||||
emailVerified: false
|
||||
}
|
||||
|
||||
// 尝试解析 ID Token (JWT)
|
||||
if (form.value.idToken) {
|
||||
try {
|
||||
const idTokenParts = form.value.idToken.split('.')
|
||||
if (idTokenParts.length === 3) {
|
||||
const payload = JSON.parse(atob(idTokenParts[1]))
|
||||
const authClaims = payload['https://api.openai.com/auth'] || {}
|
||||
|
||||
accountInfo = {
|
||||
accountId: authClaims.accountId || '',
|
||||
chatgptUserId: authClaims.chatgptUserId || '',
|
||||
organizationId: authClaims.organizationId || '',
|
||||
organizationRole: authClaims.organizationRole || '',
|
||||
organizationTitle: authClaims.organizationTitle || '',
|
||||
planType: authClaims.planType || '',
|
||||
email: payload.email || '',
|
||||
emailVerified: payload.email_verified || false
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('Failed to parse ID Token:', e)
|
||||
}
|
||||
}
|
||||
|
||||
data.accountInfo = accountInfo
|
||||
// OpenAI 手动模式必须刷新以获取完整信息(包括 ID Token)
|
||||
data.needsImmediateRefresh = true
|
||||
data.requireRefreshSuccess = true // 必须刷新成功才能创建账户
|
||||
data.priority = form.value.priority || 50
|
||||
} else if (form.value.platform === 'claude-console') {
|
||||
// Claude Console 账户特定数据
|
||||
@@ -2601,6 +2771,9 @@ const createAccount = async () => {
|
||||
data.userAgent = form.value.userAgent || null
|
||||
// 如果不启用限流,传递 0 表示不限流
|
||||
data.rateLimitDuration = form.value.enableRateLimit ? form.value.rateLimitDuration || 60 : 0
|
||||
// 额度管理字段
|
||||
data.dailyQuota = form.value.dailyQuota || 0
|
||||
data.quotaResetTime = form.value.quotaResetTime || '00:00'
|
||||
} else if (form.value.platform === 'bedrock') {
|
||||
// Bedrock 账户特定数据 - 构造 awsCredentials 对象
|
||||
data.awsCredentials = {
|
||||
@@ -2647,7 +2820,35 @@ const createAccount = async () => {
|
||||
|
||||
emit('success', result)
|
||||
} catch (error) {
|
||||
showToast(error.message || '账户创建失败', 'error')
|
||||
// 显示详细的错误信息
|
||||
const errorMessage = error.response?.data?.error || error.message || '账户创建失败'
|
||||
const suggestion = error.response?.data?.suggestion || ''
|
||||
const errorDetails = error.response?.data?.errorDetails || null
|
||||
|
||||
// 构建完整的错误提示
|
||||
let fullMessage = errorMessage
|
||||
if (suggestion) {
|
||||
fullMessage += `\n${suggestion}`
|
||||
}
|
||||
|
||||
// 如果有详细的 OAuth 错误信息,也显示出来
|
||||
if (errorDetails && errorDetails.error_description) {
|
||||
fullMessage += `\n详细信息: ${errorDetails.error_description}`
|
||||
} else if (errorDetails && errorDetails.error && errorDetails.error.message) {
|
||||
// 处理 OpenAI 格式的错误
|
||||
fullMessage += `\n详细信息: ${errorDetails.error.message}`
|
||||
}
|
||||
|
||||
showToast(fullMessage, 'error', '', 8000)
|
||||
|
||||
// 在控制台打印完整的错误信息以便调试
|
||||
console.error('账户创建失败:', {
|
||||
message: errorMessage,
|
||||
suggestion,
|
||||
errorDetails,
|
||||
errorCode: error.response?.data?.errorCode,
|
||||
networkError: error.response?.data?.networkError
|
||||
})
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
@@ -2751,11 +2952,17 @@ const updateAccount = async () => {
|
||||
: 365 * 24 * 60 * 60 * 1000 // 1年
|
||||
|
||||
data.openaiOauth = {
|
||||
idToken: form.value.idToken || '', // 更新时使用用户输入的 ID Token
|
||||
idToken: '', // 不需要用户输入
|
||||
accessToken: form.value.accessToken || '',
|
||||
refreshToken: form.value.refreshToken || '',
|
||||
expires_in: Math.floor(expiresInMs / 1000) // 转换为秒
|
||||
}
|
||||
|
||||
// 编辑 OpenAI 账户时,如果更新了 Refresh Token,也需要验证
|
||||
if (form.value.refreshToken && form.value.refreshToken !== props.account.refreshToken) {
|
||||
data.needsImmediateRefresh = true
|
||||
data.requireRefreshSuccess = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2798,6 +3005,9 @@ const updateAccount = async () => {
|
||||
data.userAgent = form.value.userAgent || null
|
||||
// 如果不启用限流,传递 0 表示不限流
|
||||
data.rateLimitDuration = form.value.enableRateLimit ? form.value.rateLimitDuration || 60 : 0
|
||||
// 额度管理字段
|
||||
data.dailyQuota = form.value.dailyQuota || 0
|
||||
data.quotaResetTime = form.value.quotaResetTime || '00:00'
|
||||
}
|
||||
|
||||
// Bedrock 特定更新
|
||||
@@ -2859,7 +3069,35 @@ const updateAccount = async () => {
|
||||
|
||||
emit('success')
|
||||
} catch (error) {
|
||||
showToast(error.message || '账户更新失败', 'error')
|
||||
// 显示详细的错误信息
|
||||
const errorMessage = error.response?.data?.error || error.message || '账户更新失败'
|
||||
const suggestion = error.response?.data?.suggestion || ''
|
||||
const errorDetails = error.response?.data?.errorDetails || null
|
||||
|
||||
// 构建完整的错误提示
|
||||
let fullMessage = errorMessage
|
||||
if (suggestion) {
|
||||
fullMessage += `\n${suggestion}`
|
||||
}
|
||||
|
||||
// 如果有详细的 OAuth 错误信息,也显示出来
|
||||
if (errorDetails && errorDetails.error_description) {
|
||||
fullMessage += `\n详细信息: ${errorDetails.error_description}`
|
||||
} else if (errorDetails && errorDetails.error && errorDetails.error.message) {
|
||||
// 处理 OpenAI 格式的错误
|
||||
fullMessage += `\n详细信息: ${errorDetails.error.message}`
|
||||
}
|
||||
|
||||
showToast(fullMessage, 'error', '', 8000)
|
||||
|
||||
// 在控制台打印完整的错误信息以便调试
|
||||
console.error('账户更新失败:', {
|
||||
message: errorMessage,
|
||||
suggestion,
|
||||
errorDetails,
|
||||
errorCode: error.response?.data?.errorCode,
|
||||
networkError: error.response?.data?.networkError
|
||||
})
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
@@ -3207,7 +3445,16 @@ watch(
|
||||
// Azure OpenAI 特定字段
|
||||
azureEndpoint: newAccount.azureEndpoint || '',
|
||||
apiVersion: newAccount.apiVersion || '',
|
||||
deploymentName: newAccount.deploymentName || ''
|
||||
deploymentName: newAccount.deploymentName || '',
|
||||
// 额度管理字段
|
||||
dailyQuota: newAccount.dailyQuota || 0,
|
||||
dailyUsage: newAccount.dailyUsage || 0,
|
||||
quotaResetTime: newAccount.quotaResetTime || '00:00'
|
||||
}
|
||||
|
||||
// 如果是Claude Console账户,加载实时使用情况
|
||||
if (newAccount.platform === 'claude-console') {
|
||||
loadAccountUsage()
|
||||
}
|
||||
|
||||
// 如果是分组类型,加载分组ID
|
||||
@@ -3287,6 +3534,10 @@ const clearUnifiedCache = async () => {
|
||||
onMounted(() => {
|
||||
// 获取Claude Code统一User-Agent信息
|
||||
fetchUnifiedUserAgent()
|
||||
// 如果是编辑模式且是Claude Console账户,加载使用情况
|
||||
if (isEdit.value && props.account?.platform === 'claude-console') {
|
||||
loadAccountUsage()
|
||||
}
|
||||
})
|
||||
|
||||
// 监听平台变化,当切换到Claude平台时获取统一User-Agent信息
|
||||
|
||||
@@ -36,7 +36,7 @@
|
||||
>
|
||||
<select
|
||||
v-model="proxy.type"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200"
|
||||
>
|
||||
<option value="socks5">SOCKS5</option>
|
||||
<option value="http">HTTP</option>
|
||||
@@ -51,7 +51,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="proxy.host"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
placeholder="例如: 192.168.1.100"
|
||||
type="text"
|
||||
/>
|
||||
@@ -62,7 +62,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="proxy.port"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
placeholder="例如: 1080"
|
||||
type="number"
|
||||
/>
|
||||
@@ -92,7 +92,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="proxy.username"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
placeholder="代理用户名"
|
||||
type="text"
|
||||
/>
|
||||
@@ -104,7 +104,7 @@
|
||||
<div class="relative">
|
||||
<input
|
||||
v-model="proxy.password"
|
||||
class="form-input w-full pr-10 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 pr-10 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
placeholder="代理密码"
|
||||
:type="showPassword ? 'text' : 'password'"
|
||||
/>
|
||||
|
||||
@@ -127,7 +127,7 @@
|
||||
<div class="flex gap-2">
|
||||
<input
|
||||
v-model="newTag"
|
||||
class="form-input flex-1 dark:border-gray-600 dark:bg-gray-800 dark:text-gray-200"
|
||||
class="form-input flex-1 border-gray-300 dark:border-gray-600 dark:bg-gray-800 dark:text-gray-200"
|
||||
placeholder="输入新标签名称"
|
||||
type="text"
|
||||
@keypress.enter.prevent="addTag"
|
||||
@@ -166,7 +166,7 @@
|
||||
</label>
|
||||
<input
|
||||
v-model="form.rateLimitWindow"
|
||||
class="form-input w-full text-sm dark:border-gray-600 dark:bg-gray-800 dark:text-gray-200"
|
||||
class="form-input w-full border-gray-300 text-sm dark:border-gray-600 dark:bg-gray-800 dark:text-gray-200"
|
||||
min="1"
|
||||
placeholder="不修改"
|
||||
type="number"
|
||||
@@ -179,7 +179,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="form.rateLimitRequests"
|
||||
class="form-input w-full text-sm dark:border-gray-600 dark:bg-gray-800 dark:text-gray-200"
|
||||
class="form-input w-full border-gray-300 text-sm dark:border-gray-600 dark:bg-gray-800 dark:text-gray-200"
|
||||
min="1"
|
||||
placeholder="不修改"
|
||||
type="number"
|
||||
@@ -188,12 +188,14 @@
|
||||
|
||||
<div>
|
||||
<label class="mb-1 block text-xs font-medium text-gray-700 dark:text-gray-300"
|
||||
>Token 限制</label
|
||||
>费用限制 (美元)</label
|
||||
>
|
||||
<input
|
||||
v-model="form.tokenLimit"
|
||||
class="form-input w-full text-sm dark:border-gray-600 dark:bg-gray-800 dark:text-gray-200"
|
||||
v-model="form.rateLimitCost"
|
||||
class="form-input w-full border-gray-300 text-sm dark:border-gray-600 dark:bg-gray-800 dark:text-gray-200"
|
||||
min="0"
|
||||
placeholder="不修改"
|
||||
step="0.01"
|
||||
type="number"
|
||||
/>
|
||||
</div>
|
||||
@@ -208,7 +210,7 @@
|
||||
</label>
|
||||
<input
|
||||
v-model="form.dailyCostLimit"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-800 dark:text-gray-200"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-800 dark:text-gray-200"
|
||||
min="0"
|
||||
placeholder="不修改 (0 表示无限制)"
|
||||
step="0.01"
|
||||
@@ -216,6 +218,24 @@
|
||||
/>
|
||||
</div>
|
||||
|
||||
<!-- Opus 模型周费用限制 -->
|
||||
<div>
|
||||
<label class="mb-3 block text-sm font-semibold text-gray-700 dark:text-gray-300">
|
||||
Opus 模型周费用限制 (美元)
|
||||
</label>
|
||||
<input
|
||||
v-model="form.weeklyOpusCostLimit"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-800 dark:text-gray-200"
|
||||
min="0"
|
||||
placeholder="不修改 (0 表示无限制)"
|
||||
step="0.01"
|
||||
type="number"
|
||||
/>
|
||||
<p class="mt-1 text-xs text-gray-500 dark:text-gray-400">
|
||||
设置 Opus 模型的周费用限制(周一到周日),仅限 Claude 官方账户
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<!-- 并发限制 -->
|
||||
<div>
|
||||
<label class="mb-3 block text-sm font-semibold text-gray-700 dark:text-gray-300"
|
||||
@@ -223,7 +243,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="form.concurrencyLimit"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-800 dark:text-gray-200"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-800 dark:text-gray-200"
|
||||
min="0"
|
||||
placeholder="不修改 (0 表示无限制)"
|
||||
type="number"
|
||||
@@ -310,7 +330,7 @@
|
||||
>
|
||||
<select
|
||||
v-model="form.claudeAccountId"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-800 dark:text-gray-200"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-800 dark:text-gray-200"
|
||||
:disabled="form.permissions === 'gemini' || form.permissions === 'openai'"
|
||||
>
|
||||
<option value="">不修改</option>
|
||||
@@ -345,7 +365,7 @@
|
||||
>
|
||||
<select
|
||||
v-model="form.geminiAccountId"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-800 dark:text-gray-200"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-800 dark:text-gray-200"
|
||||
:disabled="form.permissions === 'claude' || form.permissions === 'openai'"
|
||||
>
|
||||
<option value="">不修改</option>
|
||||
@@ -376,7 +396,7 @@
|
||||
>
|
||||
<select
|
||||
v-model="form.openaiAccountId"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-800 dark:text-gray-200"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-800 dark:text-gray-200"
|
||||
:disabled="form.permissions === 'claude' || form.permissions === 'gemini'"
|
||||
>
|
||||
<option value="">不修改</option>
|
||||
@@ -407,7 +427,7 @@
|
||||
>
|
||||
<select
|
||||
v-model="form.bedrockAccountId"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-800 dark:text-gray-200"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-800 dark:text-gray-200"
|
||||
:disabled="form.permissions === 'gemini' || form.permissions === 'openai'"
|
||||
>
|
||||
<option value="">不修改</option>
|
||||
@@ -496,11 +516,12 @@ const unselectedTags = computed(() => {
|
||||
|
||||
// 表单数据
|
||||
const form = reactive({
|
||||
tokenLimit: '',
|
||||
rateLimitCost: '', // 费用限制替代token限制
|
||||
rateLimitWindow: '',
|
||||
rateLimitRequests: '',
|
||||
concurrencyLimit: '',
|
||||
dailyCostLimit: '',
|
||||
weeklyOpusCostLimit: '', // 新增Opus周费用限制
|
||||
permissions: '', // 空字符串表示不修改
|
||||
claudeAccountId: '',
|
||||
geminiAccountId: '',
|
||||
@@ -616,8 +637,8 @@ const batchUpdateApiKeys = async () => {
|
||||
const updates = {}
|
||||
|
||||
// 只有非空值才添加到更新对象中
|
||||
if (form.tokenLimit !== '' && form.tokenLimit !== null) {
|
||||
updates.tokenLimit = parseInt(form.tokenLimit)
|
||||
if (form.rateLimitCost !== '' && form.rateLimitCost !== null) {
|
||||
updates.rateLimitCost = parseFloat(form.rateLimitCost)
|
||||
}
|
||||
if (form.rateLimitWindow !== '' && form.rateLimitWindow !== null) {
|
||||
updates.rateLimitWindow = parseInt(form.rateLimitWindow)
|
||||
@@ -631,6 +652,9 @@ const batchUpdateApiKeys = async () => {
|
||||
if (form.dailyCostLimit !== '' && form.dailyCostLimit !== null) {
|
||||
updates.dailyCostLimit = parseFloat(form.dailyCostLimit)
|
||||
}
|
||||
if (form.weeklyOpusCostLimit !== '' && form.weeklyOpusCostLimit !== null) {
|
||||
updates.weeklyOpusCostLimit = parseFloat(form.weeklyOpusCostLimit)
|
||||
}
|
||||
|
||||
// 权限设置
|
||||
if (form.permissions !== '') {
|
||||
|
||||
@@ -81,7 +81,7 @@
|
||||
<div class="flex items-center gap-2">
|
||||
<input
|
||||
v-model.number="form.batchCount"
|
||||
class="form-input w-full text-sm dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 text-sm dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
max="500"
|
||||
min="2"
|
||||
placeholder="输入数量 (2-500)"
|
||||
@@ -112,7 +112,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="form.name"
|
||||
class="form-input w-full text-sm dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 text-sm dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
:class="{ 'border-red-500': errors.name }"
|
||||
:placeholder="
|
||||
form.createType === 'batch'
|
||||
@@ -184,7 +184,7 @@
|
||||
<div class="flex gap-2">
|
||||
<input
|
||||
v-model="newTag"
|
||||
class="form-input flex-1 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input flex-1 border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
placeholder="输入新标签名称"
|
||||
type="text"
|
||||
@keypress.enter.prevent="addTag"
|
||||
@@ -228,7 +228,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="form.rateLimitWindow"
|
||||
class="form-input w-full text-sm dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 text-sm dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
min="1"
|
||||
placeholder="无限制"
|
||||
type="number"
|
||||
@@ -242,7 +242,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="form.rateLimitRequests"
|
||||
class="form-input w-full text-sm dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 text-sm dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
min="1"
|
||||
placeholder="无限制"
|
||||
type="number"
|
||||
@@ -256,7 +256,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="form.rateLimitCost"
|
||||
class="form-input w-full text-sm dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 text-sm dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
min="0"
|
||||
placeholder="无限制"
|
||||
step="0.01"
|
||||
@@ -321,7 +321,7 @@
|
||||
</div>
|
||||
<input
|
||||
v-model="form.dailyCostLimit"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
min="0"
|
||||
placeholder="0 表示无限制"
|
||||
step="0.01"
|
||||
@@ -370,7 +370,7 @@
|
||||
</div>
|
||||
<input
|
||||
v-model="form.weeklyOpusCostLimit"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
min="0"
|
||||
placeholder="0 表示无限制"
|
||||
step="0.01"
|
||||
@@ -388,7 +388,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="form.concurrencyLimit"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
min="0"
|
||||
placeholder="0 表示无限制"
|
||||
type="number"
|
||||
@@ -404,7 +404,7 @@
|
||||
>
|
||||
<textarea
|
||||
v-model="form.description"
|
||||
class="form-input w-full resize-none text-sm dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full resize-none border-gray-300 text-sm dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
placeholder="描述此 API Key 的用途..."
|
||||
rows="2"
|
||||
/>
|
||||
@@ -412,34 +412,103 @@
|
||||
|
||||
<div>
|
||||
<label class="mb-2 block text-sm font-semibold text-gray-700 dark:text-gray-300"
|
||||
>有效期限</label
|
||||
>过期设置</label
|
||||
>
|
||||
<select
|
||||
v-model="form.expireDuration"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200"
|
||||
@change="updateExpireAt"
|
||||
<!-- 过期模式选择 -->
|
||||
<div
|
||||
class="mb-3 rounded-lg border border-gray-200 bg-gray-50 p-3 dark:border-gray-700 dark:bg-gray-800"
|
||||
>
|
||||
<option value="">永不过期</option>
|
||||
<option value="1d">1 天</option>
|
||||
<option value="7d">7 天</option>
|
||||
<option value="30d">30 天</option>
|
||||
<option value="90d">90 天</option>
|
||||
<option value="180d">180 天</option>
|
||||
<option value="365d">365 天</option>
|
||||
<option value="custom">自定义日期</option>
|
||||
</select>
|
||||
<div v-if="form.expireDuration === 'custom'" class="mt-3">
|
||||
<input
|
||||
v-model="form.customExpireDate"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200"
|
||||
:min="minDateTime"
|
||||
type="datetime-local"
|
||||
@change="updateCustomExpireAt"
|
||||
/>
|
||||
<div class="flex items-center gap-4">
|
||||
<label class="flex cursor-pointer items-center">
|
||||
<input
|
||||
v-model="form.expirationMode"
|
||||
class="mr-2 text-blue-600 focus:ring-blue-500 dark:border-gray-600 dark:bg-gray-700"
|
||||
type="radio"
|
||||
value="fixed"
|
||||
/>
|
||||
<span class="text-sm text-gray-700 dark:text-gray-300">固定时间过期</span>
|
||||
</label>
|
||||
<label class="flex cursor-pointer items-center">
|
||||
<input
|
||||
v-model="form.expirationMode"
|
||||
class="mr-2 text-blue-600 focus:ring-blue-500 dark:border-gray-600 dark:bg-gray-700"
|
||||
type="radio"
|
||||
value="activation"
|
||||
/>
|
||||
<span class="text-sm text-gray-700 dark:text-gray-300">首次使用后激活</span>
|
||||
</label>
|
||||
</div>
|
||||
<p class="mt-2 text-xs text-gray-500 dark:text-gray-400">
|
||||
<span v-if="form.expirationMode === 'fixed'">
|
||||
<i class="fas fa-info-circle mr-1" />
|
||||
固定时间模式:Key 创建后立即生效,按设定时间过期
|
||||
</span>
|
||||
<span v-else>
|
||||
<i class="fas fa-info-circle mr-1" />
|
||||
激活模式:Key 首次使用时激活,激活后按设定天数过期(适合批量销售)
|
||||
</span>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<!-- 固定时间模式 -->
|
||||
<div v-if="form.expirationMode === 'fixed'">
|
||||
<select
|
||||
v-model="form.expireDuration"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200"
|
||||
@change="updateExpireAt"
|
||||
>
|
||||
<option value="">永不过期</option>
|
||||
<option value="1d">1 天</option>
|
||||
<option value="7d">7 天</option>
|
||||
<option value="30d">30 天</option>
|
||||
<option value="90d">90 天</option>
|
||||
<option value="180d">180 天</option>
|
||||
<option value="365d">365 天</option>
|
||||
<option value="custom">自定义日期</option>
|
||||
</select>
|
||||
<div v-if="form.expireDuration === 'custom'" class="mt-3">
|
||||
<input
|
||||
v-model="form.customExpireDate"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200"
|
||||
:min="minDateTime"
|
||||
type="datetime-local"
|
||||
@change="updateCustomExpireAt"
|
||||
/>
|
||||
</div>
|
||||
<p v-if="form.expiresAt" class="mt-2 text-xs text-gray-500 dark:text-gray-400">
|
||||
将于 {{ formatExpireDate(form.expiresAt) }} 过期
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<!-- 激活模式 -->
|
||||
<div v-else>
|
||||
<div class="flex items-center gap-2">
|
||||
<input
|
||||
v-model.number="form.activationDays"
|
||||
class="form-input flex-1 border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200"
|
||||
max="3650"
|
||||
min="1"
|
||||
placeholder="输入天数"
|
||||
type="number"
|
||||
/>
|
||||
<span class="text-sm text-gray-600 dark:text-gray-400">天</span>
|
||||
</div>
|
||||
<div class="mt-2 flex flex-wrap gap-2">
|
||||
<button
|
||||
v-for="days in [30, 90, 180, 365]"
|
||||
:key="days"
|
||||
class="rounded-md border border-gray-300 px-3 py-1 text-xs hover:bg-gray-100 dark:border-gray-600 dark:hover:bg-gray-700"
|
||||
type="button"
|
||||
@click="form.activationDays = days"
|
||||
>
|
||||
{{ days }}天
|
||||
</button>
|
||||
</div>
|
||||
<p class="mt-2 text-xs text-gray-500 dark:text-gray-400">
|
||||
<i class="fas fa-clock mr-1" />
|
||||
Key 将在首次使用后激活,激活后 {{ form.activationDays || 30 }} 天过期
|
||||
</p>
|
||||
</div>
|
||||
<p v-if="form.expiresAt" class="mt-2 text-xs text-gray-500 dark:text-gray-400">
|
||||
将于 {{ formatExpireDate(form.expiresAt) }} 过期
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
@@ -794,6 +863,8 @@ const form = reactive({
|
||||
expireDuration: '',
|
||||
customExpireDate: '',
|
||||
expiresAt: null,
|
||||
expirationMode: 'fixed', // 过期模式:fixed(固定) 或 activation(激活)
|
||||
activationDays: 30, // 激活后有效天数
|
||||
permissions: 'all',
|
||||
claudeAccountId: '',
|
||||
geminiAccountId: '',
|
||||
@@ -1082,7 +1153,9 @@ const createApiKey = async () => {
|
||||
form.weeklyOpusCostLimit !== '' && form.weeklyOpusCostLimit !== null
|
||||
? parseFloat(form.weeklyOpusCostLimit)
|
||||
: 0,
|
||||
expiresAt: form.expiresAt || undefined,
|
||||
expiresAt: form.expirationMode === 'fixed' ? form.expiresAt || undefined : undefined,
|
||||
expirationMode: form.expirationMode,
|
||||
activationDays: form.expirationMode === 'activation' ? form.activationDays : undefined,
|
||||
permissions: form.permissions,
|
||||
tags: form.tags.length > 0 ? form.tags : undefined,
|
||||
enableModelRestriction: form.enableModelRestriction,
|
||||
|
||||
@@ -33,12 +33,36 @@
|
||||
>名称</label
|
||||
>
|
||||
<input
|
||||
class="form-input w-full cursor-not-allowed bg-gray-100 text-sm dark:border-gray-600 dark:bg-gray-800 dark:text-gray-400"
|
||||
disabled
|
||||
v-model="form.name"
|
||||
class="form-input w-full border-gray-300 text-sm dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
maxlength="100"
|
||||
placeholder="请输入API Key名称"
|
||||
required
|
||||
type="text"
|
||||
:value="form.name"
|
||||
/>
|
||||
<p class="mt-1 text-xs text-gray-500 dark:text-gray-400 sm:mt-2">名称不可修改</p>
|
||||
<p class="mt-1 text-xs text-gray-500 dark:text-gray-400 sm:mt-2">
|
||||
用于识别此 API Key 的用途
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<!-- 所有者选择 -->
|
||||
<div>
|
||||
<label
|
||||
class="mb-1.5 block text-xs font-semibold text-gray-700 dark:text-gray-300 sm:mb-3 sm:text-sm"
|
||||
>所有者</label
|
||||
>
|
||||
<select
|
||||
v-model="form.ownerId"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200"
|
||||
>
|
||||
<option v-for="user in availableUsers" :key="user.id" :value="user.id">
|
||||
{{ user.displayName }} ({{ user.username }})
|
||||
<span v-if="user.role === 'admin'" class="text-gray-500">- 管理员</span>
|
||||
</option>
|
||||
</select>
|
||||
<p class="mt-1 text-xs text-gray-500 dark:text-gray-400 sm:mt-2">
|
||||
分配此 API Key 给指定用户或管理员,管理员分配时不受用户 API Key 数量限制
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<!-- 标签 -->
|
||||
@@ -98,7 +122,7 @@
|
||||
<div class="flex gap-2">
|
||||
<input
|
||||
v-model="newTag"
|
||||
class="form-input flex-1 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input flex-1 border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
placeholder="输入新标签名称"
|
||||
type="text"
|
||||
@keypress.enter.prevent="addTag"
|
||||
@@ -142,7 +166,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="form.rateLimitWindow"
|
||||
class="form-input w-full text-sm dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 text-sm dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
min="1"
|
||||
placeholder="无限制"
|
||||
type="number"
|
||||
@@ -156,7 +180,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="form.rateLimitRequests"
|
||||
class="form-input w-full text-sm dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 text-sm dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
min="1"
|
||||
placeholder="无限制"
|
||||
type="number"
|
||||
@@ -170,7 +194,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="form.rateLimitCost"
|
||||
class="form-input w-full text-sm dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 text-sm dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
min="0"
|
||||
placeholder="无限制"
|
||||
step="0.01"
|
||||
@@ -235,7 +259,7 @@
|
||||
</div>
|
||||
<input
|
||||
v-model="form.dailyCostLimit"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
min="0"
|
||||
placeholder="0 表示无限制"
|
||||
step="0.01"
|
||||
@@ -284,7 +308,7 @@
|
||||
</div>
|
||||
<input
|
||||
v-model="form.weeklyOpusCostLimit"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
min="0"
|
||||
placeholder="0 表示无限制"
|
||||
step="0.01"
|
||||
@@ -302,7 +326,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="form.concurrencyLimit"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
min="0"
|
||||
placeholder="0 表示无限制"
|
||||
type="number"
|
||||
@@ -534,7 +558,7 @@
|
||||
<div class="flex gap-2">
|
||||
<input
|
||||
v-model="form.modelInput"
|
||||
class="form-input flex-1 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input flex-1 border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
placeholder="输入模型名称,按回车添加"
|
||||
type="text"
|
||||
@keydown.enter.prevent="addRestrictedModel"
|
||||
@@ -666,6 +690,9 @@ const localAccounts = ref({
|
||||
// 支持的客户端列表
|
||||
const supportedClients = ref([])
|
||||
|
||||
// 可用用户列表
|
||||
const availableUsers = ref([])
|
||||
|
||||
// 标签相关
|
||||
const newTag = ref('')
|
||||
const availableTags = ref([])
|
||||
@@ -696,7 +723,8 @@ const form = reactive({
|
||||
enableClientRestriction: false,
|
||||
allowedClients: [],
|
||||
tags: [],
|
||||
isActive: true
|
||||
isActive: true,
|
||||
ownerId: '' // 新增:所有者ID
|
||||
})
|
||||
|
||||
// 添加限制的模型
|
||||
@@ -774,6 +802,7 @@ const updateApiKey = async () => {
|
||||
try {
|
||||
// 准备提交的数据
|
||||
const data = {
|
||||
name: form.name, // 添加名称字段
|
||||
tokenLimit: 0, // 清除历史token限制
|
||||
rateLimitWindow:
|
||||
form.rateLimitWindow !== '' && form.rateLimitWindow !== null
|
||||
@@ -856,6 +885,11 @@ const updateApiKey = async () => {
|
||||
// 活跃状态
|
||||
data.isActive = form.isActive
|
||||
|
||||
// 所有者
|
||||
if (form.ownerId !== undefined) {
|
||||
data.ownerId = form.ownerId
|
||||
}
|
||||
|
||||
const result = await apiClient.put(`/admin/api-keys/${props.apiKey.id}`, data)
|
||||
|
||||
if (result.success) {
|
||||
@@ -947,11 +981,45 @@ const refreshAccounts = async () => {
|
||||
}
|
||||
}
|
||||
|
||||
// 加载用户列表
|
||||
const loadUsers = async () => {
|
||||
try {
|
||||
const response = await apiClient.get('/admin/users')
|
||||
if (response.success) {
|
||||
availableUsers.value = response.data || []
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to load users:', error)
|
||||
availableUsers.value = [
|
||||
{
|
||||
id: 'admin',
|
||||
username: 'admin',
|
||||
displayName: 'Admin',
|
||||
email: '',
|
||||
role: 'admin'
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化表单数据
|
||||
onMounted(async () => {
|
||||
// 加载支持的客户端和已存在的标签
|
||||
supportedClients.value = await clientsStore.loadSupportedClients()
|
||||
availableTags.value = await apiKeysStore.fetchTags()
|
||||
try {
|
||||
// 并行加载所有需要的数据
|
||||
const [clients, tags] = await Promise.all([
|
||||
clientsStore.loadSupportedClients(),
|
||||
apiKeysStore.fetchTags(),
|
||||
loadUsers()
|
||||
])
|
||||
|
||||
supportedClients.value = clients || []
|
||||
availableTags.value = tags || []
|
||||
} catch (error) {
|
||||
console.error('Error loading initial data:', error)
|
||||
// Fallback to empty arrays if loading fails
|
||||
supportedClients.value = []
|
||||
availableTags.value = []
|
||||
}
|
||||
|
||||
// 初始化账号数据
|
||||
if (props.accounts) {
|
||||
@@ -1001,6 +1069,9 @@ onMounted(async () => {
|
||||
form.enableClientRestriction = props.apiKey.enableClientRestriction || false
|
||||
// 初始化活跃状态,默认为 true
|
||||
form.isActive = props.apiKey.isActive !== undefined ? props.apiKey.isActive : true
|
||||
|
||||
// 初始化所有者
|
||||
form.ownerId = props.apiKey.userId || 'admin'
|
||||
})
|
||||
</script>
|
||||
|
||||
|
||||
@@ -39,11 +39,18 @@
|
||||
>
|
||||
<div class="flex items-center justify-between">
|
||||
<div>
|
||||
<p class="mb-1 text-xs font-medium text-gray-600 dark:text-gray-400">
|
||||
当前过期时间
|
||||
</p>
|
||||
<p class="mb-1 text-xs font-medium text-gray-600 dark:text-gray-400">当前状态</p>
|
||||
<p class="text-sm font-semibold text-gray-800 dark:text-gray-200">
|
||||
<template v-if="apiKey.expiresAt">
|
||||
<!-- 未激活状态 -->
|
||||
<template v-if="apiKey.expirationMode === 'activation' && !apiKey.isActivated">
|
||||
<i class="fas fa-pause-circle mr-1 text-blue-500" />
|
||||
未激活
|
||||
<span class="ml-2 text-xs font-normal text-gray-600">
|
||||
(激活后 {{ apiKey.activationDays || 30 }} 天过期)
|
||||
</span>
|
||||
</template>
|
||||
<!-- 已设置过期时间 -->
|
||||
<template v-else-if="apiKey.expiresAt">
|
||||
{{ formatExpireDate(apiKey.expiresAt) }}
|
||||
<span
|
||||
v-if="getExpiryStatus(apiKey.expiresAt)"
|
||||
@@ -53,6 +60,7 @@
|
||||
({{ getExpiryStatus(apiKey.expiresAt).text }})
|
||||
</span>
|
||||
</template>
|
||||
<!-- 永不过期 -->
|
||||
<template v-else>
|
||||
<i class="fas fa-infinity mr-1 text-gray-500" />
|
||||
永不过期
|
||||
@@ -74,6 +82,21 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 激活按钮(仅在未激活状态显示) -->
|
||||
<div v-if="apiKey.expirationMode === 'activation' && !apiKey.isActivated" class="mb-4">
|
||||
<button
|
||||
class="w-full rounded-lg bg-gradient-to-r from-blue-500 to-blue-600 px-4 py-3 font-semibold text-white transition-all hover:from-blue-600 hover:to-blue-700 hover:shadow-lg"
|
||||
@click="handleActivateNow"
|
||||
>
|
||||
<i class="fas fa-rocket mr-2" />
|
||||
立即激活 (激活后 {{ apiKey.activationDays || 30 }} 天过期)
|
||||
</button>
|
||||
<p class="mt-2 text-xs text-gray-500 dark:text-gray-400">
|
||||
<i class="fas fa-info-circle mr-1" />
|
||||
点击立即激活此 API Key,激活后将在 {{ apiKey.activationDays || 30 }} 天后过期
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<!-- 快捷选项 -->
|
||||
<div>
|
||||
<label class="mb-3 block text-sm font-semibold text-gray-700 dark:text-gray-300"
|
||||
@@ -115,7 +138,7 @@
|
||||
>
|
||||
<input
|
||||
v-model="localForm.customExpireDate"
|
||||
class="form-input w-full dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200"
|
||||
class="form-input w-full border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200"
|
||||
:min="minDateTime"
|
||||
type="datetime-local"
|
||||
@change="updateCustomExpiryPreview"
|
||||
@@ -370,6 +393,35 @@ const handleSave = () => {
|
||||
})
|
||||
}
|
||||
|
||||
// 立即激活
|
||||
const handleActivateNow = async () => {
|
||||
// 使用确认弹窗
|
||||
let confirmed = true
|
||||
if (window.showConfirm) {
|
||||
confirmed = await window.showConfirm(
|
||||
'激活 API Key',
|
||||
`确定要立即激活此 API Key 吗?激活后将在 ${props.apiKey.activationDays || 30} 天后自动过期。`,
|
||||
'确定激活',
|
||||
'取消'
|
||||
)
|
||||
} else {
|
||||
// 降级方案
|
||||
confirmed = confirm(
|
||||
`确定要立即激活此 API Key 吗?激活后将在 ${props.apiKey.activationDays || 30} 天后自动过期。`
|
||||
)
|
||||
}
|
||||
|
||||
if (!confirmed) {
|
||||
return
|
||||
}
|
||||
|
||||
saving.value = true
|
||||
emit('save', {
|
||||
keyId: props.apiKey.id,
|
||||
activateNow: true
|
||||
})
|
||||
}
|
||||
|
||||
// 重置保存状态
|
||||
const resetSaving = () => {
|
||||
saving.value = false
|
||||
|
||||
202
web/admin-spa/src/components/apistats/AggregatedStatsCard.vue
Normal file
202
web/admin-spa/src/components/apistats/AggregatedStatsCard.vue
Normal file
@@ -0,0 +1,202 @@
|
||||
<template>
|
||||
<div class="card h-full p-4 md:p-6">
|
||||
<h3
|
||||
class="mb-3 flex flex-col text-lg font-bold text-gray-900 dark:text-gray-100 sm:flex-row sm:items-center md:mb-4 md:text-xl"
|
||||
>
|
||||
<span class="flex items-center">
|
||||
<i class="fas fa-chart-pie mr-2 text-sm text-orange-500 md:mr-3 md:text-base" />
|
||||
使用占比
|
||||
</span>
|
||||
<span class="text-xs font-normal text-gray-600 dark:text-gray-400 sm:ml-2 md:text-sm"
|
||||
>({{ statsPeriod === 'daily' ? '今日' : '本月' }})</span
|
||||
>
|
||||
</h3>
|
||||
|
||||
<div v-if="aggregatedStats && individualStats.length > 0" class="space-y-2 md:space-y-3">
|
||||
<!-- 各Key使用占比列表 -->
|
||||
<div v-for="(stat, index) in topKeys" :key="stat.apiId" class="relative">
|
||||
<div class="mb-1 flex items-center justify-between text-sm">
|
||||
<span class="truncate font-medium text-gray-700 dark:text-gray-300">
|
||||
{{ stat.name || `Key ${index + 1}` }}
|
||||
</span>
|
||||
<span class="text-xs text-gray-600 dark:text-gray-400">
|
||||
{{ calculatePercentage(stat) }}%
|
||||
</span>
|
||||
</div>
|
||||
<div class="h-2 w-full rounded-full bg-gray-200 dark:bg-gray-700">
|
||||
<div
|
||||
class="h-2 rounded-full transition-all duration-300"
|
||||
:class="getProgressColor(index)"
|
||||
:style="{ width: calculatePercentage(stat) + '%' }"
|
||||
/>
|
||||
</div>
|
||||
<div
|
||||
class="mt-1 flex items-center justify-between text-xs text-gray-500 dark:text-gray-400"
|
||||
>
|
||||
<span>{{ formatNumber(getStatUsage(stat)?.requests || 0) }}次</span>
|
||||
<span>{{ getStatUsage(stat)?.formattedCost || '$0.00' }}</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 其他Keys汇总 -->
|
||||
<div v-if="otherKeysCount > 0" class="border-t border-gray-200 pt-2 dark:border-gray-700">
|
||||
<div class="flex items-center justify-between text-sm text-gray-600 dark:text-gray-400">
|
||||
<span>其他 {{ otherKeysCount }} 个Keys</span>
|
||||
<span>{{ otherPercentage }}%</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 单个Key模式提示 -->
|
||||
<div
|
||||
v-else-if="!multiKeyMode"
|
||||
class="flex h-32 items-center justify-center text-sm text-gray-500 dark:text-gray-400"
|
||||
>
|
||||
<div class="text-center">
|
||||
<i class="fas fa-chart-pie mb-2 text-2xl" />
|
||||
<p>使用占比仅在多Key查询时显示</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div
|
||||
v-else
|
||||
class="flex h-32 items-center justify-center text-sm text-gray-500 dark:text-gray-400"
|
||||
>
|
||||
<i class="fas fa-chart-pie mr-2" />
|
||||
暂无数据
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { computed } from 'vue'
|
||||
import { storeToRefs } from 'pinia'
|
||||
import { useApiStatsStore } from '@/stores/apistats'
|
||||
|
||||
const apiStatsStore = useApiStatsStore()
|
||||
const { aggregatedStats, individualStats, statsPeriod, multiKeyMode } = storeToRefs(apiStatsStore)
|
||||
|
||||
// 获取当前时间段的使用数据
|
||||
const getStatUsage = (stat) => {
|
||||
if (!stat) return null
|
||||
|
||||
if (statsPeriod.value === 'daily') {
|
||||
return stat.dailyUsage || stat.usage
|
||||
} else {
|
||||
return stat.monthlyUsage || stat.usage
|
||||
}
|
||||
}
|
||||
|
||||
// 获取TOP Keys(最多显示5个)
|
||||
const topKeys = computed(() => {
|
||||
if (!individualStats.value || individualStats.value.length === 0) return []
|
||||
|
||||
return [...individualStats.value]
|
||||
.sort((a, b) => {
|
||||
const aUsage = getStatUsage(a)
|
||||
const bUsage = getStatUsage(b)
|
||||
return (bUsage?.cost || 0) - (aUsage?.cost || 0)
|
||||
})
|
||||
.slice(0, 5)
|
||||
})
|
||||
|
||||
// 计算其他Keys数量
|
||||
const otherKeysCount = computed(() => {
|
||||
if (!individualStats.value) return 0
|
||||
return Math.max(0, individualStats.value.length - 5)
|
||||
})
|
||||
|
||||
// 计算其他Keys的占比
|
||||
const otherPercentage = computed(() => {
|
||||
if (!individualStats.value || !aggregatedStats.value) return 0
|
||||
|
||||
const topKeysCost = topKeys.value.reduce((sum, stat) => {
|
||||
const usage = getStatUsage(stat)
|
||||
return sum + (usage?.cost || 0)
|
||||
}, 0)
|
||||
const totalCost =
|
||||
statsPeriod.value === 'daily'
|
||||
? aggregatedStats.value.dailyUsage?.cost || 0
|
||||
: aggregatedStats.value.monthlyUsage?.cost || 0
|
||||
|
||||
if (totalCost === 0) return 0
|
||||
const otherCost = totalCost - topKeysCost
|
||||
return Math.max(0, Math.round((otherCost / totalCost) * 100))
|
||||
})
|
||||
|
||||
// 计算单个Key的百分比
|
||||
const calculatePercentage = (stat) => {
|
||||
if (!aggregatedStats.value) return 0
|
||||
|
||||
const totalCost =
|
||||
statsPeriod.value === 'daily'
|
||||
? aggregatedStats.value.dailyUsage?.cost || 0
|
||||
: aggregatedStats.value.monthlyUsage?.cost || 0
|
||||
|
||||
if (totalCost === 0) return 0
|
||||
const usage = getStatUsage(stat)
|
||||
const percentage = ((usage?.cost || 0) / totalCost) * 100
|
||||
return Math.round(percentage)
|
||||
}
|
||||
|
||||
// 获取进度条颜色
|
||||
const getProgressColor = (index) => {
|
||||
const colors = ['bg-blue-500', 'bg-green-500', 'bg-purple-500', 'bg-yellow-500', 'bg-pink-500']
|
||||
return colors[index] || 'bg-gray-400'
|
||||
}
|
||||
|
||||
// 格式化数字
|
||||
const formatNumber = (num) => {
|
||||
if (typeof num !== 'number') {
|
||||
num = parseInt(num) || 0
|
||||
}
|
||||
|
||||
if (num === 0) return '0'
|
||||
|
||||
if (num >= 1000000) {
|
||||
return (num / 1000000).toFixed(1) + 'M'
|
||||
} else if (num >= 1000) {
|
||||
return (num / 1000).toFixed(1) + 'K'
|
||||
} else {
|
||||
return num.toLocaleString()
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
/* 卡片样式 - 使用CSS变量 */
|
||||
.card {
|
||||
background: var(--surface-color);
|
||||
border-radius: 16px;
|
||||
border: 1px solid var(--border-color);
|
||||
box-shadow:
|
||||
0 10px 15px -3px rgba(0, 0, 0, 0.1),
|
||||
0 4px 6px -2px rgba(0, 0, 0, 0.05);
|
||||
overflow: hidden;
|
||||
position: relative;
|
||||
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
|
||||
}
|
||||
|
||||
.card::before {
|
||||
content: '';
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
height: 1px;
|
||||
background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.5), transparent);
|
||||
}
|
||||
|
||||
.card:hover {
|
||||
transform: translateY(-2px);
|
||||
box-shadow:
|
||||
0 20px 25px -5px rgba(0, 0, 0, 0.15),
|
||||
0 10px 10px -5px rgba(0, 0, 0, 0.08);
|
||||
}
|
||||
|
||||
:global(.dark) .card:hover {
|
||||
box-shadow:
|
||||
0 20px 25px -5px rgba(0, 0, 0, 0.5),
|
||||
0 10px 10px -5px rgba(0, 0, 0, 0.35);
|
||||
}
|
||||
</style>
|
||||
@@ -1,24 +1,64 @@
|
||||
<template>
|
||||
<div class="api-input-wide-card mb-8 rounded-3xl p-6 shadow-xl">
|
||||
<!-- 标题区域 -->
|
||||
<div class="wide-card-title mb-6 text-center">
|
||||
<h2 class="mb-2 text-2xl font-bold text-gray-900 dark:text-white">
|
||||
<div class="wide-card-title mb-6">
|
||||
<h2 class="mb-2 text-2xl font-bold text-gray-900 dark:text-gray-200">
|
||||
<i class="fas fa-chart-line mr-3" />
|
||||
使用统计查询
|
||||
</h2>
|
||||
<p class="text-base text-gray-600 dark:text-gray-300">查询您的 API Key 使用情况和统计数据</p>
|
||||
<p class="text-base text-gray-600 dark:text-gray-400">查询您的 API Key 使用情况和统计数据</p>
|
||||
</div>
|
||||
|
||||
<!-- 输入区域 -->
|
||||
<div class="mx-auto max-w-4xl">
|
||||
<div class="api-input-grid grid grid-cols-1 lg:grid-cols-4">
|
||||
<!-- 控制栏 -->
|
||||
<div class="control-bar mb-4 flex flex-wrap items-center justify-between gap-3">
|
||||
<!-- API Key 标签 -->
|
||||
<label class="text-sm font-medium text-gray-700 dark:text-gray-300">
|
||||
<i class="fas fa-key mr-2" />
|
||||
{{ multiKeyMode ? '输入您的 API Keys(每行一个或用逗号分隔)' : '输入您的 API Key' }}
|
||||
</label>
|
||||
|
||||
<!-- 模式切换和查询按钮组 -->
|
||||
<div class="button-group flex items-center gap-2">
|
||||
<!-- 模式切换 -->
|
||||
<div
|
||||
class="mode-switch-group flex items-center rounded-lg bg-gray-100 p-1 dark:bg-gray-800"
|
||||
>
|
||||
<button
|
||||
class="mode-switch-btn"
|
||||
:class="{ active: !multiKeyMode }"
|
||||
title="单一模式"
|
||||
@click="multiKeyMode = false"
|
||||
>
|
||||
<i class="fas fa-key" />
|
||||
<span class="ml-2 hidden sm:inline">单一</span>
|
||||
</button>
|
||||
<button
|
||||
class="mode-switch-btn"
|
||||
:class="{ active: multiKeyMode }"
|
||||
title="聚合模式"
|
||||
@click="multiKeyMode = true"
|
||||
>
|
||||
<i class="fas fa-layer-group" />
|
||||
<span class="ml-2 hidden sm:inline">聚合</span>
|
||||
<span
|
||||
v-if="multiKeyMode && parsedApiKeys.length > 0"
|
||||
class="ml-1 rounded-full bg-white/20 px-1.5 py-0.5 text-xs font-semibold"
|
||||
>
|
||||
{{ parsedApiKeys.length }}
|
||||
</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="api-input-grid grid grid-cols-1 gap-4 lg:grid-cols-4">
|
||||
<!-- API Key 输入 -->
|
||||
<div class="lg:col-span-3">
|
||||
<label class="mb-2 block text-sm font-medium text-gray-700 dark:text-gray-200">
|
||||
<i class="fas fa-key mr-2" />
|
||||
输入您的 API Key
|
||||
</label>
|
||||
<!-- 单 Key 模式输入框 -->
|
||||
<input
|
||||
v-if="!multiKeyMode"
|
||||
v-model="apiKey"
|
||||
class="wide-card-input w-full"
|
||||
:disabled="loading"
|
||||
@@ -26,16 +66,33 @@
|
||||
type="password"
|
||||
@keyup.enter="queryStats"
|
||||
/>
|
||||
|
||||
<!-- 多 Key 模式输入框 -->
|
||||
<div v-else class="relative">
|
||||
<textarea
|
||||
v-model="apiKey"
|
||||
class="wide-card-input w-full resize-y"
|
||||
:disabled="loading"
|
||||
placeholder="请输入您的 API Keys,支持以下格式: cr_xxx cr_yyy 或 cr_xxx, cr_yyy"
|
||||
rows="4"
|
||||
@keyup.ctrl.enter="queryStats"
|
||||
/>
|
||||
<button
|
||||
v-if="apiKey && !loading"
|
||||
class="absolute right-2 top-2 text-gray-400 hover:text-gray-600 dark:text-gray-500 dark:hover:text-gray-300"
|
||||
title="清空输入"
|
||||
@click="clearInput"
|
||||
>
|
||||
<i class="fas fa-times-circle" />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 查询按钮 -->
|
||||
<div class="lg:col-span-1">
|
||||
<label class="mb-2 hidden text-sm font-medium text-gray-700 dark:text-gray-200 lg:block">
|
||||
|
||||
</label>
|
||||
<button
|
||||
class="btn btn-primary btn-query flex h-full w-full items-center justify-center gap-2"
|
||||
:disabled="loading || !apiKey.trim()"
|
||||
:disabled="loading || !hasValidInput"
|
||||
@click="queryStats"
|
||||
>
|
||||
<i v-if="loading" class="fas fa-spinner loading-spinner" />
|
||||
@@ -48,19 +105,56 @@
|
||||
<!-- 安全提示 -->
|
||||
<div class="security-notice mt-4">
|
||||
<i class="fas fa-shield-alt mr-2" />
|
||||
您的 API Key 仅用于查询自己的统计数据,不会被存储或用于其他用途
|
||||
{{
|
||||
multiKeyMode
|
||||
? '您的 API Keys 仅用于查询统计数据,不会被存储。聚合模式下部分个体化信息将不显示。'
|
||||
: '您的 API Key 仅用于查询自己的统计数据,不会被存储或用于其他用途'
|
||||
}}
|
||||
</div>
|
||||
|
||||
<!-- 多 Key 模式额外提示 -->
|
||||
<div
|
||||
v-if="multiKeyMode"
|
||||
class="mt-2 rounded-lg bg-blue-50 p-3 text-sm text-blue-700 dark:bg-blue-900/20 dark:text-blue-400"
|
||||
>
|
||||
<i class="fas fa-lightbulb mr-2" />
|
||||
<span>提示:最多支持同时查询 30 个 API Keys。使用 Ctrl+Enter 快速查询。</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { computed } from 'vue'
|
||||
import { storeToRefs } from 'pinia'
|
||||
import { useApiStatsStore } from '@/stores/apistats'
|
||||
|
||||
const apiStatsStore = useApiStatsStore()
|
||||
const { apiKey, loading } = storeToRefs(apiStatsStore)
|
||||
const { queryStats } = apiStatsStore
|
||||
const { apiKey, loading, multiKeyMode } = storeToRefs(apiStatsStore)
|
||||
const { queryStats, clearInput } = apiStatsStore
|
||||
|
||||
// 解析输入的 API Keys
|
||||
const parsedApiKeys = computed(() => {
|
||||
if (!multiKeyMode.value || !apiKey.value) return []
|
||||
|
||||
// 支持逗号和换行符分隔
|
||||
const keys = apiKey.value
|
||||
.split(/[,\n]+/)
|
||||
.map((key) => key.trim())
|
||||
.filter((key) => key.length > 0)
|
||||
|
||||
// 去重并限制最多30个
|
||||
const uniqueKeys = [...new Set(keys)]
|
||||
return uniqueKeys.slice(0, 30)
|
||||
})
|
||||
|
||||
// 判断是否有有效输入
|
||||
const hasValidInput = computed(() => {
|
||||
if (multiKeyMode.value) {
|
||||
return parsedApiKeys.value.length > 0
|
||||
}
|
||||
return apiKey.value && apiKey.value.trim().length > 0
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
@@ -101,7 +195,6 @@ const { queryStats } = apiStatsStore
|
||||
|
||||
/* 标题样式 */
|
||||
.wide-card-title h2 {
|
||||
color: #1f2937;
|
||||
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
|
||||
font-weight: 700;
|
||||
}
|
||||
@@ -112,12 +205,12 @@ const { queryStats } = apiStatsStore
|
||||
}
|
||||
|
||||
.wide-card-title p {
|
||||
color: #4b5563;
|
||||
color: #6b7280;
|
||||
text-shadow: 0 1px 1px rgba(0, 0, 0, 0.05);
|
||||
}
|
||||
|
||||
:global(.dark) .wide-card-title p {
|
||||
color: #d1d5db;
|
||||
color: #9ca3af;
|
||||
text-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
|
||||
}
|
||||
|
||||
@@ -251,6 +344,93 @@ const { queryStats } = apiStatsStore
|
||||
text-shadow: 0 1px 2px rgba(16, 185, 129, 0.2);
|
||||
}
|
||||
|
||||
/* 控制栏 */
|
||||
.control-bar {
|
||||
padding-bottom: 0.5rem;
|
||||
border-bottom: 1px solid rgba(229, 231, 235, 0.3);
|
||||
}
|
||||
|
||||
:global(.dark) .control-bar {
|
||||
border-bottom-color: rgba(75, 85, 99, 0.3);
|
||||
}
|
||||
|
||||
/* 按钮组 */
|
||||
.button-group {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
/* 模式切换组 */
|
||||
.mode-switch-group {
|
||||
display: inline-flex;
|
||||
padding: 4px;
|
||||
background: #f3f4f6;
|
||||
border-radius: 0.5rem;
|
||||
box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.05);
|
||||
}
|
||||
|
||||
:global(.dark) .mode-switch-group {
|
||||
background: #1f2937;
|
||||
box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.3);
|
||||
}
|
||||
|
||||
/* 模式切换按钮 */
|
||||
.mode-switch-btn {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
padding: 6px 12px;
|
||||
font-size: 0.875rem;
|
||||
font-weight: 500;
|
||||
color: #6b7280;
|
||||
background: transparent;
|
||||
border: none;
|
||||
border-radius: 0.375rem;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s ease;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
:global(.dark) .mode-switch-btn {
|
||||
color: #9ca3af;
|
||||
}
|
||||
|
||||
.mode-switch-btn:hover:not(.active) {
|
||||
color: #374151;
|
||||
background: rgba(0, 0, 0, 0.05);
|
||||
}
|
||||
|
||||
:global(.dark) .mode-switch-btn:hover:not(.active) {
|
||||
color: #d1d5db;
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
}
|
||||
|
||||
.mode-switch-btn.active {
|
||||
color: white;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
box-shadow: 0 2px 4px rgba(102, 126, 234, 0.2);
|
||||
}
|
||||
|
||||
.mode-switch-btn.active:hover {
|
||||
box-shadow: 0 4px 6px rgba(102, 126, 234, 0.3);
|
||||
}
|
||||
|
||||
.mode-switch-btn i {
|
||||
font-size: 0.875rem;
|
||||
}
|
||||
|
||||
/* 淡入淡出动画 */
|
||||
.fade-enter-active,
|
||||
.fade-leave-active {
|
||||
transition: all 0.3s ease;
|
||||
}
|
||||
|
||||
.fade-enter-from,
|
||||
.fade-leave-to {
|
||||
opacity: 0;
|
||||
transform: translateX(-10px);
|
||||
}
|
||||
|
||||
/* 加载动画 */
|
||||
.loading-spinner {
|
||||
animation: spin 1s linear infinite;
|
||||
@@ -267,6 +447,18 @@ const { queryStats } = apiStatsStore
|
||||
}
|
||||
|
||||
/* 响应式优化 */
|
||||
@media (max-width: 768px) {
|
||||
.control-bar {
|
||||
flex-direction: column;
|
||||
align-items: stretch;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.button-group {
|
||||
justify-content: center;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.api-input-wide-card {
|
||||
padding: 1.25rem;
|
||||
@@ -304,6 +496,22 @@ const { queryStats } = apiStatsStore
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 480px) {
|
||||
.mode-toggle-btn {
|
||||
padding: 5px 8px;
|
||||
}
|
||||
|
||||
.toggle-icon {
|
||||
width: 18px;
|
||||
height: 18px;
|
||||
}
|
||||
|
||||
.hint-text {
|
||||
font-size: 0.7rem;
|
||||
padding: 4px 8px;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 480px) {
|
||||
.api-input-wide-card {
|
||||
padding: 1rem;
|
||||
|
||||
@@ -1,14 +1,108 @@
|
||||
<template>
|
||||
<div>
|
||||
<!-- 限制配置 -->
|
||||
<!-- 限制配置 / 聚合模式提示 -->
|
||||
<div class="card p-4 md:p-6">
|
||||
<h3
|
||||
class="mb-3 flex items-center text-lg font-bold text-gray-900 dark:text-gray-100 md:mb-4 md:text-xl"
|
||||
>
|
||||
<i class="fas fa-shield-alt mr-2 text-sm text-red-500 md:mr-3 md:text-base" />
|
||||
限制配置
|
||||
{{ multiKeyMode ? '限制配置(聚合查询模式)' : '限制配置' }}
|
||||
</h3>
|
||||
<div class="space-y-4 md:space-y-5">
|
||||
|
||||
<!-- 多 Key 模式下的聚合统计信息 -->
|
||||
<div v-if="multiKeyMode && aggregatedStats" class="space-y-4">
|
||||
<!-- API Keys 概况 -->
|
||||
<div
|
||||
class="rounded-lg bg-gradient-to-r from-blue-50 to-indigo-50 p-4 dark:from-blue-900/20 dark:to-indigo-900/20"
|
||||
>
|
||||
<div class="mb-3 flex items-center justify-between">
|
||||
<span class="text-sm font-medium text-gray-700 dark:text-gray-300">
|
||||
<i class="fas fa-layer-group mr-2 text-blue-500" />
|
||||
API Keys 概况
|
||||
</span>
|
||||
<span
|
||||
class="rounded-full bg-blue-100 px-2 py-1 text-xs font-semibold text-blue-700 dark:bg-blue-800 dark:text-blue-200"
|
||||
>
|
||||
{{ aggregatedStats.activeKeys }}/{{ aggregatedStats.totalKeys }}
|
||||
</span>
|
||||
</div>
|
||||
<div class="grid grid-cols-2 gap-3">
|
||||
<div class="text-center">
|
||||
<div class="text-lg font-bold text-gray-900 dark:text-gray-100">
|
||||
{{ aggregatedStats.totalKeys }}
|
||||
</div>
|
||||
<div class="text-xs text-gray-600 dark:text-gray-400">总计 Keys</div>
|
||||
</div>
|
||||
<div class="text-center">
|
||||
<div class="text-lg font-bold text-green-600">
|
||||
{{ aggregatedStats.activeKeys }}
|
||||
</div>
|
||||
<div class="text-xs text-gray-600 dark:text-gray-400">激活 Keys</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 聚合统计数据 -->
|
||||
<div
|
||||
class="rounded-lg bg-gradient-to-r from-purple-50 to-pink-50 p-4 dark:from-purple-900/20 dark:to-pink-900/20"
|
||||
>
|
||||
<div class="mb-3 flex items-center">
|
||||
<i class="fas fa-chart-pie mr-2 text-purple-500" />
|
||||
<span class="text-sm font-medium text-gray-700 dark:text-gray-300">聚合统计摘要</span>
|
||||
</div>
|
||||
<div class="space-y-2">
|
||||
<div class="flex items-center justify-between">
|
||||
<span class="text-xs text-gray-600 dark:text-gray-400">
|
||||
<i class="fas fa-database mr-1 text-gray-400" />
|
||||
总请求数
|
||||
</span>
|
||||
<span class="text-sm font-medium text-gray-900 dark:text-gray-100">
|
||||
{{ formatNumber(aggregatedStats.usage.requests) }}
|
||||
</span>
|
||||
</div>
|
||||
<div class="flex items-center justify-between">
|
||||
<span class="text-xs text-gray-600 dark:text-gray-400">
|
||||
<i class="fas fa-coins mr-1 text-yellow-500" />
|
||||
总 Tokens
|
||||
</span>
|
||||
<span class="text-sm font-medium text-gray-900 dark:text-gray-100">
|
||||
{{ formatNumber(aggregatedStats.usage.allTokens) }}
|
||||
</span>
|
||||
</div>
|
||||
<div class="flex items-center justify-between">
|
||||
<span class="text-xs text-gray-600 dark:text-gray-400">
|
||||
<i class="fas fa-dollar-sign mr-1 text-green-500" />
|
||||
总费用
|
||||
</span>
|
||||
<span class="text-sm font-medium text-gray-900 dark:text-gray-100">
|
||||
{{ aggregatedStats.usage.formattedCost }}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 无效 Keys 提示 -->
|
||||
<div
|
||||
v-if="invalidKeys && invalidKeys.length > 0"
|
||||
class="rounded-lg bg-red-50 p-3 text-sm dark:bg-red-900/20"
|
||||
>
|
||||
<i class="fas fa-exclamation-triangle mr-2 text-red-600 dark:text-red-400" />
|
||||
<span class="text-red-700 dark:text-red-300">
|
||||
{{ invalidKeys.length }} 个无效的 API Key
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<!-- 提示信息 -->
|
||||
<div
|
||||
class="rounded-lg bg-gray-50 p-3 text-xs text-gray-600 dark:bg-gray-800 dark:text-gray-400"
|
||||
>
|
||||
<i class="fas fa-info-circle mr-1" />
|
||||
每个 API Key 有独立的限制设置,聚合模式下不显示单个限制配置
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 仅在单 Key 模式下显示限制配置 -->
|
||||
<div v-if="!multiKeyMode" class="space-y-4 md:space-y-5">
|
||||
<!-- 每日费用限制 -->
|
||||
<div>
|
||||
<div class="mb-2 flex items-center justify-between">
|
||||
@@ -221,7 +315,7 @@ import { useApiStatsStore } from '@/stores/apistats'
|
||||
import WindowCountdown from '@/components/apikeys/WindowCountdown.vue'
|
||||
|
||||
const apiStatsStore = useApiStatsStore()
|
||||
const { statsData } = storeToRefs(apiStatsStore)
|
||||
const { statsData, multiKeyMode, aggregatedStats, invalidKeys } = storeToRefs(apiStatsStore)
|
||||
|
||||
// 获取每日费用进度
|
||||
const getDailyCostProgress = () => {
|
||||
@@ -239,6 +333,24 @@ const getDailyCostProgressColor = () => {
|
||||
if (progress >= 80) return 'bg-yellow-500'
|
||||
return 'bg-green-500'
|
||||
}
|
||||
|
||||
// 格式化数字
|
||||
const formatNumber = (num) => {
|
||||
if (typeof num !== 'number') {
|
||||
num = parseInt(num) || 0
|
||||
}
|
||||
|
||||
if (num === 0) return '0'
|
||||
|
||||
// 大数字使用简化格式
|
||||
if (num >= 1000000) {
|
||||
return (num / 1000000).toFixed(1) + 'M'
|
||||
} else if (num >= 1000) {
|
||||
return (num / 1000).toFixed(1) + 'K'
|
||||
} else {
|
||||
return num.toLocaleString()
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
|
||||
@@ -1,14 +1,83 @@
|
||||
<template>
|
||||
<div class="mb-6 grid grid-cols-1 gap-4 md:mb-8 md:gap-6 lg:grid-cols-2">
|
||||
<!-- API Key 基本信息 -->
|
||||
<!-- API Key 基本信息 / 批量查询概要 -->
|
||||
<div class="card p-4 md:p-6">
|
||||
<h3
|
||||
class="mb-3 flex items-center text-lg font-bold text-gray-900 dark:text-gray-100 md:mb-4 md:text-xl"
|
||||
>
|
||||
<i class="fas fa-info-circle mr-2 text-sm text-blue-500 md:mr-3 md:text-base" />
|
||||
API Key 信息
|
||||
<i
|
||||
class="mr-2 text-sm md:mr-3 md:text-base"
|
||||
:class="
|
||||
multiKeyMode ? 'fas fa-layer-group text-purple-500' : 'fas fa-info-circle text-blue-500'
|
||||
"
|
||||
/>
|
||||
{{ multiKeyMode ? '批量查询概要' : 'API Key 信息' }}
|
||||
</h3>
|
||||
<div class="space-y-2 md:space-y-3">
|
||||
|
||||
<!-- 多 Key 模式下的概要信息 -->
|
||||
<div v-if="multiKeyMode && aggregatedStats" class="space-y-2 md:space-y-3">
|
||||
<div class="flex items-center justify-between">
|
||||
<span class="text-sm text-gray-600 dark:text-gray-400 md:text-base">查询 Keys 数</span>
|
||||
<span class="text-sm font-medium text-gray-900 dark:text-gray-100 md:text-base">
|
||||
{{ aggregatedStats.totalKeys }} 个
|
||||
</span>
|
||||
</div>
|
||||
<div class="flex items-center justify-between">
|
||||
<span class="text-sm text-gray-600 dark:text-gray-400 md:text-base">有效 Keys 数</span>
|
||||
<span class="text-sm font-medium text-green-600 md:text-base">
|
||||
<i class="fas fa-check-circle mr-1 text-xs md:text-sm" />
|
||||
{{ aggregatedStats.activeKeys }} 个
|
||||
</span>
|
||||
</div>
|
||||
<div v-if="invalidKeys.length > 0" class="flex items-center justify-between">
|
||||
<span class="text-sm text-gray-600 dark:text-gray-400 md:text-base">无效 Keys 数</span>
|
||||
<span class="text-sm font-medium text-red-600 md:text-base">
|
||||
<i class="fas fa-times-circle mr-1 text-xs md:text-sm" />
|
||||
{{ invalidKeys.length }} 个
|
||||
</span>
|
||||
</div>
|
||||
<div class="flex items-center justify-between">
|
||||
<span class="text-sm text-gray-600 dark:text-gray-400 md:text-base">总请求数</span>
|
||||
<span class="text-sm font-medium text-gray-900 dark:text-gray-100 md:text-base">
|
||||
{{ formatNumber(aggregatedStats.usage.requests) }}
|
||||
</span>
|
||||
</div>
|
||||
<div class="flex items-center justify-between">
|
||||
<span class="text-sm text-gray-600 dark:text-gray-400 md:text-base">总 Token 数</span>
|
||||
<span class="text-sm font-medium text-gray-900 dark:text-gray-100 md:text-base">
|
||||
{{ formatNumber(aggregatedStats.usage.allTokens) }}
|
||||
</span>
|
||||
</div>
|
||||
<div class="flex items-center justify-between">
|
||||
<span class="text-sm text-gray-600 dark:text-gray-400 md:text-base">总费用</span>
|
||||
<span class="text-sm font-medium text-indigo-600 md:text-base">
|
||||
{{ aggregatedStats.usage.formattedCost }}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<!-- 各 Key 贡献占比(可选) -->
|
||||
<div
|
||||
v-if="individualStats.length > 1"
|
||||
class="border-t border-gray-200 pt-2 dark:border-gray-700"
|
||||
>
|
||||
<div class="mb-2 text-xs text-gray-500 dark:text-gray-400">各 Key 贡献占比</div>
|
||||
<div class="space-y-1">
|
||||
<div
|
||||
v-for="stat in topContributors"
|
||||
:key="stat.apiId"
|
||||
class="flex items-center justify-between text-xs"
|
||||
>
|
||||
<span class="truncate text-gray-600 dark:text-gray-400">{{ stat.name }}</span>
|
||||
<span class="text-gray-900 dark:text-gray-100"
|
||||
>{{ calculateContribution(stat) }}%</span
|
||||
>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 单 Key 模式下的详细信息 -->
|
||||
<div v-else class="space-y-2 md:space-y-3">
|
||||
<div class="flex items-center justify-between">
|
||||
<span class="text-sm text-gray-600 dark:text-gray-400 md:text-base">名称</span>
|
||||
<span
|
||||
@@ -128,12 +197,38 @@
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
/* eslint-disable no-unused-vars */
|
||||
import { computed } from 'vue'
|
||||
import { storeToRefs } from 'pinia'
|
||||
import { useApiStatsStore } from '@/stores/apistats'
|
||||
import dayjs from 'dayjs'
|
||||
|
||||
const apiStatsStore = useApiStatsStore()
|
||||
const { statsData, statsPeriod, currentPeriodData } = storeToRefs(apiStatsStore)
|
||||
const {
|
||||
statsData,
|
||||
statsPeriod,
|
||||
currentPeriodData,
|
||||
multiKeyMode,
|
||||
aggregatedStats,
|
||||
individualStats,
|
||||
invalidKeys
|
||||
} = storeToRefs(apiStatsStore)
|
||||
|
||||
// 计算前3个贡献最大的 Key
|
||||
const topContributors = computed(() => {
|
||||
if (!individualStats.value || individualStats.value.length === 0) return []
|
||||
|
||||
return [...individualStats.value]
|
||||
.sort((a, b) => (b.usage?.allTokens || 0) - (a.usage?.allTokens || 0))
|
||||
.slice(0, 3)
|
||||
})
|
||||
|
||||
// 计算单个 Key 的贡献占比
|
||||
const calculateContribution = (stat) => {
|
||||
if (!aggregatedStats.value || !aggregatedStats.value.usage.allTokens) return 0
|
||||
const percentage = ((stat.usage?.allTokens || 0) / aggregatedStats.value.usage.allTokens) * 100
|
||||
return percentage.toFixed(1)
|
||||
}
|
||||
|
||||
// 格式化日期
|
||||
const formatDate = (dateString) => {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<div ref="triggerRef" class="relative">
|
||||
<!-- 选择器主体 -->
|
||||
<div
|
||||
class="form-input flex w-full cursor-pointer items-center justify-between dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200"
|
||||
class="form-input flex w-full cursor-pointer items-center justify-between border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200"
|
||||
:class="{ 'opacity-50': disabled }"
|
||||
@click="!disabled && toggleDropdown()"
|
||||
>
|
||||
@@ -40,7 +40,7 @@
|
||||
<input
|
||||
ref="searchInput"
|
||||
v-model="searchQuery"
|
||||
class="form-input w-full text-sm dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
class="form-input w-full border-gray-300 text-sm dark:border-gray-600 dark:bg-gray-700 dark:text-gray-200 dark:placeholder-gray-400"
|
||||
placeholder="搜索账号名称..."
|
||||
style="padding-left: 40px; padding-right: 36px"
|
||||
type="text"
|
||||
|
||||
@@ -159,7 +159,11 @@
|
||||
</button>
|
||||
|
||||
<button
|
||||
v-if="!(apiKey.isDeleted === 'true' || apiKey.deletedAt) && apiKey.isActive"
|
||||
v-if="
|
||||
!(apiKey.isDeleted === 'true' || apiKey.deletedAt) &&
|
||||
apiKey.isActive &&
|
||||
allowUserDeleteApiKeys
|
||||
"
|
||||
class="inline-flex items-center rounded border border-transparent p-1 text-red-400 hover:text-red-600"
|
||||
title="Delete API Key"
|
||||
@click="deleteApiKey(apiKey)"
|
||||
@@ -255,6 +259,7 @@ const userStore = useUserStore()
|
||||
const loading = ref(true)
|
||||
const apiKeys = ref([])
|
||||
const maxApiKeys = computed(() => userStore.config?.maxApiKeysPerUser || 5)
|
||||
const allowUserDeleteApiKeys = computed(() => userStore.config?.allowUserDeleteApiKeys === true)
|
||||
|
||||
const showCreateModal = ref(false)
|
||||
const showViewModal = ref(false)
|
||||
|
||||
@@ -82,7 +82,16 @@ class ApiClient {
|
||||
|
||||
// 如果响应不成功,抛出错误
|
||||
if (!response.ok) {
|
||||
throw new Error(data.message || `HTTP ${response.status}`)
|
||||
// 创建一个包含完整错误信息的错误对象
|
||||
const error = new Error(data.message || `HTTP ${response.status}`)
|
||||
// 保留完整的响应数据,以便错误处理时可以访问详细信息
|
||||
error.response = {
|
||||
status: response.status,
|
||||
data: data
|
||||
}
|
||||
// 为了向后兼容,也保留原始的 message
|
||||
error.message = data.message || error.message
|
||||
throw error
|
||||
}
|
||||
|
||||
return data
|
||||
|
||||
@@ -76,6 +76,22 @@ class ApiStatsClient {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 批量查询统计数据
|
||||
async getBatchStats(apiIds) {
|
||||
return this.request('/apiStats/api/batch-stats', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ apiIds })
|
||||
})
|
||||
}
|
||||
|
||||
// 批量查询模型统计
|
||||
async getBatchModelStats(apiIds, period = 'daily') {
|
||||
return this.request('/apiStats/api/batch-model-stats', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ apiIds, period })
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export const apiStatsClient = new ApiStatsClient()
|
||||
|
||||
@@ -21,6 +21,14 @@ export const useApiStatsStore = defineStore('apistats', () => {
|
||||
siteIconData: ''
|
||||
})
|
||||
|
||||
// 多 Key 模式相关状态
|
||||
const multiKeyMode = ref(false)
|
||||
const apiKeys = ref([]) // 多个 API Key 数组
|
||||
const apiIds = ref([]) // 对应的 ID 数组
|
||||
const aggregatedStats = ref(null) // 聚合后的统计数据
|
||||
const individualStats = ref([]) // 各个 Key 的独立数据
|
||||
const invalidKeys = ref([]) // 无效的 Keys 列表
|
||||
|
||||
// 计算属性
|
||||
const currentPeriodData = computed(() => {
|
||||
const defaultData = {
|
||||
@@ -34,6 +42,16 @@ export const useApiStatsStore = defineStore('apistats', () => {
|
||||
formattedCost: '$0.000000'
|
||||
}
|
||||
|
||||
// 聚合模式下使用聚合数据
|
||||
if (multiKeyMode.value && aggregatedStats.value) {
|
||||
if (statsPeriod.value === 'daily') {
|
||||
return aggregatedStats.value.dailyUsage || defaultData
|
||||
} else {
|
||||
return aggregatedStats.value.monthlyUsage || defaultData
|
||||
}
|
||||
}
|
||||
|
||||
// 单个 Key 模式下使用原有逻辑
|
||||
if (statsPeriod.value === 'daily') {
|
||||
return dailyStats.value || defaultData
|
||||
} else {
|
||||
@@ -69,6 +87,11 @@ export const useApiStatsStore = defineStore('apistats', () => {
|
||||
|
||||
// 查询统计数据
|
||||
async function queryStats() {
|
||||
// 多 Key 模式处理
|
||||
if (multiKeyMode.value) {
|
||||
return queryBatchStats()
|
||||
}
|
||||
|
||||
if (!apiKey.value.trim()) {
|
||||
error.value = '请输入 API Key'
|
||||
return
|
||||
@@ -204,6 +227,12 @@ export const useApiStatsStore = defineStore('apistats', () => {
|
||||
|
||||
statsPeriod.value = period
|
||||
|
||||
// 多 Key 模式下加载批量模型统计
|
||||
if (multiKeyMode.value && apiIds.value.length > 0) {
|
||||
await loadBatchModelStats(period)
|
||||
return
|
||||
}
|
||||
|
||||
// 如果对应时间段的数据还没有加载,则加载它
|
||||
if (
|
||||
(period === 'daily' && !dailyStats.value) ||
|
||||
@@ -297,6 +326,127 @@ export const useApiStatsStore = defineStore('apistats', () => {
|
||||
}
|
||||
}
|
||||
|
||||
// 批量查询统计数据
|
||||
async function queryBatchStats() {
|
||||
const keys = parseApiKeys()
|
||||
if (keys.length === 0) {
|
||||
error.value = '请输入至少一个有效的 API Key'
|
||||
return
|
||||
}
|
||||
|
||||
loading.value = true
|
||||
error.value = ''
|
||||
aggregatedStats.value = null
|
||||
individualStats.value = []
|
||||
invalidKeys.value = []
|
||||
modelStats.value = []
|
||||
apiKeys.value = keys
|
||||
apiIds.value = []
|
||||
|
||||
try {
|
||||
// 批量获取 API Key IDs
|
||||
const idResults = await Promise.allSettled(keys.map((key) => apiStatsClient.getKeyId(key)))
|
||||
|
||||
const validIds = []
|
||||
const validKeys = []
|
||||
|
||||
idResults.forEach((result, index) => {
|
||||
if (result.status === 'fulfilled' && result.value.success) {
|
||||
validIds.push(result.value.data.id)
|
||||
validKeys.push(keys[index])
|
||||
} else {
|
||||
invalidKeys.value.push(keys[index])
|
||||
}
|
||||
})
|
||||
|
||||
if (validIds.length === 0) {
|
||||
throw new Error('所有 API Key 都无效')
|
||||
}
|
||||
|
||||
apiIds.value = validIds
|
||||
apiKeys.value = validKeys
|
||||
|
||||
// 批量查询统计数据
|
||||
const batchResult = await apiStatsClient.getBatchStats(validIds)
|
||||
|
||||
if (batchResult.success) {
|
||||
aggregatedStats.value = batchResult.data.aggregated
|
||||
individualStats.value = batchResult.data.individual
|
||||
statsData.value = batchResult.data.aggregated // 兼容现有组件
|
||||
|
||||
// 设置聚合模式下的日期统计数据,以保证现有组件的兼容性
|
||||
dailyStats.value = batchResult.data.aggregated.dailyUsage || null
|
||||
monthlyStats.value = batchResult.data.aggregated.monthlyUsage || null
|
||||
|
||||
// 加载聚合的模型统计
|
||||
await loadBatchModelStats(statsPeriod.value)
|
||||
|
||||
// 更新 URL
|
||||
updateBatchURL()
|
||||
} else {
|
||||
throw new Error(batchResult.message || '批量查询失败')
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Batch query error:', err)
|
||||
error.value = err.message || '批量查询统计数据失败'
|
||||
aggregatedStats.value = null
|
||||
individualStats.value = []
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
// 加载批量模型统计
|
||||
async function loadBatchModelStats(period = 'daily') {
|
||||
if (apiIds.value.length === 0) return
|
||||
|
||||
modelStatsLoading.value = true
|
||||
|
||||
try {
|
||||
const result = await apiStatsClient.getBatchModelStats(apiIds.value, period)
|
||||
|
||||
if (result.success) {
|
||||
modelStats.value = result.data || []
|
||||
} else {
|
||||
throw new Error(result.message || '加载批量模型统计失败')
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Load batch model stats error:', err)
|
||||
modelStats.value = []
|
||||
} finally {
|
||||
modelStatsLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
// 解析 API Keys
|
||||
function parseApiKeys() {
|
||||
if (!apiKey.value) return []
|
||||
|
||||
const keys = apiKey.value
|
||||
.split(/[,\n]+/)
|
||||
.map((key) => key.trim())
|
||||
.filter((key) => key.length > 0)
|
||||
|
||||
// 去重并限制最多30个
|
||||
const uniqueKeys = [...new Set(keys)]
|
||||
return uniqueKeys.slice(0, 30)
|
||||
}
|
||||
|
||||
// 更新批量查询 URL
|
||||
function updateBatchURL() {
|
||||
if (apiIds.value.length > 0) {
|
||||
const url = new URL(window.location)
|
||||
url.searchParams.set('apiIds', apiIds.value.join(','))
|
||||
url.searchParams.set('batch', 'true')
|
||||
window.history.pushState({}, '', url)
|
||||
}
|
||||
}
|
||||
|
||||
// 清空输入
|
||||
function clearInput() {
|
||||
apiKey.value = ''
|
||||
}
|
||||
|
||||
// 清除数据
|
||||
function clearData() {
|
||||
statsData.value = null
|
||||
@@ -306,11 +456,18 @@ export const useApiStatsStore = defineStore('apistats', () => {
|
||||
error.value = ''
|
||||
statsPeriod.value = 'daily'
|
||||
apiId.value = null
|
||||
// 清除多 Key 模式数据
|
||||
apiKeys.value = []
|
||||
apiIds.value = []
|
||||
aggregatedStats.value = null
|
||||
individualStats.value = []
|
||||
invalidKeys.value = []
|
||||
}
|
||||
|
||||
// 重置
|
||||
function reset() {
|
||||
apiKey.value = ''
|
||||
multiKeyMode.value = false
|
||||
clearData()
|
||||
}
|
||||
|
||||
@@ -328,6 +485,13 @@ export const useApiStatsStore = defineStore('apistats', () => {
|
||||
dailyStats,
|
||||
monthlyStats,
|
||||
oemSettings,
|
||||
// 多 Key 模式状态
|
||||
multiKeyMode,
|
||||
apiKeys,
|
||||
apiIds,
|
||||
aggregatedStats,
|
||||
individualStats,
|
||||
invalidKeys,
|
||||
|
||||
// Computed
|
||||
currentPeriodData,
|
||||
@@ -335,13 +499,16 @@ export const useApiStatsStore = defineStore('apistats', () => {
|
||||
|
||||
// Actions
|
||||
queryStats,
|
||||
queryBatchStats,
|
||||
loadAllPeriodStats,
|
||||
loadPeriodStats,
|
||||
loadModelStats,
|
||||
loadBatchModelStats,
|
||||
switchPeriod,
|
||||
loadStatsWithApiId,
|
||||
loadOemSettings,
|
||||
clearData,
|
||||
clearInput,
|
||||
reset
|
||||
}
|
||||
})
|
||||
|
||||
@@ -31,6 +31,9 @@ export function showToast(message, type = 'info', title = '', duration = 3000) {
|
||||
info: 'fas fa-info-circle'
|
||||
}
|
||||
|
||||
// 处理消息中的换行符,转换为 HTML 换行
|
||||
const formattedMessage = message.replace(/\n/g, '<br>')
|
||||
|
||||
toast.innerHTML = `
|
||||
<div class="flex items-start gap-3">
|
||||
<div class="flex-shrink-0 mt-0.5">
|
||||
@@ -38,7 +41,7 @@ export function showToast(message, type = 'info', title = '', duration = 3000) {
|
||||
</div>
|
||||
<div class="flex-1 min-w-0">
|
||||
${title ? `<h4 class="font-semibold text-sm mb-1">${title}</h4>` : ''}
|
||||
<p class="text-sm opacity-90 leading-relaxed">${message}</p>
|
||||
<p class="text-sm opacity-90 leading-relaxed">${formattedMessage}</p>
|
||||
</div>
|
||||
<button onclick="this.parentElement.parentElement.remove()"
|
||||
class="flex-shrink-0 text-white/70 hover:text-white transition-colors ml-2">
|
||||
|
||||
@@ -376,9 +376,11 @@
|
||||
? 'bg-orange-100 text-orange-800'
|
||||
: account.status === 'unauthorized'
|
||||
? 'bg-red-100 text-red-800'
|
||||
: account.isActive
|
||||
? 'bg-green-100 text-green-800'
|
||||
: 'bg-red-100 text-red-800'
|
||||
: account.status === 'temp_error'
|
||||
? 'bg-orange-100 text-orange-800'
|
||||
: account.isActive
|
||||
? 'bg-green-100 text-green-800'
|
||||
: 'bg-red-100 text-red-800'
|
||||
]"
|
||||
>
|
||||
<div
|
||||
@@ -388,9 +390,11 @@
|
||||
? 'bg-orange-500'
|
||||
: account.status === 'unauthorized'
|
||||
? 'bg-red-500'
|
||||
: account.isActive
|
||||
? 'bg-green-500'
|
||||
: 'bg-red-500'
|
||||
: account.status === 'temp_error'
|
||||
? 'bg-orange-500'
|
||||
: account.isActive
|
||||
? 'bg-green-500'
|
||||
: 'bg-red-500'
|
||||
]"
|
||||
/>
|
||||
{{
|
||||
@@ -398,9 +402,11 @@
|
||||
? '已封锁'
|
||||
: account.status === 'unauthorized'
|
||||
? '异常'
|
||||
: account.isActive
|
||||
? '正常'
|
||||
: '异常'
|
||||
: account.status === 'temp_error'
|
||||
? '临时异常'
|
||||
: account.isActive
|
||||
? '正常'
|
||||
: '异常'
|
||||
}}
|
||||
</span>
|
||||
<span
|
||||
@@ -578,6 +584,44 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- Claude Console: 显示每日额度使用进度 -->
|
||||
<div v-else-if="account.platform === 'claude-console'" class="space-y-2">
|
||||
<div v-if="Number(account.dailyQuota) > 0">
|
||||
<div class="flex items-center justify-between text-xs">
|
||||
<span class="text-gray-600 dark:text-gray-300">额度进度</span>
|
||||
<span class="font-medium text-gray-700 dark:text-gray-200">
|
||||
{{ getQuotaUsagePercent(account).toFixed(1) }}%
|
||||
</span>
|
||||
</div>
|
||||
<div class="flex items-center gap-2">
|
||||
<div class="h-2 w-24 rounded-full bg-gray-200 dark:bg-gray-700">
|
||||
<div
|
||||
:class="[
|
||||
'h-2 rounded-full transition-all duration-300',
|
||||
getQuotaBarClass(getQuotaUsagePercent(account))
|
||||
]"
|
||||
:style="{ width: Math.min(100, getQuotaUsagePercent(account)) + '%' }"
|
||||
/>
|
||||
</div>
|
||||
<span
|
||||
class="min-w-[32px] text-xs font-medium text-gray-700 dark:text-gray-200"
|
||||
>
|
||||
${{ formatCost(account.usage?.daily?.cost || 0) }} / ${{
|
||||
Number(account.dailyQuota).toFixed(2)
|
||||
}}
|
||||
</span>
|
||||
</div>
|
||||
<div class="text-xs text-gray-600 dark:text-gray-400">
|
||||
剩余 ${{ formatRemainingQuota(account) }}
|
||||
<span class="ml-2 text-gray-400"
|
||||
>重置 {{ account.quotaResetTime || '00:00' }}</span
|
||||
>
|
||||
</div>
|
||||
</div>
|
||||
<div v-else class="text-sm text-gray-400">
|
||||
<i class="fas fa-minus" />
|
||||
</div>
|
||||
</div>
|
||||
<div v-else-if="account.platform === 'claude'" class="text-sm text-gray-400">
|
||||
<i class="fas fa-minus" />
|
||||
</div>
|
||||
@@ -1630,6 +1674,9 @@ const getSchedulableReason = (account) => {
|
||||
if (account.status === 'unauthorized') {
|
||||
return '认证失败(401错误)'
|
||||
}
|
||||
if (account.status === 'temp_error' && account.errorMessage) {
|
||||
return account.errorMessage
|
||||
}
|
||||
if (account.status === 'error' && account.errorMessage) {
|
||||
return account.errorMessage
|
||||
}
|
||||
@@ -1668,6 +1715,8 @@ const getAccountStatusText = (account) => {
|
||||
account.rateLimitStatus === 'limited'
|
||||
)
|
||||
return '限流中'
|
||||
// 检查是否临时错误
|
||||
if (account.status === 'temp_error') return '临时异常'
|
||||
// 检查是否错误
|
||||
if (account.status === 'error' || !account.isActive) return '错误'
|
||||
// 检查是否可调度
|
||||
@@ -1692,6 +1741,9 @@ const getAccountStatusClass = (account) => {
|
||||
) {
|
||||
return 'bg-orange-100 text-orange-800'
|
||||
}
|
||||
if (account.status === 'temp_error') {
|
||||
return 'bg-orange-100 text-orange-800'
|
||||
}
|
||||
if (account.status === 'error' || !account.isActive) {
|
||||
return 'bg-red-100 text-red-800'
|
||||
}
|
||||
@@ -1717,6 +1769,9 @@ const getAccountStatusDotClass = (account) => {
|
||||
) {
|
||||
return 'bg-orange-500'
|
||||
}
|
||||
if (account.status === 'temp_error') {
|
||||
return 'bg-orange-500'
|
||||
}
|
||||
if (account.status === 'error' || !account.isActive) {
|
||||
return 'bg-red-500'
|
||||
}
|
||||
@@ -1771,6 +1826,29 @@ const formatCost = (cost) => {
|
||||
return cost.toFixed(2)
|
||||
}
|
||||
|
||||
// 额度使用百分比(Claude Console)
|
||||
const getQuotaUsagePercent = (account) => {
|
||||
const used = Number(account?.usage?.daily?.cost || 0)
|
||||
const quota = Number(account?.dailyQuota || 0)
|
||||
if (!quota || quota <= 0) return 0
|
||||
return (used / quota) * 100
|
||||
}
|
||||
|
||||
// 额度进度条颜色(Claude Console)
|
||||
const getQuotaBarClass = (percent) => {
|
||||
if (percent >= 90) return 'bg-red-500'
|
||||
if (percent >= 70) return 'bg-yellow-500'
|
||||
return 'bg-green-500'
|
||||
}
|
||||
|
||||
// 剩余额度(Claude Console)
|
||||
const formatRemainingQuota = (account) => {
|
||||
const used = Number(account?.usage?.daily?.cost || 0)
|
||||
const quota = Number(account?.dailyQuota || 0)
|
||||
if (!quota || quota <= 0) return '0.00'
|
||||
return Math.max(0, quota - used).toFixed(2)
|
||||
}
|
||||
|
||||
// 计算每日费用(使用后端返回的精确费用数据)
|
||||
const calculateDailyCost = (account) => {
|
||||
if (!account.usage || !account.usage.daily) return '0.0000'
|
||||
|
||||
@@ -54,7 +54,8 @@
|
||||
<!-- Tab Content -->
|
||||
<!-- 活跃 API Keys Tab Panel -->
|
||||
<div v-if="activeTab === 'active'" class="tab-panel">
|
||||
<div class="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
|
||||
<!-- 工具栏区域 - 添加 mb-4 增加与表格的间距 -->
|
||||
<div class="mb-4 flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
|
||||
<!-- 筛选器组 -->
|
||||
<div class="flex flex-col gap-3 sm:flex-row sm:flex-wrap sm:items-center sm:gap-3">
|
||||
<!-- 时间范围筛选 -->
|
||||
@@ -104,7 +105,7 @@
|
||||
<input
|
||||
v-model="searchKeyword"
|
||||
class="w-full rounded-lg border border-gray-200 bg-white px-3 py-2 pl-9 text-sm text-gray-700 placeholder-gray-400 shadow-sm transition-all duration-200 hover:border-gray-300 focus:border-cyan-500 focus:outline-none focus:ring-2 focus:ring-cyan-500/20 dark:border-gray-600 dark:bg-gray-800 dark:text-gray-200 dark:placeholder-gray-500 dark:hover:border-gray-500"
|
||||
placeholder="搜索名称..."
|
||||
:placeholder="isLdapEnabled ? '搜索名称或所有者...' : '搜索名称...'"
|
||||
type="text"
|
||||
@input="currentPage = 1"
|
||||
/>
|
||||
@@ -136,8 +137,35 @@
|
||||
/>
|
||||
<span class="relative">刷新</span>
|
||||
</button>
|
||||
|
||||
<!-- 批量编辑按钮 - 移到刷新按钮旁边 -->
|
||||
<button
|
||||
v-if="selectedApiKeys.length > 0"
|
||||
class="group relative flex items-center justify-center gap-2 rounded-lg border border-blue-200 bg-blue-50 px-4 py-2 text-sm font-medium text-blue-700 shadow-sm transition-all duration-200 hover:border-blue-300 hover:bg-blue-100 hover:shadow-md dark:border-blue-700 dark:bg-blue-900/30 dark:text-blue-300 dark:hover:bg-blue-900/50 sm:w-auto"
|
||||
@click="openBatchEditModal()"
|
||||
>
|
||||
<div
|
||||
class="absolute -inset-0.5 rounded-lg bg-gradient-to-r from-blue-500 to-indigo-500 opacity-0 blur transition duration-300 group-hover:opacity-20"
|
||||
></div>
|
||||
<i class="fas fa-edit relative text-blue-600 dark:text-blue-400" />
|
||||
<span class="relative">编辑选中 ({{ selectedApiKeys.length }})</span>
|
||||
</button>
|
||||
|
||||
<!-- 批量删除按钮 - 移到刷新按钮旁边 -->
|
||||
<button
|
||||
v-if="selectedApiKeys.length > 0"
|
||||
class="group relative flex items-center justify-center gap-2 rounded-lg border border-red-200 bg-red-50 px-4 py-2 text-sm font-medium text-red-700 shadow-sm transition-all duration-200 hover:border-red-300 hover:bg-red-100 hover:shadow-md dark:border-red-700 dark:bg-red-900/30 dark:text-red-300 dark:hover:bg-red-900/50 sm:w-auto"
|
||||
@click="batchDeleteApiKeys()"
|
||||
>
|
||||
<div
|
||||
class="absolute -inset-0.5 rounded-lg bg-gradient-to-r from-red-500 to-pink-500 opacity-0 blur transition duration-300 group-hover:opacity-20"
|
||||
></div>
|
||||
<i class="fas fa-trash relative text-red-600 dark:text-red-400" />
|
||||
<span class="relative">删除选中 ({{ selectedApiKeys.length }})</span>
|
||||
</button>
|
||||
</div>
|
||||
<!-- 创建按钮 -->
|
||||
|
||||
<!-- 创建按钮 - 独立在右侧 -->
|
||||
<button
|
||||
class="flex w-full items-center justify-center gap-2 rounded-lg bg-gradient-to-r from-blue-500 to-blue-600 px-5 py-2.5 text-sm font-medium text-white shadow-md transition-all duration-200 hover:from-blue-600 hover:to-blue-700 hover:shadow-lg sm:w-auto"
|
||||
@click.stop="openCreateApiKeyModal"
|
||||
@@ -145,32 +173,6 @@
|
||||
<i class="fas fa-plus"></i>
|
||||
<span>创建新 Key</span>
|
||||
</button>
|
||||
|
||||
<!-- 批量编辑按钮 -->
|
||||
<button
|
||||
v-if="selectedApiKeys.length > 0"
|
||||
class="group relative flex items-center justify-center gap-2 rounded-lg border border-blue-200 bg-blue-50 px-4 py-2 text-sm font-medium text-blue-700 shadow-sm transition-all duration-200 hover:border-blue-300 hover:bg-blue-100 hover:shadow-md dark:border-blue-700 dark:bg-blue-900/30 dark:text-blue-300 dark:hover:bg-blue-900/50 sm:w-auto"
|
||||
@click="openBatchEditModal()"
|
||||
>
|
||||
<div
|
||||
class="absolute -inset-0.5 rounded-lg bg-gradient-to-r from-blue-500 to-indigo-500 opacity-0 blur transition duration-300 group-hover:opacity-20"
|
||||
></div>
|
||||
<i class="fas fa-edit relative text-blue-600 dark:text-blue-400" />
|
||||
<span class="relative">编辑选中 ({{ selectedApiKeys.length }})</span>
|
||||
</button>
|
||||
|
||||
<!-- 批量删除按钮 -->
|
||||
<button
|
||||
v-if="selectedApiKeys.length > 0"
|
||||
class="group relative flex items-center justify-center gap-2 rounded-lg border border-red-200 bg-red-50 px-4 py-2 text-sm font-medium text-red-700 shadow-sm transition-all duration-200 hover:border-red-300 hover:bg-red-100 hover:shadow-md dark:border-red-700 dark:bg-red-900/30 dark:text-red-300 dark:hover:bg-red-900/50 sm:w-auto"
|
||||
@click="batchDeleteApiKeys()"
|
||||
>
|
||||
<div
|
||||
class="absolute -inset-0.5 rounded-lg bg-gradient-to-r from-red-500 to-pink-500 opacity-0 blur transition duration-300 group-hover:opacity-20"
|
||||
></div>
|
||||
<i class="fas fa-trash relative text-red-600 dark:text-red-400" />
|
||||
<span class="relative">删除选中 ({{ selectedApiKeys.length }})</span>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div v-if="apiKeysLoading" class="py-12 text-center">
|
||||
@@ -242,22 +244,39 @@
|
||||
<th
|
||||
class="w-[17%] min-w-[140px] px-3 py-4 text-left text-xs font-bold uppercase tracking-wider text-gray-700 dark:text-gray-300"
|
||||
>
|
||||
使用统计
|
||||
<span
|
||||
class="cursor-pointer rounded px-2 py-1 hover:bg-gray-100 dark:hover:bg-gray-600"
|
||||
@click="sortApiKeys('cost')"
|
||||
>
|
||||
(费用
|
||||
<i
|
||||
v-if="apiKeysSortBy === 'cost'"
|
||||
:class="[
|
||||
'fas',
|
||||
apiKeysSortOrder === 'asc' ? 'fa-sort-up' : 'fa-sort-down',
|
||||
'ml-1'
|
||||
]"
|
||||
/>
|
||||
<i v-else class="fas fa-sort ml-1 text-gray-400" />)
|
||||
</span>
|
||||
<div class="flex items-center gap-2">
|
||||
<span>使用统计</span>
|
||||
<span
|
||||
class="cursor-pointer rounded px-1.5 py-0.5 text-xs normal-case hover:bg-gray-100 dark:hover:bg-gray-600"
|
||||
@click="sortApiKeys('dailyCost')"
|
||||
>
|
||||
今日费用
|
||||
<i
|
||||
v-if="apiKeysSortBy === 'dailyCost'"
|
||||
:class="[
|
||||
'fas',
|
||||
apiKeysSortOrder === 'asc' ? 'fa-sort-up' : 'fa-sort-down',
|
||||
'ml-0.5 text-[10px]'
|
||||
]"
|
||||
/>
|
||||
<i v-else class="fas fa-sort ml-0.5 text-[10px] text-gray-400" />
|
||||
</span>
|
||||
<span
|
||||
class="cursor-pointer rounded px-1.5 py-0.5 text-xs normal-case hover:bg-gray-100 dark:hover:bg-gray-600"
|
||||
@click="sortApiKeys('totalCost')"
|
||||
>
|
||||
总费用
|
||||
<i
|
||||
v-if="apiKeysSortBy === 'totalCost'"
|
||||
:class="[
|
||||
'fas',
|
||||
apiKeysSortOrder === 'asc' ? 'fa-sort-up' : 'fa-sort-down',
|
||||
'ml-0.5 text-[10px]'
|
||||
]"
|
||||
/>
|
||||
<i v-else class="fas fa-sort ml-0.5 text-[10px] text-gray-400" />
|
||||
</span>
|
||||
</div>
|
||||
</th>
|
||||
<th
|
||||
class="w-[10%] min-w-[90px] cursor-pointer px-3 py-4 text-left text-xs font-bold uppercase tracking-wider text-gray-700 hover:bg-gray-100 dark:text-gray-300 dark:hover:bg-gray-600"
|
||||
@@ -402,6 +421,14 @@
|
||||
使用共享池
|
||||
</div>
|
||||
</div>
|
||||
<!-- 显示所有者信息 -->
|
||||
<div
|
||||
v-if="isLdapEnabled && key.ownerDisplayName"
|
||||
class="mt-1 text-xs text-red-600"
|
||||
>
|
||||
<i class="fas fa-user mr-1" />
|
||||
{{ key.ownerDisplayName }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
@@ -455,6 +482,12 @@
|
||||
>${{ (key.dailyCost || 0).toFixed(4) }}</span
|
||||
>
|
||||
</div>
|
||||
<div class="flex items-center justify-between text-sm">
|
||||
<span class="text-gray-600 dark:text-gray-400">总费用</span>
|
||||
<span class="font-semibold text-blue-600"
|
||||
>${{ (key.totalCost || 0).toFixed(4) }}</span
|
||||
>
|
||||
</div>
|
||||
<div class="flex items-center justify-between text-sm">
|
||||
<span class="text-gray-600 dark:text-gray-400">最后使用</span>
|
||||
<span class="font-medium text-gray-700 dark:text-gray-300">{{
|
||||
@@ -537,7 +570,16 @@
|
||||
</td>
|
||||
<td class="whitespace-nowrap px-3 py-4 text-sm">
|
||||
<div class="inline-flex items-center gap-1.5">
|
||||
<span v-if="key.expiresAt">
|
||||
<!-- 未激活状态 -->
|
||||
<span
|
||||
v-if="key.expirationMode === 'activation' && !key.isActivated"
|
||||
class="inline-flex items-center text-blue-600 dark:text-blue-400"
|
||||
>
|
||||
<i class="fas fa-pause-circle mr-1" />
|
||||
未激活 ({{ key.activationDays || 30 }}天)
|
||||
</span>
|
||||
<!-- 已设置过期时间 -->
|
||||
<span v-else-if="key.expiresAt">
|
||||
<span
|
||||
v-if="isApiKeyExpired(key.expiresAt)"
|
||||
class="inline-flex items-center text-red-600"
|
||||
@@ -556,6 +598,7 @@
|
||||
{{ formatExpireDate(key.expiresAt) }}
|
||||
</span>
|
||||
</span>
|
||||
<!-- 永不过期 -->
|
||||
<span
|
||||
v-else
|
||||
class="inline-flex items-center text-gray-400 dark:text-gray-500"
|
||||
@@ -1023,6 +1066,11 @@
|
||||
<i class="fas fa-share-alt mr-1" />
|
||||
使用共享池
|
||||
</div>
|
||||
<!-- 显示所有者信息 -->
|
||||
<div v-if="isLdapEnabled && key.ownerDisplayName" class="text-xs text-red-600">
|
||||
<i class="fas fa-user mr-1" />
|
||||
{{ key.ownerDisplayName }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 统计信息 -->
|
||||
@@ -1302,131 +1350,176 @@
|
||||
</div>
|
||||
|
||||
<!-- 已删除的 API Keys 表格 -->
|
||||
<div v-else class="table-container">
|
||||
<table class="w-full table-fixed">
|
||||
<thead class="bg-gray-50/80 backdrop-blur-sm dark:bg-gray-700/80">
|
||||
<tr>
|
||||
<th
|
||||
class="w-[20%] min-w-[150px] px-3 py-4 text-left text-xs font-bold uppercase tracking-wider text-gray-700 dark:text-gray-300"
|
||||
>
|
||||
名称
|
||||
</th>
|
||||
<th
|
||||
class="w-[15%] min-w-[120px] px-3 py-4 text-left text-xs font-bold uppercase tracking-wider text-gray-700 dark:text-gray-300"
|
||||
>
|
||||
创建者
|
||||
</th>
|
||||
<th
|
||||
class="w-[15%] min-w-[120px] px-3 py-4 text-left text-xs font-bold uppercase tracking-wider text-gray-700 dark:text-gray-300"
|
||||
>
|
||||
创建时间
|
||||
</th>
|
||||
<th
|
||||
class="w-[15%] min-w-[120px] px-3 py-4 text-left text-xs font-bold uppercase tracking-wider text-gray-700 dark:text-gray-300"
|
||||
>
|
||||
删除者
|
||||
</th>
|
||||
<th
|
||||
class="w-[15%] min-w-[120px] px-3 py-4 text-left text-xs font-bold uppercase tracking-wider text-gray-700 dark:text-gray-300"
|
||||
>
|
||||
删除时间
|
||||
</th>
|
||||
<th
|
||||
class="w-[20%] min-w-[150px] px-3 py-4 text-left text-xs font-bold uppercase tracking-wider text-gray-700 dark:text-gray-300"
|
||||
>
|
||||
使用统计
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="divide-y divide-gray-200/50 dark:divide-gray-600/50">
|
||||
<tr v-for="key in deletedApiKeys" :key="key.id" class="table-row">
|
||||
<td class="px-3 py-4">
|
||||
<div class="flex items-center">
|
||||
<div
|
||||
class="mr-2 flex h-8 w-8 flex-shrink-0 items-center justify-center rounded-lg bg-gradient-to-br from-red-500 to-red-600"
|
||||
>
|
||||
<i class="fas fa-trash text-xs text-white" />
|
||||
</div>
|
||||
<div class="min-w-0">
|
||||
<div v-else>
|
||||
<!-- 工具栏 -->
|
||||
<div class="mb-4 flex justify-end">
|
||||
<button
|
||||
v-if="deletedApiKeys.length > 0"
|
||||
class="rounded-lg bg-red-600 px-4 py-2 text-sm font-medium text-white transition-colors hover:bg-red-700 dark:bg-red-600 dark:hover:bg-red-700"
|
||||
@click="clearAllDeletedApiKeys"
|
||||
>
|
||||
<i class="fas fa-trash-alt mr-2" />
|
||||
清空所有已删除 ({{ deletedApiKeys.length }})
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="table-container">
|
||||
<table class="w-full table-fixed">
|
||||
<thead class="bg-gray-50/80 backdrop-blur-sm dark:bg-gray-700/80">
|
||||
<tr>
|
||||
<th
|
||||
class="w-[20%] min-w-[150px] px-3 py-4 text-left text-xs font-bold uppercase tracking-wider text-gray-700 dark:text-gray-300"
|
||||
>
|
||||
名称
|
||||
</th>
|
||||
<th
|
||||
v-if="isLdapEnabled"
|
||||
class="w-[15%] min-w-[120px] px-3 py-4 text-left text-xs font-bold uppercase tracking-wider text-gray-700 dark:text-gray-300"
|
||||
>
|
||||
创建者
|
||||
</th>
|
||||
<th
|
||||
class="w-[15%] min-w-[120px] px-3 py-4 text-left text-xs font-bold uppercase tracking-wider text-gray-700 dark:text-gray-300"
|
||||
>
|
||||
创建时间
|
||||
</th>
|
||||
<th
|
||||
class="w-[15%] min-w-[120px] px-3 py-4 text-left text-xs font-bold uppercase tracking-wider text-gray-700 dark:text-gray-300"
|
||||
>
|
||||
删除者
|
||||
</th>
|
||||
<th
|
||||
class="w-[15%] min-w-[120px] px-3 py-4 text-left text-xs font-bold uppercase tracking-wider text-gray-700 dark:text-gray-300"
|
||||
>
|
||||
删除时间
|
||||
</th>
|
||||
<th
|
||||
class="w-[20%] min-w-[150px] px-3 py-4 text-left text-xs font-bold uppercase tracking-wider text-gray-700 dark:text-gray-300"
|
||||
>
|
||||
使用统计
|
||||
</th>
|
||||
<th
|
||||
class="w-[10%] min-w-[100px] px-3 py-4 text-left text-xs font-bold uppercase tracking-wider text-gray-700 dark:text-gray-300"
|
||||
>
|
||||
操作
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="divide-y divide-gray-200/50 dark:divide-gray-600/50">
|
||||
<tr v-for="key in deletedApiKeys" :key="key.id" class="table-row">
|
||||
<td class="px-3 py-4">
|
||||
<div class="flex items-center">
|
||||
<div
|
||||
class="truncate text-sm font-semibold text-gray-900 dark:text-gray-100"
|
||||
:title="key.name"
|
||||
class="mr-2 flex h-8 w-8 flex-shrink-0 items-center justify-center rounded-lg bg-gradient-to-br from-red-500 to-red-600"
|
||||
>
|
||||
{{ key.name }}
|
||||
<i class="fas fa-trash text-xs text-white" />
|
||||
</div>
|
||||
<div
|
||||
class="truncate text-xs text-gray-500 dark:text-gray-400"
|
||||
:title="key.id"
|
||||
>
|
||||
{{ key.id }}
|
||||
<div class="min-w-0">
|
||||
<div
|
||||
class="truncate text-sm font-semibold text-gray-900 dark:text-gray-100"
|
||||
:title="key.name"
|
||||
>
|
||||
{{ key.name }}
|
||||
</div>
|
||||
<div
|
||||
class="truncate text-xs text-gray-500 dark:text-gray-400"
|
||||
:title="key.id"
|
||||
>
|
||||
{{ key.id }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
<td class="px-3 py-4">
|
||||
<div class="text-sm">
|
||||
<span v-if="key.createdBy === 'admin'" class="text-blue-600">
|
||||
<i class="fas fa-user-shield mr-1" />
|
||||
管理员
|
||||
</span>
|
||||
<span v-else-if="key.userUsername" class="text-green-600">
|
||||
<i class="fas fa-user mr-1" />
|
||||
{{ key.userUsername }}
|
||||
</span>
|
||||
<span v-else class="text-gray-500 dark:text-gray-400">
|
||||
<i class="fas fa-question-circle mr-1" />
|
||||
未知
|
||||
</span>
|
||||
</div>
|
||||
</td>
|
||||
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500 dark:text-gray-400">
|
||||
{{ formatDate(key.createdAt) }}
|
||||
</td>
|
||||
<td class="px-3 py-4">
|
||||
<div class="text-sm">
|
||||
<span v-if="key.deletedByType === 'admin'" class="text-blue-600">
|
||||
<i class="fas fa-user-shield mr-1" />
|
||||
{{ key.deletedBy }}
|
||||
</span>
|
||||
<span v-else-if="key.deletedByType === 'user'" class="text-green-600">
|
||||
<i class="fas fa-user mr-1" />
|
||||
{{ key.deletedBy }}
|
||||
</span>
|
||||
<span v-else class="text-gray-500 dark:text-gray-400">
|
||||
<i class="fas fa-cog mr-1" />
|
||||
{{ key.deletedBy }}
|
||||
</span>
|
||||
</div>
|
||||
</td>
|
||||
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500 dark:text-gray-400">
|
||||
{{ formatDate(key.deletedAt) }}
|
||||
</td>
|
||||
<td class="px-3 py-4">
|
||||
<div class="text-sm">
|
||||
<div class="flex items-center justify-between">
|
||||
<span class="text-gray-600 dark:text-gray-400">请求</span>
|
||||
<span class="font-semibold text-gray-900 dark:text-gray-100">
|
||||
{{ formatNumber(key.usage?.total?.requests || 0) }}次
|
||||
</td>
|
||||
<td v-if="isLdapEnabled" class="px-3 py-4">
|
||||
<div class="text-sm">
|
||||
<span v-if="key.createdBy === 'admin'" class="text-blue-600">
|
||||
<i class="fas fa-user-shield mr-1" />
|
||||
管理员
|
||||
</span>
|
||||
<span v-else-if="key.userUsername" class="text-green-600">
|
||||
<i class="fas fa-user mr-1" />
|
||||
{{ key.userUsername }}
|
||||
</span>
|
||||
<span v-else class="text-gray-500 dark:text-gray-400">
|
||||
<i class="fas fa-question-circle mr-1" />
|
||||
未知
|
||||
</span>
|
||||
</div>
|
||||
<div class="flex items-center justify-between">
|
||||
<span class="text-gray-600 dark:text-gray-400">费用</span>
|
||||
<span class="font-semibold text-green-600">
|
||||
${{ (key.usage?.total?.cost || 0).toFixed(4) }}
|
||||
</td>
|
||||
<td
|
||||
class="whitespace-nowrap px-3 py-4 text-sm text-gray-500 dark:text-gray-400"
|
||||
>
|
||||
{{ formatDate(key.createdAt) }}
|
||||
</td>
|
||||
<td class="px-3 py-4">
|
||||
<div class="text-sm">
|
||||
<span v-if="key.deletedByType === 'admin'" class="text-blue-600">
|
||||
<i class="fas fa-user-shield mr-1" />
|
||||
{{ key.deletedBy }}
|
||||
</span>
|
||||
<span v-else-if="key.deletedByType === 'user'" class="text-green-600">
|
||||
<i class="fas fa-user mr-1" />
|
||||
{{ key.deletedBy }}
|
||||
</span>
|
||||
<span v-else class="text-gray-500 dark:text-gray-400">
|
||||
<i class="fas fa-cog mr-1" />
|
||||
{{ key.deletedBy }}
|
||||
</span>
|
||||
</div>
|
||||
<div v-if="key.lastUsedAt" class="flex items-center justify-between">
|
||||
<span class="text-gray-600 dark:text-gray-400">最后使用</span>
|
||||
<span class="font-medium text-gray-700 dark:text-gray-300">
|
||||
{{ formatLastUsed(key.lastUsedAt) }}
|
||||
</span>
|
||||
</td>
|
||||
<td
|
||||
class="whitespace-nowrap px-3 py-4 text-sm text-gray-500 dark:text-gray-400"
|
||||
>
|
||||
{{ formatDate(key.deletedAt) }}
|
||||
</td>
|
||||
<td class="px-3 py-4">
|
||||
<div class="text-sm">
|
||||
<div class="flex items-center justify-between">
|
||||
<span class="text-gray-600 dark:text-gray-400">请求</span>
|
||||
<span class="font-semibold text-gray-900 dark:text-gray-100">
|
||||
{{ formatNumber(key.usage?.total?.requests || 0) }}次
|
||||
</span>
|
||||
</div>
|
||||
<div class="flex items-center justify-between">
|
||||
<span class="text-gray-600 dark:text-gray-400">费用</span>
|
||||
<span class="font-semibold text-green-600">
|
||||
${{ (key.usage?.total?.cost || 0).toFixed(4) }}
|
||||
</span>
|
||||
</div>
|
||||
<div v-if="key.lastUsedAt" class="flex items-center justify-between">
|
||||
<span class="text-gray-600 dark:text-gray-400">最后使用</span>
|
||||
<span class="font-medium text-gray-700 dark:text-gray-300">
|
||||
{{ formatLastUsed(key.lastUsedAt) }}
|
||||
</span>
|
||||
</div>
|
||||
<div v-else class="text-xs text-gray-400">从未使用</div>
|
||||
</div>
|
||||
<div v-else class="text-xs text-gray-400">从未使用</div>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</td>
|
||||
<td class="px-3 py-4">
|
||||
<div class="flex items-center gap-2">
|
||||
<button
|
||||
v-if="key.canRestore"
|
||||
class="rounded-lg bg-green-50 px-3 py-1.5 text-xs font-medium text-green-600 transition-colors hover:bg-green-100 dark:bg-green-900/30 dark:text-green-400 dark:hover:bg-green-900/50"
|
||||
title="恢复 API Key"
|
||||
@click="restoreApiKey(key.id)"
|
||||
>
|
||||
<i class="fas fa-undo mr-1" />
|
||||
恢复
|
||||
</button>
|
||||
<button
|
||||
class="rounded-lg bg-red-50 px-3 py-1.5 text-xs font-medium text-red-600 transition-colors hover:bg-red-100 dark:bg-red-900/30 dark:text-red-400 dark:hover:bg-red-900/50"
|
||||
title="彻底删除 API Key"
|
||||
@click="permanentDeleteApiKey(key.id)"
|
||||
>
|
||||
<i class="fas fa-times mr-1" />
|
||||
彻底删除
|
||||
</button>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -1499,6 +1592,7 @@ import { ref, computed, onMounted, watch } from 'vue'
|
||||
import { showToast } from '@/utils/toast'
|
||||
import { apiClient } from '@/config/api'
|
||||
import { useClientsStore } from '@/stores/clients'
|
||||
import { useAuthStore } from '@/stores/auth'
|
||||
import CreateApiKeyModal from '@/components/apikeys/CreateApiKeyModal.vue'
|
||||
import EditApiKeyModal from '@/components/apikeys/EditApiKeyModal.vue'
|
||||
import RenewApiKeyModal from '@/components/apikeys/RenewApiKeyModal.vue'
|
||||
@@ -1512,8 +1606,12 @@ import CustomDropdown from '@/components/common/CustomDropdown.vue'
|
||||
|
||||
// 响应式数据
|
||||
const clientsStore = useClientsStore()
|
||||
const authStore = useAuthStore()
|
||||
const apiKeys = ref([])
|
||||
|
||||
// 获取 LDAP 启用状态
|
||||
const isLdapEnabled = computed(() => authStore.oemSettings?.ldapEnabled || false)
|
||||
|
||||
// 多选相关状态
|
||||
const selectedApiKeys = ref([])
|
||||
const selectAllChecked = ref(false)
|
||||
@@ -1525,8 +1623,8 @@ const apiKeyStatsTimeRange = ref('today')
|
||||
const activeTab = ref('active')
|
||||
const deletedApiKeys = ref([])
|
||||
const deletedApiKeysLoading = ref(false)
|
||||
const apiKeysSortBy = ref('')
|
||||
const apiKeysSortOrder = ref('asc')
|
||||
const apiKeysSortBy = ref('dailyCost')
|
||||
const apiKeysSortOrder = ref('desc')
|
||||
const expandedApiKeys = ref({})
|
||||
const apiKeyModelStats = ref({})
|
||||
const apiKeyDateFilters = ref({})
|
||||
@@ -1601,12 +1699,22 @@ const sortedApiKeys = computed(() => {
|
||||
)
|
||||
}
|
||||
|
||||
// 然后进行名称搜索
|
||||
// 然后进行名称搜索(搜索API Key名称和所有者名称)
|
||||
if (searchKeyword.value) {
|
||||
const keyword = searchKeyword.value.toLowerCase().trim()
|
||||
filteredKeys = filteredKeys.filter(
|
||||
(key) => key.name && key.name.toLowerCase().includes(keyword)
|
||||
)
|
||||
filteredKeys = filteredKeys.filter((key) => {
|
||||
// 搜索API Key名称
|
||||
const nameMatch = key.name && key.name.toLowerCase().includes(keyword)
|
||||
// 如果启用了 LDAP,搜索所有者名称
|
||||
if (isLdapEnabled.value) {
|
||||
const ownerMatch =
|
||||
key.ownerDisplayName && key.ownerDisplayName.toLowerCase().includes(keyword)
|
||||
// 如果API Key名称或所有者名称匹配,则包含该条目
|
||||
return nameMatch || ownerMatch
|
||||
}
|
||||
// 未启用 LDAP 时只搜索名称
|
||||
return nameMatch
|
||||
})
|
||||
}
|
||||
|
||||
// 如果没有排序字段,返回筛选后的结果
|
||||
@@ -1621,9 +1729,12 @@ const sortedApiKeys = computed(() => {
|
||||
if (apiKeysSortBy.value === 'status') {
|
||||
aVal = a.isActive ? 1 : 0
|
||||
bVal = b.isActive ? 1 : 0
|
||||
} else if (apiKeysSortBy.value === 'cost') {
|
||||
aVal = parseFloat(calculateApiKeyCost(a.usage).replace('$', ''))
|
||||
bVal = parseFloat(calculateApiKeyCost(b.usage).replace('$', ''))
|
||||
} else if (apiKeysSortBy.value === 'dailyCost') {
|
||||
aVal = a.dailyCost || 0
|
||||
bVal = b.dailyCost || 0
|
||||
} else if (apiKeysSortBy.value === 'totalCost') {
|
||||
aVal = a.totalCost || 0
|
||||
bVal = b.totalCost || 0
|
||||
} else if (apiKeysSortBy.value === 'createdAt' || apiKeysSortBy.value === 'expiresAt') {
|
||||
aVal = aVal ? new Date(aVal).getTime() : 0
|
||||
bVal = bVal ? new Date(bVal).getTime() : 0
|
||||
@@ -1808,13 +1919,6 @@ const formatNumber = (num) => {
|
||||
return num.toLocaleString('zh-CN')
|
||||
}
|
||||
|
||||
// 计算API Key费用
|
||||
const calculateApiKeyCost = (usage) => {
|
||||
if (!usage || !usage.total) return '$0.0000'
|
||||
const cost = usage.total.cost || 0
|
||||
return `$${cost.toFixed(4)}`
|
||||
}
|
||||
|
||||
// 获取绑定账户名称
|
||||
const getBoundAccountName = (accountId) => {
|
||||
if (!accountId) return '未知账户'
|
||||
@@ -2309,6 +2413,118 @@ const deleteApiKey = async (keyId) => {
|
||||
}
|
||||
}
|
||||
|
||||
// 恢复API Key
|
||||
const restoreApiKey = async (keyId) => {
|
||||
let confirmed = false
|
||||
|
||||
if (window.showConfirm) {
|
||||
confirmed = await window.showConfirm(
|
||||
'恢复 API Key',
|
||||
'确定要恢复这个 API Key 吗?恢复后可以重新使用。',
|
||||
'确定恢复',
|
||||
'取消'
|
||||
)
|
||||
} else {
|
||||
// 降级方案
|
||||
confirmed = confirm('确定要恢复这个 API Key 吗?恢复后可以重新使用。')
|
||||
}
|
||||
|
||||
if (!confirmed) return
|
||||
|
||||
try {
|
||||
const data = await apiClient.post(`/admin/api-keys/${keyId}/restore`)
|
||||
if (data.success) {
|
||||
showToast('API Key 已成功恢复', 'success')
|
||||
// 刷新已删除列表
|
||||
await loadDeletedApiKeys()
|
||||
// 同时刷新活跃列表
|
||||
await loadApiKeys()
|
||||
} else {
|
||||
showToast(data.error || '恢复失败', 'error')
|
||||
}
|
||||
} catch (error) {
|
||||
showToast(error.response?.data?.error || '恢复失败', 'error')
|
||||
}
|
||||
}
|
||||
|
||||
// 彻底删除API Key
|
||||
const permanentDeleteApiKey = async (keyId) => {
|
||||
let confirmed = false
|
||||
|
||||
if (window.showConfirm) {
|
||||
confirmed = await window.showConfirm(
|
||||
'彻底删除 API Key',
|
||||
'确定要彻底删除这个 API Key 吗?此操作不可恢复,所有相关数据将被永久删除。',
|
||||
'确定彻底删除',
|
||||
'取消'
|
||||
)
|
||||
} else {
|
||||
// 降级方案
|
||||
confirmed = confirm('确定要彻底删除这个 API Key 吗?此操作不可恢复,所有相关数据将被永久删除。')
|
||||
}
|
||||
|
||||
if (!confirmed) return
|
||||
|
||||
try {
|
||||
const data = await apiClient.delete(`/admin/api-keys/${keyId}/permanent`)
|
||||
if (data.success) {
|
||||
showToast('API Key 已彻底删除', 'success')
|
||||
// 刷新已删除列表
|
||||
loadDeletedApiKeys()
|
||||
} else {
|
||||
showToast(data.error || '彻底删除失败', 'error')
|
||||
}
|
||||
} catch (error) {
|
||||
showToast(error.response?.data?.error || '彻底删除失败', 'error')
|
||||
}
|
||||
}
|
||||
|
||||
// 清空所有已删除的API Keys
|
||||
const clearAllDeletedApiKeys = async () => {
|
||||
const count = deletedApiKeys.value.length
|
||||
if (count === 0) {
|
||||
showToast('没有需要清空的 API Keys', 'info')
|
||||
return
|
||||
}
|
||||
|
||||
let confirmed = false
|
||||
|
||||
if (window.showConfirm) {
|
||||
confirmed = await window.showConfirm(
|
||||
'清空所有已删除的 API Keys',
|
||||
`确定要彻底删除全部 ${count} 个已删除的 API Keys 吗?此操作不可恢复,所有相关数据将被永久删除。`,
|
||||
'确定清空全部',
|
||||
'取消'
|
||||
)
|
||||
} else {
|
||||
// 降级方案
|
||||
confirmed = confirm(`确定要彻底删除全部 ${count} 个已删除的 API Keys 吗?此操作不可恢复。`)
|
||||
}
|
||||
|
||||
if (!confirmed) return
|
||||
|
||||
try {
|
||||
const data = await apiClient.delete('/admin/api-keys/deleted/clear-all')
|
||||
if (data.success) {
|
||||
showToast(data.message || '已清空所有已删除的 API Keys', 'success')
|
||||
|
||||
// 如果有失败的,显示详细信息
|
||||
if (data.details && data.details.failedCount > 0) {
|
||||
const errors = data.details.errors
|
||||
console.error('部分API Keys清空失败:', errors)
|
||||
showToast(`${data.details.failedCount} 个清空失败,请查看控制台`, 'warning')
|
||||
}
|
||||
|
||||
// 刷新已删除列表
|
||||
loadDeletedApiKeys()
|
||||
} else {
|
||||
showToast(data.error || '清空失败', 'error')
|
||||
}
|
||||
} catch (error) {
|
||||
showToast(error.response?.data?.error || '清空失败', 'error')
|
||||
}
|
||||
}
|
||||
|
||||
// 批量删除API Keys
|
||||
const batchDeleteApiKeys = async () => {
|
||||
const selectedCount = selectedApiKeys.value.length
|
||||
@@ -2444,18 +2660,29 @@ const closeExpiryEdit = () => {
|
||||
}
|
||||
|
||||
// 保存过期时间
|
||||
const handleSaveExpiry = async ({ keyId, expiresAt }) => {
|
||||
const handleSaveExpiry = async ({ keyId, expiresAt, activateNow }) => {
|
||||
try {
|
||||
const data = await apiClient.put(`/admin/api-keys/${keyId}`, {
|
||||
expiresAt: expiresAt || null
|
||||
// 使用新的PATCH端点来修改过期时间
|
||||
const data = await apiClient.patch(`/admin/api-keys/${keyId}/expiration`, {
|
||||
expiresAt: expiresAt || null,
|
||||
activateNow: activateNow || false
|
||||
})
|
||||
|
||||
if (data.success) {
|
||||
showToast('过期时间已更新', 'success')
|
||||
showToast(activateNow ? 'API Key已激活' : '过期时间已更新', 'success')
|
||||
// 更新本地数据
|
||||
const key = apiKeys.value.find((k) => k.id === keyId)
|
||||
if (key) {
|
||||
key.expiresAt = expiresAt || null
|
||||
if (activateNow && data.updates) {
|
||||
key.isActivated = true
|
||||
key.activatedAt = data.updates.activatedAt
|
||||
key.expiresAt = data.updates.expiresAt
|
||||
} else {
|
||||
key.expiresAt = expiresAt || null
|
||||
if (expiresAt && !key.isActivated) {
|
||||
key.isActivated = true
|
||||
}
|
||||
}
|
||||
}
|
||||
closeExpiryEdit()
|
||||
} else {
|
||||
|
||||
@@ -123,7 +123,10 @@
|
||||
<!-- Token 分布和限制配置 -->
|
||||
<div class="mb-6 grid grid-cols-1 gap-4 md:mb-8 md:gap-6 lg:grid-cols-2">
|
||||
<TokenDistribution />
|
||||
<LimitConfig />
|
||||
<!-- 单key模式下显示限制配置 -->
|
||||
<LimitConfig v-if="!multiKeyMode" />
|
||||
<!-- 多key模式下显示聚合统计卡片,填充右侧空白 -->
|
||||
<AggregatedStatsCard v-if="multiKeyMode" />
|
||||
</div>
|
||||
|
||||
<!-- 模型使用统计 -->
|
||||
@@ -153,6 +156,7 @@ import ApiKeyInput from '@/components/apistats/ApiKeyInput.vue'
|
||||
import StatsOverview from '@/components/apistats/StatsOverview.vue'
|
||||
import TokenDistribution from '@/components/apistats/TokenDistribution.vue'
|
||||
import LimitConfig from '@/components/apistats/LimitConfig.vue'
|
||||
import AggregatedStatsCard from '@/components/apistats/AggregatedStatsCard.vue'
|
||||
import ModelUsageStats from '@/components/apistats/ModelUsageStats.vue'
|
||||
import TutorialView from './TutorialView.vue'
|
||||
|
||||
@@ -175,7 +179,8 @@ const {
|
||||
error,
|
||||
statsPeriod,
|
||||
statsData,
|
||||
oemSettings
|
||||
oemSettings,
|
||||
multiKeyMode
|
||||
} = storeToRefs(apiStatsStore)
|
||||
|
||||
const { queryStats, switchPeriod, loadStatsWithApiId, loadOemSettings, reset } = apiStatsStore
|
||||
|
||||
@@ -420,60 +420,42 @@
|
||||
</p>
|
||||
|
||||
<div class="space-y-4">
|
||||
<div class="rounded-lg border border-indigo-200 bg-white p-3 sm:p-4">
|
||||
<h6 class="mb-2 text-sm font-medium text-gray-800 dark:text-gray-600 sm:text-base">
|
||||
PowerShell 设置方法
|
||||
</h6>
|
||||
<p class="mb-3 text-sm text-gray-600">在 PowerShell 中运行以下命令:</p>
|
||||
<div class="rounded-lg border border-yellow-200 bg-yellow-50 p-3 sm:p-4">
|
||||
<h6 class="mb-2 font-medium text-yellow-800">Codex 配置文件</h6>
|
||||
<p class="mb-3 text-sm text-yellow-700">
|
||||
在
|
||||
<code class="rounded bg-yellow-100 px-1">~/.codex/config.toml</code>
|
||||
文件中添加以下配置:
|
||||
</p>
|
||||
<div
|
||||
class="overflow-x-auto rounded bg-gray-900 p-2 font-mono text-xs text-green-400 sm:p-3 sm:text-sm"
|
||||
>
|
||||
<div class="whitespace-nowrap text-gray-300">
|
||||
$env:OPENAI_BASE_URL = "{{ openaiBaseUrl }}"
|
||||
</div>
|
||||
<div class="whitespace-nowrap text-gray-300">
|
||||
$env:OPENAI_API_KEY = "你的API密钥"
|
||||
</div>
|
||||
<div class="whitespace-nowrap text-gray-300">model_provider = "crs"</div>
|
||||
<div class="whitespace-nowrap text-gray-300">model = "gpt-5"</div>
|
||||
<div class="whitespace-nowrap text-gray-300">model_reasoning_effort = "high"</div>
|
||||
<div class="whitespace-nowrap text-gray-300">disable_response_storage = true</div>
|
||||
<div class="mt-2"></div>
|
||||
<div class="whitespace-nowrap text-gray-300">[model_providers.crs]</div>
|
||||
<div class="whitespace-nowrap text-gray-300">name = "crs"</div>
|
||||
<div class="whitespace-nowrap text-gray-300">base_url = "{{ openaiBaseUrl }}"</div>
|
||||
<div class="whitespace-nowrap text-gray-300">wire_api = "responses"</div>
|
||||
</div>
|
||||
<p class="mt-3 text-sm text-yellow-700">
|
||||
在
|
||||
<code class="rounded bg-yellow-100 px-1">~/.codex/auth.json</code>
|
||||
文件中配置API密钥:
|
||||
</p>
|
||||
<div
|
||||
class="mt-2 overflow-x-auto rounded bg-gray-900 p-2 font-mono text-xs text-green-400 sm:p-3 sm:text-sm"
|
||||
>
|
||||
<div class="whitespace-nowrap text-gray-300">{</div>
|
||||
<div class="whitespace-nowrap text-gray-300">"OPENAI_API_KEY": "你的API密钥"</div>
|
||||
<div class="whitespace-nowrap text-gray-300">}</div>
|
||||
</div>
|
||||
<p class="mt-2 text-xs text-yellow-700">
|
||||
💡 使用与 Claude Code 相同的 API 密钥即可,格式如 cr_xxxxxxxxxx。
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="rounded-lg border border-indigo-200 bg-white p-3 sm:p-4">
|
||||
<h6 class="mb-2 text-sm font-medium text-gray-800 dark:text-gray-600 sm:text-base">
|
||||
PowerShell 永久设置(用户级)
|
||||
</h6>
|
||||
<p class="mb-3 text-sm text-gray-600">在 PowerShell 中运行以下命令:</p>
|
||||
<div
|
||||
class="mb-3 overflow-x-auto rounded bg-gray-900 p-2 font-mono text-xs text-green-400 sm:p-3 sm:text-sm"
|
||||
>
|
||||
<div class="mb-2"># 设置用户级环境变量(永久生效)</div>
|
||||
<div class="whitespace-nowrap text-gray-300">
|
||||
[System.Environment]::SetEnvironmentVariable("OPENAI_BASE_URL", "{{
|
||||
openaiBaseUrl
|
||||
}}", [System.EnvironmentVariableTarget]::User)
|
||||
</div>
|
||||
<div class="whitespace-nowrap text-gray-300">
|
||||
[System.Environment]::SetEnvironmentVariable("OPENAI_API_KEY", "你的API密钥",
|
||||
[System.EnvironmentVariableTarget]::User)
|
||||
</div>
|
||||
</div>
|
||||
<p class="mt-2 text-xs text-blue-700">
|
||||
💡 设置后需要重新打开 PowerShell 窗口才能生效。
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="rounded-lg border border-indigo-200 bg-indigo-50 p-3 sm:p-4">
|
||||
<h6 class="mb-2 font-medium text-indigo-800">验证 Codex 环境变量</h6>
|
||||
<p class="mb-3 text-sm text-indigo-700">在 PowerShell 中验证:</p>
|
||||
<div
|
||||
class="space-y-1 overflow-x-auto rounded bg-gray-900 p-2 font-mono text-xs text-green-400 sm:p-3 sm:text-sm"
|
||||
>
|
||||
<div class="whitespace-nowrap text-gray-300">echo $env:OPENAI_BASE_URL</div>
|
||||
<div class="whitespace-nowrap text-gray-300">echo $env:OPENAI_API_KEY</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -924,67 +906,42 @@
|
||||
</p>
|
||||
|
||||
<div class="space-y-4">
|
||||
<div class="rounded-lg border border-indigo-200 bg-white p-3 sm:p-4">
|
||||
<h6 class="mb-2 text-sm font-medium text-gray-800 dark:text-gray-600 sm:text-base">
|
||||
Terminal 设置方法
|
||||
</h6>
|
||||
<p class="mb-3 text-sm text-gray-600">在 Terminal 中运行以下命令:</p>
|
||||
<div class="rounded-lg border border-yellow-200 bg-yellow-50 p-3 sm:p-4">
|
||||
<h6 class="mb-2 font-medium text-yellow-800">Codex 配置文件</h6>
|
||||
<p class="mb-3 text-sm text-yellow-700">
|
||||
在
|
||||
<code class="rounded bg-yellow-100 px-1">~/.codex/config.toml</code>
|
||||
文件中添加以下配置:
|
||||
</p>
|
||||
<div
|
||||
class="overflow-x-auto rounded bg-gray-900 p-2 font-mono text-xs text-green-400 sm:p-3 sm:text-sm"
|
||||
>
|
||||
<div class="whitespace-nowrap text-gray-300">
|
||||
export OPENAI_BASE_URL="{{ openaiBaseUrl }}"
|
||||
</div>
|
||||
<div class="whitespace-nowrap text-gray-300">
|
||||
export OPENAI_API_KEY="你的API密钥"
|
||||
</div>
|
||||
<div class="whitespace-nowrap text-gray-300">model_provider = "crs"</div>
|
||||
<div class="whitespace-nowrap text-gray-300">model = "gpt-5"</div>
|
||||
<div class="whitespace-nowrap text-gray-300">model_reasoning_effort = "high"</div>
|
||||
<div class="whitespace-nowrap text-gray-300">disable_response_storage = true</div>
|
||||
<div class="mt-2"></div>
|
||||
<div class="whitespace-nowrap text-gray-300">[model_providers.crs]</div>
|
||||
<div class="whitespace-nowrap text-gray-300">name = "crs"</div>
|
||||
<div class="whitespace-nowrap text-gray-300">base_url = "{{ openaiBaseUrl }}"</div>
|
||||
<div class="whitespace-nowrap text-gray-300">wire_api = "responses"</div>
|
||||
</div>
|
||||
<p class="mt-3 text-sm text-yellow-700">
|
||||
在
|
||||
<code class="rounded bg-yellow-100 px-1">~/.codex/auth.json</code>
|
||||
文件中配置API密钥:
|
||||
</p>
|
||||
<div
|
||||
class="mt-2 overflow-x-auto rounded bg-gray-900 p-2 font-mono text-xs text-green-400 sm:p-3 sm:text-sm"
|
||||
>
|
||||
<div class="whitespace-nowrap text-gray-300">{</div>
|
||||
<div class="whitespace-nowrap text-gray-300">"OPENAI_API_KEY": "你的API密钥"</div>
|
||||
<div class="whitespace-nowrap text-gray-300">}</div>
|
||||
</div>
|
||||
<p class="mt-2 text-xs text-yellow-700">
|
||||
💡 使用与 Claude Code 相同的 API 密钥即可,格式如 cr_xxxxxxxxxx。
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="rounded-lg border border-indigo-200 bg-white p-3 sm:p-4">
|
||||
<h6 class="mb-2 text-sm font-medium text-gray-800 dark:text-gray-600 sm:text-base">
|
||||
永久设置方法
|
||||
</h6>
|
||||
<p class="mb-3 text-sm text-gray-600">添加到你的 shell 配置文件:</p>
|
||||
<div
|
||||
class="mb-3 overflow-x-auto rounded bg-gray-900 p-2 font-mono text-xs text-green-400 sm:p-3 sm:text-sm"
|
||||
>
|
||||
<div class="mb-2"># 对于 zsh (默认)</div>
|
||||
<div class="whitespace-nowrap text-gray-300">
|
||||
echo 'export OPENAI_BASE_URL="{{ openaiBaseUrl }}"' >> ~/.zshrc
|
||||
</div>
|
||||
<div class="whitespace-nowrap text-gray-300">
|
||||
echo 'export OPENAI_API_KEY="你的API密钥"' >> ~/.zshrc
|
||||
</div>
|
||||
<div class="whitespace-nowrap text-gray-300">source ~/.zshrc</div>
|
||||
</div>
|
||||
<div
|
||||
class="overflow-x-auto rounded bg-gray-900 p-2 font-mono text-xs text-green-400 sm:p-3 sm:text-sm"
|
||||
>
|
||||
<div class="mb-2"># 对于 bash</div>
|
||||
<div class="whitespace-nowrap text-gray-300">
|
||||
echo 'export OPENAI_BASE_URL="{{ openaiBaseUrl }}"' >> ~/.bash_profile
|
||||
</div>
|
||||
<div class="whitespace-nowrap text-gray-300">
|
||||
echo 'export OPENAI_API_KEY="你的API密钥"' >> ~/.bash_profile
|
||||
</div>
|
||||
<div class="whitespace-nowrap text-gray-300">source ~/.bash_profile</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="rounded-lg border border-indigo-200 bg-indigo-50 p-3 sm:p-4">
|
||||
<h6 class="mb-2 font-medium text-indigo-800">验证 Codex 环境变量</h6>
|
||||
<p class="mb-3 text-sm text-indigo-700">在 Terminal 中验证:</p>
|
||||
<div
|
||||
class="space-y-1 overflow-x-auto rounded bg-gray-900 p-2 font-mono text-xs text-green-400 sm:p-3 sm:text-sm"
|
||||
>
|
||||
<div class="whitespace-nowrap text-gray-300">echo $OPENAI_BASE_URL</div>
|
||||
<div class="whitespace-nowrap text-gray-300">echo $OPENAI_API_KEY</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -1426,67 +1383,42 @@
|
||||
</p>
|
||||
|
||||
<div class="space-y-4">
|
||||
<div class="rounded-lg border border-indigo-200 bg-white p-3 sm:p-4">
|
||||
<h6 class="mb-2 text-sm font-medium text-gray-800 dark:text-gray-600 sm:text-base">
|
||||
终端设置方法
|
||||
</h6>
|
||||
<p class="mb-3 text-sm text-gray-600">在终端中运行以下命令:</p>
|
||||
<div class="rounded-lg border border-yellow-200 bg-yellow-50 p-3 sm:p-4">
|
||||
<h6 class="mb-2 font-medium text-yellow-800">Codex 配置文件</h6>
|
||||
<p class="mb-3 text-sm text-yellow-700">
|
||||
在
|
||||
<code class="rounded bg-yellow-100 px-1">~/.codex/config.toml</code>
|
||||
文件中添加以下配置:
|
||||
</p>
|
||||
<div
|
||||
class="overflow-x-auto rounded bg-gray-900 p-2 font-mono text-xs text-green-400 sm:p-3 sm:text-sm"
|
||||
>
|
||||
<div class="whitespace-nowrap text-gray-300">
|
||||
export OPENAI_BASE_URL="{{ openaiBaseUrl }}"
|
||||
</div>
|
||||
<div class="whitespace-nowrap text-gray-300">
|
||||
export OPENAI_API_KEY="你的API密钥"
|
||||
</div>
|
||||
<div class="whitespace-nowrap text-gray-300">model_provider = "crs"</div>
|
||||
<div class="whitespace-nowrap text-gray-300">model = "gpt-5"</div>
|
||||
<div class="whitespace-nowrap text-gray-300">model_reasoning_effort = "high"</div>
|
||||
<div class="whitespace-nowrap text-gray-300">disable_response_storage = true</div>
|
||||
<div class="mt-2"></div>
|
||||
<div class="whitespace-nowrap text-gray-300">[model_providers.crs]</div>
|
||||
<div class="whitespace-nowrap text-gray-300">name = "crs"</div>
|
||||
<div class="whitespace-nowrap text-gray-300">base_url = "{{ openaiBaseUrl }}"</div>
|
||||
<div class="whitespace-nowrap text-gray-300">wire_api = "responses"</div>
|
||||
</div>
|
||||
<p class="mt-3 text-sm text-yellow-700">
|
||||
在
|
||||
<code class="rounded bg-yellow-100 px-1">~/.codex/auth.json</code>
|
||||
文件中配置API密钥:
|
||||
</p>
|
||||
<div
|
||||
class="mt-2 overflow-x-auto rounded bg-gray-900 p-2 font-mono text-xs text-green-400 sm:p-3 sm:text-sm"
|
||||
>
|
||||
<div class="whitespace-nowrap text-gray-300">{</div>
|
||||
<div class="whitespace-nowrap text-gray-300">"OPENAI_API_KEY": "你的API密钥"</div>
|
||||
<div class="whitespace-nowrap text-gray-300">}</div>
|
||||
</div>
|
||||
<p class="mt-2 text-xs text-yellow-700">
|
||||
💡 使用与 Claude Code 相同的 API 密钥即可,格式如 cr_xxxxxxxxxx。
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="rounded-lg border border-indigo-200 bg-white p-3 sm:p-4">
|
||||
<h6 class="mb-2 text-sm font-medium text-gray-800 dark:text-gray-600 sm:text-base">
|
||||
永久设置方法
|
||||
</h6>
|
||||
<p class="mb-3 text-sm text-gray-600">添加到你的 shell 配置文件:</p>
|
||||
<div
|
||||
class="mb-3 overflow-x-auto rounded bg-gray-900 p-2 font-mono text-xs text-green-400 sm:p-3 sm:text-sm"
|
||||
>
|
||||
<div class="mb-2"># 对于 bash (默认)</div>
|
||||
<div class="whitespace-nowrap text-gray-300">
|
||||
echo 'export OPENAI_BASE_URL="{{ openaiBaseUrl }}"' >> ~/.bashrc
|
||||
</div>
|
||||
<div class="whitespace-nowrap text-gray-300">
|
||||
echo 'export OPENAI_API_KEY="你的API密钥"' >> ~/.bashrc
|
||||
</div>
|
||||
<div class="whitespace-nowrap text-gray-300">source ~/.bashrc</div>
|
||||
</div>
|
||||
<div
|
||||
class="overflow-x-auto rounded bg-gray-900 p-2 font-mono text-xs text-green-400 sm:p-3 sm:text-sm"
|
||||
>
|
||||
<div class="mb-2"># 对于 zsh</div>
|
||||
<div class="whitespace-nowrap text-gray-300">
|
||||
echo 'export OPENAI_BASE_URL="{{ openaiBaseUrl }}"' >> ~/.zshrc
|
||||
</div>
|
||||
<div class="whitespace-nowrap text-gray-300">
|
||||
echo 'export OPENAI_API_KEY="你的API密钥"' >> ~/.zshrc
|
||||
</div>
|
||||
<div class="whitespace-nowrap text-gray-300">source ~/.zshrc</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="rounded-lg border border-indigo-200 bg-indigo-50 p-3 sm:p-4">
|
||||
<h6 class="mb-2 font-medium text-indigo-800">验证 Codex 环境变量</h6>
|
||||
<p class="mb-3 text-sm text-indigo-700">在终端中验证:</p>
|
||||
<div
|
||||
class="space-y-1 overflow-x-auto rounded bg-gray-900 p-2 font-mono text-xs text-green-400 sm:p-3 sm:text-sm"
|
||||
>
|
||||
<div class="whitespace-nowrap text-gray-300">echo $OPENAI_BASE_URL</div>
|
||||
<div class="whitespace-nowrap text-gray-300">echo $OPENAI_API_KEY</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -554,13 +554,17 @@ const formatDate = (dateString) => {
|
||||
const loadUsers = async () => {
|
||||
loading.value = true
|
||||
try {
|
||||
// Build params object, only including parameters with actual values
|
||||
const params = {}
|
||||
if (selectedRole.value && selectedRole.value.trim() !== '') {
|
||||
params.role = selectedRole.value
|
||||
}
|
||||
if (selectedStatus.value !== '') {
|
||||
params.isActive = selectedStatus.value
|
||||
}
|
||||
|
||||
const [usersResponse, statsResponse] = await Promise.all([
|
||||
apiClient.get('/users', {
|
||||
params: {
|
||||
role: selectedRole.value || undefined,
|
||||
isActive: selectedStatus.value !== '' ? selectedStatus.value : undefined
|
||||
}
|
||||
}),
|
||||
apiClient.get('/users', { params }),
|
||||
apiClient.get('/users/stats/overview')
|
||||
])
|
||||
|
||||
|
||||
Reference in New Issue
Block a user