use-evaluat.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue'
  2. import dayjs from 'dayjs'
  3. import { IPostMessage, listenerMessage, postMessage, removeListenerMessage } from '/src/helpers/native-message'
  4. import runtime, * as RuntimeUtils from '/src/pages/detail/runtime'
  5. import { permissionPopup } from '/src/subpages/colexiu/popups/permission/permission'
  6. import SettingState from '/src/pages/detail/setting-state'
  7. import detailState from '/src/pages/detail/state'
  8. import { storeKeys } from '/src/constant/store-keys'
  9. import { soundEffectShow } from '../popups/sound-effect'
  10. import { evaluatingShow } from '../popups/evaluating'
  11. import { Toast } from 'vant'
  12. /** 打开校音或者距离上一次校音超过一天 */
  13. const soundNeedShow = () => {
  14. if (runtime.evaluatingStatus) {
  15. let time: number = -1
  16. const timeString = localStorage.getItem(storeKeys.lastCheckTime)
  17. try {
  18. const date = dayjs(parseFloat(timeString || '0'))
  19. if (date.isValid()) {
  20. time = Math.abs(date.diff(dayjs(new Date().valueOf()), 'day'))
  21. }
  22. } catch (e) {}
  23. console.log(SettingState.sett.tuning, time, timeString)
  24. if (SettingState.sett.tuning || time > 0 || !timeString) {
  25. soundEffectShow.value = true
  26. }
  27. }
  28. }
  29. /**
  30. * 检查耳机连接状态,并且打开耳机连接状态的提示,以及校音
  31. * @param res IPostMessage
  32. */
  33. const setWiredStatus = (res?: IPostMessage) => {
  34. if (runtime.evaluatingStatus) {
  35. permissionPopup.active = 'earphone'
  36. permissionPopup.show = !res?.content.checkIsWired
  37. soundNeedShow()
  38. } else {
  39. permissionPopup.show = false
  40. }
  41. }
  42. /**
  43. * 开启评测模式后,需要效验 耳机,效音
  44. */
  45. export const handleCheckEvaluatStatus = () => {
  46. // 是否需要效音
  47. soundNeedShow()
  48. // 检测耳机状态
  49. // postMessage({ api: 'isWiredHeadsetOn' }, setWiredStatus)
  50. postMessage({ api: 'isWiredHeadsetOn' }, (evt) => {
  51. // console.log('🚀 ~ 耳机状态', evt)
  52. permissionPopup.active = 'earphone'
  53. permissionPopup.show = !evt?.content.checkIsWired
  54. })
  55. }
  56. /**
  57. * 监听评测状态打开
  58. */
  59. export const useWiredHeadsetCheck = () => {
  60. const wiredStatus = ref(false)
  61. // watch(
  62. // () => runtime.evaluatingStatus,
  63. // () => {
  64. // if (!runtime.evaluatingStatus) return
  65. // postMessage(
  66. // {
  67. // api: 'isWiredHeadsetOn',
  68. // },
  69. // setWiredStatus
  70. // )
  71. // }
  72. // )
  73. // watch(
  74. // () => permissionPopup.show,
  75. // () => {
  76. // soundNeedShow()
  77. // }
  78. // )
  79. /** 监听评测状态,退出评测模式停止播放,清除提示信息 */
  80. watch([() => runtime.evaluatingStatus], () => {
  81. // Toast.clear()
  82. // if (!runtime.evaluatingStatus) {
  83. // runtime.evaluatingTips = false
  84. // RuntimeUtils.pause()
  85. // RuntimeUtils.setCurrentTime(0)
  86. // }
  87. if (SettingState.sett.fingering) {
  88. // RuntimeUtils.event.emit('settingFingeringChange')
  89. }
  90. // console.log('settingFingeringChange', 11, runtime.evaluatingStatus)
  91. })
  92. /** 监听耳机状态啊 */
  93. watch(
  94. [() => runtime.evaluatingStatus, () => permissionPopup.show, () => soundEffectShow.value],
  95. ([evaluatingStatus, permissionShow, soundEffectShow]) => {
  96. // wiredStatus.value = evaluatingStatus && !permissionShow && !soundEffectShow
  97. // if (!runtime.evaluatingStatus) {
  98. // detailState.evaluatings = {}
  99. // evaluatingShow.value = false
  100. // }
  101. }
  102. )
  103. // onMounted(() => {
  104. // listenerMessage('listenerWiredStatus', setWiredStatus)
  105. // })
  106. // onBeforeUnmount(() => {
  107. // removeListenerMessage('listenerWiredStatus', setWiredStatus)
  108. // })
  109. return [wiredStatus]
  110. }