Files
smart-crop-ui/crop-x/src/app/(app)/central-config/user/department/components/departmentCreateApi.ts

130 lines
3.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* filekorolheader: 部门管理API接口 - 部门数据CRUD操作接口服务
* 功能API请求封装、数据转换、错误处理、部门树形管理
* 路径:/central-config/user/department/components/departmentApi
* 规范遵循crop-x/docs/开发项目规范.md使用SDK API调用TypeScript类型安全
*/
import { getAuthToken } from "@/utils/token";
import {
createDepartmentApiV1DepartmentsPost,
} from "@/lib/api/sdk.gen";
import {
Department,
CreateDepartmentForm,
} from '../types';
/**
* API请求创建部门的数据结构对应Python字段
*/
export interface CreateDepartmentApiRequest {
code: string; // 部门编码
name: string; // 部门名称
description?: string; // 部门描述
manager_name?: string; // 管理者名称
manager_phone?: string; // 管理者电话
manager_email?: string; // 管理者邮箱(必须符合邮箱规则)
parent_id: string; // 父级部门ID一级部门为空字符串
order_index: number; // 排序索引0-10000的整数
status: string; // 状态,默认"active"表示有效
}
/**
* API响应数据结构
*/
export interface CreateDepartmentApiResponse {
code: string;
name: string;
description?: string;
manager_name?: string;
manager_phone?: string;
manager_email?: string;
parent_id: string;
order_index: number;
status: string;
}
/**
* 将React表单数据驼峰命名法转换为API请求数据Python蛇形命名法
*/
export function transformCreateDepartmentData(formData: CreateDepartmentForm): CreateDepartmentApiRequest {
return {
code: formData.code,
name: formData.name,
description: formData.description || null,
manager_name: formData.manager || undefined,
manager_phone: formData.phone || undefined,
manager_email: formData.email || undefined,
parent_id: formData.parentId || "",
order_index: formData.sort || 0,
status: formData.status || "active",
};
}
/**
* 邮箱格式验证
*/
export function isValidEmail(email: string): boolean {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
/**
* 创建部门API调用
*/
export async function createDepartment(formData: CreateDepartmentForm): Promise<CreateDepartmentApiResponse> {
try {
// 获取认证token
const token = getAuthToken();
console.log('创建部门API调用参数:', formData);
// 转换表单数据为API请求格式
const apiRequestData = transformCreateDepartmentData(formData);
// 邮箱格式验证
if (apiRequestData.manager_email && !isValidEmail(apiRequestData.manager_email)) {
throw new Error('邮箱格式不正确');
}
// 使用真正的SDK API调用
const response = await createDepartmentApiV1DepartmentsPost({
body: apiRequestData,
headers: token ? {
'Authorization': `Bearer ${token}`,
} : undefined,
});
if (response.error) {
throw new Error(`API error: ${response.error.message || 'Unknown error'}`);
}
const data = response.data as CreateDepartmentApiResponse;
console.log('创建部门API响应:', data);
return data;
} catch (error) {
console.error('Failed to create department:', error);
throw error;
}
}
/**
* 生成随机排序索引0-10000
*/
export function generateRandomOrderIndex(): number {
return Math.floor(Math.random() * 10001);
}
/**
* 防抖函数
*/
export function debounce<T extends (...args: any[]) => any>(
func: T,
delay: number
): (...args: Parameters<T>) => void {
let timeoutId: NodeJS.Timeout;
return (...args: Parameters<T>) => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => func(...args), delay);
};
}