u-pdf.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <template>
  2. <view class="pdf-page">
  3. <view class="pdf-wrap" :style="{'height':viewHeight}">
  4. <view ref="pdfRef" id="pdfView"></view>
  5. </view>
  6. <view v-if="showBottom" class="pdf-btn-group">
  7. <slot>
  8. <button class="btn-readed" size="mini" @click="handleComplete"
  9. :disabled="btnDisabled">{{showTimer ? countTimer<=1 ? '':'('+countTimer+'s)':''}}我已阅读</button>
  10. </slot>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. import Pdfh5 from "pdfh5";
  16. import "pdfh5/css/pdfh5.css";
  17. export default {
  18. name: "u-pdf",
  19. props: {
  20. url: {
  21. type: String,
  22. require: true
  23. },
  24. viewHeight: {
  25. type: String,
  26. default: "100%"
  27. },
  28. showBottom: {
  29. type: Boolean,
  30. default: true
  31. },
  32. options: {
  33. type: Object,
  34. default: () => ({
  35. cMapUrl: "https://unpkg.com/pdfjs-dist@3.8.162/cmaps/",
  36. lazy: false,
  37. withCredentials: true,
  38. // pdfLoaded:false,
  39. renderType: "svg",
  40. maxZoom: 3, //手势缩放最大倍数 默认3
  41. scrollEnable: true, //是否允许pdf滚动
  42. zoomEnable: true //是否允许pdf手势缩放
  43. })
  44. },
  45. disabled:{
  46. type:Boolean,
  47. default:true
  48. },
  49. showTimer:Boolean
  50. },
  51. data() {
  52. return {
  53. totalNum: 0,
  54. pdfh5: undefined,
  55. countTimer:10,
  56. timer:undefined,
  57. btnDisabled:true
  58. }
  59. },
  60. watch:{
  61. disabled(n,o){
  62. this.btnDisabled = this.disabled
  63. },
  64. showTimer(n,o){
  65. console.log("showTimer ",n);
  66. if(this.showTimer){
  67. this.startCountDownTimer()
  68. }
  69. }
  70. },
  71. mounted() {
  72. this.$nextTick(() => {
  73. this.loadPdf()
  74. })
  75. },
  76. // onPageScroll(scroll) {
  77. // console.log(`onPageScroll ${scroll}`,scroll);
  78. // },
  79. // onReachBottom(scroll) {
  80. // console.log(`onReachBottom 滚动到底部`);
  81. // },
  82. methods: {
  83. handleComplete(){
  84. this.$emit("onClickReader")
  85. },
  86. startCountDownTimer(){
  87. this.timer= setInterval(()=>{
  88. if(this.countTimer<=1){
  89. this.btnDisabled=false
  90. clearInterval(this.timer)
  91. }else{
  92. this.countTimer--
  93. }
  94. },1000)
  95. },
  96. loadPdf() {
  97. if (!this.url) {
  98. console.error("pdf url is null ");
  99. return;
  100. }
  101. const opts = {
  102. pdfurl: this.url,
  103. ...this.options
  104. }
  105. console.log(opts);
  106. this.pdfh5 = new Pdfh5('#pdfView', opts);
  107. // 准备加载pdf
  108. this.pdfh5.on("ready", () => {
  109. this.$emit("ready")
  110. })
  111. //pdf 监听完成事件
  112. this.pdfh5.on("complete", (status, msg, time) => {
  113. console.log(`PDF complete status=${status}, msg =${msg}`);
  114. this.$emit("complete", { status, msg })
  115. });
  116. // pdf 滚动事件 Svg 方式不会执行此方法, 需要在page中监听 onReachBottom,onPageScroll
  117. this.pdfh5.on("scroll", (scrollTop, currentNum) => {
  118. console.log(`pdf scrollTop =${scrollTop} , currentNum =${currentNum}`);
  119. this.$emit("scroll", { scrollTop, currentNum })
  120. });
  121. }
  122. }
  123. }
  124. </script>
  125. <style scoped>
  126. @import 'pdfh5/css/pdfh5.css';
  127. .pdf-page {
  128. width: 100%;
  129. height: 100%;
  130. display: flex;
  131. flex-direction: column;
  132. align-items: center;
  133. }
  134. .pdf-wrap {
  135. width: 100%;
  136. flex: 1;
  137. }
  138. .pdf-btn-group {
  139. width: 100%;
  140. border-top: 2rpx solid #F8F9FC;
  141. height: 8%;
  142. position: fixed;
  143. bottom: -1rpx;
  144. z-index: 999;
  145. background: white;
  146. display: flex;
  147. align-items: center;
  148. justify-content: center;
  149. }
  150. .btn-readed {
  151. background: #4E70F6;
  152. color: #ffffff;
  153. font-size: 32rpx;
  154. }
  155. </style>