playRecordTime.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. const route = useRoute()
  32. const saveModel = reactive({
  33. loading: true,
  34. /**当前时长 */
  35. currentTime: 0,
  36. /**记录的开始时间 */
  37. startTime: 0,
  38. timer: null as any,
  39. /** 已经播放的时间 */
  40. playTime: 0
  41. })
  42. /** 建议学习总时长 */
  43. const total = computed(() => {
  44. const _total = props.list.reduce(
  45. (_total: number, item: any) => _total + item.adviseStudyTimeSecond,
  46. 0
  47. )
  48. return _total
  49. })
  50. const getPlayTime = async () => {
  51. saveModel.loading = true
  52. try {
  53. const res: any = await request.post(
  54. `${state.platformApi}/courseSchedule/getCoursewarePlayTime?courseScheduleId=${route.query.courseId}`
  55. )
  56. if (res.data) {
  57. saveModel.playTime = res.data
  58. }
  59. } catch (error) {}
  60. saveModel.loading = false
  61. handleStartInterval()
  62. }
  63. /** 记录时长 */
  64. const handleRecord = (isOut = false) => {
  65. saveModel.currentTime++
  66. const playTime = saveModel.currentTime - saveModel.startTime
  67. // 1分钟记录1次
  68. if (playTime >= 5 || isOut) {
  69. console.log('isOut', isOut)
  70. saveModel.startTime = saveModel.currentTime
  71. request.post(`${state.platformApi}/courseSchedule/coursewarePlayTime`, {
  72. params: {
  73. courseScheduleId: route.query.courseId,
  74. playTime
  75. },
  76. hideLoading: true
  77. })
  78. }
  79. }
  80. const timerRecord = ref()
  81. const handleStartInterval = () => {
  82. clearInterval(timerRecord.value)
  83. timerRecord.value = setInterval(() => handleRecord(), 1000)
  84. }
  85. onMounted(() => {
  86. getPlayTime()
  87. })
  88. onUnmounted(() => {
  89. clearInterval(timerRecord.value)
  90. })
  91. return () => (
  92. <div
  93. class={styles.playRecordTimeWrap}
  94. style={{ display: saveModel.loading || (saveModel.currentTime + saveModel.playTime > total.value) ? 'none' : '' }}
  95. >
  96. <div class={styles.playRecordTime}>
  97. <div class={styles.timeLoad}></div>
  98. <div>
  99. {getSecondRPM(saveModel.currentTime + saveModel.playTime)} / {getSecondRPM(total.value)}
  100. </div>
  101. </div>
  102. </div>
  103. )
  104. }
  105. })