Files
leaudit-platform-frontend/app/api/files/files-upload.ts
T

651 lines
20 KiB
TypeScript

import { postgrestGet, type PostgrestParams } from '../postgrest-client';
import dayjs from 'dayjs';
import { UPLOAD_URL, API_BASE_URL } from '../../config/api-config';
import axios from 'axios';
import { CONTRACT_TYPES, DEFAULT_CONTRACT_TYPE, type ContractType } from '~/constants/contractTypes';
/**
* 检查文档名称是否重复
* @param name 文档名称
* @param typeId 文档类型ID
* @returns 重复检查结果
*/
export async function checkDocumentDuplicate(
name: string,
typeId: number
): Promise<{ is_duplicate: boolean; count: number }> {
try {
// 获取 token
let token: string | null = null;
if (typeof window !== 'undefined') {
token = localStorage.getItem('access_token');
}
const headers: Record<string, string> = {
'Accept': 'application/json'
};
if (token) {
headers['Authorization'] = `Bearer ${token}`;
}
const response = await axios.get(
`${API_BASE_URL}/api/v2/documents/check-duplicate`,
{
params: { name, type_id: typeId },
headers
}
);
// 解析响应数据
const data = response.data;
if (data && typeof data === 'object') {
// 处理标准响应格式 { code, msg, data }
if ('data' in data && data.data) {
return {
is_duplicate: data.data.is_duplicate ?? false,
count: data.data.count ?? 0
};
}
// 直接返回数据格式
return {
is_duplicate: data.is_duplicate ?? false,
count: data.count ?? 0
};
}
return { is_duplicate: false, count: 0 };
} catch (error) {
console.error('【文档重名检查】检查失败:', error);
// 检查失败时默认允许上传
return { is_duplicate: false, count: 0 };
}
}
// import { API_BASE_URL } from '../client';
/**
* 从不同格式的 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;
}
/**
* 从 sessionStorage 获取文档类型 ID 列表(客户端专用)
* @returns 文档类型 ID 数组,如果不存在则返回 null
*/
function getDocumentTypeIdsFromSession(): number[] | null {
if (typeof window === 'undefined') {
return null; // 服务端环境返回 null
}
try {
const typeIdsStr = sessionStorage.getItem('documentTypeIds');
if (!typeIdsStr) {
return null;
}
const typeIds = JSON.parse(typeIdsStr);
if (Array.isArray(typeIds) && typeIds.every(id => typeof id === 'number')) {
return typeIds;
}
console.warn('⚠️ [getDocumentTypeIds] documentTypeIds 格式不正确:', typeIds);
return null;
} catch (error) {
console.error('❌ [getDocumentTypeIds] 解析 documentTypeIds 失败:', error);
return null;
}
}
// 文档状态枚举
export enum DocumentStatus {
waiting = 'waiting',
WAITING = "Waiting",
QUEUED = "Queued", // 排队中
CUTTING = "Cutting",
EXTRACTIONING = "Extractioning",
EVALUATIONING = "Evaluationing",
FAILED = "Failed",
PROCESSED = "Processed"
}
// 文档类型接口
export interface DocumentType {
id: number;
name: string;
code?: string;
entryModuleId?: number;
isEnabled?: boolean;
ruleSetIds?: number[];
}
// 提取结果接口
interface ExtractedResult {
[key: string]: unknown;
}
// 摘要接口
interface Summary {
[key: string]: unknown;
}
// 文档接口
export interface Document {
id: number;
name: string;
type_id: number;
file_size: number;
status: DocumentStatus;
created_at: string;
document_number?: string;
path?: string;
storage_type?: string;
is_test_document?: boolean;
evaluation_level?: string;
ocr_result?: Record<string, string>;
extracted_results?: ExtractedResult;
sumary?: Summary;
remark?: string;
audit_status?: number;
}
// 合同结构比较表接口
export interface ContractStructureComparison {
id: number;
template_contract_name: string;
file_size: number;
status: DocumentStatus;
created_at: string;
document_id?: number;
template_contract_path?: string;
ocr_results?: Record<string, unknown>;
comparison_results?: Record<string, unknown>;
}
// 文件上传响应接口(兼容旧前端)
export interface UploadResult {
success: boolean;
documentId: number;
fileId: number;
fileName: string;
fileSize: number;
typeId: number;
region: string;
processingStatus: string;
duplicateUpload: boolean;
error?: string;
}
// 旧接口上传响应(uploadContractTemplate / appendContractAttachments 仍在使用)
interface LegacyUploadResponse {
success: boolean;
result?: { id: number; file_name: string; file_size: number; [key: string]: unknown };
error: string | null;
}
// 新后端上传响应
interface NewUploadResponse {
documentId: number;
internalDocumentNo: number;
versionGroupKey: string;
versionNo: number;
previousVersionId: number | null;
rootVersionId: number;
duplicateUpload: boolean;
fileId: number;
typeId: number;
typeCode: string;
region: string;
fileName: string;
ossUrl: string;
speed: string;
processingStatus: string;
autoRunTriggered: boolean;
}
/**
* 将文件转换为二进制数据
*/
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 是否为重新上传
* @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: LegacyUploadResponse; 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'
};
// 从 localStorage 获取 token
if (typeof window !== 'undefined') {
const token = localStorage.getItem('access_token');
if (token) {
headers['Authorization'] = `Bearer ${token}`;
}
}
// 发送请求
const response = await axios.post(uploadUrl, formData, {
headers
});
console.log('【合同模板上传】服务器响应状态:', response.status);
const result = response.data;
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
* @param files 附件文件列表
* @param mergeMode 合并模式:'overwrite'(覆盖原文档)或 'new'(新建文档记录)
* @param isReprocess 是否触发重新处理
* @param remark 备注
* @param token JWT token(可选)
* @returns 上传结果
*/
export async function appendContractAttachments(
documentId: number,
files: File[],
mergeMode: 'overwrite' | 'new' = 'overwrite',
isReprocess: boolean = true,
remark?: string,
token?: string
): Promise<{data: LegacyUploadResponse; error?: never} | {data?: never; error: string; status?: number}> {
try {
console.log('【合同附件追加】开始追加附件:', { documentId, fileCount: files.length, mergeMode });
// 创建FormData对象
const formData = new FormData();
// 添加多个文件
files.forEach(file => {
formData.append('files', file);
});
// 添加其他参数
formData.append('merge_mode', mergeMode);
formData.append('is_reprocess', isReprocess.toString());
if (remark) {
formData.append('remark', remark);
}
// 构建请求URL
const uploadUrl = `${UPLOAD_URL}/contracts/${documentId}/append_attachments`;
console.log('【合同附件追加】准备发送请求到服务器:', uploadUrl);
// 设置请求头
const headers: HeadersInit = {
'Accept': 'application/json'
};
// 使用传入的 token 或从 localStorage 获取
const authToken = token || (typeof window !== 'undefined' ? localStorage.getItem('access_token') : null);
if (authToken) {
headers['Authorization'] = `Bearer ${authToken}`;
}
// 发送请求
const response = await axios.post(uploadUrl, formData, {
headers
});
console.log('【合同附件追加】服务器响应状态:', response.status);
const result = response.data;
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 : '附件追加过程中发生未知错误'
};
}
}
export async function uploadDocumentToServer(
binaryData: ArrayBuffer,
fileName: string,
fileType: string,
typeId: number,
region: string = "default",
createdBy?: number,
autoRun: boolean = true,
speed: string = "normal",
jwtToken?: string,
): Promise<{ data: UploadResult } | { error: string; status?: number }> {
try {
const formData = new FormData();
const blob = new Blob([binaryData], { type: fileType });
formData.append("file", blob, fileName);
formData.append("typeId", String(typeId));
formData.append("region", region);
formData.append("fileRole", "primary");
if (createdBy !== undefined) {
formData.append("createdBy", String(createdBy));
}
formData.append("autoRun", String(autoRun));
formData.append("speed", speed);
const headers: Record<string, string> = {
"X-File-Name": encodeURIComponent(fileName),
};
if (jwtToken) {
headers["Authorization"] = `Bearer ${jwtToken}`;
}
const response = await axios.post(`${API_BASE_URL}/api/upload`, formData, { headers });
const body = response.data;
// Result<DocumentUploadVO> envelope
const uploadData: NewUploadResponse | undefined = body?.data;
if (!uploadData || !uploadData.documentId) {
return { error: body?.message || "上传响应解析失败", status: response.status };
}
return {
data: {
success: true,
documentId: uploadData.documentId,
fileId: uploadData.fileId,
fileName: uploadData.fileName,
fileSize: binaryData.byteLength,
typeId: uploadData.typeId,
region: uploadData.region,
processingStatus: uploadData.processingStatus,
duplicateUpload: uploadData.duplicateUpload,
},
};
} catch (axiosError) {
console.error("上传文档失败:", axiosError);
if (axios.isAxiosError(axiosError)) {
const serverMessage =
(axiosError.response?.data as any)?.message ||
(axiosError.response?.data as any)?.msg;
return {
error: serverMessage || `上传失败 (HTTP ${axiosError.response?.status || "unknown"})`,
status: axiosError.response?.status,
};
}
return { error: axiosError instanceof Error ? axiosError.message : "上传失败" };
}
}
/**
* 获取当天的文档列表
* @param userInfo 用户信息(必需)
* @param token JWT token
* @param documentTypeIds 文档类型 ID 列表(可选)
* @returns 文档列表
*/
export async function getTodayDocuments(
userInfo?: { user_id?: number; [key: string]: unknown },
token?: string,
documentTypeIds?: number[]
): Promise<{data: Document[]; error?: never} | {data?: never; error: string; status?: number}> {
try {
if (!userInfo?.user_id) {
return { error: "没有找到用户信息,请刷新重试", status: 401 };
}
const today = dayjs().startOf("day").format("YYYY-MM-DD");
const params: Record<string, string | number> = {
page: 1,
pageSize: 50,
userId: userInfo.user_id,
dateFrom: today,
};
if (documentTypeIds && documentTypeIds.length > 0) {
params.typeCode = ""; // 后续可按 typeId→typeCode 映射
}
const headers: Record<string, string> = {};
if (token) {
headers["Authorization"] = `Bearer ${token}`;
}
const response = await axios.get(`${API_BASE_URL}/api/documents/list`, { params, headers });
const body = response.data;
const items = body?.data?.documents || [];
const documents: Document[] = items.map((doc: any) => ({
id: doc.documentId,
name: doc.fileName || doc.normalizedName || "未命名",
type_id: doc.typeId || 0,
file_size: doc.fileSize || 0,
status: doc.processingStatus || "waiting",
created_at: doc.updatedAt || "",
document_number: String(doc.internalDocumentNo || ""),
path: doc.ossUrl || "",
audit_status: 0,
}));
return { data: documents };
} catch (error) {
console.error("获取当天文档列表失败:", error);
return {
error: error instanceof Error ? error.message : "获取当天文档列表失败",
status: 500,
};
}
}
/**
* 获取文档类型列表
* @param token JWT token (可选)
* @returns 文档类型列表
*/
export async function getDocumentTypes(token?: string): Promise<{data: DocumentType[]; error?: never} | {data?: never; error: string; status?: number}> {
try {
const documentTypeIds = getDocumentTypeIdsFromSession();
const params: Record<string, string> = {};
if (documentTypeIds && documentTypeIds.length > 0) {
params.ids = documentTypeIds.join(",");
}
const headers: Record<string, string> = {};
if (token) {
headers["Authorization"] = `Bearer ${token}`;
}
const response = await axios.get(`${API_BASE_URL}/api/document-types`, { params, headers });
const body = response.data;
if (body?.data && Array.isArray(body.data)) {
const types: DocumentType[] = body.data.map((item: { id: number; name: string; code?: string; entryModuleId?: number; isEnabled?: boolean; ruleSetIds?: number[] }) => ({
id: item.id,
name: item.name,
code: item.code,
entryModuleId: item.entryModuleId,
isEnabled: item.isEnabled,
ruleSetIds: item.ruleSetIds,
}));
return { data: types };
}
return { error: body?.message || "获取文档类型失败", status: response.status };
} catch (error) {
console.error("获取文档类型列表失败:", error);
return {
error: error instanceof Error ? error.message : "获取文档类型列表失败",
status: 500,
};
}
}
/**
* 获取指定文档的状态
* @param documentIds 文档ID列表
* @param attachmentIds 合同附件ID列表(可选)
* @param token JWT token (可选)
* @returns 文档状态列表
*/
export async function getDocumentsStatus(
documentIds: number[],
attachmentIds?: number[],
token?: string
): Promise<{data: Document[]; error?: never} | {data?: never; error: string; status?: number}> {
try {
if ((!documentIds || documentIds.length === 0) && (!attachmentIds || attachmentIds.length === 0)) {
return { data: [] };
}
// 查询主文档状态
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let documentsResponse: any = { data: [], error: undefined, status: undefined };
if (documentIds && documentIds.length > 0) {
const documentsParams: PostgrestParams = {
select: 'id, status',
filter: {
'id': `in.(${documentIds.join(',')})`
}
};
documentsResponse = await postgrestGet<Document[]>('/api/postgrest/proxy/documents', { ...documentsParams, token });
}
// 查询合同附件状态
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let attachmentResponse: any = { data: [], error: undefined, status: undefined };
if (attachmentIds && attachmentIds.length > 0) {
const attachmentParams: PostgrestParams = {
select: 'id, status',
filter: {
'id': `in.(${attachmentIds.join(',')})`
}
};
attachmentResponse = await postgrestGet<ContractStructureComparison[]>('/api/postgrest/proxy/contract_structure_comparison', { ...attachmentParams, token });
}
if (documentsResponse.error && attachmentResponse.error) {
return { error: documentsResponse.error || attachmentResponse.error, status: documentsResponse.status || attachmentResponse.status };
}
let allData: Document[] = [];
// 处理主文档数据
if (!documentsResponse.error && documentsResponse.data) {
const extractedDocuments = extractApiData<Document[]>(documentsResponse.data);
if (extractedDocuments) {
allData = [...allData, ...extractedDocuments];
}
}
// 处理合同附件数据
if (!attachmentResponse.error && attachmentResponse.data) {
const extractedAttachments = extractApiData<ContractStructureComparison[]>(attachmentResponse.data);
if (extractedAttachments) {
// 将ContractStructureComparison转换为Document格式
const convertedAttachments: Document[] = extractedAttachments.map(item => ({
id: item.id,
name: item.template_contract_name || `合同结构比较记录_${item.id}`,
type_id: 1,
file_size: item.file_size || 0,
status: item.status,
created_at: item.created_at
}));
allData = [...allData, ...convertedAttachments];
}
}
return { data: allData };
} catch (error) {
console.error('获取文档状态失败:', error);
return {
error: error instanceof Error ? error.message : '获取文档状态失败',
status: 500
};
}
}