mirror of
https://github.com/Wei-Shaw/claude-relay-service.git
synced 2026-01-22 16:43:35 +00:00
feat: management of deleted keys
This commit is contained in:
@@ -5,12 +5,7 @@ const userService = require('../services/userService')
|
||||
const apiKeyService = require('../services/apiKeyService')
|
||||
const logger = require('../utils/logger')
|
||||
const config = require('../../config/config')
|
||||
const {
|
||||
authenticateUser,
|
||||
authenticateUserOrAdmin,
|
||||
requireAdmin,
|
||||
requireRole
|
||||
} = require('../middleware/auth')
|
||||
const { authenticateUser, authenticateUserOrAdmin, requireAdmin } = require('../middleware/auth')
|
||||
|
||||
// 🔐 用户登录端点
|
||||
router.post('/login', async (req, res) => {
|
||||
@@ -253,7 +248,6 @@ router.post('/api-keys', authenticateUser, async (req, res) => {
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
// 🗑️ 删除API Key
|
||||
router.delete('/api-keys/:keyId', authenticateUser, async (req, res) => {
|
||||
try {
|
||||
@@ -313,7 +307,7 @@ router.get('/usage-stats', authenticateUser, async (req, res) => {
|
||||
}
|
||||
|
||||
// 获取使用统计
|
||||
const stats = await apiKeyService.getUsageStats(apiKeyIds, { period, model })
|
||||
const stats = await apiKeyService.getAggregatedUsageStats(apiKeyIds, { period, model })
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
@@ -584,7 +578,7 @@ router.get('/:userId/usage-stats', authenticateUserOrAdmin, requireAdmin, async
|
||||
}
|
||||
|
||||
// 获取使用统计
|
||||
const stats = await apiKeyService.getUsageStats(apiKeyIds, { period, model })
|
||||
const stats = await apiKeyService.getAggregatedUsageStats(apiKeyIds, { period, model })
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
|
||||
@@ -536,7 +536,7 @@ class ApiKeyService {
|
||||
createdAt: key.createdAt,
|
||||
lastUsedAt: key.lastUsedAt,
|
||||
expiresAt: key.expiresAt,
|
||||
usage: usage,
|
||||
usage,
|
||||
dailyCost,
|
||||
totalCost: costStats.total,
|
||||
dailyCostLimit: parseFloat(key.dailyCostLimit || 0),
|
||||
@@ -628,8 +628,8 @@ class ApiKeyService {
|
||||
}
|
||||
}
|
||||
|
||||
// 🗑️ 删除API Key
|
||||
async deleteApiKey(keyId) {
|
||||
// 🗑️ 硬删除API Key (完全移除)
|
||||
async hardDeleteApiKey(keyId) {
|
||||
try {
|
||||
const keyData = await redis.getApiKey(keyId)
|
||||
if (!keyData) {
|
||||
@@ -669,14 +669,14 @@ class ApiKeyService {
|
||||
}
|
||||
}
|
||||
|
||||
// 📊 获取使用统计(支持多个API Key)
|
||||
async getUsageStats(keyIds, options = {}) {
|
||||
// 📊 获取聚合使用统计(支持多个API Key)
|
||||
async getAggregatedUsageStats(keyIds, options = {}) {
|
||||
try {
|
||||
if (!Array.isArray(keyIds)) {
|
||||
keyIds = [keyIds]
|
||||
}
|
||||
|
||||
const { period = 'week', model } = options
|
||||
const { period: _period = 'week', model: _model } = options
|
||||
const stats = {
|
||||
totalRequests: 0,
|
||||
totalInputTokens: 0,
|
||||
|
||||
Reference in New Issue
Block a user