index.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <template>
  2. <div class="container_all">
  3. <div class="container_public" :style="{ transform: `scale(${scale})` }">
  4. <div v-for="pageNumber in loadedPages" :key="pageNumber">
  5. <vue-pdf
  6. class="container_pdf"
  7. :src="fileUrl"
  8. :page="pageNumber"
  9. @page-loaded="onPageLoaded"
  10. ></vue-pdf>
  11. </div>
  12. </div>
  13. <img
  14. v-if="loadAll"
  15. src="../../static/explain/down_up.png"
  16. class="absolute_image1"
  17. @click="download"
  18. />
  19. <!-- <img
  20. v-if="loadAll"
  21. src="../../static/explain/scale_in.png"
  22. class="absolute_image2"
  23. @click="scaleIn"
  24. />
  25. <img
  26. v-if="loadAll"
  27. src="../../static/explain/scale_out.png"
  28. class="absolute_image3"
  29. @click="scaleOut"
  30. /> -->
  31. </div>
  32. </template>
  33. <script>
  34. import { detail } from "@/api/explain.js";
  35. import VuePdf from "vue-pdf";
  36. export default {
  37. components: {
  38. VuePdf,
  39. },
  40. data() {
  41. return {
  42. loadAll: false,
  43. name: "",
  44. fileUrl: "",
  45. fileName: "",
  46. currentPage: 1,
  47. numPages: 0,
  48. scale: 1,
  49. loadedPages: 1,
  50. };
  51. },
  52. onLoad(e) {
  53. var that = this;
  54. that.id = e.id;
  55. that.getDetail();
  56. },
  57. methods: {
  58. // 滚动加载逻辑
  59. // {"type":"scroll","timeStamp":24324.79999999702,"detail":{},
  60. // "target":{"id":"","offsetLeft":0,"offsetTop":0,"dataset":{}},
  61. // "currentTarget":{"id":"","offsetLeft":0,"offsetTop":0,"dataset":{}},"touches":[],
  62. // "changedTouches":[],"mp":{"@warning":"mp is deprecated","type":"scroll",
  63. // "timeStamp":24324.79999999702,"detail":{},"target":{"id":"","offsetLeft":0,"offsetTop":0,"dataset":{}},
  64. // "currentTarget":{"id":"","offsetLeft":0,"offsetTop":0,"dataset":{}},"touches":[],"changedTouches":[]},
  65. // "_processed":true}
  66. handleScroll(e) {
  67. // const target = e.target;
  68. // var offsetTop = target.offsetTop;
  69. // // 滚动到底部且未加载完所有页
  70. // if (this.loadedPages < this.numPages) {
  71. // this.loadedPages += 1; // 加载下一页
  72. // }
  73. },
  74. ///接口获取数据
  75. getDetail() {
  76. var that = this;
  77. var id = that.id;
  78. detail(id).then((res) => {
  79. if (res.code === 0) {
  80. that.name = res.data.name;
  81. that.fileUrl = res.data.fileUrl;
  82. that.fileName = that.getFileExtension(that.fileUrl);
  83. that.getPdfPages();
  84. }
  85. });
  86. },
  87. async getPdfPages() {
  88. var that = this;
  89. const loadingTask = VuePdf.createLoadingTask(that.fileUrl);
  90. const pdf = await loadingTask.promise;
  91. this.numPages = pdf.numPages;
  92. // this.loadedPages = this.numPages;
  93. if (this.numPages == 0) {
  94. return;
  95. }
  96. this.loadedPages = this.numPages > 1 ? 1 : 1;
  97. },
  98. ///截取文件
  99. getFileExtension(fileUrl) {
  100. var filename = fileUrl.split("/").pop();
  101. var lastDotIndex = filename.lastIndexOf(".");
  102. if (lastDotIndex === -1 || lastDotIndex === filename.length - 1) {
  103. return "";
  104. } else {
  105. return filename.substring(lastDotIndex + 1).toLowerCase();
  106. }
  107. },
  108. prevPage() {
  109. if (this.currentPage > 1) {
  110. this.currentPage--;
  111. }
  112. },
  113. nextPage() {
  114. if (this.currentPage < this.numPages) {
  115. this.currentPage++;
  116. }
  117. },
  118. scaleIn() {
  119. this.scale *= 1.1;
  120. },
  121. scaleOut() {
  122. this.scale /= 1.1;
  123. },
  124. onPageLoaded(page) {
  125. if (this.numPages > 0) {
  126. if (!this.loadAll) {
  127. this.loadAll = true;
  128. }
  129. var p = this.loadedPages;
  130. if (p < this.numPages) {
  131. this.loadedPages = p + 1;
  132. }
  133. }
  134. },
  135. async download() {
  136. var that = this;
  137. var name = that.name;
  138. var fileUrl = that.fileUrl;
  139. var fileName = that.fileName;
  140. var finalName = "";
  141. if (name != null && name != "") {
  142. finalName = name + "." + fileName;
  143. } else {
  144. finalName = new Date().getTime() + "." + fileName;
  145. }
  146. try {
  147. // https://music-play.oss-cn-shenzhen.aliyuncs.com/backOss/file/f5f5ed93fae74fb683e8d116c40a884c.xlsx
  148. const response = await fetch(fileUrl);
  149. if (!response.ok) {
  150. throw new Error(`HTTP error! status: ${response.status}`);
  151. }
  152. const blob = await response.blob();
  153. const url = window.URL.createObjectURL(blob);
  154. const a = document.createElement("a");
  155. a.style.display = "none";
  156. a.href = url;
  157. a.download = finalName;
  158. document.body.appendChild(a);
  159. a.click();
  160. window.URL.revokeObjectURL(url);
  161. } catch (error) {}
  162. },
  163. },
  164. };
  165. </script>
  166. <style scoped>
  167. .container_all {
  168. width: 100vw;
  169. height: 100vh;
  170. }
  171. .container_public {
  172. width: 100vw;
  173. height: 100vh;
  174. overflow-y: auto;
  175. }
  176. .container_pdf {
  177. width: calc(100vw-2px);
  178. margin: 10px auto;
  179. border: 1px solid #e5e5e5;
  180. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  181. }
  182. .absolute_image1 {
  183. position: fixed;
  184. bottom: 365px;
  185. right: 20px;
  186. width: 30px;
  187. height: 30px;
  188. padding: 5px 5px;
  189. background-color: white;
  190. color: white;
  191. border: none;
  192. border-radius: 5px;
  193. cursor: pointer;
  194. }
  195. .absolute_image2 {
  196. position: fixed;
  197. bottom: 305px;
  198. right: 20px;
  199. width: 30px;
  200. height: 30px;
  201. padding: 5px 5px;
  202. background-color: white;
  203. color: white;
  204. border: none;
  205. border-radius: 5px;
  206. cursor: pointer;
  207. }
  208. .absolute_image3 {
  209. position: fixed;
  210. bottom: 260px;
  211. right: 20px;
  212. width: 30px;
  213. height: 30px;
  214. padding: 5px 5px;
  215. background-color: white;
  216. color: white;
  217. border: none;
  218. border-radius: 5px;
  219. cursor: pointer;
  220. }
  221. </style>