133 lines
3.6 KiB
TypeScript
133 lines
3.6 KiB
TypeScript
import { apiRequest } from './client';
|
|
import { FileType, Priority } from '~/types/enums';
|
|
|
|
/**
|
|
* 将文件转换为二进制数据
|
|
*/
|
|
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);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 上传文件到服务器
|
|
*/
|
|
export async function uploadFileToServer(
|
|
binaryData: ArrayBuffer,
|
|
fileName: string,
|
|
fileType: string,
|
|
documentType: FileType,
|
|
priority: Priority
|
|
): Promise<{ success: boolean; fileId?: string; message?: string; error?: string }> {
|
|
try {
|
|
const formData = new FormData();
|
|
formData.append('file', new Blob([binaryData], { type: fileType }), fileName);
|
|
formData.append('documentType', documentType);
|
|
formData.append('priority', priority);
|
|
|
|
const response = await apiRequest<{ success: boolean; fileId?: string; message?: string }>(
|
|
'/api/files/upload',
|
|
{
|
|
method: 'POST',
|
|
body: formData
|
|
}
|
|
);
|
|
|
|
if (response.error) {
|
|
return { success: false, error: response.error };
|
|
}
|
|
|
|
return { success: true, fileId: response.data?.fileId, message: response.data?.message };
|
|
} catch (error) {
|
|
return { success: false, error: error instanceof Error ? error.message : '上传失败' };
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 上传文件到文档审核系统
|
|
*/
|
|
export async function uploadDocumentToServer(
|
|
binaryData: ArrayBuffer,
|
|
fileName: string,
|
|
fileType: string,
|
|
typeId: string | number,
|
|
priority: string,
|
|
documentNumber?: string | null,
|
|
remark?: string | null,
|
|
isTestDocument: boolean = false
|
|
): Promise<{
|
|
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;
|
|
}> {
|
|
try {
|
|
// 创建FormData对象
|
|
const formData = new FormData();
|
|
|
|
// 将二进制数据转换为Blob并添加到FormData
|
|
const blob = new Blob([binaryData], { type: fileType });
|
|
formData.append('file', blob, fileName);
|
|
|
|
// 将信息添加到一个JSON对象中
|
|
const uploadInfo = {
|
|
type_id: Number(typeId),
|
|
evaluation_level: priority,
|
|
document_number: documentNumber || null,
|
|
remark: remark || null,
|
|
is_test_document: isTestDocument
|
|
};
|
|
|
|
// 添加JSON字符串到FormData
|
|
formData.append('upload_info', JSON.stringify(uploadInfo));
|
|
|
|
// 发送请求
|
|
const response = await fetch('http://172.16.0.55:8000/admin/documents/upload', {
|
|
method: 'POST',
|
|
headers: {
|
|
'X-File-Name': encodeURIComponent(fileName)
|
|
},
|
|
body: formData
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const errorText = await response.text();
|
|
console.error(`上传失败 (${response.status}): ${errorText}`);
|
|
return {
|
|
success: false,
|
|
error: `上传失败: ${response.status} ${response.statusText}`
|
|
};
|
|
}
|
|
|
|
const data = await response.json();
|
|
return data;
|
|
} catch (error) {
|
|
console.error('上传错误:', error);
|
|
return {
|
|
success: false,
|
|
error: error instanceof Error ? error.message : '上传失败'
|
|
};
|
|
}
|
|
}
|