303 lines
9.6 KiB
TypeScript
303 lines
9.6 KiB
TypeScript
import { UPLOAD_URL } from '../../config/api-config';
|
|
|
|
/**
|
|
* 从不同格式的 API 响应中提取数据
|
|
* @param responseData API 响应数据
|
|
* @returns 提取后的数据或 null
|
|
*/
|
|
function extractApiData<T>(responseData: unknown): T | null {
|
|
if (!responseData) return null;
|
|
|
|
// 格式1: { code: number, msg: string, data: T }
|
|
if (typeof responseData === 'object' && responseData !== null &&
|
|
'code' in responseData &&
|
|
'data' in responseData &&
|
|
(responseData as { data: unknown }).data) {
|
|
return (responseData as { data: T }).data;
|
|
}
|
|
|
|
// 格式2: 直接是数据对象
|
|
return responseData as T;
|
|
}
|
|
|
|
// 案卷类型枚举
|
|
export enum CaseType {
|
|
ADMINISTRATIVE_PENALTY = "administrative_penalty",
|
|
ADMINISTRATIVE_PERMIT = "administrative_permit"
|
|
}
|
|
|
|
// 案卷类型到type_id的映射
|
|
export const CASE_TYPE_TO_TYPE_ID: Record<CaseType, number> = {
|
|
[CaseType.ADMINISTRATIVE_PENALTY]: 3, // 行政处罚
|
|
[CaseType.ADMINISTRATIVE_PERMIT]: 2, // 行政许可
|
|
};
|
|
|
|
// 文件上传响应接口
|
|
export interface CrossCheckingFileUploadResponse {
|
|
success: boolean;
|
|
result?: {
|
|
id: number;
|
|
file_name: string;
|
|
file_size: number;
|
|
file_url: string;
|
|
type_id: number;
|
|
type_description: string;
|
|
document_number: string | null;
|
|
storage_type: string;
|
|
is_test_document: boolean;
|
|
remark: string | null;
|
|
background_processing: boolean;
|
|
evaluation_level: string;
|
|
};
|
|
error: string | null;
|
|
}
|
|
|
|
// 上传的文件接口
|
|
export interface CrossCheckingUploadedFile {
|
|
id: string; // 本地生成的唯一ID
|
|
file: File;
|
|
name: string;
|
|
size: number;
|
|
type: string;
|
|
uploadType: 'single' | 'multiple';
|
|
}
|
|
|
|
/**
|
|
* 将文件转换为二进制数据
|
|
*/
|
|
export async function uploadFileToBinary(file: File): Promise<ArrayBuffer> {
|
|
return new Promise((resolve, reject) => {
|
|
const reader = new FileReader();
|
|
reader.onload = () => {
|
|
if (reader.result instanceof ArrayBuffer) {
|
|
resolve(reader.result);
|
|
} else {
|
|
reject(new Error('文件读取失败'));
|
|
}
|
|
};
|
|
reader.onerror = () => reject(new Error('文件读取失败'));
|
|
reader.readAsArrayBuffer(file);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 上传交叉评查文件到服务器
|
|
* @param binaryData 文件的二进制数据
|
|
* @param fileName 文件名
|
|
* @param fileType 文件类型
|
|
* @param typeId 文档类型ID
|
|
* @param priority 优先级
|
|
* @param documentNumber 文档编号(可选)
|
|
* @param remark 备注信息(可选)
|
|
* @param isTestDocument 是否为测试文档
|
|
* @param documentId 关联的文档ID(用于附件上传)
|
|
* @param isReupload 是否为重新上传
|
|
* @returns 上传结果
|
|
*/
|
|
export async function uploadCrossCheckingDocument(
|
|
binaryData: ArrayBuffer,
|
|
fileName: string,
|
|
fileType: string,
|
|
typeId: number,
|
|
priority: string = 'normal',
|
|
documentNumber: string = '',
|
|
remark: string = '',
|
|
isTestDocument: boolean = false,
|
|
documentId: number | null = null,
|
|
isReupload: boolean = false,
|
|
token: string | null = null
|
|
): Promise<{data: CrossCheckingFileUploadResponse; error?: never} | {data?: never; error: string; status?: number}> {
|
|
try {
|
|
console.log('【交叉评查上传】开始上传文档:', { fileName, fileSize: binaryData.byteLength, typeId });
|
|
|
|
// 创建FormData对象
|
|
const formData = new FormData();
|
|
|
|
// 将二进制数据转换为Blob并添加到FormData
|
|
const blob = new Blob([binaryData], { type: fileType });
|
|
formData.append('file', blob, fileName);
|
|
console.log('【交叉评查上传】Blob已创建,文件大小:', blob.size);
|
|
|
|
// 将信息添加到一个JSON对象中
|
|
const uploadInfo = {
|
|
type_id: typeId,
|
|
evaluation_level: priority,
|
|
document_number: documentNumber || null,
|
|
remark: remark || null,
|
|
is_test_document: isTestDocument,
|
|
document_id: documentId || null,
|
|
is_reupload: isReupload
|
|
};
|
|
|
|
// 添加JSON字符串到FormData
|
|
formData.append('upload_info', JSON.stringify(uploadInfo));
|
|
console.log('【交叉评查上传】FormData准备完成:', JSON.stringify(uploadInfo));
|
|
|
|
// 根据是否有documentId决定使用哪个接口
|
|
const uploadEndpoint = '/batch_upload';
|
|
const uploadUrl = UPLOAD_URL + uploadEndpoint;
|
|
console.log('【交叉评查上传】准备发送请求到服务器:', uploadUrl);
|
|
|
|
// 发送请求
|
|
try {
|
|
console.log('【交叉评查上传】开始fetch请求...');
|
|
const headers: HeadersInit = {
|
|
'X-File-Name': encodeURIComponent(fileName),
|
|
};
|
|
|
|
if (token) {
|
|
headers['Authorization'] = `Bearer ${token}`;
|
|
}
|
|
|
|
const response = await fetch(uploadUrl, {
|
|
method: 'POST',
|
|
headers,
|
|
body: formData
|
|
});
|
|
|
|
console.log('【交叉评查上传】收到服务器响应:', { status: response.status, statusText: response.statusText });
|
|
|
|
if (!response.ok) {
|
|
const errorText = await response.text();
|
|
console.error(`【交叉评查上传】上传失败 (${response.status}): ${errorText}`);
|
|
return {
|
|
error: `上传失败: ${response.status} ${response.statusText} - ${errorText}`,
|
|
status: response.status
|
|
};
|
|
}
|
|
|
|
console.log('【交叉评查上传】开始解析JSON响应');
|
|
let responseData;
|
|
try {
|
|
responseData = await response.json();
|
|
console.log('【交叉评查上传】JSON响应解析成功:', responseData);
|
|
} catch (jsonError) {
|
|
console.error('【交叉评查上传】JSON解析失败:', jsonError);
|
|
return {
|
|
error: `解析响应JSON失败: ${jsonError instanceof Error ? jsonError.message : '未知错误'}`,
|
|
status: 500
|
|
};
|
|
}
|
|
|
|
const extractedData = extractApiData<CrossCheckingFileUploadResponse>(responseData);
|
|
console.log('【交叉评查上传】提取的数据:', extractedData);
|
|
|
|
if (!extractedData) {
|
|
console.error('【交叉评查上传】无法提取数据');
|
|
return { error: '处理上传响应失败', status: 500 };
|
|
}
|
|
|
|
console.log('【交叉评查上传】上传成功,返回数据');
|
|
return { data: extractedData as CrossCheckingFileUploadResponse };
|
|
} catch (fetchError) {
|
|
console.error('【交叉评查上传】fetch请求失败:', fetchError);
|
|
return {
|
|
error: `fetch请求错误: ${fetchError instanceof Error ? fetchError.message : '未知错误'}`,
|
|
status: 500
|
|
};
|
|
}
|
|
} catch (error) {
|
|
console.error('【交叉评查上传】上传过程中发生错误:', error);
|
|
return {
|
|
error: error instanceof Error ? error.message : '上传失败',
|
|
status: 500
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 批量上传交叉评查文件
|
|
* @param files 文件列表
|
|
* @param typeId 文档类型ID
|
|
* @param priority 优先级
|
|
* @param documentNumber 文档编号
|
|
* @param remark 备注信息
|
|
* @param isTestDocument 是否为测试文档
|
|
* @returns 上传结果列表
|
|
*/
|
|
export async function batchUploadCrossCheckingFiles(
|
|
files: CrossCheckingUploadedFile[],
|
|
typeId: number,
|
|
priority: string = 'normal',
|
|
documentNumber: string = '',
|
|
remark: string = '',
|
|
isTestDocument: boolean = false
|
|
): Promise<{
|
|
successes: Array<{file: CrossCheckingUploadedFile; result: CrossCheckingFileUploadResponse}>;
|
|
failures: Array<{file: CrossCheckingUploadedFile; error: string}>;
|
|
}> {
|
|
const successes: Array<{file: CrossCheckingUploadedFile; result: CrossCheckingFileUploadResponse}> = [];
|
|
const failures: Array<{file: CrossCheckingUploadedFile; error: string}> = [];
|
|
|
|
console.log('【交叉评查批量上传】开始批量上传文件,文件数量:', files.length);
|
|
|
|
for (const fileInfo of files) {
|
|
try {
|
|
console.log('【交叉评查批量上传】上传文件:', fileInfo.name);
|
|
|
|
// 转换文件为二进制格式
|
|
const binaryData = await uploadFileToBinary(fileInfo.file);
|
|
|
|
// 上传文件
|
|
const result = await uploadCrossCheckingDocument(
|
|
binaryData,
|
|
fileInfo.file.name,
|
|
fileInfo.file.type,
|
|
typeId,
|
|
priority,
|
|
documentNumber,
|
|
remark,
|
|
isTestDocument,
|
|
null, // 交叉评查文件通常没有关联的文档ID
|
|
false
|
|
);
|
|
|
|
if (result.error) {
|
|
console.error('【交叉评查批量上传】文件上传失败:', fileInfo.name, result.error);
|
|
failures.push({
|
|
file: fileInfo,
|
|
error: result.error
|
|
});
|
|
} else if (result.data) {
|
|
console.log('【交叉评查批量上传】文件上传成功:', fileInfo.name);
|
|
successes.push({
|
|
file: fileInfo,
|
|
result: result.data
|
|
});
|
|
}
|
|
} catch (error) {
|
|
console.error('【交叉评查批量上传】处理文件时发生错误:', fileInfo.name, error);
|
|
failures.push({
|
|
file: fileInfo,
|
|
error: error instanceof Error ? error.message : '上传失败'
|
|
});
|
|
}
|
|
}
|
|
|
|
console.log('【交叉评查批量上传】批量上传完成,成功:', successes.length, '失败:', failures.length);
|
|
return { successes, failures };
|
|
}
|
|
|
|
/**
|
|
* 生成唯一文件ID
|
|
* @returns 唯一ID字符串
|
|
*/
|
|
export function generateFileId(): string {
|
|
return `cross_checking_${Date.now()}_${Math.random().toString(36).substring(2, 15)}`;
|
|
}
|
|
|
|
/**
|
|
* 格式化文件大小显示
|
|
* @param bytes 字节数
|
|
* @returns 格式化后的文件大小字符串
|
|
*/
|
|
export function formatFileSize(bytes: number): string {
|
|
if (bytes === 0) return '0 Bytes';
|
|
|
|
const k = 1024;
|
|
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
|
|
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
|
|
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
|
|
}
|