mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-08 08:36:37 +08:00
压缩历史至 95081aff
This commit is contained in:
@@ -0,0 +1,372 @@
|
||||
import { BaseService } from '../core/base.service';
|
||||
import type {
|
||||
SystemConfig,
|
||||
CreateSystemConfigRequest,
|
||||
UpdateSystemConfigRequest,
|
||||
UserPayConfig,
|
||||
CreateUserPayConfigRequest,
|
||||
UpdateUserPayConfigRequest,
|
||||
TaskMeta,
|
||||
TaskTypeResponse,
|
||||
DispatchTaskRequest,
|
||||
ListUsersRequest,
|
||||
ListUsersResponse,
|
||||
UpdateUserStatusRequest,
|
||||
} from './types';
|
||||
|
||||
export type { AdminUser } from './types';
|
||||
|
||||
/**
|
||||
* 管理员服务
|
||||
* 处理系统配置和用户积分配置管理相关的 API 请求
|
||||
*
|
||||
* @remarks
|
||||
* 所有接口都需要管理员权限
|
||||
*/
|
||||
export class AdminService extends BaseService {
|
||||
protected static readonly basePath = '/api/v1/admin';
|
||||
|
||||
// ==================== 系统配置管理 ====================
|
||||
|
||||
/**
|
||||
* 创建系统配置
|
||||
* @param request - 创建系统配置的请求参数
|
||||
* @returns void
|
||||
* @throws {UnauthorizedError} 当未登录时
|
||||
* @throws {ForbiddenError} 当无管理员权限时
|
||||
* @throws {ValidationError} 当参数验证失败或配置键已存在时
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* await AdminService.createSystemConfig({
|
||||
* key: 'app.version',
|
||||
* value: '1.0.0',
|
||||
* description: '应用版本号'
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
static async createSystemConfig(
|
||||
request: CreateSystemConfigRequest,
|
||||
): Promise<void> {
|
||||
return this.post<void>('/system-configs', request);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取系统配置列表
|
||||
* @returns 系统配置列表
|
||||
* @throws {UnauthorizedError} 当未登录时
|
||||
* @throws {ForbiddenError} 当无管理员权限时
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const configs = await AdminService.listSystemConfigs();
|
||||
* console.log('系统配置数量:', configs.length);
|
||||
* ```
|
||||
*/
|
||||
static async listSystemConfigs(): Promise<SystemConfig[]> {
|
||||
return this.get<SystemConfig[]>('/system-configs');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取单个系统配置
|
||||
* @param key - 配置键
|
||||
* @returns 系统配置信息
|
||||
* @throws {UnauthorizedError} 当未登录时
|
||||
* @throws {ForbiddenError} 当无管理员权限时
|
||||
* @throws {NotFoundError} 当配置不存在时
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const config = await AdminService.getSystemConfig('app.version');
|
||||
* console.log('应用版本:', config.value);
|
||||
* ```
|
||||
*/
|
||||
static async getSystemConfig(key: string): Promise<SystemConfig> {
|
||||
return this.get<SystemConfig>(`/system-configs/${ key }`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新系统配置
|
||||
* @param key - 配置键
|
||||
* @param request - 更新系统配置的请求参数
|
||||
* @returns void
|
||||
* @throws {UnauthorizedError} 当未登录时
|
||||
* @throws {ForbiddenError} 当无管理员权限时
|
||||
* @throws {NotFoundError} 当配置不存在时
|
||||
* @throws {ValidationError} 当参数验证失败时
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* await AdminService.updateSystemConfig('app.version', {
|
||||
* value: '1.1.0',
|
||||
* description: '更新到新版本'
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
static async updateSystemConfig(
|
||||
key: string,
|
||||
request: UpdateSystemConfigRequest,
|
||||
): Promise<void> {
|
||||
return this.put<void>(`/system-configs/${ key }`, request);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除系统配置
|
||||
* @param key - 配置键
|
||||
* @returns void
|
||||
* @throws {UnauthorizedError} 当未登录时
|
||||
* @throws {ForbiddenError} 当无管理员权限时
|
||||
* @throws {NotFoundError} 当配置不存在时
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* await AdminService.deleteSystemConfig('app.version');
|
||||
* ```
|
||||
*/
|
||||
static async deleteSystemConfig(key: string): Promise<void> {
|
||||
return this.delete<void>(`/system-configs/${ key }`);
|
||||
}
|
||||
|
||||
// ==================== 用户积分配置管理 ====================
|
||||
|
||||
/**
|
||||
* 创建用户积分配置
|
||||
* @param request - 创建用户积分配置的请求参数
|
||||
* @returns 创建的用户积分配置
|
||||
* @throws {UnauthorizedError} 当未登录时
|
||||
* @throws {ForbiddenError} 当无管理员权限时
|
||||
* @throws {ValidationError} 当参数验证失败或等级已存在时
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const config = await AdminService.createUserPayConfig({
|
||||
* level: PayLevel.Premium,
|
||||
* min_score: 1000,
|
||||
* max_score: null,
|
||||
* daily_limit: 100000,
|
||||
* fee_rate: 0.01
|
||||
* });
|
||||
* console.log('配置ID:', config.id);
|
||||
* ```
|
||||
*
|
||||
* @remarks
|
||||
* - min_score 必须 >= 0
|
||||
* - max_score 必须大于 min_score(如果提供)
|
||||
* - fee_rate 必须在 0-1 之间,最多2位小数
|
||||
*/
|
||||
static async createUserPayConfig(
|
||||
request: CreateUserPayConfigRequest,
|
||||
): Promise<UserPayConfig> {
|
||||
return this.post<UserPayConfig>('/user-pay-configs', request);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户积分配置列表
|
||||
* @returns 用户积分配置列表(按最低分数升序排序)
|
||||
* @throws {UnauthorizedError} 当未登录时
|
||||
* @throws {ForbiddenError} 当无管理员权限时
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const configs = await AdminService.listUserPayConfigs();
|
||||
* console.log('积分配置数量:', configs.length);
|
||||
* ```
|
||||
*/
|
||||
static async listUserPayConfigs(): Promise<UserPayConfig[]> {
|
||||
return this.get<UserPayConfig[]>('/user-pay-configs');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取单个用户积分配置
|
||||
* @param id - 配置ID
|
||||
* @returns 用户积分配置信息
|
||||
* @throws {UnauthorizedError} 当未登录时
|
||||
* @throws {ForbiddenError} 当无管理员权限时
|
||||
* @throws {NotFoundError} 当配置不存在时
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const config = await AdminService.getUserPayConfig(123);
|
||||
* console.log('手续费率:', config.fee_rate);
|
||||
* ```
|
||||
*/
|
||||
static async getUserPayConfig(id: string): Promise<UserPayConfig> {
|
||||
return this.get<UserPayConfig>(`/user-pay-configs/${ id }`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新用户积分配置
|
||||
* @param id - 配置ID
|
||||
* @param request - 更新用户积分配置的请求参数
|
||||
* @returns void
|
||||
* @throws {UnauthorizedError} 当未登录时
|
||||
* @throws {ForbiddenError} 当无管理员权限时
|
||||
* @throws {NotFoundError} 当配置不存在时
|
||||
* @throws {ValidationError} 当参数验证失败时
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* await AdminService.updateUserPayConfig(123, {
|
||||
* min_score: 500,
|
||||
* max_score: 999,
|
||||
* daily_limit: 50000,
|
||||
* fee_rate: 0.02
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
* @remarks
|
||||
* - min_score 必须 >= 0
|
||||
* - max_score 必须大于 min_score(如果提供)
|
||||
* - fee_rate 必须在 0-1 之间,最多2位小数
|
||||
*/
|
||||
static async updateUserPayConfig(
|
||||
id: string,
|
||||
request: UpdateUserPayConfigRequest,
|
||||
): Promise<void> {
|
||||
return this.put<void>(`/user-pay-configs/${ id }`, request);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除用户积分配置
|
||||
* @param id - 配置ID
|
||||
* @returns void
|
||||
* @throws {UnauthorizedError} 当未登录时
|
||||
* @throws {ForbiddenError} 当无管理员权限时
|
||||
* @throws {NotFoundError} 当配置不存在时
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* await AdminService.deleteUserPayConfig(123);
|
||||
* ```
|
||||
*/
|
||||
static async deleteUserPayConfig(id: string): Promise<void> {
|
||||
return this.delete<void>(`/user-pay-configs/${ id }`);
|
||||
}
|
||||
|
||||
// ==================== 任务管理 ====================
|
||||
|
||||
/**
|
||||
* 获取支持的任务类型列表
|
||||
* @returns 任务类型列表
|
||||
* @throws {UnauthorizedError} 当未登录时
|
||||
* @throws {ForbiddenError} 当无管理员权限时
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const taskTypes = await AdminService.getTaskTypes();
|
||||
* console.log('可用任务类型:', taskTypes);
|
||||
* ```
|
||||
*/
|
||||
static async getTaskTypes(): Promise<TaskMeta[]> {
|
||||
const response = await this.get<TaskTypeResponse[]>('/tasks/types');
|
||||
// Adapt backend PascalCase to frontend snake_case
|
||||
return response.map(item => ({
|
||||
type: item.Type || item.type || '',
|
||||
asynq_task: item.AsynqTask || item.asynq_task || '',
|
||||
name: item.Name || item.name || '',
|
||||
description: item.Description || item.description || '',
|
||||
supports_time: item.SupportsTime ?? item.supports_time ?? false,
|
||||
max_retry: item.MaxRetry ?? item.max_retry ?? 0,
|
||||
queue: item.Queue || item.queue || '',
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* 下发任务
|
||||
* @param request - 下发任务请求参数
|
||||
* @returns void
|
||||
* @throws {UnauthorizedError} 当未登录时
|
||||
* @throws {ForbiddenError} 当无管理员权限时
|
||||
* @throws {ValidationError} 当参数验证失败时
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* // 下发订单同步任务(带时间范围)
|
||||
* await AdminService.dispatchTask({
|
||||
* task_type: 'order_sync',
|
||||
* start_time: '2025-12-01T00:00:00Z',
|
||||
* end_time: '2025-12-27T23:59:59Z'
|
||||
* });
|
||||
*
|
||||
* // 下发用户积分更新任务
|
||||
* await AdminService.dispatchTask({
|
||||
* task_type: 'user_gamification',
|
||||
* user_id: 123
|
||||
* });
|
||||
*
|
||||
* // 下发争议自动退款任务
|
||||
* await AdminService.dispatchTask({
|
||||
* task_type: 'dispute_auto_refund'
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
* @remarks
|
||||
* - 不同任务类型需要不同的参数
|
||||
* - order_sync 支持 start_time 和 end_time 参数
|
||||
* - user_gamification 需要 user_id 参数
|
||||
* - 其他任务无需额外参数
|
||||
*/
|
||||
static async dispatchTask(request: DispatchTaskRequest): Promise<void> {
|
||||
return this.post<void>('/tasks/dispatch', request);
|
||||
}
|
||||
|
||||
// ==================== 用户管理 ====================
|
||||
|
||||
/**
|
||||
* 获取用户列表
|
||||
* @param request - 查询参数
|
||||
* @returns 用户列表及总数
|
||||
* @throws {UnauthorizedError} 当未登录时
|
||||
* @throws {ForbiddenError} 当无管理员权限时
|
||||
* @throws {ValidationError} 当参数验证失败时
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const result = await AdminService.listUsers({
|
||||
* page: 1,
|
||||
* page_size: 20,
|
||||
* user_id: '10001',
|
||||
* username: 'test'
|
||||
* });
|
||||
* console.log('用户总数:', result.total);
|
||||
* console.log('用户列表:', result.users);
|
||||
* ```
|
||||
*
|
||||
* @remarks
|
||||
* - page 从 1 开始
|
||||
* - page_size 范围 1-100
|
||||
* - user_id 按用户 ID 精确搜索
|
||||
* - username 按用户名做前缀搜索
|
||||
*/
|
||||
static async listUsers(request: ListUsersRequest): Promise<ListUsersResponse> {
|
||||
return this.get<ListUsersResponse>('/users', request as unknown as Record<string, unknown>);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新用户状态
|
||||
* @param id - 用户 ID
|
||||
* @param request - 更新状态请求参数
|
||||
* @returns void
|
||||
* @throws {UnauthorizedError} 当未登录时
|
||||
* @throws {ForbiddenError} 当无管理员权限或禁用管理员用户时
|
||||
* @throws {NotFoundError} 当用户不存在时
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* // 禁用用户
|
||||
* await AdminService.updateUserStatus(123, { is_active: false });
|
||||
*
|
||||
* // 启用用户
|
||||
* await AdminService.updateUserStatus(123, { is_active: true });
|
||||
* ```
|
||||
*
|
||||
* @remarks
|
||||
* - 不能禁用管理员用户
|
||||
*/
|
||||
static async updateUserStatus(
|
||||
id: string,
|
||||
request: UpdateUserStatusRequest
|
||||
): Promise<void> {
|
||||
return this.put<void>(`/users/${ id }/status`, request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* 管理员服务模块
|
||||
*
|
||||
* @description
|
||||
* 提供系统配置和用户积分配置管理功能,包括:
|
||||
* - 系统配置管理(创建、查询、更新、删除)
|
||||
* - 用户积分配置管理(创建、查询、更新、删除)
|
||||
*
|
||||
* @remarks
|
||||
* 所有接口都需要管理员权限
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* import { AdminService } from '@/lib/services';
|
||||
*
|
||||
* // 获取系统配置列表
|
||||
* const configs = await AdminService.listSystemConfigs();
|
||||
*
|
||||
* // 创建用户积分配置
|
||||
* await AdminService.createUserPayConfig({
|
||||
* level: 1,
|
||||
* min_score: 0,
|
||||
* max_score: 999,
|
||||
* daily_limit: 10000,
|
||||
* fee_rate: 0.01
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
|
||||
export { AdminService } from './admin.service';
|
||||
export type {
|
||||
SystemConfig,
|
||||
CreateSystemConfigRequest,
|
||||
UpdateSystemConfigRequest,
|
||||
UserPayConfig,
|
||||
CreateUserPayConfigRequest,
|
||||
UpdateUserPayConfigRequest,
|
||||
TaskMeta,
|
||||
DispatchTaskRequest,
|
||||
AdminUser,
|
||||
ListUsersRequest,
|
||||
ListUsersResponse,
|
||||
UpdateUserStatusRequest,
|
||||
} from './types';
|
||||
|
||||
@@ -0,0 +1,231 @@
|
||||
import type { PayLevel } from '@/lib/services/auth/types';
|
||||
|
||||
/**
|
||||
* 系统配置信息
|
||||
*/
|
||||
export interface SystemConfig {
|
||||
/** 配置键 */
|
||||
key: string;
|
||||
/** 配置值 */
|
||||
value: string;
|
||||
/** 配置描述 */
|
||||
description: string;
|
||||
/** 创建时间 */
|
||||
created_at: string;
|
||||
/** 更新时间 */
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建系统配置请求参数
|
||||
*/
|
||||
export interface CreateSystemConfigRequest {
|
||||
/** 配置键(最大64字符) */
|
||||
key: string;
|
||||
/** 配置值(最大255字符) */
|
||||
value: string;
|
||||
/** 配置描述(最大255字符,可选) */
|
||||
description?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新系统配置请求参数
|
||||
*/
|
||||
export interface UpdateSystemConfigRequest {
|
||||
/** 配置值(最大255字符) */
|
||||
value: string;
|
||||
/** 配置描述(最大255字符,可选) */
|
||||
description?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户积分配置信息
|
||||
*/
|
||||
export interface UserPayConfig {
|
||||
/** 配置ID */
|
||||
id: string;
|
||||
/** 积分等级 */
|
||||
level: PayLevel;
|
||||
/** 最低分数 */
|
||||
min_score: number;
|
||||
/** 最高分数(可选) */
|
||||
max_score: number | null;
|
||||
/** 每日限额(可选) */
|
||||
daily_limit: number | null;
|
||||
/** 手续费率(0-1之间的小数,最多2位小数) */
|
||||
fee_rate: number | string;
|
||||
/** 积分费率(0-1之间的小数,最多2位小数) */
|
||||
score_rate: number | string;
|
||||
/** 分发费率(0-1之间的小数,最多2位小数) */
|
||||
distribute_rate: number | string;
|
||||
/** 创建时间 */
|
||||
created_at: string;
|
||||
/** 更新时间 */
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建用户积分配置请求参数
|
||||
*/
|
||||
export interface CreateUserPayConfigRequest {
|
||||
/** 积分等级 */
|
||||
level: PayLevel;
|
||||
/** 最低分数(必须 >= 0) */
|
||||
min_score: number;
|
||||
/** 最高分数(可选,必须大于 min_score) */
|
||||
max_score?: number | null;
|
||||
/** 每日限额(可选) */
|
||||
daily_limit?: number | null;
|
||||
/** 手续费率(0-1之间的小数,最多2位小数) */
|
||||
fee_rate: number | string;
|
||||
/** 积分费率(0-1之间的小数,最多2位小数) */
|
||||
score_rate: number | string;
|
||||
/** 分发费率(0-1之间的小数,最多2位小数) */
|
||||
distribute_rate: number | string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新用户积分配置请求参数
|
||||
*/
|
||||
export interface UpdateUserPayConfigRequest {
|
||||
/** 最低分数(必须 >= 0) */
|
||||
min_score: number;
|
||||
/** 最高分数(可选,必须大于 min_score) */
|
||||
max_score?: number | null;
|
||||
/** 每日限额(可选) */
|
||||
daily_limit?: number | null;
|
||||
/** 手续费率(0-1之间的小数,最多2位小数) */
|
||||
fee_rate: number | string;
|
||||
/** 积分费率(0-1之间的小数,最多2位小数) */
|
||||
score_rate: number | string;
|
||||
/** 分发费率(0-1之间的小数,最多2位小数) */
|
||||
distribute_rate: number | string;
|
||||
}
|
||||
|
||||
// ==================== 任务管理 ====================
|
||||
|
||||
/**
|
||||
* 任务类型响应
|
||||
*/
|
||||
export interface TaskTypeResponse {
|
||||
Type?: string;
|
||||
type?: string;
|
||||
AsynqTask?: string;
|
||||
asynq_task?: string;
|
||||
Name?: string;
|
||||
name?: string;
|
||||
Description?: string;
|
||||
description?: string;
|
||||
SupportsTime?: boolean;
|
||||
supports_time?: boolean;
|
||||
MaxRetry?: number;
|
||||
max_retry?: number;
|
||||
Queue?: string;
|
||||
queue?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 任务元数据
|
||||
*/
|
||||
export interface TaskMeta {
|
||||
/** 任务类型标识 */
|
||||
type: string;
|
||||
/** Asynq 任务名称 */
|
||||
asynq_task: string;
|
||||
/** 任务名称 */
|
||||
name: string;
|
||||
/** 任务描述 */
|
||||
description: string;
|
||||
/** 是否支持时间范围参数 */
|
||||
supports_time: boolean;
|
||||
/** 最大重试次数 */
|
||||
max_retry: number;
|
||||
/** 队列名称 */
|
||||
queue: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 下发任务请求参数
|
||||
*/
|
||||
export interface DispatchTaskRequest {
|
||||
/** 任务类型 */
|
||||
task_type: string;
|
||||
/** 开始时间(可选,仅部分任务支持) */
|
||||
start_time?: string;
|
||||
/** 结束时间(可选,仅部分任务支持) */
|
||||
end_time?: string;
|
||||
/** 用户 ID(可选,仅部分任务需要) */
|
||||
user_id?: string;
|
||||
}
|
||||
|
||||
// ==================== 用户管理 ====================
|
||||
|
||||
/**
|
||||
* 管理员用户信息
|
||||
*/
|
||||
export interface AdminUser {
|
||||
/** 用户 ID */
|
||||
id: string;
|
||||
/** 用户名 */
|
||||
username: string;
|
||||
/** 昵称 */
|
||||
nickname: string;
|
||||
/** 头像 URL */
|
||||
avatar_url: string;
|
||||
/** 信任等级 */
|
||||
trust_level: number;
|
||||
/** 支付积分 */
|
||||
pay_score: number;
|
||||
/** 累计收入 */
|
||||
total_receive: string;
|
||||
/** 累计支出 */
|
||||
total_payment: string;
|
||||
/** 累计社区积分 */
|
||||
total_community: string;
|
||||
/** 社区余额 */
|
||||
community_balance: string;
|
||||
/** 可用余额 */
|
||||
available_balance: string;
|
||||
/** 是否激活 */
|
||||
is_active: boolean;
|
||||
/** 是否管理员 */
|
||||
is_admin: boolean;
|
||||
/** 最后登录时间 */
|
||||
last_login_at: string;
|
||||
/** 创建时间 */
|
||||
created_at: string;
|
||||
/** 更新时间 */
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户列表查询请求参数
|
||||
*/
|
||||
export interface ListUsersRequest {
|
||||
/** 页码(从 1 开始) */
|
||||
page: number;
|
||||
/** 每页数量(1-100) */
|
||||
page_size: number;
|
||||
/** 用户 ID 精确过滤(可选) */
|
||||
user_id?: string;
|
||||
/** 用户名前缀过滤(可选) */
|
||||
username?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户列表响应
|
||||
*/
|
||||
export interface ListUsersResponse {
|
||||
/** 用户列表 */
|
||||
users: AdminUser[];
|
||||
/** 总数 */
|
||||
total: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新用户状态请求参数
|
||||
*/
|
||||
export interface UpdateUserStatusRequest {
|
||||
/** 是否激活 */
|
||||
is_active: boolean;
|
||||
}
|
||||
Reference in New Issue
Block a user