index.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <template>
  2. <div class="container_all">
  3. <div class="container_public">
  4. <div v-if="numPages > 0">
  5. <div v-for="pageNumber in numPages" :key="pageNumber">
  6. <vue-pdf
  7. class="container_pdf"
  8. :src="fileUrl"
  9. :page="pageNumber"
  10. @page-loaded="onPageLoaded"
  11. ></vue-pdf>
  12. </div>
  13. </div>
  14. <vue-pdf
  15. v-else
  16. class="container_pdf"
  17. :src="fileUrl"
  18. @num-pages="setNumPages"
  19. v-show="false"
  20. ></vue-pdf>
  21. </div>
  22. <img
  23. v-if="loadAll"
  24. src="../../static/explain/down_up.png"
  25. class="absolute_image1"
  26. @click="download"
  27. />
  28. <img
  29. v-if="loadAll"
  30. src="../../static/explain/scale_in.png"
  31. class="absolute_image2"
  32. @click="scaleIn"
  33. />
  34. <img
  35. v-if="loadAll"
  36. src="../../static/explain/scale_out.png"
  37. class="absolute_image3"
  38. @click="scaleOut"
  39. />
  40. </div>
  41. </template>
  42. <script>
  43. import { detail } from "@/api/explain.js";
  44. import VuePdf from "vue-pdf";
  45. export default {
  46. components: {
  47. VuePdf,
  48. },
  49. data() {
  50. return {
  51. loadAll: false,
  52. name: "",
  53. fileUrl: "",
  54. fileName: "",
  55. currentPage: 1,
  56. numPages: 0,
  57. scale: 1,
  58. };
  59. },
  60. onLoad(e) {
  61. var that = this;
  62. that.id = e.id;
  63. that.getDetail();
  64. },
  65. methods: {
  66. ///接口获取数据
  67. getDetail() {
  68. var that = this;
  69. var id = that.id;
  70. detail(id).then((res) => {
  71. if (res.code === 0) {
  72. that.name = res.data.name;
  73. that.fileUrl = res.data.fileUrl;
  74. that.fileName = that.getFileExtension(that.fileUrl);
  75. }
  76. });
  77. },
  78. ///截取文件
  79. getFileExtension(fileUrl) {
  80. var filename = fileUrl.split("/").pop();
  81. var lastDotIndex = filename.lastIndexOf(".");
  82. if (lastDotIndex === -1 || lastDotIndex === filename.length - 1) {
  83. return "";
  84. } else {
  85. return filename.substring(lastDotIndex + 1).toLowerCase();
  86. }
  87. },
  88. setNumPages(numPages) {
  89. this.numPages = numPages;
  90. },
  91. onPageLoaded(page) {
  92. console.log("gadsfqwerqwerqrqr====" + page + "===" + this.numPages);
  93. if (this.numPages > 0 && page == this.numPages) {
  94. this.loadAll = true;
  95. }
  96. },
  97. prevPage() {
  98. if (this.currentPage > 1) {
  99. this.currentPage--;
  100. }
  101. },
  102. nextPage() {
  103. if (this.currentPage < this.numPages) {
  104. this.currentPage++;
  105. }
  106. },
  107. scaleIn() {
  108. this.scale *= 1.1;
  109. },
  110. scaleOut() {
  111. this.scale /= 1.1;
  112. },
  113. async download() {
  114. var that = this;
  115. var name = that.name;
  116. var fileUrl = that.fileUrl;
  117. var fileName = that.fileName;
  118. var finalName = "";
  119. if (name != null && name != "") {
  120. finalName = name + "." + fileName;
  121. } else {
  122. finalName = new Date().getTime() + "." + fileName;
  123. }
  124. try {
  125. // https://music-play.oss-cn-shenzhen.aliyuncs.com/backOss/file/f5f5ed93fae74fb683e8d116c40a884c.xlsx
  126. const response = await fetch(fileUrl);
  127. if (!response.ok) {
  128. throw new Error(`HTTP error! status: ${response.status}`);
  129. }
  130. const blob = await response.blob();
  131. const url = window.URL.createObjectURL(blob);
  132. const a = document.createElement("a");
  133. a.style.display = "none";
  134. a.href = url;
  135. a.download = finalName;
  136. document.body.appendChild(a);
  137. a.click();
  138. window.URL.revokeObjectURL(url);
  139. } catch (error) {}
  140. },
  141. },
  142. };
  143. </script>
  144. <style scoped>
  145. .container_all {
  146. width: 100vw;
  147. height: 100%;
  148. }
  149. .container_public {
  150. width: 100vw;
  151. height: 100%;
  152. display: flex;
  153. flex-direction: column;
  154. align-items: center;
  155. }
  156. .container_pdf {
  157. width: 100vw;
  158. margin: 10px 0;
  159. }
  160. .absolute_image1 {
  161. position: fixed;
  162. bottom: 365px;
  163. right: 20px;
  164. width: 30px;
  165. height: 30px;
  166. padding: 5px 5px;
  167. background-color: white;
  168. color: white;
  169. border: none;
  170. border-radius: 5px;
  171. cursor: pointer;
  172. }
  173. .absolute_image2 {
  174. position: fixed;
  175. bottom: 305px;
  176. right: 20px;
  177. width: 30px;
  178. height: 30px;
  179. padding: 5px 5px;
  180. background-color: white;
  181. color: white;
  182. border: none;
  183. border-radius: 5px;
  184. cursor: pointer;
  185. }
  186. .absolute_image3 {
  187. position: fixed;
  188. bottom: 260px;
  189. right: 20px;
  190. width: 30px;
  191. height: 30px;
  192. padding: 5px 5px;
  193. background-color: white;
  194. color: white;
  195. border: none;
  196. border-radius: 5px;
  197. cursor: pointer;
  198. }
  199. </style>