超管后台 - 对接获取管理员列表

This commit is contained in:
柳清爽
2025-04-09 18:35:36 +08:00
parent c451d5b0eb
commit d33988aa4e
3 changed files with 200 additions and 14 deletions

View File

@@ -0,0 +1,74 @@
import { getConfig } from './config';
// 管理员接口数据类型定义
export interface Administrator {
id: number;
username: string;
name: string;
role: string;
status: number;
createdAt: string;
lastLogin: string;
permissions: string[];
}
// 分页响应数据类型
export interface PaginatedResponse<T> {
list: T[];
total: number;
page: number;
limit: number;
}
// API响应数据结构
export interface ApiResponse<T> {
code: number;
msg: string;
data: T | null;
}
/**
* 获取管理员列表
* @param page 页码
* @param limit 每页数量
* @param keyword 搜索关键词
* @returns 管理员列表
*/
export async function getAdministrators(
page: number = 1,
limit: number = 10,
keyword: string = ''
): Promise<ApiResponse<PaginatedResponse<Administrator>>> {
const { apiBaseUrl } = getConfig();
// 构建查询参数
const params = new URLSearchParams();
params.append('page', page.toString());
params.append('limit', limit.toString());
if (keyword) {
params.append('keyword', keyword);
}
try {
// 发送请求
const response = await fetch(`${apiBaseUrl}/administrator/list?${params.toString()}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});
if (!response.ok) {
throw new Error(`请求失败,状态码: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('获取管理员列表失败:', error);
return {
code: 500,
msg: '获取管理员列表失败',
data: null
};
}
}

12
SuperAdmin/lib/config.ts Normal file
View File

@@ -0,0 +1,12 @@
/**
* 获取应用配置
* @returns 应用配置
*/
export function getConfig() {
// 优先获取环境变量中配置的API地址
const apiBaseUrl = process.env.NEXT_PUBLIC_API_BASE_URL || 'http://yishi.com';
return {
apiBaseUrl
};
}