diff --git a/src/main/resources/static/components/plugins/sysFilePreview/PdfIndex.vue b/src/main/resources/static/components/plugins/sysFilePreview/PdfIndex.vue index 3708c58..1dd1c21 100644 --- a/src/main/resources/static/components/plugins/sysFilePreview/PdfIndex.vue +++ b/src/main/resources/static/components/plugins/sysFilePreview/PdfIndex.vue @@ -21,37 +21,61 @@ module.exports = { }, }, watch: { - + content: { + immediate: true, + handler() { + // 详情数据通常在组件创建后异步回填,每次内容变化都重新识别并渲染 PDF。 + this.init() + } + } }, data() { return { pdf: true, pdfObj: null, + previewVersion: 0, } }, methods: { init() { + this.destroyPdf() + const currentVersion = ++this.previewVersion try { - const parser = new DOMParser(); - const doc = parser.parseFromString(this.content, 'text/html'); - const link = doc.querySelector('a'); - const href = link.getAttribute('href'); - if(!href) { + const parser = new DOMParser() + const doc = parser.parseFromString(this.content || '', 'text/html') + const link = doc.querySelector('a') + const href = link ? link.getAttribute('href') : '' + if (!href) { this.pdf = false return } + + const parsedUrl = new URL(href, window.location.origin) + const linkText = (link.textContent || '').trim().toLowerCase() + const isPdf = linkText.endsWith('.pdf') || parsedUrl.pathname.toLowerCase().endsWith('.pdf') + if (!isPdf) { + this.pdf = false + return + } + + // 系统上传文件可能保存了其他环境的绝对域名,内部下载地址统一使用当前站点。 + const pdfUrl = parsedUrl.pathname === '/platform/sys/file/download' + ? parsedUrl.pathname + parsedUrl.search : parsedUrl.href + this.pdf = true this.$nextTick(() => { + if (currentVersion !== this.previewVersion || !this.pdf) return this.pdfObj = new Pdfh5('#pdf-container', { - pdfurl: href, - }); - this.pdfObj.on("complete", function () { - const elements = document.querySelectorAll('[class*="canvasImg"]') - let classArray = [] + pdfurl: pdfUrl, + }) + this.pdfObj.on("complete", () => { + if (currentVersion !== this.previewVersion) return + const elements = this.$el.querySelectorAll('[class*="canvasImg"]') + const classArray = [] elements.forEach(element => { classArray.push(element.getAttribute('src')) }) elements.forEach((element,index) => { - element.addEventListener('click', function() { + element.addEventListener('click', () => { vant.ImagePreview({ images: classArray, startPosition: index, @@ -65,9 +89,17 @@ module.exports = { this.pdf = false } }, + destroyPdf() { + // 内容切换或组件销毁时释放 PDF 渲染实例,避免重复打开详情产生资源残留。 + if (this.pdfObj && this.pdfObj.destroy) { + this.pdfObj.destroy() + } + this.pdfObj = null + }, }, - created() { - this.init() + beforeDestroy() { + this.previewVersion += 1 + this.destroyPdf() }, } diff --git a/src/main/resources/views/platform/zhgh/activity/trainSignUp/apply/index.html b/src/main/resources/views/platform/zhgh/activity/trainSignUp/apply/index.html index a7c74d8..12dda84 100644 --- a/src/main/resources/views/platform/zhgh/activity/trainSignUp/apply/index.html +++ b/src/main/resources/views/platform/zhgh/activity/trainSignUp/apply/index.html @@ -3,7 +3,175 @@ layout("/layouts/platform.html"){ #-->
@@ -60,7 +228,7 @@ layout("/layouts/platform.html"){