index.vue 7.0 KB

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