handouts.vue 2.2 KB

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