fix: bug修复

This commit is contained in:
lmr
2025-09-16 10:31:44 +08:00
parent 2252c16d83
commit 1bef1efdcb
5 changed files with 101 additions and 17 deletions
@@ -286,7 +286,7 @@ export default function AdjustFilesUpload() {
variant="outline"
size="icon"
className="bg-[#fff] size-8"
onClick={handleBack}
onClick={() => navigate(-1)}
>
<ChevronLeft />
</Button>
@@ -3,13 +3,32 @@ import * as mammoth from "mammoth";
import { LoadingIcon } from "@/components/bs-icons/loading";
const DocxPreview = ({ filePath }) => {
console.log(filePath,445);
console.log(filePath, 445);
// 检查文件路径是否以.doc结尾(不区分大小写)
const isDocFile = filePath.toLowerCase().endsWith('.doc');
const [htmlContent, setHtmlContent] = useState("");
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
const previewRef = useRef(null);
// 如果是.doc文件,直接返回不支持预览提示,不执行后续逻辑
if (isDocFile) {
return (
<div className="flex justify-center items-center h-full text-gray-400">
<div className="text-center">
<img
className="size-52 block"
src={__APP_ENV__.BASE_URL + "/assets/knowledge/damage.svg"}
alt="文件不支持预览"
/>
<p>此文件类型不支持预览</p>
</div>
</div>
);
}
useEffect(() => {
const fetchAndConvertDocx = async () => {
try {
@@ -64,9 +83,11 @@ const DocxPreview = ({ filePath }) => {
`;
if (loading) {
return <div className="absolute left-0 top-0 z-10 flex h-full w-full items-center justify-center bg-[rgba(255,255,255,0.6)] dark:bg-blur-shared">
<LoadingIcon />
</div>
return (
<div className="absolute left-0 top-0 z-10 flex h-full w-full items-center justify-center bg-[rgba(255,255,255,0.6)] dark:bg-blur-shared">
<LoadingIcon />
</div>
);
}
return (
@@ -91,4 +112,5 @@ const DocxPreview = ({ filePath }) => {
);
};
export default DocxPreview;
export default DocxPreview;
@@ -88,10 +88,19 @@ const latestPreviewUrlRef = useRef('');
let chunkBboxes = [];
try {
const firstChunk = response.data?.[0];
if (firstChunk?.metadata?.bbox) {
if (firstChunk?.metadata?.bbox) {
console.log(JSON.parse(firstChunk?.metadata?.bbox).chunk_bboxes,6666666666666);
// 先判断bbox是否为空字符串
if (typeof firstChunk.metadata.bbox === 'string' && JSON.parse(firstChunk?.metadata?.bbox).chunk_bboxes === '') {
console.log('bbox为空字符串');
chunkBboxes = [];
} else {
// 解析JSON
const bboxObj = JSON.parse(firstChunk.metadata.bbox);
chunkBboxes = bboxObj.chunk_bboxes || [];
}
}
} catch (e) {
console.error('解析 chunk_bboxes 失败:', e);
chunkBboxes = [];
@@ -115,19 +124,69 @@ const fetchFileUrl = useCallback(async (fileId) => {
const pares = await getFileBboxApi(fileId);
setPartitions(pares || []);
// 严格判断URL有效性
// 获取当前选中的文件信息
const currentFile = rawFiles.find(f => String(f.id) === String(fileId));
let finalUrl = '';
if (typeof res.preview_url === 'string' && res.preview_url.trim() !== '') {
finalUrl = res.preview_url.trim();
} else if (typeof res.original_url === 'string' && res.original_url.trim() !== '') {
finalUrl = res.original_url.trim();
let finalPreviewUrl = '';
// 检查是否有有效的preview_url和original_url
const hasPreviewUrl = typeof res.preview_url === 'string' && res.preview_url.trim() !== '';
const hasOriginalUrl = typeof res.original_url === 'string' && res.original_url.trim() !== '';
if (currentFile) {
console.log(currentFile,3);
// 判断是否为UNS或LOCAL类型
const isUnsOrLocal = currentFile.parse_type === "uns" || currentFile.parse_type === "local";
console.log(isUnsOrLocal,currentFile,4444444);
if (isUnsOrLocal) {
// UNS或LOCAL类型:根据bbox是否有效选择URL
const isBboxesValid = hasChunkBboxes;
const isBboxesEmpty = !hasChunkBboxes || chunkBboxes.length === 0;
if (!isBboxesEmpty && hasPreviewUrl) {
// 有有效bbox且有preview_url → 使用preview_url
console.log(1111);
finalUrl = res.preview_url.trim();
finalPreviewUrl = res.preview_url.trim();
console.log('UNS/LOCAL类型(有有效bbox):使用preview_url');
} else {
// 无有效bbox(为空数组/字符串)或无preview_url → 强制使用original_url
console.log(2222);
finalUrl = hasOriginalUrl ? res.original_url.trim() : '';
finalPreviewUrl = finalUrl;
console.log('UNS/LOCAL类型(无有效bbox或无preview_url):使用original_url');
}
} else {
// 其他类型:优先使用preview_url,无则使用original_url
if (hasPreviewUrl) {
// 有preview_url → 优先使用
finalUrl = res.preview_url.trim();
finalPreviewUrl = res.preview_url.trim();
console.log('其他类型:使用preview_url');
} else {
// 无preview_url → 使用original_url或备选URL
finalUrl = hasOriginalUrl ? res.original_url.trim() : '';
finalPreviewUrl = finalUrl;
console.log('其他类型(无preview_url):使用original_url');
}
}
} else {
// 如果没有找到当前文件,使用默认策略
finalUrl = hasPreviewUrl ? res.preview_url.trim() : (hasOriginalUrl ? res.original_url.trim() : '');
finalPreviewUrl = finalUrl;
console.log('未找到文件信息,使用默认URL策略');
}
if (finalUrl) {
finalUrl = decodeURIComponent(finalUrl);
finalPreviewUrl = decodeURIComponent(finalPreviewUrl);
// 同时更新状态和ref(ref会同步生效)
setFileUrl(finalUrl);
setPreviewUrl(finalUrl);
setPreviewUrl(finalPreviewUrl);
return finalUrl;
} else {
setFileUrl('');
@@ -143,7 +202,7 @@ const fetchFileUrl = useCallback(async (fileId) => {
} finally {
setIsFetchingUrl(false);
}
}, []);
}, [rawFiles, hasChunkBboxes]);
@@ -32,7 +32,7 @@ export default function PreviewResult({ showPreview, previewCount, rules, step,
const { id } = useParams()
const [chunks, setChunks] = useState([]) // 当前文件分块
const [partitions, setPartitions] = useState<Partition>({}) // 当前文件分区
const [partitions, setPartitions] = useState<Partition>(null) // 当前文件分区
const [selectId, setSelectId] = useState(''); // 当前选择文件id
const [syncChunksSelectId, setSelectIdSyncChunks] = useState(''); // 当前选择文件id(与chunk更新保持同步)
useEffect(() => {
@@ -73,6 +73,8 @@ export default function PreviewResult({ showPreview, previewCount, rules, step,
const currentFile = fileList.find(file => file.id === selectId)
let preview_url
console.log(currentFile,789);
if (showPreview) {
preview_url = currentFile.previewUrl || currentFile?.filePath
} else {
@@ -151,7 +153,8 @@ export default function PreviewResult({ showPreview, previewCount, rules, step,
}
return (<div className={cn("h-full flex gap-2 justify-center", "w-full")}>
{(step === 3 || step === 2 && !previewCount) && currentFile && <PreviewFile
{(step === 3 || step === 2 && !previewCount) && currentFile && !loading && <PreviewFile
urlState={fileViewUrl}
file={currentFile}
step={step}
@@ -193,7 +193,7 @@ export default function FilesUpload() {
variant="outline"
size="icon"
className="bg-[#fff] size-8"
onClick={handleBack}
onClick={() => navigate(-1)}
>
<ChevronLeft />
</Button>