From 973b9e45bdb3f33d49344ab6bc158753cd911c2d Mon Sep 17 00:00:00 2001 From: tangw Date: Mon, 14 Sep 2026 14:24:31 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=9F=E8=AE=A1=E8=8F=9C=E5=8D=95=E5=AF=BC?= =?UTF-8?q?=E5=87=BA=E4=B8=8E=E6=89=93=E5=8D=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/liswork/xtwh/StatsparameterApi.ts | 6 +- .../statisticalAnalysis/report/index.vue | 72 ++++++++++++++++--- 2 files changed, 65 insertions(+), 13 deletions(-) diff --git a/src/api/liswork/xtwh/StatsparameterApi.ts b/src/api/liswork/xtwh/StatsparameterApi.ts index feb7d95..905e68a 100644 --- a/src/api/liswork/xtwh/StatsparameterApi.ts +++ b/src/api/liswork/xtwh/StatsparameterApi.ts @@ -67,7 +67,8 @@ export function printTemplate(params: any) { return request({ url: '/stats/print', method: 'post', - data: params + data: params, + responseType: 'blob' }) } // 导出excel @@ -75,7 +76,8 @@ export function exportTemplate(params: any) { return request({ url: '/stats/exportExcel', method: 'post', - data: params + data: params, + responseType: 'blob' }) } diff --git a/src/views/statisticalAnalysis/report/index.vue b/src/views/statisticalAnalysis/report/index.vue index 568703a..a7cd98f 100644 --- a/src/views/statisticalAnalysis/report/index.vue +++ b/src/views/statisticalAnalysis/report/index.vue @@ -299,24 +299,65 @@ const getPrintFooterText = (col, idx) => { // 打印 const print = () => { - originalColumn.value.forEach(item => { - const matchTabCol = tableColumns.value.find(col => col.prop == item.paramCode) - if (matchTabCol) { - item.paramSequence = matchTabCol.paramSequence - } else { - item.paramSequence = 0 - } + // 不直接改 originalColumn,拷贝一份,避免影响导出等其他功能 + const printColumns = originalColumn.value.map(item => { + const matchTabCol = tableColumns.value.find(col => col.prop === item.paramCode) + return { ...item, paramSequence: matchTabCol ? matchTabCol.paramSequence : 0 } }) + const data = { Parameter: Parameter.value, - Column: originalColumn.value, + Column: printColumns, Template: Template.value - }; + } + printTemplate(data).then(res => { - classCom.printBase64PDF(res.data) + // 后端错误 JSON 判断,避免静默失败 + if (res instanceof Blob && res.type.includes('application/json')) { + const reader = new FileReader() + reader.onload = e => { + const err = JSON.parse(e.target.result) + ElMessage.error(err.msg || '打印失败') + } + reader.readAsText(res) + return + } + + const blob = new Blob([res], { type: 'application/pdf' }) + const pdfUrl = URL.createObjectURL(blob) + + // 不要 display:none,Chrome 对隐藏 iframe 的 PDF 打印有兼容问题;移出屏幕即可 + const iframe = document.createElement('iframe') + iframe.style.position = 'fixed' + iframe.style.right = '0' + iframe.style.bottom = '0' + iframe.style.width = '0' + iframe.style.height = '0' + iframe.style.border = '0' + iframe.src = pdfUrl + document.body.appendChild(iframe) + + iframe.onload = () => { + // 等 PDF 渲染完再打印,避免空白页 + setTimeout(() => { + const win = iframe.contentWindow + if (win) { + win.focus() + win.print() // Chrome/Firefox 阻塞到打印对话框关闭 + } + // print() 阻塞时,这里在对话框关闭后才执行;留 2s 缓冲再清理 + setTimeout(() => { + URL.revokeObjectURL(pdfUrl) + iframe.remove() + }, 2000) + }, 300) + } + }).catch(() => { + ElMessage.error('打印失败') }) } + const exportHandle = () => { originalColumn.value.forEach(item => { const matchTabCol = tableColumns.value.find(col => col.prop == item.paramCode) @@ -332,7 +373,16 @@ const exportHandle = () => { Template: Template.value }; exportTemplate(data).then(res => { - classCom.printBase64PDF(res.data, 'excel', '统计报表') + //classCom.printBase64PDF(res.data, 'excel', '统计报表') + // 若依拦截器对 blob 直接透传,res 就是 Blob + const blob = new Blob([res], { + type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' + }) + const link = document.createElement('a') + link.href = URL.createObjectURL(blob) + link.download = '导出_' + new Date().getTime() + '.xlsx' + link.click() + URL.revokeObjectURL(link.href) // 释放内存 }) }