handouts.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <template>
  2. <div style="height: 100%; overflow-y: auto">
  3. <div v-if="!showPdf">渲染pdf失败</div>
  4. <div class="mb-4" v-if="showPdf">
  5. <a-button type="primary" @click="downPdf">下载讲义</a-button>
  6. </div>
  7. <vue-office-pdf
  8. :src="props.itemObj.url"
  9. @error="errorHandler"
  10. ref="scrollDiv"
  11. @scroll="handleScroll"
  12. :options="{ width: '100%' }"
  13. style="height: calc(100% - 50px)"
  14. />
  15. </div>
  16. </template>
  17. <script setup>
  18. import classCentre from '@/api/student/classCentre'
  19. import { message } from 'ant-design-vue'
  20. import VueOfficePdf from '@vue-office/pdf'
  21. const scrollDiv = ref(null)
  22. const scrollPercent = ref(0)
  23. const showPdf = ref(true)
  24. const props = defineProps({
  25. itemObj: {
  26. type: Object,
  27. default: () => {}
  28. },
  29. hourId: {
  30. type: [String, Number],
  31. default: 0
  32. }
  33. })
  34. function errorHandler() {
  35. showPdf.value = false
  36. }
  37. const maxStr = ref(0)
  38. function handleScroll() {
  39. const el = scrollDiv.value.$el
  40. if (!el) return
  41. const scrollTop = el.scrollTop
  42. const scrollHeight = el.scrollHeight
  43. const clientHeight = el.clientHeight
  44. // 计算百分比
  45. scrollPercent.value = Math.round((scrollTop / (scrollHeight - clientHeight)) * 100)
  46. if (scrollPercent.value > maxStr.value) {
  47. maxStr.value = scrollPercent.value
  48. }
  49. }
  50. const downPdf = () => {
  51. const link = document.createElement('a')
  52. link.href = props.itemObj.url
  53. link.download = props.itemObj.name
  54. document.body.appendChild(link)
  55. link.click()
  56. document.body.removeChild(link)
  57. message.success('开始下载文件')
  58. addClassPlan()
  59. }
  60. const nowTimesStr = Date.now()
  61. const outNowTimesStr = ref()
  62. const addClassPlan = () => {
  63. classCentre
  64. .classPlanAdd({
  65. hourId: props.hourId,
  66. type: 2,
  67. funcType: 2
  68. })
  69. .then((data) => {})
  70. }
  71. const addScrollPlan = () => {
  72. const el = scrollDiv.value?.$el
  73. if(el && el.clientHeight == el.scrollHeight){
  74. maxStr.value = 100
  75. }
  76. classCentre
  77. .classPlanAdd({
  78. progress: showPdf.value ? maxStr.value : 0,
  79. hourId: props.hourId,
  80. stayTime: outNowTimesStr.value - nowTimesStr,
  81. type: 1,
  82. funcType: 2
  83. })
  84. .then((data) => {})
  85. }
  86. onBeforeUnmount(() => {
  87. outNowTimesStr.value = Date.now()
  88. addScrollPlan()
  89. })
  90. </script>
  91. <style scoped lang="less">
  92. .flc {
  93. display: flex;
  94. align-items: center;
  95. }
  96. .fcbc {
  97. display: flex;
  98. justify-content: space-between;
  99. align-items: center;
  100. }
  101. </style>