Initial commit
This commit is contained in:
623
utils/fileManager.js
Normal file
623
utils/fileManager.js
Normal file
@@ -0,0 +1,623 @@
|
||||
/**
|
||||
* 文件管理工具类
|
||||
* 用于统一处理文件列表操作,包括添加、删除、解析等
|
||||
*/
|
||||
class FileManager {
|
||||
constructor() {
|
||||
// 文件类型配置映射
|
||||
this.fileTypeConfig = {
|
||||
// 仪表盘照片
|
||||
1: {
|
||||
dataKey: "dashboardPic",
|
||||
dataKey2: "dashboard2Pic",
|
||||
isArray: false,
|
||||
hasMultiple: true,
|
||||
isPathOnly: true,
|
||||
},
|
||||
// 正面照片
|
||||
2: {
|
||||
dataKey: "frontPic",
|
||||
isArray: false,
|
||||
isPathOnly: true,
|
||||
},
|
||||
// 左前方照片
|
||||
3: {
|
||||
dataKey: "leftFrontPic",
|
||||
isArray: false,
|
||||
isPathOnly: true,
|
||||
},
|
||||
// 右前方照片
|
||||
4: {
|
||||
dataKey: "rightFrontPic",
|
||||
isArray: false,
|
||||
isPathOnly: true,
|
||||
},
|
||||
// 左后方照片
|
||||
5: {
|
||||
dataKey: "leftRearPic",
|
||||
isArray: false,
|
||||
isPathOnly: true,
|
||||
},
|
||||
// 右后方照片
|
||||
6: {
|
||||
dataKey: "rightRearPic",
|
||||
isArray: false,
|
||||
isPathOnly: true,
|
||||
},
|
||||
// 瑕疵照片
|
||||
7: {
|
||||
dataKey: "blemish",
|
||||
isArray: true,
|
||||
isPathOnly: true,
|
||||
},
|
||||
// 轮胎照片
|
||||
8: {
|
||||
dataKey: "tirePic",
|
||||
isArray: true,
|
||||
isPathOnly: true,
|
||||
},
|
||||
// 其它
|
||||
9: {
|
||||
dataKey: "otherPic",
|
||||
isArray: true,
|
||||
isPathOnly: true,
|
||||
},
|
||||
// 司机证件
|
||||
10: {
|
||||
dataKey: "documents",
|
||||
isArray: true,
|
||||
isPathOnly: true,
|
||||
},
|
||||
// 电子文档
|
||||
11: {
|
||||
dataKey: "esignatureAttachment",
|
||||
isArray: true,
|
||||
isPathOnly: false,
|
||||
},
|
||||
// 培训文件签字照片
|
||||
12: {
|
||||
dataKey: "signAttachment",
|
||||
isArray: true,
|
||||
isPathOnly: false,
|
||||
},
|
||||
// 安全培训照片
|
||||
13: {
|
||||
dataKey: "safetyTrainingPicture",
|
||||
isArray: true,
|
||||
isPathOnly: false,
|
||||
},
|
||||
// 安全培训附件
|
||||
14: {
|
||||
dataKey: "safetyTrainingAttachment",
|
||||
isArray: true,
|
||||
isPathOnly: false,
|
||||
},
|
||||
// 轮胎照片 - 左前轮
|
||||
15: {
|
||||
dataKey: "leftFrontTirePic",
|
||||
isArray: false,
|
||||
isPathOnly: true,
|
||||
},
|
||||
// 轮胎照片 - 左后轮(内)
|
||||
16: {
|
||||
dataKey: "leftRearTirePicIn",
|
||||
isArray: false,
|
||||
isPathOnly: true,
|
||||
},
|
||||
// 轮胎照片 - 左后轮(外)
|
||||
17: {
|
||||
dataKey: "leftRearTirePicOut",
|
||||
isArray: false,
|
||||
isPathOnly: true,
|
||||
},
|
||||
// 轮胎照片 - 右前轮
|
||||
18: {
|
||||
dataKey: "rightFrontTirePic",
|
||||
isArray: false,
|
||||
isPathOnly: true,
|
||||
},
|
||||
// 轮胎照片 - 右后轮(内)
|
||||
19: {
|
||||
dataKey: "rightRearTirePicIn",
|
||||
isArray: false,
|
||||
isPathOnly: true,
|
||||
},
|
||||
// 轮胎照片 - 右后轮(外)
|
||||
20: {
|
||||
dataKey: "rightRearTirePicOut",
|
||||
isArray: false,
|
||||
isPathOnly: true,
|
||||
},
|
||||
// 轮胎照片 - 备胎
|
||||
21: {
|
||||
dataKey: "spareTirePic",
|
||||
isArray: false,
|
||||
isPathOnly: true,
|
||||
},
|
||||
// 轮胎照片 - 左中内
|
||||
22: {
|
||||
dataKey: "leftMiddleTirePicIn",
|
||||
isArray: false,
|
||||
isPathOnly: true,
|
||||
},
|
||||
// 轮胎照片 - 左中外
|
||||
23: {
|
||||
dataKey: "leftMiddleTirePicOut",
|
||||
isArray: false,
|
||||
isPathOnly: true,
|
||||
},
|
||||
// 轮胎照片 - 左后内
|
||||
24: {
|
||||
dataKey: "leftRearMiddleTirePicIn",
|
||||
isArray: false,
|
||||
isPathOnly: true,
|
||||
},
|
||||
// 轮胎照片 - 左后外
|
||||
25: {
|
||||
dataKey: "leftRearMiddleTirePicOut",
|
||||
isArray: false,
|
||||
isPathOnly: true,
|
||||
},
|
||||
// 轮胎照片 - 右中内
|
||||
26: {
|
||||
dataKey: "rightMiddleTirePicIn",
|
||||
isArray: false,
|
||||
isPathOnly: true,
|
||||
},
|
||||
// 轮胎照片 - 右中外
|
||||
27: {
|
||||
dataKey: "rightMiddleTirePicOut",
|
||||
isArray: false,
|
||||
isPathOnly: true,
|
||||
},
|
||||
// 轮胎照片 - 右后内
|
||||
28: {
|
||||
dataKey: "rightRearMiddleTirePicIn",
|
||||
isArray: false,
|
||||
isPathOnly: true,
|
||||
},
|
||||
// 轮胎照片 - 右后外
|
||||
29: {
|
||||
dataKey: "rightRearMiddleTirePicOut",
|
||||
isArray: false,
|
||||
isPathOnly: true,
|
||||
},
|
||||
// 底盘照片 - 正前方底部照片
|
||||
30: {
|
||||
dataKey: "frontBottomPic",
|
||||
isArray: false,
|
||||
isPathOnly: true,
|
||||
},
|
||||
// 底盘照片 - 左侧前部底部照片
|
||||
31: {
|
||||
dataKey: "leftFrontBottomPic",
|
||||
isArray: false,
|
||||
isPathOnly: true,
|
||||
},
|
||||
// 底盘照片 - 左侧后方底部照片
|
||||
32: {
|
||||
dataKey: "leftRearBottomPic",
|
||||
isArray: false,
|
||||
isPathOnly: true,
|
||||
},
|
||||
// 底盘照片 - 右侧前方底部照片
|
||||
33: {
|
||||
dataKey: "rightFrontBottomPic",
|
||||
isArray: false,
|
||||
isPathOnly: true,
|
||||
},
|
||||
// 底盘照片 - 右侧后方底部照片
|
||||
34: {
|
||||
dataKey: "rightRearBottomPic",
|
||||
isArray: false,
|
||||
isPathOnly: true,
|
||||
},
|
||||
// 底盘照片 - 正后方底部照片
|
||||
35: {
|
||||
dataKey: "rearBottomPic",
|
||||
isArray: false,
|
||||
isPathOnly: true,
|
||||
},
|
||||
// 12轮胎车辆 - 左前外
|
||||
36: {
|
||||
dataKey: "leftFrontTirePicOut",
|
||||
isArray: false,
|
||||
isPathOnly: true,
|
||||
},
|
||||
// 12轮胎车辆 - 左前内
|
||||
37: {
|
||||
dataKey: "leftFrontTirePicIn",
|
||||
isArray: false,
|
||||
isPathOnly: true,
|
||||
},
|
||||
// 12轮胎车辆 - 右前外
|
||||
38: {
|
||||
dataKey: "rightFrontTirePicOut",
|
||||
isArray: false,
|
||||
isPathOnly: true,
|
||||
},
|
||||
// 12轮胎车辆 - 右前内
|
||||
39: {
|
||||
dataKey: "rightFrontTirePicIn",
|
||||
isArray: false,
|
||||
isPathOnly: true,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加文件到数据对象
|
||||
* @param {Object} data - 数据对象
|
||||
* @param {string} fileTypeId - 文件类型ID
|
||||
* @param {string} imgUrl - 图片URL
|
||||
* @param {Object} extraData - 额外数据(如文件名等)
|
||||
*/
|
||||
addFileToData(data, fileTypeId, imgUrl, extraData = {}) {
|
||||
const config = this.fileTypeConfig[fileTypeId];
|
||||
if (!config) return;
|
||||
|
||||
// 仪表盘照片特殊处理,有两个字段
|
||||
if (fileTypeId === "1") {
|
||||
if (!data.dashboardPic || data.dashboardPic === "") {
|
||||
data.dashboardPic = imgUrl;
|
||||
} else {
|
||||
data.dashboard2Pic = imgUrl;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// 数组类型处理
|
||||
if (config.isArray) {
|
||||
if (!data[config.dataKey]) {
|
||||
data[config.dataKey] = [];
|
||||
}
|
||||
|
||||
if (config.isPathOnly) {
|
||||
// 只保存路径
|
||||
data[config.dataKey].push(imgUrl);
|
||||
} else {
|
||||
// 保存对象
|
||||
data[config.dataKey].push({
|
||||
path: imgUrl,
|
||||
fileName: extraData.fileName || "",
|
||||
thumb: extraData.thumb || "",
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// 单个字段处理
|
||||
if (config.isPathOnly) {
|
||||
data[config.dataKey] = imgUrl;
|
||||
} else {
|
||||
data[config.dataKey] = {
|
||||
path: imgUrl,
|
||||
fileName: extraData.fileName || "",
|
||||
thumb: extraData.thumb || "",
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从数据对象中删除文件
|
||||
* @param {Object} data - 数据对象
|
||||
* @param {string} fileTypeId - 文件类型ID
|
||||
* @param {number} index - 要删除的索引
|
||||
* @param {Array} fileList - 当前的文件列表(用于仪表盘照片特殊处理)
|
||||
*/
|
||||
removeFileFromData(data, fileTypeId, index, fileList) {
|
||||
const config = this.fileTypeConfig[fileTypeId];
|
||||
if (!config) return;
|
||||
|
||||
// 仪表盘照片特殊处理
|
||||
if (fileTypeId === "1") {
|
||||
if (fileList && fileList.length === 0) {
|
||||
data.dashboardPic = "";
|
||||
data.dashboard2Pic = "";
|
||||
} else {
|
||||
if (index === 0) {
|
||||
data.dashboardPic = data.dashboard2Pic || "";
|
||||
data.dashboard2Pic = "";
|
||||
} else {
|
||||
data.dashboard2Pic = "";
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// 数组类型处理
|
||||
if (config.isArray) {
|
||||
if (data[config.dataKey] && data[config.dataKey].length > index) {
|
||||
data[config.dataKey].splice(index, 1);
|
||||
}
|
||||
} else {
|
||||
// 单个字段处理
|
||||
data[config.dataKey] = "";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从后端数据解析文件列表
|
||||
* @param {Object} data - 数据对象
|
||||
* @param {Object} compressPicMap - 压缩图片映射
|
||||
*/
|
||||
parseFileListFromData(data, compressPicMap) {
|
||||
const result = {};
|
||||
|
||||
// 处理仪表盘照片
|
||||
result.fileList1 = [];
|
||||
if (data.dashboardPicData?.path) {
|
||||
result.fileList1.push({
|
||||
url: data.dashboardPicData.path,
|
||||
thumb:
|
||||
data.dashboardPicData.compressedImagePath ||
|
||||
data.dashboardPicData.path,
|
||||
});
|
||||
compressPicMap.set(
|
||||
data.dashboardPicData.path,
|
||||
data.dashboardPicData.compressedImagePath || data.dashboardPicData.path
|
||||
);
|
||||
}
|
||||
data.dashboardPic = data.dashboardPicData?.path ?? "";
|
||||
|
||||
// 仪表盘第二张照片
|
||||
if (data.dashboard2PicData?.path) {
|
||||
result.fileList1.push({
|
||||
url: data.dashboard2PicData.path,
|
||||
thumb:
|
||||
data.dashboard2PicData.compressedImagePath ||
|
||||
data.dashboard2PicData.path,
|
||||
});
|
||||
compressPicMap.set(
|
||||
data.dashboard2PicData.path,
|
||||
data.dashboard2PicData.compressedImagePath ||
|
||||
data.dashboard2PicData.path
|
||||
);
|
||||
}
|
||||
data.dashboard2Pic = data.dashboard2PicData?.path ?? "";
|
||||
|
||||
// 处理其他类型的文件
|
||||
const singlePicTypes = [
|
||||
{ id: "2", dataKey: "frontPic", dataKeyData: "frontPicData" },
|
||||
{ id: "3", dataKey: "leftFrontPic", dataKeyData: "leftFrontPicData" },
|
||||
{ id: "4", dataKey: "rightFrontPic", dataKeyData: "rightFrontPicData" },
|
||||
{ id: "5", dataKey: "leftRearPic", dataKeyData: "leftRearPicData" },
|
||||
{ id: "6", dataKey: "rightRearPic", dataKeyData: "rightRearPicData" },
|
||||
];
|
||||
|
||||
singlePicTypes.forEach((type) => {
|
||||
result[`fileList${type.id}`] = [];
|
||||
if (data[`${type.dataKeyData}`]?.path) {
|
||||
result[`fileList${type.id}`] = [
|
||||
{
|
||||
url: data[`${type.dataKeyData}`].path,
|
||||
thumb:
|
||||
data[`${type.dataKeyData}`].compressedImagePath ||
|
||||
data[`${type.dataKeyData}`].path,
|
||||
},
|
||||
];
|
||||
compressPicMap.set(
|
||||
data[`${type.dataKeyData}`].path,
|
||||
data[`${type.dataKeyData}`].compressedImagePath ||
|
||||
data[`${type.dataKeyData}`].path
|
||||
);
|
||||
}
|
||||
data[type.dataKey] = data[`${type.dataKeyData}`]?.path ?? "";
|
||||
});
|
||||
|
||||
// 处理数组类型的文件
|
||||
const arrayPicTypes = [
|
||||
{ id: "7", dataKey: "blemish" },
|
||||
{ id: "8", dataKey: "tirePic" },
|
||||
{ id: "9", dataKey: "otherPic" },
|
||||
{ id: "10", dataKey: "documents" },
|
||||
];
|
||||
|
||||
arrayPicTypes.forEach((type) => {
|
||||
result[`fileList${type.id}`] = data[type.dataKey].map((x) => ({
|
||||
url: x.path,
|
||||
thumb: x.compressedImagePath || x.path,
|
||||
}));
|
||||
|
||||
data[type.dataKey].forEach((x) => {
|
||||
compressPicMap.set(x.path, x.compressedImagePath || x.path);
|
||||
});
|
||||
data[type.dataKey] = data[type.dataKey].map((x) => x.path);
|
||||
// 交车页面的"其它"文件是对象数组,需要保持原结构
|
||||
// if (type.id === "9") {
|
||||
// // 交车页面的otherPic是对象数组,不需要转换 将错就错了 在交车save/submit的时候会处理 正常这里改了 那边就可以不处理了
|
||||
// } else {
|
||||
// data[type.dataKey] = data[type.dataKey].map((x) => x.path);
|
||||
// }
|
||||
});
|
||||
|
||||
// 处理电子文档
|
||||
if (data.esignatureAttachment && data.esignatureAttachment.length >= 1) {
|
||||
result.fileList11 = [
|
||||
{
|
||||
url: data.esignatureAttachment[0].path,
|
||||
thumb: "/static/pdf.png",
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
// 处理培训文件签字照片
|
||||
result.fileList12 = data.signAttachment.map((x) => ({
|
||||
url: x.path,
|
||||
thumb: x.compressedImagePath || x.path,
|
||||
}));
|
||||
|
||||
data.signAttachment.forEach((x) => {
|
||||
compressPicMap.set(x.path, x.compressedImagePath || x.path);
|
||||
});
|
||||
|
||||
// 处理安全培训照片
|
||||
result.fileList13 =
|
||||
data?.safetyTrainingPicture?.map((x) => ({
|
||||
url: x.path,
|
||||
thumb: x.compressedImagePath || x.path,
|
||||
})) || [];
|
||||
|
||||
data?.safetyTrainingPicture?.forEach((x) => {
|
||||
compressPicMap.set(x.path, x.compressedImagePath || x.path);
|
||||
});
|
||||
|
||||
data.safetyTrainingPicture =
|
||||
data?.safetyTrainingPicture?.map((x) => ({
|
||||
path: x.path,
|
||||
})) || [];
|
||||
|
||||
// 处理安全培训附件
|
||||
result.fileList14 =
|
||||
data?.safetyTrainingAttachment?.map((x) => ({
|
||||
url: x.path,
|
||||
name: x.fileName,
|
||||
type: x.fileName.substring(x.fileName.lastIndexOf(".") + 1) || "",
|
||||
thumb: x.compressedImagePath || x.path,
|
||||
})) || [];
|
||||
|
||||
data.safetyTrainingAttachment =
|
||||
data?.safetyTrainingAttachment?.map((x) => ({
|
||||
path: x.path,
|
||||
fileName: x.fileName,
|
||||
})) || [];
|
||||
|
||||
// 处理轮胎照片
|
||||
const tireKeys1 = [
|
||||
"leftFrontTirePic",
|
||||
"leftRearTirePicIn",
|
||||
"leftRearTirePicOut",
|
||||
"rightFrontTirePic",
|
||||
"rightRearTirePicIn",
|
||||
"rightRearTirePicOut",
|
||||
"spareTirePic",
|
||||
];
|
||||
|
||||
let tireIndex = 15;
|
||||
tireKeys1.forEach((key) => {
|
||||
if (data[key]) {
|
||||
const picData = data[key];
|
||||
if (picData?.path) {
|
||||
result[`fileList${tireIndex}`] = [
|
||||
{
|
||||
url: picData.path,
|
||||
thumb: picData.compressedImagePath || picData.path,
|
||||
},
|
||||
];
|
||||
compressPicMap.set(
|
||||
picData.path,
|
||||
picData.compressedImagePath || picData.path
|
||||
);
|
||||
}
|
||||
data[key] = picData?.path ?? "";
|
||||
}
|
||||
tireIndex++;
|
||||
});
|
||||
|
||||
// 处理49吨轮胎照片
|
||||
const tireKeys2 = [
|
||||
"leftMiddleTirePicIn",
|
||||
"leftMiddleTirePicOut",
|
||||
"leftRearMiddleTirePicIn",
|
||||
"leftRearMiddleTirePicOut",
|
||||
"rightMiddleTirePicIn",
|
||||
"rightMiddleTirePicOut",
|
||||
"rightRearMiddleTirePicIn",
|
||||
"rightRearMiddleTirePicOut",
|
||||
];
|
||||
|
||||
tireIndex = 22;
|
||||
tireKeys2.forEach((key) => {
|
||||
if (data[key]) {
|
||||
const picData = data[key];
|
||||
if (picData?.path) {
|
||||
result[`fileList${tireIndex}`] = [
|
||||
{
|
||||
url: picData.path,
|
||||
thumb: picData.compressedImagePath || picData.path,
|
||||
},
|
||||
];
|
||||
compressPicMap.set(
|
||||
picData.path,
|
||||
picData.compressedImagePath || picData.path
|
||||
);
|
||||
}
|
||||
data[key] = picData?.path ?? "";
|
||||
}
|
||||
tireIndex++;
|
||||
});
|
||||
|
||||
// 处理底盘照片
|
||||
const bottomKeys = [
|
||||
"frontBottomPic",
|
||||
"leftFrontBottomPic",
|
||||
"leftRearBottomPic",
|
||||
"rightFrontBottomPic",
|
||||
"rightRearBottomPic",
|
||||
"rearBottomPic",
|
||||
];
|
||||
|
||||
let bottomIndex = 30;
|
||||
bottomKeys.forEach((key) => {
|
||||
if (data[key]) {
|
||||
const picData = data[key];
|
||||
if (picData?.path) {
|
||||
// 对象格式 {path:"",compressedImagePath:""}
|
||||
result[`fileList${bottomIndex}`] = [
|
||||
{
|
||||
url: picData.path,
|
||||
thumb: picData.compressedImagePath || picData.path,
|
||||
},
|
||||
];
|
||||
compressPicMap.set(
|
||||
picData.path,
|
||||
picData.compressedImagePath || picData.path
|
||||
);
|
||||
data[key] = picData.path; // 转换为字符串格式
|
||||
} else if (typeof picData === "string") {
|
||||
// 字符串格式
|
||||
result[`fileList${bottomIndex}`] = [
|
||||
{
|
||||
url: picData,
|
||||
thumb: picData,
|
||||
},
|
||||
];
|
||||
compressPicMap.set(picData, picData);
|
||||
}
|
||||
}
|
||||
bottomIndex++;
|
||||
});
|
||||
|
||||
// 处理12轮胎车辆的新增轮胎字段
|
||||
const twelveTireKeys = [
|
||||
"leftFrontTirePicOut",
|
||||
"leftFrontTirePicIn",
|
||||
"rightFrontTirePicOut",
|
||||
"rightFrontTirePicIn",
|
||||
];
|
||||
|
||||
let twelveTireIndex = 36;
|
||||
twelveTireKeys.forEach((key) => {
|
||||
if (data[key]) {
|
||||
const picData = data[key];
|
||||
if (picData?.path) {
|
||||
result[`fileList${twelveTireIndex}`] = [
|
||||
{
|
||||
url: picData.path,
|
||||
thumb: picData.compressedImagePath || picData.path,
|
||||
},
|
||||
];
|
||||
compressPicMap.set(
|
||||
picData.path,
|
||||
picData.compressedImagePath || picData.path
|
||||
);
|
||||
}
|
||||
data[key] = picData?.path ?? "";
|
||||
}
|
||||
twelveTireIndex++;
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
export default FileManager;
|
||||
Reference in New Issue
Block a user