playRecordTime.tsx 3.1 KB

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