playRecordTime.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import request from '@/helpers/request'
  2. import { getSecondRPM } from '@/helpers/utils'
  3. import { state } from '@/state'
  4. import { usePageVisibility } from '@vant/use'
  5. import { computed, defineComponent, onMounted, onUnmounted, reactive, ref, watch } from 'vue'
  6. import { useRoute } from 'vue-router'
  7. import styles from './index.module.less'
  8. export default defineComponent({
  9. name: 'playRecordTime',
  10. props: {
  11. list: {
  12. type: Array,
  13. default: () => []
  14. },
  15. isCurrentCoursewareMenu: {
  16. type: Boolean,
  17. default: false,
  18. }
  19. },
  20. setup(props, { expose }) {
  21. const pageVisibility = usePageVisibility()
  22. watch(pageVisibility, (value) => {
  23. if (value == 'hidden') {
  24. handleOut()
  25. } else {
  26. // 页面显示
  27. handleStartInterval()
  28. }
  29. })
  30. watch(() => props.isCurrentCoursewareMenu, () => {
  31. if (!props.isCurrentCoursewareMenu) {
  32. handleOut()
  33. } else {
  34. getPlayTime(true)
  35. }
  36. })
  37. const handleOut = () => {
  38. clearInterval(timerRecord.value)
  39. handleRecord(true)
  40. }
  41. expose({
  42. handleOut
  43. })
  44. const route = useRoute()
  45. const saveModel = reactive({
  46. loading: true,
  47. /**当前时长 */
  48. currentTime: 0,
  49. /**记录的开始时间 */
  50. startTime: 0,
  51. timer: null as any,
  52. /** 已经播放的时间 */
  53. playTime: 0
  54. })
  55. /** 建议学习总时长 */
  56. const total = computed(() => {
  57. const _total = props.list.reduce(
  58. (_total: number, item: any) => _total + (item.totalMaterialTimeSecond || 0),
  59. 0
  60. )
  61. return _total
  62. })
  63. const getPlayTime = async (noLoading: boolean = false) => {
  64. if(!noLoading) saveModel.loading = true
  65. try {
  66. const res: any = await request.post(
  67. `${state.platformApi}/courseSchedule/getCoursewarePlayTime?courseScheduleId=${route.query.courseId}`
  68. )
  69. if (res.data) {
  70. saveModel.playTime = res.data
  71. }
  72. } catch (error) {}
  73. saveModel.loading = false
  74. handleStartInterval()
  75. }
  76. /** 记录时长 */
  77. const handleRecord = (isOut = false) => {
  78. saveModel.currentTime++
  79. const playTime = saveModel.currentTime - saveModel.startTime
  80. // 1分钟记录1次
  81. if (playTime >= 5 || isOut) {
  82. saveModel.startTime = saveModel.currentTime
  83. request.post(`${state.platformApi}/courseSchedule/coursewarePlayTime`, {
  84. params: {
  85. courseScheduleId: route.query.courseId,
  86. playTime
  87. },
  88. hideLoading: true
  89. })
  90. }
  91. }
  92. const timerRecord = ref()
  93. const handleStartInterval = () => {
  94. clearInterval(timerRecord.value)
  95. timerRecord.value = setInterval(() => handleRecord(), 1000)
  96. }
  97. onMounted(() => {
  98. getPlayTime()
  99. })
  100. onUnmounted(() => {
  101. clearInterval(timerRecord.value)
  102. })
  103. return () => (
  104. <div
  105. class={styles.playRecordTimeWrap}
  106. style={{ display: saveModel.loading || (saveModel.currentTime + saveModel.playTime > total.value) ? 'none' : '' }}
  107. >
  108. <div class={styles.playRecordTime}>
  109. <div class={styles.timeLoad}></div>
  110. <div>
  111. {getSecondRPM(saveModel.currentTime + saveModel.playTime)} / {getSecondRPM(total.value)}
  112. </div>
  113. </div>
  114. </div>
  115. )
  116. }
  117. })