mirror of
https://github.com/fawney19/Aether.git
synced 2026-01-12 04:28:28 +08:00
- 新增 Management Token(访问令牌)功能,支持创建、更新、删除和管理 - 前端添加访问令牌管理页面,支持普通用户和管理员 - 后端实现完整的令牌生命周期管理 API - 添加数据库迁移脚本创建 management_tokens 表 - Nginx 配置添加 gzip 压缩,优化响应传输 - Dialog 组件添加 persistent 属性,防止意外关闭 - 为管理后台 API 添加详细的中文文档注释 - 简化多处类型注解,统一代码风格
204 lines
4.6 KiB
TypeScript
204 lines
4.6 KiB
TypeScript
/**
|
||
* Management Token API
|
||
*/
|
||
|
||
import apiClient from './client'
|
||
|
||
// ============== 类型定义 ==============
|
||
|
||
export interface ManagementToken {
|
||
id: string
|
||
user_id: string
|
||
name: string
|
||
description?: string
|
||
token_display: string
|
||
allowed_ips?: string[] | null
|
||
expires_at?: string | null
|
||
last_used_at?: string | null
|
||
last_used_ip?: string | null
|
||
usage_count: number
|
||
is_active: boolean
|
||
created_at: string
|
||
updated_at: string
|
||
user?: {
|
||
id: string
|
||
email: string
|
||
username: string
|
||
role: string
|
||
}
|
||
}
|
||
|
||
export interface CreateManagementTokenRequest {
|
||
name: string
|
||
description?: string
|
||
allowed_ips?: string[]
|
||
expires_at?: string | null
|
||
}
|
||
|
||
export interface CreateManagementTokenResponse {
|
||
message: string
|
||
token: string
|
||
data: ManagementToken
|
||
}
|
||
|
||
export interface UpdateManagementTokenRequest {
|
||
name?: string
|
||
description?: string | null
|
||
allowed_ips?: string[] | null
|
||
expires_at?: string | null
|
||
is_active?: boolean
|
||
}
|
||
|
||
export interface ManagementTokenListResponse {
|
||
items: ManagementToken[]
|
||
total: number
|
||
skip: number
|
||
limit: number
|
||
quota?: {
|
||
used: number
|
||
max: number
|
||
}
|
||
}
|
||
|
||
// ============== 用户自助管理 API ==============
|
||
|
||
export const managementTokenApi = {
|
||
/**
|
||
* 列出当前用户的 Management Tokens
|
||
*/
|
||
async listTokens(params?: {
|
||
is_active?: boolean
|
||
skip?: number
|
||
limit?: number
|
||
}): Promise<ManagementTokenListResponse> {
|
||
const response = await apiClient.get<ManagementTokenListResponse>(
|
||
'/api/me/management-tokens',
|
||
{ params }
|
||
)
|
||
return response.data
|
||
},
|
||
|
||
/**
|
||
* 创建 Management Token
|
||
*/
|
||
async createToken(
|
||
data: CreateManagementTokenRequest
|
||
): Promise<CreateManagementTokenResponse> {
|
||
const response = await apiClient.post<CreateManagementTokenResponse>(
|
||
'/api/me/management-tokens',
|
||
data
|
||
)
|
||
return response.data
|
||
},
|
||
|
||
/**
|
||
* 获取 Token 详情
|
||
*/
|
||
async getToken(tokenId: string): Promise<ManagementToken> {
|
||
const response = await apiClient.get<ManagementToken>(
|
||
`/api/me/management-tokens/${tokenId}`
|
||
)
|
||
return response.data
|
||
},
|
||
|
||
/**
|
||
* 更新 Token
|
||
*/
|
||
async updateToken(
|
||
tokenId: string,
|
||
data: UpdateManagementTokenRequest
|
||
): Promise<{ message: string; data: ManagementToken }> {
|
||
const response = await apiClient.put<{ message: string; data: ManagementToken }>(
|
||
`/api/me/management-tokens/${tokenId}`,
|
||
data
|
||
)
|
||
return response.data
|
||
},
|
||
|
||
/**
|
||
* 删除 Token
|
||
*/
|
||
async deleteToken(tokenId: string): Promise<{ message: string }> {
|
||
const response = await apiClient.delete<{ message: string }>(
|
||
`/api/me/management-tokens/${tokenId}`
|
||
)
|
||
return response.data
|
||
},
|
||
|
||
/**
|
||
* 切换 Token 状态
|
||
*/
|
||
async toggleToken(
|
||
tokenId: string
|
||
): Promise<{ message: string; data: ManagementToken }> {
|
||
const response = await apiClient.patch<{ message: string; data: ManagementToken }>(
|
||
`/api/me/management-tokens/${tokenId}/status`
|
||
)
|
||
return response.data
|
||
},
|
||
|
||
/**
|
||
* 重新生成 Token
|
||
*/
|
||
async regenerateToken(
|
||
tokenId: string
|
||
): Promise<{ token: string; data: ManagementToken }> {
|
||
const response = await apiClient.post<{ token: string; data: ManagementToken }>(
|
||
`/api/me/management-tokens/${tokenId}/regenerate`
|
||
)
|
||
return response.data
|
||
}
|
||
}
|
||
|
||
// ============== 管理员 API ==============
|
||
|
||
export const adminManagementTokenApi = {
|
||
/**
|
||
* 列出所有 Management Tokens(管理员)
|
||
*/
|
||
async listAllTokens(params?: {
|
||
user_id?: string
|
||
is_active?: boolean
|
||
skip?: number
|
||
limit?: number
|
||
}): Promise<ManagementTokenListResponse> {
|
||
const response = await apiClient.get<ManagementTokenListResponse>(
|
||
'/api/admin/management-tokens',
|
||
{ params }
|
||
)
|
||
return response.data
|
||
},
|
||
|
||
/**
|
||
* 获取 Token 详情(管理员)
|
||
*/
|
||
async getToken(tokenId: string): Promise<ManagementToken> {
|
||
const response = await apiClient.get<ManagementToken>(
|
||
`/api/admin/management-tokens/${tokenId}`
|
||
)
|
||
return response.data
|
||
},
|
||
|
||
/**
|
||
* 删除任意 Token(管理员)
|
||
*/
|
||
async deleteToken(tokenId: string): Promise<{ message: string }> {
|
||
const response = await apiClient.delete<{ message: string }>(
|
||
`/api/admin/management-tokens/${tokenId}`
|
||
)
|
||
return response.data
|
||
},
|
||
|
||
/**
|
||
* 切换任意 Token 状态(管理员)
|
||
*/
|
||
async toggleToken(
|
||
tokenId: string
|
||
): Promise<{ message: string; data: ManagementToken }> {
|
||
const response = await apiClient.patch<{ message: string; data: ManagementToken }>(
|
||
`/api/admin/management-tokens/${tokenId}/status`
|
||
)
|
||
return response.data
|
||
}
|
||
}
|