123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- <template>
- <div class="container_all">
- <div class="container_public" :style="{ transform: `scale(${scale})` }">
- <div v-for="pageNumber in loadedPages" :key="pageNumber">
- <vue-pdf
- class="container_pdf"
- :src="fileUrl"
- :page="pageNumber"
- @page-loaded="onPageLoaded"
- ></vue-pdf>
- </div>
- </div>
- <img
- v-if="loadAll"
- src="../../static/explain/down_up.png"
- class="absolute_image1"
- @click="download"
- />
- <!-- <img
- v-if="loadAll"
- src="../../static/explain/scale_in.png"
- class="absolute_image2"
- @click="scaleIn"
- />
- <img
- v-if="loadAll"
- src="../../static/explain/scale_out.png"
- class="absolute_image3"
- @click="scaleOut"
- /> -->
- </div>
- </template>
- <script>
- import { detail } from "@/api/explain.js";
- import VuePdf from "vue-pdf";
- export default {
- components: {
- VuePdf,
- },
- data() {
- return {
- loadAll: false,
- name: "",
- fileUrl: "",
- fileName: "",
- currentPage: 1,
- numPages: 0,
- scale: 1,
- loadedPages: 1,
- };
- },
- onLoad(e) {
- var that = this;
- that.id = e.id;
- that.getDetail();
- },
- methods: {
- // 滚动加载逻辑
- // {"type":"scroll","timeStamp":24324.79999999702,"detail":{},
- // "target":{"id":"","offsetLeft":0,"offsetTop":0,"dataset":{}},
- // "currentTarget":{"id":"","offsetLeft":0,"offsetTop":0,"dataset":{}},"touches":[],
- // "changedTouches":[],"mp":{"@warning":"mp is deprecated","type":"scroll",
- // "timeStamp":24324.79999999702,"detail":{},"target":{"id":"","offsetLeft":0,"offsetTop":0,"dataset":{}},
- // "currentTarget":{"id":"","offsetLeft":0,"offsetTop":0,"dataset":{}},"touches":[],"changedTouches":[]},
- // "_processed":true}
- handleScroll(e) {
- // const target = e.target;
- // var offsetTop = target.offsetTop;
- // // 滚动到底部且未加载完所有页
- // if (this.loadedPages < this.numPages) {
- // this.loadedPages += 1; // 加载下一页
- // }
- },
- ///接口获取数据
- getDetail() {
- var that = this;
- var id = that.id;
- detail(id).then((res) => {
- if (res.code === 0) {
- that.name = res.data.name;
- that.fileUrl = res.data.fileUrl;
- that.fileName = that.getFileExtension(that.fileUrl);
- that.getPdfPages();
- }
- });
- },
- async getPdfPages() {
- var that = this;
- const loadingTask = VuePdf.createLoadingTask(that.fileUrl);
- const pdf = await loadingTask.promise;
- this.numPages = pdf.numPages;
- // this.loadedPages = this.numPages;
- if (this.numPages == 0) {
- return;
- }
- this.loadedPages = this.numPages > 1 ? 1 : 1;
- },
- ///截取文件
- getFileExtension(fileUrl) {
- var filename = fileUrl.split("/").pop();
- var lastDotIndex = filename.lastIndexOf(".");
- if (lastDotIndex === -1 || lastDotIndex === filename.length - 1) {
- return "";
- } else {
- return filename.substring(lastDotIndex + 1).toLowerCase();
- }
- },
- prevPage() {
- if (this.currentPage > 1) {
- this.currentPage--;
- }
- },
- nextPage() {
- if (this.currentPage < this.numPages) {
- this.currentPage++;
- }
- },
- scaleIn() {
- this.scale *= 1.1;
- },
- scaleOut() {
- this.scale /= 1.1;
- },
- onPageLoaded(page) {
- if (this.numPages > 0) {
- if (!this.loadAll) {
- this.loadAll = true;
- }
- var p = this.loadedPages;
- if (p < this.numPages) {
- this.loadedPages = p + 1;
- }
- }
- },
- async download() {
- var that = this;
- var name = that.name;
- var fileUrl = that.fileUrl;
- var fileName = that.fileName;
- var finalName = "";
- if (name != null && name != "") {
- finalName = name + "." + fileName;
- } else {
- finalName = new Date().getTime() + "." + fileName;
- }
- try {
- // https://music-play.oss-cn-shenzhen.aliyuncs.com/backOss/file/f5f5ed93fae74fb683e8d116c40a884c.xlsx
- const response = await fetch(fileUrl);
- if (!response.ok) {
- throw new Error(`HTTP error! status: ${response.status}`);
- }
- const blob = await response.blob();
- const url = window.URL.createObjectURL(blob);
- const a = document.createElement("a");
- a.style.display = "none";
- a.href = url;
- a.download = finalName;
- document.body.appendChild(a);
- a.click();
- window.URL.revokeObjectURL(url);
- } catch (error) {}
- },
- },
- };
- </script>
- <style scoped>
- .container_all {
- width: 100vw;
- height: 100vh;
- }
- .container_public {
- width: 100vw;
- height: 100vh;
- overflow-y: auto;
- }
- .container_pdf {
- width: calc(100vw-2px);
- margin: 10px auto;
- border: 1px solid #e5e5e5;
- box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
- }
- .absolute_image1 {
- position: fixed;
- bottom: 365px;
- right: 20px;
- width: 30px;
- height: 30px;
- padding: 5px 5px;
- background-color: white;
- color: white;
- border: none;
- border-radius: 5px;
- cursor: pointer;
- }
- .absolute_image2 {
- position: fixed;
- bottom: 305px;
- right: 20px;
- width: 30px;
- height: 30px;
- padding: 5px 5px;
- background-color: white;
- color: white;
- border: none;
- border-radius: 5px;
- cursor: pointer;
- }
- .absolute_image3 {
- position: fixed;
- bottom: 260px;
- right: 20px;
- width: 30px;
- height: 30px;
- padding: 5px 5px;
- background-color: white;
- color: white;
- border: none;
- border-radius: 5px;
- cursor: pointer;
- }
- </style>
|