playRecordTime.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. },
  16. setup(props, { expose }) {
  17. const pageVisibility = usePageVisibility()
  18. watch(pageVisibility, (value) => {
  19. if (value == 'hidden') {
  20. handleOut()
  21. } else {
  22. // 页面显示
  23. handleStartInterval()
  24. }
  25. })
  26. const handleOut = () => {
  27. clearInterval(timerRecord.value)
  28. handleRecord()
  29. }
  30. expose({
  31. handleOut
  32. })
  33. const route = useRoute()
  34. const saveModel = reactive({
  35. /**当前时长 */
  36. currentTime: 0,
  37. /**记录的开始时间 */
  38. startTime: 0,
  39. timer: null as any,
  40. /** 已经播放的时间 */
  41. playTime: 0
  42. })
  43. /** 建议学习总时长 */
  44. const total = computed(() => {
  45. const _total = props.list.reduce(
  46. (_total: number, item: any) => _total + item.adviseStudyTimeSecond,
  47. 0
  48. )
  49. return _total
  50. })
  51. const getPlayTime = async () => {
  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. handleStartInterval()
  61. }
  62. /** 记录时长 */
  63. const handleRecord = () => {
  64. saveModel.currentTime++
  65. const playTime = saveModel.currentTime - saveModel.startTime
  66. // 1分钟记录1次
  67. if (playTime >= 5) {
  68. saveModel.startTime = saveModel.currentTime
  69. request.post(`${state.platformApi}/courseSchedule/coursewarePlayTime`, {
  70. params: {
  71. courseScheduleId: route.query.courseId,
  72. playTime
  73. },
  74. hideLoading: true
  75. })
  76. }
  77. }
  78. const timerRecord = ref()
  79. const handleStartInterval = () => {
  80. clearInterval(timerRecord.value)
  81. timerRecord.value = setInterval(handleRecord, 1000)
  82. }
  83. onMounted(() => {
  84. getPlayTime()
  85. })
  86. onUnmounted(() => {
  87. clearInterval(timerRecord.value)
  88. })
  89. return () => (
  90. <div
  91. class={styles.playRecordTimeWrap}
  92. style={{ display: saveModel.currentTime + saveModel.playTime > total.value ? 'none' : '' }}
  93. >
  94. <div class={styles.playRecordTime}>
  95. <div class={styles.timeLoad}></div>
  96. <div>
  97. {getSecondRPM(saveModel.currentTime + saveModel.playTime)} / {getSecondRPM(total.value)}
  98. </div>
  99. </div>
  100. </div>
  101. )
  102. }
  103. })