user1
2024-06-13 59b7c7975f7ee38fc5a048a6b63cdaf9312908ee
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
<template>
  <div class="imgbox">
    <img
      ref="image"
      :onLoad="initFun"
      :onDragStart="dragStart"
      :onWheel="handleScroll"
      :src="currentPageSrc"
      alt=""
    />
  </div>
</template>
<script>
export default {
  name: "pdf_view",
  props: {
    md5: {
      type: String,
    },
    title: {
      type: String,
    },
  },
  data() {
    return {
      currentPageSrc: "",
      fileLoading: false,
      visible: true,
      close: null,
      currentPage: 1,
      totalPage: 1,
      rcViewerOptions: {
        // inline: true
      },
      zoom: 1,
      catalogVisible: false,
      tocData: [],
      generateList: [],
      expandedKeys: [],
      searchValue: "",
      autoExpandParent: true,
      drawerSize: "default",
    };
  },
  watch: {
    md5: {
      handler(newVal, oldVal) {
        if (newVal) {
          this.getFileInfo();
        }
      },
    },
  },
  created() {
    this.getFileInfo();
  },
  methods: {
    initFun() {
      // 初始化拖拽
      let image = this.refs.image;
      let imageBox = this.refs.imageBox;
      let initLeft = imageBox.offsetWidth / 2 - image.offsetWidth / 2;
      image.style.left = initLeft + "px";
      image.style.top = 0 + "px";
      let canMove = false;
      let offsetX, offsetY, oldLeft, oldTop;
      image.onmousedown = function (e) {
        canMove = true;
        offsetX = e.x;
        offsetY = e.y;
        oldLeft = parseFloat(image.style.left.split("px")[0]);
        oldTop = parseFloat(image.style.top.split("px")[0]);
      };
      imageBox.onmousemove = function (e) {
        if (canMove == true) {
          let left = e.clientX - offsetX;
          let top = e.clientY - offsetY;
          image.style.left = oldLeft + left + "px";
          image.style.top = oldTop + top + "px";
        }
      };
      image.onmouseup = function () {
        canMove = false;
      };
      this.fileLoading = false;
    },
    dragStart(e) {
      if (e && e.preventDefault) {
        e.preventDefault();
      } else {
        window.event.returnValue = false;
      }
    },
    handleScroll(e) {
      if (e.nativeEvent.deltaY <= 0) {
        if (this.zoom < 3) {
          let newZoom = this.zoom + 0.1;
          this.zoom = newZoom;
          this.refs.image.style.height = newZoom * 100 + "%";
        }
      } else {
        if (this.zoom > 0.5) {
          let newZoom = this.zoom - 0.1;
          this.zoom = newZoom;
          this.refs.image.style.height = newZoom * 100 + "%";
        }
      }
    },
    getFileInfo() {
      // 获取目录
      this.MG.file
        .getPdfInfo({ md5: this.md5 })
        .then((res) => {
          this.totalPage = res.totalPages;
          this.getPageImage(this.currentPage);
        })
        .catch((err) => {
          console.error(err);
        });
    },
    getPageImage(page) {
      const ctx = process.env.VUE_APP_API_URL;
      this.fileLoading = true;
      this.currentPageSrc =
        ctx +
        "/file/GetPdfPageImage" +
        "?md5=" +
        this.md5 +
        "&index=" +
        page +
        "&dpi=150";
    },
  },
};
</script>
<style scoped lang="less">
.imgbox {
  width: 100%;
  height: 100%;
  background-color: aquamarine;
  img {
    width: 100%;
  }
}
</style>