新增合同模板上传功能,支持选择PDF和Word格式文件,并实现上传逻辑及状态管理。
This commit is contained in:
@@ -136,6 +136,85 @@ export async function uploadFileToBinary(file: File): Promise<ArrayBuffer> {
|
||||
* @param jwtToken JWT token
|
||||
* @returns 上传结果
|
||||
*/
|
||||
/**
|
||||
* 上传合同模板(用于与合同文档结构对比)
|
||||
* @param file 模板文件
|
||||
* @param documentId 源合同文档ID
|
||||
* @param comparisonId 已有对比记录ID(可选)
|
||||
* @param jwtToken JWT token
|
||||
* @returns 上传结果
|
||||
*/
|
||||
export async function uploadContractTemplate(
|
||||
file: File,
|
||||
documentId: number,
|
||||
comparisonId?: number,
|
||||
jwtToken?: string
|
||||
): Promise<{data: FileUploadResponse; error?: never} | {data?: never; error: string; status?: number}> {
|
||||
try {
|
||||
console.log('【合同模板上传】开始上传模板:', { fileName: file.name, documentId, comparisonId });
|
||||
|
||||
// 创建FormData对象
|
||||
const formData = new FormData();
|
||||
|
||||
// 添加文件
|
||||
formData.append('file', file);
|
||||
|
||||
// 添加上传信息
|
||||
const uploadInfo = {
|
||||
document_id: documentId,
|
||||
...(comparisonId && { comparison_id: comparisonId })
|
||||
};
|
||||
|
||||
formData.append('upload_info', JSON.stringify(uploadInfo));
|
||||
|
||||
// 构建请求URL
|
||||
const uploadUrl = `${UPLOAD_URL}/upload_contract_template`;
|
||||
console.log('【合同模板上传】准备发送请求到服务器:', uploadUrl);
|
||||
|
||||
// 设置请求头
|
||||
const headers: HeadersInit = {
|
||||
'Accept': 'application/json'
|
||||
};
|
||||
|
||||
if (jwtToken) {
|
||||
headers['Authorization'] = `Bearer ${jwtToken}`;
|
||||
}
|
||||
|
||||
// 发送请求
|
||||
const response = await fetch(uploadUrl, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: formData
|
||||
});
|
||||
|
||||
console.log('【合同模板上传】服务器响应状态:', response.status);
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
console.error('【合同模板上传】服务器返回错误:', errorText);
|
||||
return {
|
||||
error: `服务器错误: ${response.status} ${response.statusText}`,
|
||||
status: response.status
|
||||
};
|
||||
}
|
||||
|
||||
const result = await response.json();
|
||||
console.log('【合同模板上传】服务器返回结果:', result);
|
||||
|
||||
if (result.success) {
|
||||
return { data: result.result };
|
||||
} else {
|
||||
return { error: result.error || '合同模板上传失败' };
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('【合同模板上传】上传过程中发生错误:', error);
|
||||
return {
|
||||
error: error instanceof Error ? error.message : '合同模板上传过程中发生未知错误'
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 合同文档追加附件并合并
|
||||
* @param documentId 合同文档ID
|
||||
|
||||
Reference in New Issue
Block a user