import config from "@/config.js"; // 统一文件预览地址转换(对齐 inspectionUpload 的 normalizeFileUrl 规则) export const normalizeFileUrl = (rawUrl = "") => { let fileUrl = rawUrl || ""; if (typeof fileUrl === "string") { fileUrl = fileUrl.trim().replace(/^['"]|['"]$/g, ""); } const javaApi = config.fileUrl || config.baseUrl || ""; const localPrefixes = ["wxfile://", "file://", "content://", "blob:", "data:"]; if (localPrefixes.some(prefix => String(fileUrl).startsWith(prefix))) { return fileUrl; } if (fileUrl && String(fileUrl).indexOf("\\") > -1) { const lowerPath = String(fileUrl).toLowerCase(); const uploadPathIndex = lowerPath.indexOf("uploadpath"); const prodIndex = lowerPath.indexOf("\\prod\\"); const tempIndex = lowerPath.indexOf("\\temp\\"); if (uploadPathIndex > -1) { fileUrl = String(fileUrl).substring(uploadPathIndex).replace(/\\/g, "/"); } else if (prodIndex > -1) { const relative = String(fileUrl) .substring(prodIndex + "\\prod\\".length) .replace(/\\/g, "/"); fileUrl = `/profile/prod/${relative}`; } else if (tempIndex > -1) { const relative = String(fileUrl) .substring(tempIndex + "\\temp\\".length) .replace(/\\/g, "/"); fileUrl = `/profile/temp/${relative}`; } else { fileUrl = String(fileUrl).replace(/\\/g, "/"); } } // /javaWork/.../file/prod/xxx -> /profile/prod/xxx const normalizedLower = String(fileUrl).toLowerCase(); const fileProdIdx = normalizedLower.indexOf("/file/prod/"); if (fileProdIdx > -1) { fileUrl = `/profile/prod/${String(fileUrl).substring( fileProdIdx + "/file/prod/".length )}`; } // /javaWork/.../file/temp/xxx -> /profile/temp/xxx const fileTempIdx = normalizedLower.indexOf("/file/temp/"); if (fileTempIdx > -1) { fileUrl = `/profile/temp/${String(fileUrl).substring( fileTempIdx + "/file/temp/".length )}`; } if (/^\/?uploadPath/i.test(String(fileUrl))) { fileUrl = String(fileUrl).replace(/^\/?uploadPath/i, "/profile"); } if (fileUrl && !String(fileUrl).startsWith("http")) { if (!String(fileUrl).startsWith("/")) fileUrl = "/" + fileUrl; fileUrl = javaApi + fileUrl; } return fileUrl; };