feat: management of deleted keys

This commit is contained in:
Feng Yue
2025-08-14 12:42:39 +08:00
parent 5d850a7c1c
commit aff9966ed1
3 changed files with 1313 additions and 1085 deletions

View File

@@ -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,

View File

@@ -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,