useVideoRecord.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import {reactive} from 'vue'
  2. import {saveWatchRecord} from "@/api/webApi/webVideo";
  3. export const useVideoRecord = function () {
  4. const state = reactive({
  5. committed: false,
  6. duration: 0,
  7. percent: 0
  8. })
  9. const reset = function () {
  10. state.committed = false
  11. state.duration = 0
  12. state.percent = 0
  13. }
  14. const syncProgress = function ({currentTime, duration}) {
  15. state.duration = duration
  16. state.percent = 0 // for safe division
  17. if (duration > 0) state.percent = currentTime * 100 / duration
  18. }
  19. const commit = async function ({src, aliIdType}) {
  20. if (!src) return // 无效数据
  21. if (state.committed) return // 已提交
  22. if (!state.percent) return // 无效数据
  23. if (state.percent < 2) return // <2%播放太短,也不记,防止过度触发提交
  24. await saveWatchRecord(src, state.duration, state.percent, aliIdType)
  25. state.committed = true
  26. console.log('saveWatchRecord committed')
  27. }
  28. return {
  29. state,
  30. reset,
  31. syncProgress,
  32. commit
  33. }
  34. }