preview.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <template>
  2. <div class="head">
  3. <van-popup :close-on-click-overlay="false" round v-model:show="sendEamilVisible">
  4. <div class="email">
  5. <div
  6. @touchstart="
  7. () => {
  8. sendEamilVisible = false;
  9. }
  10. "
  11. class="email-close"
  12. >
  13. <img class="" src="@/assets/img/close_gray.png" />
  14. </div>
  15. <img class="email-letter" src="@/assets/img/letter.png" />
  16. <div class="email-text1">
  17. <span>请输入您的邮箱</span>
  18. </div>
  19. <div class="email-text2">
  20. <span>5分钟内将发送到您的邮箱,请注意查收。 </span>
  21. </div>
  22. <div class="email-filed">
  23. <input v-model="emailAddress" class="email-filed-text" type="email" />
  24. <div class="email-filed-icon">
  25. <img src="@/assets/img/email-icon.png" />
  26. </div>
  27. </div>
  28. <div @touchstart="send" class="email-buttons">
  29. <div class="email-buttons-sure">确认</div>
  30. </div>
  31. </div>
  32. </van-popup>
  33. <span class="head-text">学习资料提取</span>
  34. <div class="head-button">
  35. <div class="head-button-download">
  36. <img
  37. class=""
  38. style="width: 12px; height: 13px; margin-right: 5px"
  39. src="@/assets/img/downIcon.png"
  40. />
  41. 下载到本地
  42. </div>
  43. <div
  44. @touchstart="
  45. () => {
  46. sendEamilVisible = true;
  47. }
  48. "
  49. class="head-button-send"
  50. >
  51. <img
  52. style="width: 12px; height: 13px; margin-right: 5px"
  53. src="@/assets/img/sendIcon.png"
  54. />发送到邮箱
  55. </div>
  56. </div>
  57. </div>
  58. <div id="pdf-canvas"></div>
  59. </template>
  60. <script lang="ts">
  61. import { defineComponent } from 'vue';
  62. import * as PDFJS from 'pdfjs-dist/legacy/build/pdf'; // 引入PDFJS
  63. import pdfjsWorker from 'pdfjs-dist/legacy/build/pdf.worker.entry.js'; // 引入workerSrc的地址
  64. import 'pdfjs-dist/web/pdf_viewer.css';
  65. import { PDFDocumentProxy } from 'pdfjs-dist/types/web/pdf_find_controller';
  66. PDFJS.GlobalWorkerOptions.workerSrc = pdfjsWorker; //设置PDFJS.GlobalWorkerOptions.workerSrc的地址
  67. document.title = '预览pdf';
  68. import { Toast } from 'vant';
  69. import api from '@/api/api';
  70. export default defineComponent({
  71. setup() {},
  72. data() {
  73. return {
  74. pdfPagesNum: 1,
  75. readerpdfDoc: null,
  76. sendEamilVisible: false,
  77. pdfUrl: '',
  78. emailAddress: '',
  79. };
  80. },
  81. methods: {
  82. download() {
  83. const name: string = this.$route.query.name as string;
  84. let src = window.location.origin + `/pdf/${name}.pdf`;
  85. tt.miniProgram.navigateTo({
  86. url:'/otherPages/download/index?src='+src
  87. })
  88. },
  89. send() {
  90. let reg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
  91. if (reg.test(this.emailAddress)) {
  92. api
  93. .studentEmailInfo({
  94. address: this.emailAddress,
  95. dictCode: Number(this.$route.query.code),
  96. })
  97. .then((res) => {
  98. Toast.success('成功发送');
  99. });
  100. } else {
  101. Toast.fail('邮箱格式错误');
  102. }
  103. },
  104. getPdf(url: string) {
  105. let that = this;
  106. (document.getElementById('pdf-canvas') as HTMLDivElement).innerHTML = '';
  107. PDFJS.getDocument(url).promise.then((pdfDoc) => {
  108. this.pdfPagesNum = pdfDoc.numPages; // pdf的总页数
  109. //获取第pageNum页的数据
  110. for (let index = 1; index <= pdfDoc.numPages; index++) {
  111. that.showPdfs(pdfDoc, index);
  112. }
  113. });
  114. },
  115. showPdfs(pdfDoc: PDFDocumentProxy, pageNum: number) {
  116. pdfDoc.getPage(pageNum).then((page) => {
  117. // 设置canvas相关的属性
  118. // console.log(canvasDomArr.value)
  119. const canvas: HTMLCanvasElement = document.createElement('canvas');
  120. canvas.classList.add('pdf-canvas');
  121. const ctx = canvas.getContext('2d');
  122. const dpr = window.devicePixelRatio || 1;
  123. const bsr =
  124. ctx?.webkitBackingStorePixelRatio ||
  125. ctx?.mozBackingStorePixelRatio ||
  126. ctx?.msBackingStorePixelRatio ||
  127. ctx?.oBackingStorePixelRatio ||
  128. ctx?.backingStorePixelRatio ||
  129. 1;
  130. const ratio = dpr / bsr;
  131. const viewport = page.getViewport({ scale: 1 });
  132. canvas.width = viewport.width * ratio;
  133. canvas.height = viewport.height * ratio;
  134. // canvas.style.width = viewport.width + "px";
  135. // canvas.style.height = viewport.height + "px";
  136. ctx.setTransform(ratio, 0, 0, ratio, 0, 0);
  137. const context = {
  138. canvasContext: ctx,
  139. viewport: viewport,
  140. };
  141. // 数据渲染到canvas画布上
  142. page.render(context);
  143. //插入canvas
  144. document.getElementById('pdf-canvas')?.appendChild(canvas);
  145. });
  146. },
  147. },
  148. mounted() {
  149. const token: string = this.$route.query.token as string;
  150. window.localStorage.setItem('token', token);
  151. const name: string = this.$route.query.name as string;
  152. this.getPdf(`./pdf/${name}.pdf`);
  153. },
  154. });
  155. // PDFJS.GlobalWorkerOptions.workerSrc = pdfjsWorker; //设置PDFJS.GlobalWorkerOptions.workerSrc的地址
  156. // let container = ref(null);
  157. // let canvasDomArr = ref([]);
  158. // let renderContext = (el) => {
  159. // canvasDomArr.value.push(el);
  160. // };
  161. // let pdfUrl = './web/科目一标识.pdf';
  162. // let pdfPagesNum = ref(0);
  163. // let readerpdfDoc = null;
  164. // function pdfChange(event) {
  165. // if (window.createObjectURL != undefined) {
  166. // // basic
  167. // pdfUrl = window.createObjectURL(event.target.files[0]);
  168. // } else if (window.URL != undefined) {
  169. // // mozilla(firefox)
  170. // pdfUrl = window.URL.createObjectURL(event.target.files[0]);
  171. // } else if (window.webkitURL != undefined) {
  172. // // webkit or chrome
  173. // pdfUrl = window.webkitURL.createObjectURL(event.target.files[0]);
  174. // }
  175. // getPdf(pdfUrl, 1);
  176. // }
  177. // function getPdf(url, pageNum) {
  178. // PDFJS.getDocument(url).promise.then((pdfDoc) => {
  179. // pdfPagesNum.value = pdfDoc.numPages; // pdf的总页数
  180. // //获取第pageNum页的数据
  181. // for (let index = 1; index <= pdfDoc.numPages; index++) {
  182. // readerpdfDoc = pdfDoc;
  183. // showPdfs(pdfDoc, index);
  184. // }
  185. // });
  186. // }
  187. // function currentChange(num) {
  188. // showPdfs(readerpdfDoc, num);
  189. // }
  190. // function showPdfs(pdfDoc, pageNum) {
  191. // pdfDoc.getPage(pageNum).then((page) => {
  192. // // 设置canvas相关的属性
  193. // // console.log(canvasDomArr.value)
  194. // const canvas = canvasDomArr.value[pageNum - 1];
  195. // console.log(canvas)
  196. // const ctx = canvas.getContext('2d');
  197. // const dpr = window.devicePixelRatio || 1;
  198. // const bsr =
  199. // ctx.webkitBackingStorePixelRatio ||
  200. // ctx.mozBackingStorePixelRatio ||
  201. // ctx.msBackingStorePixelRatio ||
  202. // ctx.oBackingStorePixelRatio ||
  203. // ctx.backingStorePixelRatio ||
  204. // 1;
  205. // const ratio = dpr / bsr;
  206. // const viewport = page.getViewport({ scale: 1 });
  207. // canvas.width = viewport.width * ratio;
  208. // canvas.height = viewport.height * ratio;
  209. // // canvas.style.width = viewport.width + "px";
  210. // // canvas.style.height = viewport.height + "px";
  211. // ctx.setTransform(ratio, 0, 0, ratio, 0, 0);
  212. // const context = {
  213. // canvasContext: ctx,
  214. // viewport: viewport,
  215. // };
  216. // // 数据渲染到canvas画布上
  217. // page.render(context);
  218. // });
  219. // }
  220. // onMounted(() => {
  221. // getPdf('./web/科目一标识.pdf', 1);
  222. // });
  223. </script>
  224. <style lang="scss">
  225. .email {
  226. position: relative;
  227. width: 580px;
  228. .email-close {
  229. position: absolute;
  230. top: 48px;
  231. right: 28px;
  232. > img {
  233. width: 32px;
  234. height: 32px;
  235. }
  236. }
  237. .email-letter {
  238. width: 580px;
  239. height: 18px;
  240. }
  241. .email-text1 {
  242. padding-top: 44px;
  243. padding-bottom: 40px;
  244. font-weight: 600;
  245. }
  246. .email-text2 {
  247. font-size: 26px;
  248. color: #5c6066;
  249. margin-bottom: 30px;
  250. }
  251. .email-buttons {
  252. padding-top: 40px;
  253. padding-bottom: 52px;
  254. display: flex;
  255. justify-content: center;
  256. .email-buttons-sure {
  257. width: 200px;
  258. font-size: 26px;
  259. height: 60px;
  260. line-height: 60px;
  261. background: #498ef5;
  262. color: #fff;
  263. border-radius: 30px;
  264. overflow: hidden;
  265. }
  266. }
  267. .email-filed {
  268. width: 100%;
  269. display: flex;
  270. justify-content: center;
  271. position: relative;
  272. .email-filed-icon {
  273. position: absolute;
  274. left: 60px;
  275. top: 12px;
  276. > img {
  277. width: 29px;
  278. height: 22px;
  279. }
  280. }
  281. .email-filed-text {
  282. width: 500px;
  283. background: #f2f3f5;
  284. border: 2px solid #e8e8e8;
  285. border-radius: 30px;
  286. overflow: hidden;
  287. color: #6f747c;
  288. height: 60px;
  289. padding-left: 60px;
  290. }
  291. }
  292. }
  293. .head {
  294. width: 100%;
  295. height: 118px;
  296. padding-left: 20px;
  297. padding-right: 40px;
  298. display: flex;
  299. align-content: center;
  300. align-items: center;
  301. justify-content: space-between;
  302. .head-text {
  303. color: #498ef5;
  304. font-size: 34px;
  305. }
  306. .head-button {
  307. display: flex;
  308. justify-content: space-between;
  309. .head-button-download {
  310. background: #01c18d;
  311. width: 204px;
  312. height: 48px;
  313. color: #fff;
  314. border-radius: 60px;
  315. margin-right: 15px;
  316. line-height: 48px;
  317. font-size: 26px;
  318. display: flex;
  319. align-content: center;
  320. align-items: center;
  321. justify-content: center;
  322. cursor: pointer;
  323. }
  324. .head-button-send {
  325. background: #498ef5;
  326. width: 204px;
  327. height: 48px;
  328. color: #fff;
  329. border-radius: 60px;
  330. line-height: 48px;
  331. font-size: 26px;
  332. display: flex;
  333. align-content: center;
  334. align-items: center;
  335. justify-content: center;
  336. cursor: pointer;
  337. }
  338. }
  339. }
  340. .pdf-canvas {
  341. width: 100%;
  342. }
  343. </style>