From dd58b8cb95e2de89864f91eb3d5dfc68358e392d Mon Sep 17 00:00:00 2001 From: BianLzhaoMin <11485688+bianliangzhaomin123@user.noreply.gitee.com> Date: Thu, 25 Dec 2025 10:11:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/DocumentPreview/index.vue | 24 +++++++++++++++++++ .../work/contract/contractDetails/index.vue | 4 ++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/components/DocumentPreview/index.vue b/src/components/DocumentPreview/index.vue index b49c866..bc83ed3 100644 --- a/src/components/DocumentPreview/index.vue +++ b/src/components/DocumentPreview/index.vue @@ -205,6 +205,30 @@ const getPdfPreviewUrl = (url) => { // 使用 pdf.js viewer 方案(适用于微信内置浏览器无法直接预览的场景) if (props.previewService === 'pdfjs') { + // #ifdef H5 + // H5环境下,如果是内网地址(http://)或相对路径,直接使用原URL避免CORS问题 + // 只有https的外部地址才使用pdfjs viewer + const isLocalOrHttp = fullUrl.startsWith('http://') || fullUrl.startsWith('/') + + // 检查是否是同源地址 + let isSameOrigin = false + try { + if (typeof window !== 'undefined' && window.location) { + const urlObj = new URL(fullUrl, window.location.origin) + isSameOrigin = urlObj.origin === window.location.origin + } + } catch (e) { + // URL解析失败,忽略 + } + + if (isLocalOrHttp || isSameOrigin) { + // 内网地址、相对路径或同源地址,直接使用原URL,浏览器原生支持 + console.log('检测到内网/同源地址,使用直接预览模式避免CORS问题') + return fullUrl + } + // #endif + + // 外部https地址使用pdfjs viewer return `https://mozilla.github.io/pdf.js/web/viewer.html?file=${encodeURIComponent(fullUrl)}` } diff --git a/src/pages/work/contract/contractDetails/index.vue b/src/pages/work/contract/contractDetails/index.vue index e8528bb..9acaf67 100644 --- a/src/pages/work/contract/contractDetails/index.vue +++ b/src/pages/work/contract/contractDetails/index.vue @@ -572,13 +572,13 @@ const attachments = ref([ url: 'http://192.168.0.38:18080/bnscloud/realnameapp/gzRealName/contract/pdf/施工人员安全告知书.pdf', }, { - url: 'https://mozilla.github.io/pdf.js/web/compressed.tracemonkey-pldi-09.pdf', + url: 'http://bns-cloud.oss-cn-beijing.aliyuncs.com/upload/app/lsdPhoto/pdf/2025/12/24/01d319cb-a09f-4fe6-a16e-b34491913c94.pdf?Expires=1766631029&OSSAccessKeyId=TMP.3KnER1yK3SmuHqhpR7cyButgj9YipTKQazvma5ESj1VAHwJDZY83yvgyMFjra1SWYEFcbrmrQhRcoSD65kfTzuPQrkGPzY&Signature=qRCf3f4qPiWbmKDZu5%2BdVoL5ZCU%3D', isSign: false, fileType: '5', title: '签订用工协议承诺书', }, { - url: 'https://mozilla.github.io/pdf.js/web/compressed.tracemonkey-pldi-09.pdf', + url: 'https://bns-cloud.oss-cn-beijing.aliyuncs.com/upload/app/lsdPhoto/pdf/2025/12/24/01d319cb-a09f-4fe6-a16e-b34491913c94.pdf?Expires=1766630782&OSSAccessKeyId=TMP.3KnER1yK3SmuHqhpR7cyButgj9YipTKQazvma5ESj1VAHwJDZY83yvgyMFjra1SWYEFcbrmrQhRcoSD65kfTzuPQrkGPzY&Signature=Lg%2FHxLITIMnaQnWfjGOb1h5rcxc%3D', isSign: false, fileType: '4', title: '安全承诺书',