427 lines
13 KiB
TypeScript
427 lines
13 KiB
TypeScript
import { UPLOAD_URL, API_BASE_URL } from '../../config/api-config';
|
|
import axios from 'axios';
|
|
import { CONTRACT_TYPES, DEFAULT_CONTRACT_TYPE } from '~/constants/contractTypes';
|
|
|
|
/**
|
|
* 从不同格式的 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 | Blob): Promise<ArrayBuffer> {
|
|
return new Promise((resolve, reject) => {
|
|
// 只保留简单类型检查和调试
|
|
if (!(file instanceof File) && !(file instanceof Blob)) {
|
|
reject(new Error(`参数必须是File或Blob对象,当前类型: ${typeof file}`));
|
|
return;
|
|
}
|
|
|
|
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 {
|
|
const formData = new FormData();
|
|
const blob = new Blob([binaryData], { type: fileType });
|
|
formData.append('file', blob, fileName);
|
|
formData.append('typeId', String(typeId));
|
|
formData.append('region', 'default');
|
|
formData.append('fileRole', 'primary');
|
|
formData.append('autoRun', 'true');
|
|
formData.append('speed', priority === 'urgent' ? 'urgent' : 'normal');
|
|
|
|
try {
|
|
const headers: Record<string, string> = {
|
|
'X-File-Name': encodeURIComponent(fileName),
|
|
};
|
|
|
|
if (token) {
|
|
headers['Authorization'] = `Bearer ${token}`;
|
|
}
|
|
|
|
const response = await axios.post(`${API_BASE_URL}/api/upload`, formData, {
|
|
headers
|
|
});
|
|
const uploadData = response.data?.data;
|
|
if (!uploadData?.documentId) {
|
|
return { error: response.data?.message || response.data?.msg || '处理上传响应失败', status: response.status };
|
|
}
|
|
|
|
return {
|
|
data: {
|
|
success: true,
|
|
result: {
|
|
id: Number(uploadData.documentId),
|
|
file_name: uploadData.fileName || fileName,
|
|
file_size: binaryData.byteLength,
|
|
file_url: uploadData.storagePath || '',
|
|
type_id: Number(uploadData.typeId || typeId),
|
|
type_description: uploadData.typeCode || '',
|
|
document_number: documentNumber || null,
|
|
storage_type: 'oss',
|
|
is_test_document: isTestDocument,
|
|
remark: remark || null,
|
|
background_processing: true,
|
|
evaluation_level: priority,
|
|
},
|
|
error: null,
|
|
}
|
|
};
|
|
} catch (axiosError) {
|
|
if (axios.isAxiosError(axiosError)) {
|
|
const errorText =
|
|
axiosError.response?.data?.message ||
|
|
axiosError.response?.data?.msg ||
|
|
axiosError.response?.data?.detail ||
|
|
axiosError.message;
|
|
return {
|
|
error: `上传失败: ${errorText}`,
|
|
status: axiosError.response?.status || 500
|
|
};
|
|
}
|
|
return {
|
|
error: `axios请求错误: ${axiosError instanceof Error ? axiosError.message : '未知错误'}`,
|
|
status: 500
|
|
};
|
|
}
|
|
} catch (error) {
|
|
return {
|
|
error: error instanceof Error ? error.message : '上传失败',
|
|
status: 500
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 旧的一体化“上传并分配任务”接口适配。
|
|
*
|
|
* 现在创建交叉评查任务已改为:
|
|
* 1. 先上传文档
|
|
* 2. 再调用 `createCrossReviewTask()` 创建 v3 任务
|
|
*
|
|
* 这里先保留兼容实现,避免影响可能的旧调用方。
|
|
*/
|
|
export async function batchUploadAndAssignCrossCheckingFiles(
|
|
files: CrossCheckingUploadedFile[],
|
|
typeId: number,
|
|
priority: string = 'normal',
|
|
documentNumber: string = '',
|
|
remark: string = '',
|
|
isTestDocument: boolean = false,
|
|
assignUserIds: number[],
|
|
taskName: string,
|
|
docType: string,
|
|
taskType: string = '市局间交叉评查',
|
|
token: string | null = null,
|
|
principalUserIds: number[] = [],
|
|
attributeType?: string
|
|
): Promise<{
|
|
successes: Array<{file: CrossCheckingUploadedFile; result: Record<string, unknown>}>;
|
|
failures: Array<{file: CrossCheckingUploadedFile; error: string}>;
|
|
}> {
|
|
const successes: Array<{file: CrossCheckingUploadedFile; result: Record<string, unknown>}> = [];
|
|
const failures: Array<{file: CrossCheckingUploadedFile; error: string}> = [];
|
|
const uploadEndpoint = '/admin/v2/documents/cross_review/documents/upload_and_assign';
|
|
const uploadUrl = API_BASE_URL + uploadEndpoint;
|
|
|
|
// console.log('[批量上传] 任务类型:', taskType, '文档类型:', docType, '负责人ID:', leaderId);
|
|
|
|
|
|
for (const fileInfo of files) {
|
|
try {
|
|
const formData = new FormData();
|
|
formData.append('file', fileInfo.file, fileInfo.name);
|
|
const uploadInfo = {
|
|
type_id: typeof typeId === 'string' ? parseInt(typeId, 10) : typeId,
|
|
evaluation_level: priority,
|
|
document_number: documentNumber || null,
|
|
remark: remark || null,
|
|
is_test_document: isTestDocument,
|
|
task_name: taskName,
|
|
doc_type: typeof docType === 'string' ? docType.toUpperCase() : docType,
|
|
task_type: taskType,
|
|
attribute_type: attributeType || null
|
|
};
|
|
// console.log('fileInfo', fileInfo)
|
|
|
|
formData.append('upload_info', JSON.stringify(uploadInfo));
|
|
formData.append('assign_user_ids', JSON.stringify(assignUserIds));
|
|
|
|
// 添加负责人ID数组(包含主要负责人和额外负责人)
|
|
if (principalUserIds.length > 0) {
|
|
formData.append('principal_user_ids', JSON.stringify(principalUserIds));
|
|
}
|
|
|
|
const headers: Record<string, string> = {};
|
|
|
|
if (token) headers['Authorization'] = `Bearer ${token}`;
|
|
|
|
const response = await axios.post(uploadUrl, formData, {
|
|
headers
|
|
});
|
|
|
|
const result = response.data;
|
|
if (result && result.success) {
|
|
successes.push({ file: fileInfo, result });
|
|
} else {
|
|
failures.push({ file: fileInfo, error: result.error || '未知错误' });
|
|
}
|
|
} catch (error) {
|
|
failures.push({ file: fileInfo, error: error instanceof Error ? error.message : '上传失败' });
|
|
}
|
|
}
|
|
return { successes, failures };
|
|
}
|
|
|
|
/**
|
|
* 创建交叉评查任务(v3)。
|
|
* 先由前端完成文档上传,再将上传成功后的 documentIds 挂到任务上。
|
|
*/
|
|
export async function createCrossReviewTask(taskData: {
|
|
documentIds: number[];
|
|
userIds: number[];
|
|
principalUserIds?: number[];
|
|
taskName: string;
|
|
docTypeId?: number;
|
|
docType: string;
|
|
taskType?: string;
|
|
}, token: string | null = null): Promise<{
|
|
success: boolean;
|
|
data?: unknown;
|
|
error?: string;
|
|
}> {
|
|
try {
|
|
const requestBody = {
|
|
documentIds: taskData.documentIds,
|
|
memberUserIds: taskData.userIds,
|
|
principalUserIds: taskData.principalUserIds || [],
|
|
taskName: taskData.taskName,
|
|
docTypeId: taskData.docTypeId,
|
|
docTypeCode: taskData.docType,
|
|
taskType: taskData.taskType || 'CITY'
|
|
};
|
|
|
|
// console.log('[创建任务] 请求数据:', requestBody);
|
|
|
|
const headers: Record<string, string> = {
|
|
'Content-Type': 'application/json'
|
|
};
|
|
if (token) {
|
|
headers['Authorization'] = `Bearer ${token}`;
|
|
}
|
|
|
|
const response = await axios.post(
|
|
`${API_BASE_URL}/api/v3/cross-review/tasks`,
|
|
requestBody,
|
|
{ headers }
|
|
);
|
|
|
|
console.log('[创建任务] 成功:', response.data);
|
|
return {
|
|
success: true,
|
|
data: response.data
|
|
};
|
|
} catch (error) {
|
|
console.error('[创建任务] 失败:', error);
|
|
|
|
let errorMessage = '创建任务失败';
|
|
if (axios.isAxiosError(error) && error.response?.data) {
|
|
errorMessage = error.response.data.msg || errorMessage;
|
|
} else if (error instanceof Error) {
|
|
errorMessage = error.message || errorMessage;
|
|
}
|
|
|
|
return {
|
|
success: false,
|
|
error: errorMessage
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 生成唯一文件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];
|
|
}
|
|
|
|
/**
|
|
* 向已有任务上传新文档
|
|
*
|
|
* POST /api/v3/cross-review/tasks/{task_id}/documents/upload
|
|
*
|
|
* @param params 上传参数
|
|
* @returns 上传结果
|
|
*/
|
|
export async function uploadDocumentToTask(params: {
|
|
taskId: number;
|
|
file: File;
|
|
jwtToken?: string | null;
|
|
}): Promise<{
|
|
success: boolean;
|
|
data?: unknown;
|
|
error?: string;
|
|
}> {
|
|
const { taskId, file, jwtToken } = params;
|
|
|
|
try {
|
|
console.log('[上传文档到任务] 开始上传:', { taskId, fileName: file.name });
|
|
|
|
const formData = new FormData();
|
|
formData.append('file', file, file.name);
|
|
|
|
const uploadEndpoint = `/api/v3/cross-review/tasks/${taskId}/documents/upload`;
|
|
const uploadUrl = API_BASE_URL + uploadEndpoint;
|
|
|
|
const headers: Record<string, string> = {};
|
|
if (jwtToken) {
|
|
headers['Authorization'] = `Bearer ${jwtToken}`;
|
|
}
|
|
|
|
const response = await axios.post(uploadUrl, formData, { headers });
|
|
const result = response.data;
|
|
|
|
if (result && (result.success || result.code === 0)) {
|
|
console.log('[上传文档到任务] 上传成功:', result.message);
|
|
return { success: true, data: result.data || result };
|
|
} else {
|
|
console.error('[上传文档到任务] 上传失败:', result.detail || result.message);
|
|
return { success: false, error: result.detail || result.message || '上传失败' };
|
|
}
|
|
} catch (error) {
|
|
console.error('[上传文档到任务] 请求失败:', error);
|
|
|
|
let errorMessage = '上传文档失败';
|
|
if (axios.isAxiosError(error)) {
|
|
// 新接口错误格式: { detail: "错误信息" }
|
|
if (error.response?.data?.detail) {
|
|
errorMessage = error.response.data.detail;
|
|
} else if (error.response?.data?.message) {
|
|
errorMessage = error.response.data.message;
|
|
} else if (error.response?.status === 403) {
|
|
errorMessage = '无权操作:只有任务创建者或主要负责人可以上传文档';
|
|
} else if (error.response?.status === 400) {
|
|
errorMessage = '请求参数错误';
|
|
}
|
|
} else if (error instanceof Error) {
|
|
errorMessage = error.message || errorMessage;
|
|
}
|
|
|
|
return {
|
|
success: false,
|
|
error: errorMessage
|
|
};
|
|
}
|
|
}
|