index.vue 6.5 KB

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