闫增涛
2024-08-14 aea7d13fc6f00a2ddfd36a09623d3e624b99fce0
src/components/pdfview/index.vue
@@ -1,8 +1,11 @@
<!-- @format -->
<template>
  <div class="preview" v-if="this.preViewMd5">
    <div id="imageParent" class="imageBox"></div>
    <div class="bottom_tool">
      <!-- <svg
  <div class="pdfBox-component">
    <div class="preview" v-if="this.preViewMd5">
      <div id="imageParent" class="imageBox"></div>
      <div class="bottom_tool">
        <!-- <svg
        t="1718330411310"
        @click="downloadPdf"
        class="icon"
@@ -18,15 +21,20 @@
          p-id="7122"
        ></path>
      </svg> -->
      </div>
    </div>
    <div class="notBox" v-if="totalPage == 0 && !loading">
      <el-empty description="暂无数据"></el-empty>
    </div>
    <div class="notBox" v-if="loading" v-loading="loading"></div>
  </div>
</template>
<script>
import Viewer from "viewerjs";
import "viewerjs/dist/viewer.css";
import { getResourcePath } from "@/assets/methods/resources";
import Viewer from 'viewerjs'
import 'viewerjs/dist/viewer.css'
import { getResourcePath } from '@/assets/methods/resources'
export default {
  name: "pdf_view",
  name: 'pdf_view',
  props: {
    md5: {
      type: String,
@@ -41,150 +49,117 @@
  },
  data() {
    return {
      currentPageSrc: "",
      currentPageSrc: '',
      currentPage: 1,
      totalPage: 1,
      viewerCon: null,
      preViewMd5: "",
    };
      preViewMd5: '',
      loading: false,
    }
  },
  watch: {
    isClear: {
      immediate: true,
      handler(val) {
        if (val == true) this.preViewMd5 = "";
        if (val == true) this.preViewMd5 = ''
      },
    },
    md5: {
      immediate: true,
      handler(val) {
        if (val) {
          this.preViewMd5 = val;
          this.currentPage = 1;
          this.currentPageSrc = "";
          this.totalPage = 1;
          this.viewerCon?.destroy();
          this.preViewMd5 = val
          this.currentPage = 1
          this.currentPageSrc = ''
          this.totalPage = 1
          this.viewerCon?.destroy()
        }
      },
    },
  },
  mounted() {
    this.scrollBottom();
    this.clearDom();
    this.getFileInfo();
    this.scrollBottom()
    this.clearDom()
    this.getFileInfo()
  },
  methods: {
    async downloadPdf() {
      window.open(await getResourcePath(this.preViewMd5));
      window.open(await getResourcePath(this.preViewMd5))
    },
    domViewer() {
      let ele = (this.container ? this.container : document).getElementById(
        "imageParent"
      );
      let ele = document.getElementById('imageParent')
      this.viewerCon = new Viewer(ele, {
        inline: false,
        container: this.container
          ? this.container.querySelector("#app")
          : "body",
          ? this.container.querySelector('#app')
          : 'body',
        navbar: true, // 显示导航栏
        toolbar: true, // 显示工具栏
        title: true, // 显示标题
      });
      })
    },
    clearDom() {
      let ele = (this.container ? this.container : document).getElementById("imageParent");
      ele.innerHtml = "";
      let ele = document.getElementById('imageParent')
      ele.innerHtml = ''
    },
    createDom(page) {
      var that = this;
      let ele = (this.container ? this.container : document).getElementById("imageParent");
      const img = document.createElement("img");
      img.src = this.getPageImage(page);
      img.alt = "";
      img.style.maxWidth = "90%";
      img.style.padding = "30px 5%";
      img.className = "imgHover";
      var that = this
      let ele = document.getElementById('imageParent')
      const img = document.createElement('img')
      img.src = this.getPageImage(page)
      img.alt = ''
      img.style.maxWidth = '90%'
      img.style.padding = '30px 5%'
      img.className = 'imgHover'
      img.onclick = () => {
        that.viewerCon?.destroy();
        that.domViewer();
      };
      ele.appendChild(img);
        that.viewerCon?.destroy()
        that.domViewer()
      }
      ele.appendChild(img)
    },
    scrollBottom() {
      var that = this;
      var ele = (this.container ? this.container : document).getElementById("imageParent");
      ele.addEventListener("scroll", function () {
      var that = this
      var ele = document.getElementById('imageParent')
      ele.addEventListener('scroll', function () {
        // 计算滚动条距离底部的位置
        const scrollBottom =
          ele.scrollHeight - ele.scrollTop - ele.clientHeight;
        const scrollBottom = ele.scrollHeight - ele.scrollTop - ele.clientHeight
        if (scrollBottom <= 10) {
          that.currentPage++;
          that.currentPage++
          if (that.currentPage <= that.totalPage) {
            that.createDom(that.currentPage, ele);
            that.createDom(that.currentPage, ele)
          }
        }
      });
      })
    },
    getFileInfo() {
      // 获取目录
      this.loading = true;
      this.MG.file
        .getPdfInfo({ md5: this.preViewMd5 })
        .then((res) => {
          this.totalPage = res.totalPages;
          this.createDom(this.currentPage);
          this.totalPage = res.totalPages
          this.createDom(this.currentPage)
          this.loading = false
        })
        .catch((err) => {
          this.totalPage = 1;
          this.createDom(this.currentPage);
          console.error(err);
        });
          this.totalPage = 0
          this.createDom(this.currentPage)
          console.error(err)
          this.loading = false
        })
    },
    getPageImage(page) {
      const ctx = process.env.VUE_APP_API_URL;
      const ctx = process.env.VUE_APP_API_URL
      return (
        ctx +
        "/file/GetPdfPageImage" +
        "?md5=" +
        '/file/GetPdfPageImage' +
        '?md5=' +
        this.preViewMd5 +
        "&index=" +
        '&index=' +
        page +
        "&dpi=200"
      );
        '&dpi=200'
      )
    },
  },
};
</script>
<style scoped lang="less">
.preview {
  width: 100%;
  height: 100%;
  .imageBox {
    height: calc(100% - 30px);
    overflow-x: hidden;
    overflow-y: auto;
    background: #ccc;
    box-sizing: border-box;
  }
  .imageBox:hover {
    cursor: zoom-in !important;
  }
  .bottom_tool {
    height: 30px;
    background-color: rgba(0, 0, 0, 0.8);
    display: flex;
    justify-content: flex-end;
    align-items: center;
    svg {
      margin-right: 15px;
      fill: #d1d1d1;
    }
    svg:hover {
      fill: #fff;
      cursor: pointer;
    }
  }
}
</style>
</script>