|
@@ -11,23 +11,28 @@ const mainStore = useMainStore();
|
|
|
const expanded = computed(() => mainStore.getExpanded);
|
|
|
const phone = computed(() => mainStore.getPhone);
|
|
|
const recordId = computed(() => mainStore.getRecordId);
|
|
|
-
|
|
|
const isLoadOver = computed(() => mainStore.getIsLoadOver);
|
|
|
|
|
|
/**
|
|
|
* 生成 PDF Blob
|
|
|
*/
|
|
|
-async function generatePDFBlob(pdfContent?: HTMLElement, isDownload?: boolean): Promise<Blob | null> {
|
|
|
+async function generatePDFBlob(
|
|
|
+ pdfContent?: HTMLElement,
|
|
|
+ showLoading = false
|
|
|
+): Promise<Blob | null> {
|
|
|
if (!isLoadOver.value) {
|
|
|
showMessage({ type: 'warning', message: '数据加载中,请稍后再试' });
|
|
|
return null;
|
|
|
}
|
|
|
+ let loading: any = null;
|
|
|
|
|
|
- const loading = ElLoading.service({
|
|
|
+ if (showLoading) {
|
|
|
+ loading = ElLoading.service({
|
|
|
lock: true,
|
|
|
- text: isDownload ? '生成报告中...' : '上传报告中...',
|
|
|
+ text: '生成报告中...',
|
|
|
background: 'rgba(0,0,0,0.7)'
|
|
|
});
|
|
|
+ }
|
|
|
|
|
|
try {
|
|
|
if (!pdfContent) pdfContent = document.querySelector('.record') as HTMLElement;
|
|
@@ -42,6 +47,7 @@ async function generatePDFBlob(pdfContent?: HTMLElement, isDownload?: boolean):
|
|
|
useCORS: true,
|
|
|
backgroundColor: '#fff'
|
|
|
});
|
|
|
+
|
|
|
const canvasWidth = canvas.width;
|
|
|
const canvasHeight = canvas.height;
|
|
|
|
|
@@ -69,54 +75,66 @@ async function generatePDFBlob(pdfContent?: HTMLElement, isDownload?: boolean):
|
|
|
if (positionY < canvasHeight) pdf.addPage();
|
|
|
}
|
|
|
|
|
|
- const blob = pdf.output('blob'); // ✅ 生成 Blob
|
|
|
- return blob;
|
|
|
+ return pdf.output('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;
|
|
|
-
|
|
|
+function triggerDownload(blob: Blob, filename = 'analysis.pdf') {
|
|
|
const url = URL.createObjectURL(blob);
|
|
|
const a = document.createElement('a');
|
|
|
a.href = url;
|
|
|
- a.download = 'analysis.pdf';
|
|
|
+ a.download = filename;
|
|
|
a.click();
|
|
|
URL.revokeObjectURL(url);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 上传 PDF
|
|
|
+ * 上传文件
|
|
|
*/
|
|
|
-export async function uploadPDF(pdfContent?: HTMLElement) {
|
|
|
- const blob = await generatePDFBlob(pdfContent, false);
|
|
|
- if (!blob) return;
|
|
|
-
|
|
|
- // 转成 File 对象(后端一般习惯接收 File)
|
|
|
+async function uploadPDFFile(blob: Blob) {
|
|
|
const file = new File([blob], 'analysis.pdf', { type: 'application/pdf' });
|
|
|
+ const uploadRes = await uploadFile(file, { source: 'media', mediaType: 'file' });
|
|
|
+ const pdfUrl = uploadRes.data.url;
|
|
|
|
|
|
- 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;
|
|
|
+ await savePdf({
|
|
|
+ phone: phone.value,
|
|
|
+ id: recordId.value,
|
|
|
+ pdfUrl
|
|
|
+ });
|
|
|
+
|
|
|
+ return pdfUrl;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 统一入口:生成 PDF
|
|
|
+ * @param pdfContent - PDF内容DOM
|
|
|
+ * @param mode - "manual" 手动下载 | "auto" 自动下载并上传
|
|
|
+ */
|
|
|
+export async function handlePDF(pdfContent?: HTMLElement, mode: 'manual' | 'auto' = 'manual') {
|
|
|
+ const blob = await generatePDFBlob(pdfContent, true);
|
|
|
+ if (!blob) return;
|
|
|
+
|
|
|
+ // 下载
|
|
|
+ triggerDownload(blob);
|
|
|
+
|
|
|
+ // 自动模式下再上传
|
|
|
+ if (mode === 'auto') {
|
|
|
+ try {
|
|
|
+ await uploadPDFFile(blob);
|
|
|
+ // showMessage({ type: 'success', message: '报告已下载并上传成功' });
|
|
|
+ } catch (err) {
|
|
|
+ // showMessage({ type: 'error', message: '报告上传失败' });
|
|
|
+ throw err;
|
|
|
+ }
|
|
|
}
|
|
|
}
|