handouts.vue 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. classCentre
  73. .classPlanAdd({
  74. progress: maxStr.value,
  75. hourId: props.hourId,
  76. stayTime: outNowTimesStr.value - nowTimesStr,
  77. type: 1,
  78. funcType:2
  79. })
  80. .then((data) => {})
  81. }
  82. onBeforeUnmount(() => {
  83. outNowTimesStr.value = Date.now()
  84. addScrollPlan()
  85. })
  86. </script>
  87. <style scoped lang="less">
  88. .flc {
  89. display: flex;
  90. align-items: center;
  91. }
  92. .fcbc {
  93. display: flex;
  94. justify-content: space-between;
  95. align-items: center;
  96. }
  97. </style>