d5827a2146
2. 将查看文档评查结果详情对接接口,采用接口的方式进行查询。
358 lines
10 KiB
TypeScript
358 lines
10 KiB
TypeScript
/**
|
||
* 入口模块管理 API 客户端
|
||
* 提供入口模块的增删改查功能
|
||
* API 文档: auth_doc/ENTRY_MODULE_API.md
|
||
*/
|
||
|
||
import { get, post, put, del } from "../axios-client";
|
||
|
||
/**
|
||
* 地区配置接口
|
||
*/
|
||
export interface AreaConfig {
|
||
area: string; // 地区名称(如:梅州、云浮、揭阳、潮州)
|
||
enabled: boolean; // 是否启用(默认 true)
|
||
sort_order: number; // 排序号(默认 0)
|
||
}
|
||
|
||
/**
|
||
* 入口模块数据接口
|
||
*/
|
||
export interface EntryModule {
|
||
id?: number;
|
||
name: string;
|
||
description?: string | null;
|
||
path?: string | null; // logo图片路径
|
||
areas?: AreaConfig[] | null; // 地区配置列表
|
||
created_at?: string;
|
||
updated_at?: string;
|
||
}
|
||
|
||
/**
|
||
* 入口模块搜索参数
|
||
*/
|
||
export interface EntryModuleSearchParams {
|
||
name?: string;
|
||
area?: string;
|
||
page?: number;
|
||
page_size?: number; // 注意:API使用page_size而不是pageSize
|
||
}
|
||
|
||
/**
|
||
* 入口模块列表响应
|
||
*/
|
||
export interface EntryModulesResponse {
|
||
modules: EntryModule[];
|
||
total: number;
|
||
}
|
||
|
||
/**
|
||
* API统一响应格式
|
||
* 注意:后端返回的字段是 msg 而不是 message,code 为 0 表示成功
|
||
*/
|
||
interface ApiResponse<T> {
|
||
code: number; // 0 表示成功
|
||
msg: string; // 消息(不是 message)
|
||
data: T;
|
||
}
|
||
|
||
/**
|
||
* 列表数据响应格式
|
||
*/
|
||
interface ListResponse<T> {
|
||
total: number;
|
||
page: number;
|
||
page_size: number;
|
||
items: T[];
|
||
}
|
||
|
||
/**
|
||
* 获取入口模块列表
|
||
* @param searchParams 搜索参数
|
||
* @returns 入口模块列表和总数
|
||
*
|
||
* 注意:不需要传递 JWT,axios-client 会自动从 localStorage 读取并添加
|
||
*/
|
||
export async function getEntryModules(
|
||
searchParams: EntryModuleSearchParams = {}
|
||
): Promise<{ data?: EntryModulesResponse; error?: string }> {
|
||
try {
|
||
const { name, area, page = 1, page_size = 10 } = searchParams;
|
||
|
||
// 构建查询参数
|
||
const queryParams: Record<string, any> = {
|
||
page,
|
||
page_size
|
||
};
|
||
|
||
if (name) {
|
||
queryParams.name = name;
|
||
}
|
||
|
||
if (area) {
|
||
queryParams.area = area;
|
||
}
|
||
|
||
// 调用 API
|
||
const response = await get<ApiResponse<ListResponse<EntryModule>>>(
|
||
'/api/v3/entry-modules',
|
||
queryParams
|
||
);
|
||
|
||
if (response.error) {
|
||
console.error('❌ [getEntryModules] API错误:', response.error);
|
||
return { error: response.error };
|
||
}
|
||
|
||
// 🔑 解析响应数据
|
||
// axios-client 返回: { data: {后端JSON}, status: 200 }
|
||
// 后端返回: { code: 0, msg: "成功", data: { total, items } }
|
||
const backendResponse = response.data;
|
||
|
||
if (!backendResponse) {
|
||
console.error('❌ [getEntryModules] 响应为空');
|
||
return { error: '响应为空' };
|
||
}
|
||
|
||
// 检查后端返回的 code(0 表示成功)
|
||
if (backendResponse.code !== 0) {
|
||
console.error('❌ [getEntryModules] 后端返回错误:', backendResponse.msg);
|
||
return { error: backendResponse.msg || '请求失败' };
|
||
}
|
||
|
||
const apiData = backendResponse.data;
|
||
if (!apiData) {
|
||
console.error('❌ [getEntryModules] data 字段为空');
|
||
return { error: '数据为空' };
|
||
}
|
||
|
||
return {
|
||
data: {
|
||
modules: apiData.items || [],
|
||
total: apiData.total || 0
|
||
}
|
||
};
|
||
} catch (error) {
|
||
console.error("❌ [getEntryModules] 获取入口模块列表失败:", error);
|
||
return { error: error instanceof Error ? error.message : "获取入口模块列表失败" };
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 根据ID获取入口模块
|
||
* @param id 入口模块ID
|
||
* @returns 入口模块数据
|
||
*
|
||
* 注意:不需要传递 JWT,axios-client 会自动从 localStorage 读取并添加
|
||
*/
|
||
export async function getEntryModuleById(
|
||
id: number
|
||
): Promise<{ data?: EntryModule; error?: string }> {
|
||
try {
|
||
// 调用 API
|
||
const response = await get<ApiResponse<EntryModule>>(
|
||
`/api/v3/entry-modules/${id}`
|
||
);
|
||
|
||
if (response.error) {
|
||
console.error('❌ [getEntryModuleById] API错误:', response.error);
|
||
return { error: response.error };
|
||
}
|
||
|
||
const backendResponse = response.data;
|
||
if (!backendResponse || backendResponse.code !== 0) {
|
||
console.error('❌ [getEntryModuleById] 后端返回错误:', backendResponse?.msg);
|
||
return { error: backendResponse?.msg || '获取失败' };
|
||
}
|
||
|
||
const module = backendResponse.data;
|
||
if (!module) {
|
||
console.error('❌ [getEntryModuleById] 入口模块不存在');
|
||
return { error: "入口模块不存在" };
|
||
}
|
||
|
||
return { data: module };
|
||
} catch (error) {
|
||
console.error("❌ [getEntryModuleById] 获取入口模块失败:", error);
|
||
return { error: error instanceof Error ? error.message : "获取入口模块失败" };
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 创建入口模块
|
||
* @param module 入口模块数据
|
||
* @returns 创建的入口模块
|
||
*
|
||
* 注意:不需要传递 JWT,axios-client 会自动从 localStorage 读取并添加
|
||
*/
|
||
export async function createEntryModule(
|
||
module: {
|
||
name: string;
|
||
description?: string;
|
||
path?: string | null;
|
||
areas?: string[] | AreaConfig[]; // 兼容两种格式
|
||
}
|
||
): Promise<{ data?: EntryModule; error?: string }> {
|
||
try {
|
||
// 转换 areas 格式(如果是字符串数组,转换为 AreaConfig 数组)
|
||
let areasConfig: AreaConfig[] | undefined;
|
||
if (module.areas && Array.isArray(module.areas)) {
|
||
if (typeof module.areas[0] === 'string') {
|
||
// 字符串数组转换为 AreaConfig 数组
|
||
areasConfig = (module.areas as string[]).map((area, index) => ({
|
||
area,
|
||
enabled: true,
|
||
sort_order: index + 1
|
||
}));
|
||
} else {
|
||
// 已经是 AreaConfig 数组
|
||
areasConfig = module.areas as AreaConfig[];
|
||
}
|
||
}
|
||
|
||
// 构建请求体
|
||
const requestBody = {
|
||
name: module.name,
|
||
description: module.description || undefined,
|
||
path: module.path || undefined,
|
||
areas: areasConfig
|
||
};
|
||
|
||
// 调用 API
|
||
const response = await post<ApiResponse<EntryModule>>(
|
||
'/api/v3/entry-modules',
|
||
requestBody
|
||
);
|
||
|
||
if (response.error) {
|
||
console.error('❌ [createEntryModule] API错误:', response.error);
|
||
return { error: response.error };
|
||
}
|
||
|
||
const backendResponse = response.data;
|
||
if (!backendResponse || backendResponse.code !== 0) {
|
||
console.error('❌ [createEntryModule] 后端返回错误:', backendResponse?.msg);
|
||
return { error: backendResponse?.msg || '创建失败' };
|
||
}
|
||
|
||
const createdModule = backendResponse.data;
|
||
if (!createdModule) {
|
||
console.error('❌ [createEntryModule] 响应数据格式错误');
|
||
return { error: '创建失败:响应数据格式错误' };
|
||
}
|
||
|
||
return { data: createdModule };
|
||
} catch (error) {
|
||
console.error("❌ [createEntryModule] 创建入口模块失败:", error);
|
||
return { error: error instanceof Error ? error.message : "创建入口模块失败" };
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 更新入口模块
|
||
* @param id 入口模块ID
|
||
* @param module 更新的入口模块数据
|
||
* @returns 更新的入口模块
|
||
*
|
||
* 注意:不需要传递 JWT,axios-client 会自动从 localStorage 读取并添加
|
||
*/
|
||
export async function updateEntryModule(
|
||
id: number,
|
||
module: {
|
||
name?: string;
|
||
description?: string;
|
||
path?: string | null;
|
||
areas?: string[] | AreaConfig[]; // 兼容两种格式
|
||
}
|
||
): Promise<{ data?: EntryModule; error?: string }> {
|
||
try {
|
||
// 转换 areas 格式(如果是字符串数组,转换为 AreaConfig 数组)
|
||
let areasConfig: AreaConfig[] | undefined;
|
||
if (module.areas && Array.isArray(module.areas)) {
|
||
if (typeof module.areas[0] === 'string') {
|
||
// 字符串数组转换为 AreaConfig 数组
|
||
areasConfig = (module.areas as string[]).map((area, index) => ({
|
||
area,
|
||
enabled: true,
|
||
sort_order: index + 1
|
||
}));
|
||
} else {
|
||
// 已经是 AreaConfig 数组
|
||
areasConfig = module.areas as AreaConfig[];
|
||
}
|
||
}
|
||
|
||
// 构建请求体(只包含需要更新的字段)
|
||
const requestBody: any = {};
|
||
if (module.name !== undefined) requestBody.name = module.name;
|
||
if (module.description !== undefined) requestBody.description = module.description;
|
||
if (module.path !== undefined) requestBody.path = module.path;
|
||
if (areasConfig !== undefined) requestBody.areas = areasConfig;
|
||
|
||
// 调用 API
|
||
const response = await put<ApiResponse<EntryModule>>(
|
||
`/api/v3/entry-modules/${id}`,
|
||
requestBody
|
||
);
|
||
|
||
if (response.error) {
|
||
console.error('❌ [updateEntryModule] API错误:', response.error);
|
||
return { error: response.error };
|
||
}
|
||
|
||
const backendResponse = response.data;
|
||
if (!backendResponse || backendResponse.code !== 0) {
|
||
console.error('❌ [updateEntryModule] 后端返回错误:', backendResponse?.msg);
|
||
return { error: backendResponse?.msg || '更新失败' };
|
||
}
|
||
|
||
const updatedModule = backendResponse.data;
|
||
if (!updatedModule) {
|
||
console.error('❌ [updateEntryModule] 响应数据格式错误');
|
||
return { error: '更新失败:响应数据格式错误' };
|
||
}
|
||
|
||
return { data: updatedModule };
|
||
} catch (error) {
|
||
console.error("❌ [updateEntryModule] 更新入口模块失败:", error);
|
||
return { error: error instanceof Error ? error.message : "更新入口模块失败" };
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 删除入口模块
|
||
* @param id 入口模块ID
|
||
* @returns 是否成功
|
||
*
|
||
* 注意:不需要传递 JWT,axios-client 会自动从 localStorage 读取并添加
|
||
*/
|
||
export async function deleteEntryModule(
|
||
id: number
|
||
): Promise<{ success: boolean; error?: string }> {
|
||
try {
|
||
// 调用 API
|
||
const response = await del<ApiResponse<{ id: number; deleted: boolean }>>(
|
||
`/api/v3/entry-modules/${id}`
|
||
);
|
||
|
||
if (response.error) {
|
||
console.error('❌ [deleteEntryModule] API错误:', response.error);
|
||
return { success: false, error: response.error };
|
||
}
|
||
|
||
const backendResponse = response.data;
|
||
if (!backendResponse || backendResponse.code !== 0) {
|
||
console.error('❌ [deleteEntryModule] 删除失败:', backendResponse?.msg);
|
||
return { success: false, error: backendResponse?.msg || '删除失败' };
|
||
}
|
||
|
||
return { success: true };
|
||
} catch (error) {
|
||
console.error("❌ [deleteEntryModule] 删除入口模块失败:", error);
|
||
return {
|
||
success: false,
|
||
error: error instanceof Error ? error.message : "删除入口模块失败"
|
||
};
|
||
}
|
||
}
|