2025-12-10 20:52:44 +08:00
|
|
|
|
import apiClient from './client'
|
|
|
|
|
|
|
2025-12-16 18:33:14 +08:00
|
|
|
|
// 配置导出数据结构
|
|
|
|
|
|
export interface ConfigExportData {
|
|
|
|
|
|
version: string
|
|
|
|
|
|
exported_at: string
|
|
|
|
|
|
global_models: GlobalModelExport[]
|
|
|
|
|
|
providers: ProviderExport[]
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 用户导出数据结构
|
|
|
|
|
|
export interface UsersExportData {
|
|
|
|
|
|
version: string
|
|
|
|
|
|
exported_at: string
|
|
|
|
|
|
users: UserExport[]
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export interface UserExport {
|
|
|
|
|
|
email: string
|
|
|
|
|
|
username: string
|
|
|
|
|
|
password_hash: string
|
|
|
|
|
|
role: string
|
|
|
|
|
|
allowed_providers?: string[] | null
|
|
|
|
|
|
allowed_endpoints?: string[] | null
|
|
|
|
|
|
allowed_models?: string[] | null
|
|
|
|
|
|
model_capability_settings?: any
|
|
|
|
|
|
quota_usd?: number | null
|
|
|
|
|
|
used_usd?: number
|
|
|
|
|
|
total_usd?: number
|
|
|
|
|
|
is_active: boolean
|
|
|
|
|
|
api_keys: UserApiKeyExport[]
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export interface UserApiKeyExport {
|
|
|
|
|
|
key_hash: string
|
|
|
|
|
|
key_encrypted?: string | null
|
|
|
|
|
|
name?: string | null
|
|
|
|
|
|
is_standalone: boolean
|
|
|
|
|
|
balance_used_usd?: number
|
|
|
|
|
|
current_balance_usd?: number | null
|
|
|
|
|
|
allowed_providers?: string[] | null
|
|
|
|
|
|
allowed_endpoints?: string[] | null
|
|
|
|
|
|
allowed_api_formats?: string[] | null
|
|
|
|
|
|
allowed_models?: string[] | null
|
|
|
|
|
|
rate_limit?: number
|
|
|
|
|
|
concurrent_limit?: number | null
|
|
|
|
|
|
force_capabilities?: any
|
|
|
|
|
|
is_active: boolean
|
|
|
|
|
|
auto_delete_on_expiry?: boolean
|
|
|
|
|
|
total_requests?: number
|
|
|
|
|
|
total_cost_usd?: number
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export interface GlobalModelExport {
|
|
|
|
|
|
name: string
|
|
|
|
|
|
display_name: string
|
|
|
|
|
|
default_price_per_request?: number | null
|
|
|
|
|
|
default_tiered_pricing: any
|
|
|
|
|
|
supported_capabilities?: string[] | null
|
|
|
|
|
|
config?: any
|
|
|
|
|
|
is_active: boolean
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export interface ProviderExport {
|
|
|
|
|
|
name: string
|
|
|
|
|
|
display_name: string
|
|
|
|
|
|
description?: string | null
|
|
|
|
|
|
website?: string | null
|
|
|
|
|
|
billing_type?: string | null
|
|
|
|
|
|
monthly_quota_usd?: number | null
|
|
|
|
|
|
quota_reset_day?: number
|
|
|
|
|
|
rpm_limit?: number | null
|
|
|
|
|
|
provider_priority?: number
|
|
|
|
|
|
is_active: boolean
|
|
|
|
|
|
rate_limit?: number | null
|
|
|
|
|
|
concurrent_limit?: number | null
|
|
|
|
|
|
config?: any
|
|
|
|
|
|
endpoints: EndpointExport[]
|
|
|
|
|
|
models: ModelExport[]
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export interface EndpointExport {
|
|
|
|
|
|
api_format: string
|
|
|
|
|
|
base_url: string
|
|
|
|
|
|
headers?: any
|
|
|
|
|
|
timeout?: number
|
|
|
|
|
|
max_retries?: number
|
|
|
|
|
|
max_concurrent?: number | null
|
|
|
|
|
|
rate_limit?: number | null
|
|
|
|
|
|
is_active: boolean
|
|
|
|
|
|
custom_path?: string | null
|
|
|
|
|
|
config?: any
|
|
|
|
|
|
keys: KeyExport[]
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export interface KeyExport {
|
|
|
|
|
|
api_key: string
|
|
|
|
|
|
name?: string | null
|
|
|
|
|
|
note?: string | null
|
|
|
|
|
|
rate_multiplier?: number
|
|
|
|
|
|
internal_priority?: number
|
|
|
|
|
|
global_priority?: number | null
|
|
|
|
|
|
max_concurrent?: number | null
|
|
|
|
|
|
rate_limit?: number | null
|
|
|
|
|
|
daily_limit?: number | null
|
|
|
|
|
|
monthly_limit?: number | null
|
|
|
|
|
|
allowed_models?: string[] | null
|
|
|
|
|
|
capabilities?: any
|
|
|
|
|
|
is_active: boolean
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export interface ModelExport {
|
|
|
|
|
|
global_model_name: string | null
|
|
|
|
|
|
provider_model_name: string
|
|
|
|
|
|
provider_model_aliases?: any
|
|
|
|
|
|
price_per_request?: number | null
|
|
|
|
|
|
tiered_pricing?: any
|
|
|
|
|
|
supports_vision?: boolean | null
|
|
|
|
|
|
supports_function_calling?: boolean | null
|
|
|
|
|
|
supports_streaming?: boolean | null
|
|
|
|
|
|
supports_extended_thinking?: boolean | null
|
|
|
|
|
|
supports_image_generation?: boolean | null
|
|
|
|
|
|
is_active: boolean
|
|
|
|
|
|
config?: any
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export interface ConfigImportRequest extends ConfigExportData {
|
|
|
|
|
|
merge_mode: 'skip' | 'overwrite' | 'error'
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export interface UsersImportRequest extends UsersExportData {
|
|
|
|
|
|
merge_mode: 'skip' | 'overwrite' | 'error'
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export interface UsersImportResponse {
|
|
|
|
|
|
message: string
|
|
|
|
|
|
stats: {
|
|
|
|
|
|
users: { created: number; updated: number; skipped: number }
|
|
|
|
|
|
api_keys: { created: number; skipped: number }
|
|
|
|
|
|
errors: string[]
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export interface ConfigImportResponse {
|
|
|
|
|
|
message: string
|
|
|
|
|
|
stats: {
|
|
|
|
|
|
global_models: { created: number; updated: number; skipped: number }
|
|
|
|
|
|
providers: { created: number; updated: number; skipped: number }
|
|
|
|
|
|
endpoints: { created: number; updated: number; skipped: number }
|
|
|
|
|
|
keys: { created: number; updated: number; skipped: number }
|
|
|
|
|
|
models: { created: number; updated: number; skipped: number }
|
|
|
|
|
|
errors: string[]
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-10 20:52:44 +08:00
|
|
|
|
// API密钥管理相关接口定义
|
|
|
|
|
|
export interface AdminApiKey {
|
|
|
|
|
|
id: string // UUID
|
|
|
|
|
|
user_id: string // UUID
|
|
|
|
|
|
user_email?: string
|
|
|
|
|
|
username?: string
|
|
|
|
|
|
name?: string
|
|
|
|
|
|
key_display?: string // 脱敏后的密钥显示
|
|
|
|
|
|
is_active: boolean
|
|
|
|
|
|
is_standalone: boolean // 是否为独立余额Key
|
|
|
|
|
|
balance_used_usd?: number // 已使用余额(仅独立Key)
|
|
|
|
|
|
current_balance_usd?: number | null // 当前余额(独立Key预付费模式,null表示无限制)
|
|
|
|
|
|
total_requests?: number
|
|
|
|
|
|
total_tokens?: number
|
|
|
|
|
|
total_cost_usd?: number
|
|
|
|
|
|
rate_limit?: number
|
|
|
|
|
|
allowed_providers?: string[] | null // 允许的提供商列表
|
|
|
|
|
|
allowed_api_formats?: string[] | null // 允许的 API 格式列表
|
|
|
|
|
|
allowed_models?: string[] | null // 允许的模型列表
|
|
|
|
|
|
auto_delete_on_expiry?: boolean // 过期后是否自动删除
|
|
|
|
|
|
last_used_at?: string
|
|
|
|
|
|
expires_at?: string
|
|
|
|
|
|
created_at: string
|
|
|
|
|
|
updated_at?: string
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export interface CreateStandaloneApiKeyRequest {
|
|
|
|
|
|
name?: string
|
|
|
|
|
|
allowed_providers?: string[] | null
|
|
|
|
|
|
allowed_api_formats?: string[] | null
|
|
|
|
|
|
allowed_models?: string[] | null
|
|
|
|
|
|
rate_limit?: number
|
|
|
|
|
|
expire_days?: number | null // null = 永不过期
|
|
|
|
|
|
initial_balance_usd: number // 初始余额,必须设置
|
|
|
|
|
|
auto_delete_on_expiry?: boolean // 过期后是否自动删除
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export interface AdminApiKeysResponse {
|
|
|
|
|
|
api_keys: AdminApiKey[]
|
|
|
|
|
|
total: number
|
|
|
|
|
|
limit: number
|
|
|
|
|
|
skip: number
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export interface ApiKeyToggleResponse {
|
|
|
|
|
|
id: string // UUID
|
|
|
|
|
|
is_active: boolean
|
|
|
|
|
|
message: string
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 管理员API密钥管理相关API
|
|
|
|
|
|
export const adminApi = {
|
|
|
|
|
|
// 获取所有独立余额Keys列表
|
|
|
|
|
|
async getAllApiKeys(params?: {
|
|
|
|
|
|
skip?: number
|
|
|
|
|
|
limit?: number
|
|
|
|
|
|
is_active?: boolean
|
|
|
|
|
|
}): Promise<AdminApiKeysResponse> {
|
|
|
|
|
|
const response = await apiClient.get<AdminApiKeysResponse>('/api/admin/api-keys', {
|
2025-12-12 16:14:33 +08:00
|
|
|
|
params
|
2025-12-10 20:52:44 +08:00
|
|
|
|
})
|
|
|
|
|
|
return response.data
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 创建独立余额Key
|
|
|
|
|
|
async createStandaloneApiKey(data: CreateStandaloneApiKeyRequest): Promise<AdminApiKey & { key: string }> {
|
|
|
|
|
|
const response = await apiClient.post<AdminApiKey & { key: string }>(
|
|
|
|
|
|
'/api/admin/api-keys',
|
|
|
|
|
|
data
|
|
|
|
|
|
)
|
|
|
|
|
|
return response.data
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 更新独立余额Key
|
|
|
|
|
|
async updateApiKey(keyId: string, data: Partial<CreateStandaloneApiKeyRequest>): Promise<AdminApiKey & { message: string }> {
|
|
|
|
|
|
const response = await apiClient.put<AdminApiKey & { message: string }>(
|
|
|
|
|
|
`/api/admin/api-keys/${keyId}`,
|
|
|
|
|
|
data
|
|
|
|
|
|
)
|
|
|
|
|
|
return response.data
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 切换API密钥状态(启用/禁用)
|
|
|
|
|
|
async toggleApiKey(keyId: string): Promise<ApiKeyToggleResponse> {
|
|
|
|
|
|
const response = await apiClient.patch<ApiKeyToggleResponse>(
|
|
|
|
|
|
`/api/admin/api-keys/${keyId}`
|
|
|
|
|
|
)
|
|
|
|
|
|
return response.data
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 删除API密钥
|
|
|
|
|
|
async deleteApiKey(keyId: string): Promise<{ message: string }> {
|
|
|
|
|
|
const response = await apiClient.delete<{ message: string}>(
|
|
|
|
|
|
`/api/admin/api-keys/${keyId}`
|
|
|
|
|
|
)
|
|
|
|
|
|
return response.data
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 为独立余额Key调整余额
|
|
|
|
|
|
async addApiKeyBalance(keyId: string, amountUsd: number): Promise<AdminApiKey & { message: string }> {
|
|
|
|
|
|
const response = await apiClient.patch<AdminApiKey & { message: string }>(
|
|
|
|
|
|
`/api/admin/api-keys/${keyId}/balance`,
|
|
|
|
|
|
{ amount_usd: amountUsd }
|
|
|
|
|
|
)
|
|
|
|
|
|
return response.data
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 获取API密钥详情(可选包含完整密钥)
|
|
|
|
|
|
async getApiKeyDetail(keyId: string, includeKey: boolean = false): Promise<AdminApiKey & { key?: string }> {
|
|
|
|
|
|
const response = await apiClient.get<AdminApiKey & { key?: string }>(
|
|
|
|
|
|
`/api/admin/api-keys/${keyId}`,
|
|
|
|
|
|
{ params: { include_key: includeKey } }
|
|
|
|
|
|
)
|
|
|
|
|
|
return response.data
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 获取完整的API密钥(用于复制)- 便捷方法
|
|
|
|
|
|
async getFullApiKey(keyId: string): Promise<{ key: string }> {
|
|
|
|
|
|
const response = await apiClient.get<{ key: string }>(
|
|
|
|
|
|
`/api/admin/api-keys/${keyId}`,
|
|
|
|
|
|
{ params: { include_key: true } }
|
|
|
|
|
|
)
|
|
|
|
|
|
return response.data
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 系统配置相关
|
|
|
|
|
|
// 获取所有系统配置
|
|
|
|
|
|
async getAllSystemConfigs(): Promise<any[]> {
|
|
|
|
|
|
const response = await apiClient.get<any[]>('/api/admin/system/configs')
|
|
|
|
|
|
return response.data
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 获取特定系统配置
|
|
|
|
|
|
async getSystemConfig(key: string): Promise<{ key: string; value: any }> {
|
|
|
|
|
|
const response = await apiClient.get<{ key: string; value: any }>(
|
|
|
|
|
|
`/api/admin/system/configs/${key}`
|
|
|
|
|
|
)
|
|
|
|
|
|
return response.data
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 更新系统配置
|
|
|
|
|
|
async updateSystemConfig(
|
|
|
|
|
|
key: string,
|
|
|
|
|
|
value: any,
|
|
|
|
|
|
description?: string
|
|
|
|
|
|
): Promise<{ key: string; value: any; description?: string }> {
|
|
|
|
|
|
const response = await apiClient.put<{ key: string; value: any; description?: string }>(
|
|
|
|
|
|
`/api/admin/system/configs/${key}`,
|
|
|
|
|
|
{ value, description }
|
|
|
|
|
|
)
|
|
|
|
|
|
return response.data
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 删除系统配置
|
|
|
|
|
|
async deleteSystemConfig(key: string): Promise<{ message: string }> {
|
|
|
|
|
|
const response = await apiClient.delete<{ message: string }>(
|
|
|
|
|
|
`/api/admin/system/configs/${key}`
|
|
|
|
|
|
)
|
|
|
|
|
|
return response.data
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 获取系统统计
|
|
|
|
|
|
async getSystemStats(): Promise<any> {
|
|
|
|
|
|
const response = await apiClient.get<any>('/api/admin/system/stats')
|
|
|
|
|
|
return response.data
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 获取可用的API格式列表
|
|
|
|
|
|
async getApiFormats(): Promise<{ formats: Array<{ value: string; label: string; default_path: string; aliases: string[] }> }> {
|
|
|
|
|
|
const response = await apiClient.get<{ formats: Array<{ value: string; label: string; default_path: string; aliases: string[] }> }>(
|
|
|
|
|
|
'/api/admin/system/api-formats'
|
|
|
|
|
|
)
|
|
|
|
|
|
return response.data
|
2025-12-16 18:33:14 +08:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 导出配置
|
|
|
|
|
|
async exportConfig(): Promise<ConfigExportData> {
|
|
|
|
|
|
const response = await apiClient.get<ConfigExportData>('/api/admin/system/config/export')
|
|
|
|
|
|
return response.data
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 导入配置
|
|
|
|
|
|
async importConfig(data: ConfigImportRequest): Promise<ConfigImportResponse> {
|
|
|
|
|
|
const response = await apiClient.post<ConfigImportResponse>(
|
|
|
|
|
|
'/api/admin/system/config/import',
|
|
|
|
|
|
data
|
|
|
|
|
|
)
|
|
|
|
|
|
return response.data
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 导出用户数据
|
|
|
|
|
|
async exportUsers(): Promise<UsersExportData> {
|
|
|
|
|
|
const response = await apiClient.get<UsersExportData>('/api/admin/system/users/export')
|
|
|
|
|
|
return response.data
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 导入用户数据
|
|
|
|
|
|
async importUsers(data: UsersImportRequest): Promise<UsersImportResponse> {
|
|
|
|
|
|
const response = await apiClient.post<UsersImportResponse>(
|
|
|
|
|
|
'/api/admin/system/users/import',
|
|
|
|
|
|
data
|
|
|
|
|
|
)
|
|
|
|
|
|
return response.data
|
2025-12-10 20:52:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|