unknown
2024-06-13 c0e4157169e151fe4ada71d3abbd82aa78c4d71e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<template>
  <div class="preview" v-if="this.preViewMd5">
    <div id="imageParent" class="imageBox"></div>
    <div class="bottom_tool">
      <svg
        @click="downloadPdf"
        t="1718251204993"
        class="icon"
        viewBox="0 0 1024 1024"
        version="1.1"
        xmlns="http://www.w3.org/2000/svg"
        p-id="4418"
        xmlns:xlink="http://www.w3.org/1999/xlink"
        width="25"
        height="25"
      >
        <path
          d="M557 564.974l153.188-181.04c16.054-18.972 44.448-21.34 63.42-5.286 18.972 16.054 21.338 44.448 5.284 63.42L550.56 711.92a44.982 44.982 0 0 1-8.95 10.254 44.872 44.872 0 0 1-15.082 8.432A44.944 44.944 0 0 1 512 733c-16.264 0-30.512-8.628-38.42-21.556L245.65 442.068c-16.052-18.972-13.686-47.366 5.286-63.42 18.972-16.052 47.366-13.686 63.42 5.286l152.646 180.4V157c0-24.852 20.148-45 45-45s45 20.148 45 45v407.974zM823 630c0-24.852 20.148-45 45-45s45 20.148 45 45v238c0 24.87-20.176 45.026-45.046 45l-710-0.726c-24.834-0.026-44.954-20.166-44.954-45V630c0-24.852 20.148-45 45-45s45 20.148 45 45v192.32l620 0.634V630z"
          p-id="4419"
        ></path>
      </svg>
    </div>
  </div>
</template>
<script>
import Viewer from "viewerjs";
import "viewerjs/dist/viewer.css";
import { getResourcePath } from "@/assets/methods/resources";
export default {
  name: "pdf_view",
  props: {
    md5: {
      type: String,
    },
    title: {
      type: String,
    },
    isClear: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      currentPageSrc: "",
      currentPage: 1,
      totalPage: 1,
      viewerCon: null,
      preViewMd5: "",
    };
  },
  watch: {
    isClear: {
      immediate: true,
      handler(val) {
        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();
        }
      },
    },
  },
  mounted() {
    this.scrollBottom();
    this.clearDom();
    this.getFileInfo();
  },
  methods: {
    async downloadPdf() {
      window.open(await getResourcePath(this.preViewMd5));
    },
    domViewer() {
      let ele = (this.container ? this.container : document).getElementById(
        "imageParent"
      );
      this.viewerCon = new Viewer(ele, {
        inline: false,
        container: this.container
          ? this.container.querySelector("#app")
          : "body",
        navbar: true, // 显示导航栏
        toolbar: true, // 显示工具栏
        title: true, // 显示标题
      });
    },
    clearDom() {
      let ele = (this.container ? this.container : 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";
      img.onclick = () => {
        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 () {
        // 计算滚动条距离底部的位置
        const scrollBottom =
          ele.scrollHeight - ele.scrollTop - ele.clientHeight;
        if (scrollBottom <= 10) {
          that.currentPage++;
          if (that.currentPage <= that.totalPage) {
            that.createDom(that.currentPage, ele);
          }
        }
      });
    },
    getFileInfo() {
      // 获取目录
      this.MG.file
        .getPdfInfo({ md5: this.preViewMd5 })
        .then((res) => {
          this.totalPage = res.totalPages;
          this.createDom(this.currentPage);
        })
        .catch((err) => {
          this.totalPage = 1;
          this.createDom(this.currentPage);
          console.error(err);
        });
    },
    getPageImage(page) {
      const ctx = process.env.VUE_APP_API_URL;
      return (
        ctx +
        "/file/GetPdfPageImage" +
        "?md5=" +
        this.preViewMd5 +
        "&index=" +
        page +
        "&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: 10px;
      fill: #999;
    }
    svg:hover {
      fill: #fff;
      cursor: pointer;
    }
  }
}
</style>