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 apiKeyService = require('../services/apiKeyService')
const logger = require('../utils/logger') const logger = require('../utils/logger')
const config = require('../../config/config') const config = require('../../config/config')
const { const { authenticateUser, authenticateUserOrAdmin, requireAdmin } = require('../middleware/auth')
authenticateUser,
authenticateUserOrAdmin,
requireAdmin,
requireRole
} = require('../middleware/auth')
// 🔐 用户登录端点 // 🔐 用户登录端点
router.post('/login', async (req, res) => { router.post('/login', async (req, res) => {
@@ -253,7 +248,6 @@ router.post('/api-keys', authenticateUser, async (req, res) => {
} }
}) })
// 🗑️ 删除API Key // 🗑️ 删除API Key
router.delete('/api-keys/:keyId', authenticateUser, async (req, res) => { router.delete('/api-keys/:keyId', authenticateUser, async (req, res) => {
try { 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({ res.json({
success: true, 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({ res.json({
success: true, success: true,

View File

@@ -536,7 +536,7 @@ class ApiKeyService {
createdAt: key.createdAt, createdAt: key.createdAt,
lastUsedAt: key.lastUsedAt, lastUsedAt: key.lastUsedAt,
expiresAt: key.expiresAt, expiresAt: key.expiresAt,
usage: usage, usage,
dailyCost, dailyCost,
totalCost: costStats.total, totalCost: costStats.total,
dailyCostLimit: parseFloat(key.dailyCostLimit || 0), dailyCostLimit: parseFloat(key.dailyCostLimit || 0),
@@ -628,8 +628,8 @@ class ApiKeyService {
} }
} }
// 🗑️ 删除API Key // 🗑️ 删除API Key (完全移除)
async deleteApiKey(keyId) { async hardDeleteApiKey(keyId) {
try { try {
const keyData = await redis.getApiKey(keyId) const keyData = await redis.getApiKey(keyId)
if (!keyData) { if (!keyData) {
@@ -669,14 +669,14 @@ class ApiKeyService {
} }
} }
// 📊 获取使用统计支持多个API Key // 📊 获取聚合使用统计支持多个API Key
async getUsageStats(keyIds, options = {}) { async getAggregatedUsageStats(keyIds, options = {}) {
try { try {
if (!Array.isArray(keyIds)) { if (!Array.isArray(keyIds)) {
keyIds = [keyIds] keyIds = [keyIds]
} }
const { period = 'week', model } = options const { period: _period = 'week', model: _model } = options
const stats = { const stats = {
totalRequests: 0, totalRequests: 0,
totalInputTokens: 0, totalInputTokens: 0,

File diff suppressed because it is too large Load Diff