refactor.orders.style: 优化 PDF 预览 Modal 组件
- 将 v-model 替换为 :model-value 在 style-print.vue 中 - 重构 pdf-preview-modal.vue 组件,使用 modelValue 替代 show - 添加日志输出以调试 PDF 生成和预览过程 - 优化 PDF 预览 Modal 的显示和隐藏逻辑
This commit is contained in:
parent
25cbe93a94
commit
c90a52f9cc
@ -111,7 +111,7 @@ export default defineComponent({
|
||||
html2canvas: { scale: 2, useCORS: true },
|
||||
jsPDF: { unit: 'mm', format: 'a4', orientation: 'portrait' },
|
||||
};
|
||||
|
||||
console.log('开始生成PDF');
|
||||
// 生成PDF并等待Blob结果
|
||||
try {
|
||||
html2pdf()
|
||||
@ -125,7 +125,9 @@ export default defineComponent({
|
||||
console.log('成功生成PDF Blob对象:', URL.createObjectURL(blob));
|
||||
pdfBlob.value = blob;
|
||||
// 显示PDF预览Modal
|
||||
console.log('showPdfPreview.value:', showPdfPreview.value);
|
||||
showPdfPreview.value = true;
|
||||
console.log('showPdfPreview.value:', showPdfPreview.value);
|
||||
// 显示成功消息
|
||||
alertService.showInfo($t('entity.action.printSuccess'));
|
||||
} else {
|
||||
|
||||
@ -19,7 +19,7 @@
|
||||
|
||||
<!-- PDF预览Modal -->
|
||||
<pdf-preview-modal
|
||||
v-model="showPdfPreview"
|
||||
:model-value="showPdfPreview"
|
||||
:pdf-blob="pdfBlob"
|
||||
:file-name="`${$t('jewpmsApp.style.detail.title')}-${style.number}.pdf`"
|
||||
:title="$t('entity.action.preview')"
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
<template>
|
||||
<b-modal :visible="show" :title="title" size="lg" hide-footer @hidden="onHidden" @hide="onHide">
|
||||
<b-modal v-model="modelValue" :title="title" size="lg" hide-footer @hidden="onHidden" @hide="onHide">
|
||||
<div class="pdf-container">
|
||||
<div v-if="loading" class="loading-container">
|
||||
<div class="spinner-border text-primary" role="status">
|
||||
@ -9,7 +9,7 @@
|
||||
<iframe v-else :src="pdfUrl" class="pdf-iframe"></iframe>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-secondary" @click="show = false">
|
||||
<button type="button" class="btn btn-secondary" @click="onHide">
|
||||
{{ $t('entity.action.close') }}
|
||||
</button>
|
||||
<button type="button" class="btn btn-primary" @click="downloadPdf">
|
||||
@ -21,7 +21,7 @@
|
||||
</template>
|
||||
|
||||
<script lang="ts">
|
||||
import { defineComponent, ref, watch, onBeforeUnmount } from 'vue';
|
||||
import { defineComponent, ref, watch, onBeforeUnmount, nextTick } from 'vue';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import { library } from '@fortawesome/fontawesome-svg-core';
|
||||
import { faDownload } from '@fortawesome/free-solid-svg-icons';
|
||||
@ -52,23 +52,14 @@ export default defineComponent({
|
||||
},
|
||||
setup(props, { emit }) {
|
||||
const { t: $t } = useI18n();
|
||||
const show = ref(false);
|
||||
const pdfUrl = ref('');
|
||||
const loading = ref(true);
|
||||
|
||||
// 初始化时设置show的值
|
||||
show.value = props.modelValue;
|
||||
|
||||
// 监听show变化,同步到modelValue
|
||||
watch(show, value => {
|
||||
emit('update:modelValue', value);
|
||||
});
|
||||
|
||||
// 监听modelValue变化,同步到show
|
||||
// 监听modelValue变化
|
||||
watch(
|
||||
() => props.modelValue,
|
||||
value => {
|
||||
show.value = value;
|
||||
console.log(`[PdfPreviewModal] modelValue变化: ${value}`);
|
||||
if (value) {
|
||||
createPdfUrl();
|
||||
}
|
||||
@ -79,24 +70,29 @@ export default defineComponent({
|
||||
watch(
|
||||
() => props.pdfBlob,
|
||||
() => {
|
||||
if (show.value) {
|
||||
console.log('[PdfPreviewModal] pdfBlob发生变化');
|
||||
if (props.modelValue) {
|
||||
createPdfUrl();
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
const createPdfUrl = () => {
|
||||
console.log('[PdfPreviewModal] 开始创建PDF URL');
|
||||
loading.value = true;
|
||||
// 释放之前的URL
|
||||
if (pdfUrl.value) {
|
||||
console.log('[PdfPreviewModal] 释放旧的PDF URL');
|
||||
URL.revokeObjectURL(pdfUrl.value);
|
||||
}
|
||||
// 创建新的URL
|
||||
pdfUrl.value = URL.createObjectURL(props.pdfBlob);
|
||||
console.log('[PdfPreviewModal] 新的PDF URL创建完成');
|
||||
loading.value = false;
|
||||
};
|
||||
|
||||
const downloadPdf = () => {
|
||||
console.log('[PdfPreviewModal] 开始下载PDF');
|
||||
const link = document.createElement('a');
|
||||
link.href = pdfUrl.value;
|
||||
link.download = props.fileName;
|
||||
@ -104,22 +100,26 @@ export default defineComponent({
|
||||
};
|
||||
|
||||
const onHidden = () => {
|
||||
console.log('[PdfPreviewModal] Modal已隐藏');
|
||||
emit('hidden');
|
||||
};
|
||||
|
||||
const onHide = () => {
|
||||
show.value = false;
|
||||
const onHide = async () => {
|
||||
console.log('[PdfPreviewModal] Modal开始隐藏');
|
||||
emit('update:modelValue', false);
|
||||
await nextTick();
|
||||
console.log('[PdfPreviewModal] Modal隐藏完成, modelValue:', props.modelValue);
|
||||
};
|
||||
|
||||
// 组件销毁前释放URL
|
||||
onBeforeUnmount(() => {
|
||||
if (pdfUrl.value) {
|
||||
console.log('[PdfPreviewModal] 组件销毁前释放PDF URL');
|
||||
URL.revokeObjectURL(pdfUrl.value);
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
show,
|
||||
pdfUrl,
|
||||
loading,
|
||||
downloadPdf,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user