dab0835605
2. 对接数据看板的数据。 3. 添加入口模块管理的页面。
243 lines
5.9 KiB
TypeScript
243 lines
5.9 KiB
TypeScript
/**
|
|
* 入口模块管理 API 客户端
|
|
* 提供入口模块的增删改查功能
|
|
*/
|
|
|
|
import { postgrestGet, postgrestPost, postgrestPut, postgrestDelete } from "../postgrest-client";
|
|
|
|
/**
|
|
* 入口模块数据接口
|
|
*/
|
|
export interface EntryModule {
|
|
id?: number;
|
|
name: string;
|
|
description?: string;
|
|
path?: string; // logo图片路径
|
|
areas?: string[]; // 地区数组
|
|
created_at?: string;
|
|
updated_at?: string;
|
|
}
|
|
|
|
/**
|
|
* 入口模块搜索参数
|
|
*/
|
|
export interface EntryModuleSearchParams {
|
|
name?: string;
|
|
area?: string;
|
|
page?: number;
|
|
pageSize?: number;
|
|
}
|
|
|
|
/**
|
|
* 入口模块列表响应
|
|
*/
|
|
export interface EntryModulesResponse {
|
|
modules: EntryModule[];
|
|
total: number;
|
|
}
|
|
|
|
/**
|
|
* 获取入口模块列表
|
|
* @param searchParams 搜索参数
|
|
* @param jwtToken JWT令牌
|
|
* @returns 入口模块列表和总数
|
|
*/
|
|
export async function getEntryModules(
|
|
searchParams: EntryModuleSearchParams = {},
|
|
jwtToken?: string | null
|
|
): Promise<{ data?: EntryModulesResponse; error?: string }> {
|
|
try {
|
|
const { name, area, page = 1, pageSize = 10 } = searchParams;
|
|
|
|
// 构建过滤条件
|
|
const filter: Record<string, string> = {};
|
|
|
|
if (name) {
|
|
filter.name = `ilike.*${name}*`;
|
|
}
|
|
|
|
// 如果有地区筛选,使用 JSONB 查询
|
|
if (area) {
|
|
filter.areas = `cs.{"${area}"}`; // cs = contains (JSONB数组包含)
|
|
}
|
|
|
|
// 计算分页
|
|
const offset = (page - 1) * pageSize;
|
|
|
|
// 构建查询参数(一次请求获取数据和总数)
|
|
const queryParams: any = {
|
|
select: "*",
|
|
order: "created_at.desc",
|
|
limit: pageSize,
|
|
offset: offset,
|
|
headers: {
|
|
'Prefer': 'count=exact'
|
|
},
|
|
token: jwtToken
|
|
};
|
|
|
|
// 只在有过滤条件时添加 filter
|
|
if (Object.keys(filter).length > 0) {
|
|
queryParams.filter = filter;
|
|
}
|
|
|
|
// 获取分页数据
|
|
const result = await postgrestGet<EntryModule[]>("entry_modules", queryParams);
|
|
|
|
if (result.error) {
|
|
return { error: result.error };
|
|
}
|
|
|
|
// 从 Content-Range 头获取总数
|
|
let totalCount = 0;
|
|
const responseWithHeaders = result as {
|
|
data: unknown;
|
|
headers?: Record<string, string>;
|
|
};
|
|
|
|
if (responseWithHeaders.headers) {
|
|
const rangeHeader = responseWithHeaders.headers['content-range'];
|
|
if (rangeHeader) {
|
|
const total = rangeHeader.split('/')[1];
|
|
if (total !== '*') {
|
|
totalCount = parseInt(total, 10);
|
|
}
|
|
}
|
|
}
|
|
|
|
return {
|
|
data: {
|
|
modules: result.data || [],
|
|
total: totalCount || (result.data?.length || 0)
|
|
}
|
|
};
|
|
} catch (error) {
|
|
console.error("获取入口模块列表失败:", error);
|
|
return { error: error instanceof Error ? error.message : "获取入口模块列表失败" };
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 根据ID获取入口模块
|
|
* @param id 入口模块ID
|
|
* @param jwtToken JWT令牌
|
|
* @returns 入口模块数据
|
|
*/
|
|
export async function getEntryModuleById(
|
|
id: number,
|
|
jwtToken?: string | null
|
|
): Promise<{ data?: EntryModule; error?: string }> {
|
|
try {
|
|
const result = await postgrestGet<EntryModule[]>("entry_modules", {
|
|
filter: { id: `eq.${id}` },
|
|
token: jwtToken
|
|
});
|
|
|
|
if (result.error) {
|
|
return { error: result.error };
|
|
}
|
|
|
|
const module = result.data?.[0];
|
|
if (!module) {
|
|
return { error: "入口模块不存在" };
|
|
}
|
|
|
|
return { data: module };
|
|
} catch (error) {
|
|
console.error("获取入口模块失败:", error);
|
|
return { error: error instanceof Error ? error.message : "获取入口模块失败" };
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 创建入口模块
|
|
* @param module 入口模块数据
|
|
* @param jwtToken JWT令牌
|
|
* @returns 创建的入口模块
|
|
*/
|
|
export async function createEntryModule(
|
|
module: Omit<EntryModule, "id" | "created_at" | "updated_at">,
|
|
jwtToken?: string | null
|
|
): Promise<{ data?: EntryModule; error?: string }> {
|
|
try {
|
|
const result = await postgrestPost<EntryModule[], EntryModule>(
|
|
"entry_modules",
|
|
module as EntryModule,
|
|
jwtToken
|
|
);
|
|
|
|
if (result.error) {
|
|
return { error: result.error };
|
|
}
|
|
|
|
const createdModule = Array.isArray(result.data) ? result.data[0] : result.data;
|
|
return { data: createdModule as EntryModule };
|
|
} catch (error) {
|
|
console.error("创建入口模块失败:", error);
|
|
return { error: error instanceof Error ? error.message : "创建入口模块失败" };
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 更新入口模块
|
|
* @param id 入口模块ID
|
|
* @param module 更新的入口模块数据
|
|
* @param jwtToken JWT令牌
|
|
* @returns 更新的入口模块
|
|
*/
|
|
export async function updateEntryModule(
|
|
id: number,
|
|
module: Partial<Omit<EntryModule, "id" | "created_at" | "updated_at">>,
|
|
jwtToken?: string | null
|
|
): Promise<{ data?: EntryModule; error?: string }> {
|
|
try {
|
|
const result = await postgrestPut<EntryModule[], Partial<EntryModule>>(
|
|
"entry_modules",
|
|
module,
|
|
{ id: `eq.${id}` },
|
|
jwtToken
|
|
);
|
|
|
|
if (result.error) {
|
|
return { error: result.error };
|
|
}
|
|
|
|
const updatedModule = Array.isArray(result.data) ? result.data[0] : result.data;
|
|
return { data: updatedModule as EntryModule };
|
|
} catch (error) {
|
|
console.error("更新入口模块失败:", error);
|
|
return { error: error instanceof Error ? error.message : "更新入口模块失败" };
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 删除入口模块
|
|
* @param id 入口模块ID
|
|
* @param jwtToken JWT令牌
|
|
* @returns 是否成功
|
|
*/
|
|
export async function deleteEntryModule(
|
|
id: number,
|
|
jwtToken?: string | null
|
|
): Promise<{ success: boolean; error?: string }> {
|
|
try {
|
|
const result = await postgrestDelete(
|
|
"entry_modules",
|
|
{ id: `eq.${id}` },
|
|
jwtToken
|
|
);
|
|
|
|
if (result.error) {
|
|
return { success: false, error: result.error };
|
|
}
|
|
|
|
return { success: true };
|
|
} catch (error) {
|
|
console.error("删除入口模块失败:", error);
|
|
return {
|
|
success: false,
|
|
error: error instanceof Error ? error.message : "删除入口模块失败"
|
|
};
|
|
}
|
|
}
|