|
@@ -4,49 +4,61 @@ import { ElLoading } from 'element-plus';
|
|
|
import html2canvas from 'html2canvas';
|
|
|
import jsPDF from 'jspdf';
|
|
|
import { showMessage } from './common';
|
|
|
+import { uploadFile } from '@/utils/http';
|
|
|
+import { savePdf } from '@/utils/api';
|
|
|
|
|
|
const mainStore = useMainStore();
|
|
|
const expanded = computed(() => mainStore.getExpanded);
|
|
|
+const phone = computed(() => mainStore.getPhone);
|
|
|
+const recordId = computed(() => mainStore.getRecordId);
|
|
|
+
|
|
|
const isLoadOver = computed(() => mainStore.getIsLoadOver);
|
|
|
|
|
|
-export const downloadPDF = async (pdfContent?: HTMLElement) => {
|
|
|
+/**
|
|
|
+ * 生成 PDF Blob
|
|
|
+ */
|
|
|
+async function generatePDFBlob(pdfContent?: HTMLElement, mask?: boolean): Promise<Blob | null> {
|
|
|
if (!isLoadOver.value) {
|
|
|
showMessage({ type: 'warning', message: '数据加载中,请稍后再试' });
|
|
|
- return;
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
- const loading = ElLoading.service({
|
|
|
- lock: true,
|
|
|
- text: '生成中...',
|
|
|
- background: 'rgba(0,0,0,0.7)',
|
|
|
- });
|
|
|
+ let loading: any = null;
|
|
|
+ if (mask) {
|
|
|
+ loading = ElLoading.service({
|
|
|
+ lock: true,
|
|
|
+ text: '生成中...',
|
|
|
+ background: 'rgba(0,0,0,0.7)'
|
|
|
+ });
|
|
|
+ }
|
|
|
|
|
|
try {
|
|
|
if (!pdfContent) pdfContent = document.querySelector('.record') as HTMLElement;
|
|
|
-
|
|
|
if (!expanded.value) {
|
|
|
mainStore.setExpanded(true);
|
|
|
await nextTick();
|
|
|
}
|
|
|
await nextTick();
|
|
|
|
|
|
- // 高分辨率 canvas
|
|
|
- const canvas = await html2canvas(pdfContent, { scale: 2, useCORS: true, backgroundColor: '#fff' });
|
|
|
+ const canvas = await html2canvas(pdfContent, {
|
|
|
+ scale: 2,
|
|
|
+ useCORS: true,
|
|
|
+ backgroundColor: '#fff'
|
|
|
+ });
|
|
|
const canvasWidth = canvas.width;
|
|
|
const canvasHeight = canvas.height;
|
|
|
|
|
|
const pdf = new jsPDF('p', 'mm', 'a4');
|
|
|
const pageWidth = pdf.internal.pageSize.getWidth();
|
|
|
const pageHeight = pdf.internal.pageSize.getHeight();
|
|
|
- const ratio = pageWidth / canvasWidth; // 宽度缩放比例
|
|
|
- const pagePixelHeight = pageHeight / ratio; // PDF 一页对应 canvas 的高度
|
|
|
+ const ratio = pageWidth / canvasWidth;
|
|
|
+ const pagePixelHeight = pageHeight / ratio;
|
|
|
|
|
|
let positionY = 0;
|
|
|
|
|
|
while (positionY < canvasHeight) {
|
|
|
const h = Math.min(pagePixelHeight, canvasHeight - positionY);
|
|
|
|
|
|
- // 创建临时 canvas 截取当前页
|
|
|
const pageCanvas = document.createElement('canvas');
|
|
|
pageCanvas.width = canvasWidth;
|
|
|
pageCanvas.height = h;
|
|
@@ -60,12 +72,54 @@ export const downloadPDF = async (pdfContent?: HTMLElement) => {
|
|
|
if (positionY < canvasHeight) pdf.addPage();
|
|
|
}
|
|
|
|
|
|
- pdf.save('analysis.pdf');
|
|
|
+ const blob = pdf.output('blob'); // ✅ 生成 Blob
|
|
|
+ return blob;
|
|
|
} catch (error) {
|
|
|
console.error(error);
|
|
|
showMessage({ type: 'error', message: '生成失败,请稍后再试' });
|
|
|
+ return null;
|
|
|
} finally {
|
|
|
mainStore.setExpanded(false);
|
|
|
- setTimeout(() => loading.close(), 500);
|
|
|
+ setTimeout(() => loading && loading.close(), 500);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 点击下载 PDF
|
|
|
+ */
|
|
|
+export async function downloadPDF(pdfContent?: HTMLElement) {
|
|
|
+ const blob = await generatePDFBlob(pdfContent, true);
|
|
|
+ if (!blob) return;
|
|
|
+
|
|
|
+ const url = URL.createObjectURL(blob);
|
|
|
+ const a = document.createElement('a');
|
|
|
+ a.href = url;
|
|
|
+ a.download = 'analysis.pdf';
|
|
|
+ a.click();
|
|
|
+ URL.revokeObjectURL(url);
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 上传 PDF
|
|
|
+ */
|
|
|
+export async function uploadPDF(pdfContent?: HTMLElement) {
|
|
|
+ const blob = await generatePDFBlob(pdfContent, false);
|
|
|
+ if (!blob) return;
|
|
|
+
|
|
|
+ // 转成 File 对象(后端一般习惯接收 File)
|
|
|
+ const file = new File([blob], 'analysis.pdf', { type: 'application/pdf' });
|
|
|
+
|
|
|
+ try {
|
|
|
+ const uploadRes = await uploadFile(file, { source: 'media', mediaType: 'file' });
|
|
|
+ const pdfUrl = uploadRes.data.url;
|
|
|
+ await savePdf({
|
|
|
+ phone: phone.value,
|
|
|
+ id: recordId.value,
|
|
|
+ pdfUrl
|
|
|
+ });
|
|
|
+ showMessage({ type: 'success', message: '报告上传成功' });
|
|
|
+ } catch (err) {
|
|
|
+ showMessage({ type: 'error', message: '报告上传失败' });
|
|
|
+ throw err;
|
|
|
}
|
|
|
-};
|
|
|
+}
|