2024-01-05 17:33:18 +08:00
|
|
|
<template>
|
2024-01-09 19:51:01 +08:00
|
|
|
<el-dialog title="文档" :model-value="visible && model === 'dialog'">
|
|
|
|
<div v-if="visible && model === 'dialog'">
|
2024-01-05 17:33:18 +08:00
|
|
|
<vue-pdf
|
|
|
|
ref="pdfRef"
|
|
|
|
v-for="page in numOfPages"
|
|
|
|
:key="page"
|
2024-01-08 14:31:18 +08:00
|
|
|
:src="VITE_FILE_URL + src"
|
2024-01-05 17:33:18 +08:00
|
|
|
:page="page"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</el-dialog>
|
2024-01-09 19:51:01 +08:00
|
|
|
<div v-if="model === 'normal'">
|
|
|
|
<vue-pdf
|
|
|
|
ref="pdfRef"
|
|
|
|
v-for="page in numOfPages"
|
|
|
|
:key="page"
|
|
|
|
:src="VITE_FILE_URL + src"
|
|
|
|
:page="page"
|
|
|
|
/>
|
|
|
|
</div>
|
2024-01-05 17:33:18 +08:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script setup>
|
|
|
|
import { watchEffect, ref } from "vue";
|
|
|
|
import { VuePdf, createLoadingTask } from "vue3-pdfjs/esm";
|
|
|
|
import { ElMessage } from "element-plus";
|
|
|
|
import { useVModel } from "@vueuse/core";
|
|
|
|
|
2024-01-08 14:31:18 +08:00
|
|
|
const VITE_FILE_URL = import.meta.env.VITE_FILE_URL;
|
2024-01-05 17:33:18 +08:00
|
|
|
defineOptions({
|
|
|
|
name: "LayoutPdf",
|
|
|
|
});
|
|
|
|
const props = defineProps({
|
|
|
|
src: {
|
|
|
|
type: String,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
visible: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false,
|
|
|
|
required: true,
|
|
|
|
},
|
2024-01-09 19:51:01 +08:00
|
|
|
model: {
|
|
|
|
type: String,
|
|
|
|
required: false,
|
|
|
|
default: "dialog", // 模式: dialog=弹窗, normal=直接显示
|
|
|
|
},
|
2024-01-05 17:33:18 +08:00
|
|
|
});
|
|
|
|
const emits = defineEmits(["update:visible"]);
|
|
|
|
const visible = useVModel(props, "visible", emits);
|
|
|
|
const pdfRef = ref(null);
|
|
|
|
const numOfPages = ref(0);
|
|
|
|
watchEffect(() => {
|
|
|
|
if (props.visible) {
|
2024-01-08 14:31:18 +08:00
|
|
|
const loadingTask = createLoadingTask(VITE_FILE_URL + props.src);
|
2024-01-05 17:33:18 +08:00
|
|
|
loadingTask.promise
|
|
|
|
.then((pdf) => {
|
|
|
|
numOfPages.value = pdf.numPages;
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
visible.value = false;
|
|
|
|
ElMessage.error("文件加载失败");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
</script>
|