use-app.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import { onBeforeUnmount, onMounted, Ref, ref, watch } from 'vue'
  2. import { useClientType, useOriginSearch } from '.'
  3. import request from '/src/helpers/request'
  4. import originRequest from 'umi-request'
  5. import store from 'store'
  6. import runtime, * as RuntimeUtils from '/src/pages/detail/runtime'
  7. import detailState, { GradualTimes } from '/src/pages/detail/state'
  8. import SettingState from '/src/pages/detail/setting-state'
  9. import { listenerMessage, postMessage } from '/src/helpers/native-message'
  10. import audiosInstance from '/src/helpers/multiple-audio'
  11. import { formatXML, onlyVisible, getCustomInfo } from '/src/pages/detail/helpers'
  12. import { MusicSheelDetail, ShaeetStatusType } from '../index.d'
  13. import { browser, getRequestHostname } from '/src/helpers/utils'
  14. import formatId, { formatdata, getSubjectIdCode } from '../fingering/format-id'
  15. import { evaluatStopPlay } from '../buttons/evaluating'
  16. import state from '/src/pages/detail/state'
  17. import { getGradualLengthByXml } from '/src/pages/detail/calcSpeed'
  18. import { musicInfo } from '../state'
  19. import { getInstrumentName } from '../helpers/instruments'
  20. const search = useOriginSearch()
  21. const skpList = ['Ukulele']
  22. /**
  23. * 特殊教材分类id
  24. */
  25. export const classids = [1, 2, 6, 7, 8, 9, 3, 10, 11, 12, 13, 4, 14, 15, 16, 17, 30, 31, 35, 36, 108]; // 大雅金唐, 竖笛教程, 声部训练展开的分类ID
  26. // export const classids = [1, 30, 97]; // [大雅金唐, 竖笛教程, 声部训练]
  27. /**
  28. * 获取xml并前置格式化
  29. * @param url xml地址
  30. * @param detail 音乐详情
  31. * @returns Ref<string>
  32. */
  33. export const useXml = async (url: string, detail: MusicSheelDetail) => {
  34. const partIndex = Number(search['part-index']) || 0
  35. let score = ref<string>('')
  36. try {
  37. const xml = await originRequest(url)
  38. const parseXmlInfo = getCustomInfo(xml)
  39. if (skpList.includes(parseXmlInfo.code)) {
  40. score.value = xml
  41. } else {
  42. score.value = formatXML(parseXmlInfo.parsedXML, {
  43. title: detail.musicSheetName,
  44. })
  45. score.value = onlyVisible(score.value, partIndex)
  46. state.partIndex = partIndex
  47. }
  48. state.gradual = getGradualLengthByXml(xml)
  49. } catch (error) {}
  50. return score
  51. }
  52. /**
  53. * 设置音频信息
  54. * @param detail 音乐详情
  55. */
  56. export const useMp3s = async (detail: MusicSheelDetail) => {
  57. const search = useOriginSearch()
  58. const partIndex = ((search['part-index'] as string) || 0) as unknown as number
  59. const activebg = detail.background?.[partIndex]
  60. Object.assign(musicInfo, activebg)
  61. if (musicInfo.musicSvg) {
  62. try {
  63. musicInfo.musicSvg = typeof musicInfo.musicSvg === 'string' && musicInfo.musicSvg ? JSON.parse(musicInfo.musicSvg) : ''
  64. } catch (error) {}
  65. }
  66. // 伴奏
  67. const backgroundSong = encodeURI(detail.metronomeUrl || '')
  68. // 原音
  69. const musicSong = encodeURI(activebg?.audioFileUrl|| '')
  70. // 兼容未修改之前
  71. runtime.songs = {
  72. background: backgroundSong ? backgroundSong + '?t=background' : '',
  73. music: musicSong ? musicSong + '?t=music' : '',
  74. }
  75. detailState.isAppPlay = detail.audioType === 'MIDI'
  76. detailState.isPercussion = musicInfo.musicSubject == '1'
  77. let defaultExtConfigJson = {
  78. skipTick: false,
  79. repeatedBeats: false,
  80. scoreSize: 'middle',
  81. }
  82. let extConfigJson = {}
  83. detailState.activeDetail = {
  84. ...detail,
  85. examSongId: detail.id,
  86. originalSpeed: 90,
  87. isAppPlay: detail.audioType === 'MIDI',
  88. extConfigJson: {
  89. ...defaultExtConfigJson,
  90. },
  91. }
  92. try {
  93. extConfigJson = JSON.parse(detail?.extConfigJson || '')
  94. } catch (error) {}
  95. detailState.activeDetail.extConfigJson = {
  96. ...detailState.activeDetail.extConfigJson,
  97. ...extConfigJson,
  98. }
  99. const setZoom = detailState.activeDetail.extConfigJson.scoreSize
  100. const zooms = store.get('zooms') || {}
  101. if (setZoom && !zooms['' + detail.id]) {
  102. store.set('zooms', { ...zooms, ['' + detail.id]: setZoom })
  103. SettingState.sett.scoreSize = setZoom
  104. }
  105. detailState.needTick = (detail.audioType === 'MP3' && detail.mp3Type === 'MP3' && detail.musicSheetType != 'CONCERT' ) || detail.audioType === 'MIDI'
  106. detailState.skipTick = detailState.activeDetail.extConfigJson.skipTick
  107. detailState.repeatedBeats = detailState.activeDetail.extConfigJson.repeatedBeats
  108. if (!runtime.songs['music']) {
  109. RuntimeUtils.changeMode('background')
  110. }
  111. // console.log({ ...detailState.activeDetail })
  112. if (!runtime.audiosInstance) {
  113. runtime.audiosInstance = new audiosInstance(Object.values(runtime.songs) as string[])
  114. }
  115. }
  116. /**
  117. * 获取异形屏信息
  118. * @returns {Promise<void>}
  119. */
  120. export const useSpecialShapedScreen = () => {
  121. const heightRef = ref<number>(0)
  122. postMessage(
  123. {
  124. api: 'isSpecialShapedScreen',
  125. },
  126. (evt) => {
  127. const height = evt?.content.notchHeight
  128. detailState.notchHeight =
  129. (browser().ios ? height * 2 : height) || (evt?.content.isSpecialShapedScreen && browser().ios ? 100 : 0)
  130. heightRef.value = detailState.notchHeight
  131. detailState.isSpecialShapedScreen = evt?.content.isSpecialShapedScreen
  132. document.documentElement.style.setProperty('--popup-loading', detailState.notchHeight / 4 + 'px')
  133. }
  134. )
  135. return [heightRef]
  136. }
  137. /**
  138. * 获取当前曲目信息
  139. * @param id 歌曲id
  140. */
  141. export const useDetail = (id: number | string): [Ref<ShaeetStatusType>, Ref<MusicSheelDetail>] => {
  142. const prefix = getRequestHostname()
  143. const status = ref<ShaeetStatusType>('loading')
  144. const data = ref<MusicSheelDetail>({})
  145. status.value = 'loading'
  146. request
  147. .get(`/musicSheet/detail/${id}`, {
  148. prefix: prefix,
  149. })
  150. .then((res) => {
  151. useMp3s(res.data)
  152. data.value = {
  153. ...res.data,
  154. code:
  155. Array.isArray(res?.data?.background) && res.data.background.length
  156. ? getSubjectIdCode(res.data.background[0].musicSubject)
  157. : '',
  158. }
  159. // notation: 是能转简谱 0: 不可以,需要设置成五线谱模式, 是否是切换简谱
  160. if (data.value.notation == 0 || !sessionStorage.getItem('notation')) {
  161. SettingState.sett.type = 'staff'
  162. }
  163. // 设置是否特殊曲谱, 是特殊曲谱取反(不理解之前的思考逻辑), 使用后台设置的速度
  164. detailState.isSpecialBookCategory = !classids.includes(res.data.musicSheetCategoriesId)
  165. detailState.subjectId = Number(musicInfo.musicSubject)
  166. ;(window as any).DYSubjectId = formatId(data.value.code as any)
  167. status.value = 'success'
  168. // 额外配置
  169. let extConfigJson = {
  170. gradualTimes: {},
  171. }
  172. try {
  173. if (data.value?.extConfigJson) {
  174. extConfigJson = {
  175. ...extConfigJson,
  176. ...JSON.parse(data.value.extConfigJson),
  177. }
  178. }
  179. } catch (error) {}
  180. state.gradualTimes = extConfigJson.gradualTimes as GradualTimes
  181. // 合奏设置
  182. if (res?.data?.musicSheetType == 'CONCERT') {
  183. const backgrounds = res?.data?.background || [];
  184. const partIndex = Number(search['part-index']) || 0
  185. const track = backgrounds[partIndex]?.track
  186. const instrumentName = getInstrumentName(track)
  187. detailState.partName = track + (instrumentName ? `(${instrumentName})` : '')
  188. const _track = Object.keys(formatdata).filter((key) => key.includes(track) || track.includes(key))[0]
  189. data.value.code = _track
  190. }
  191. })
  192. .catch(() => (status.value = 'error'))
  193. return [status, data]
  194. }
  195. /**
  196. * 监听后台切换状态,暂停播放与评测
  197. */
  198. export const useSuspendPlay = () => {
  199. listenerMessage('suspendPlay', () => {
  200. if (detailState.activeTick > -1) {
  201. RuntimeUtils.stopTick()
  202. }
  203. console.log(runtime.playState)
  204. if (runtime.playState === 'play') {
  205. RuntimeUtils.resetPlayStatus()
  206. if (runtime.evaluatingStatus) {
  207. // postMessage(
  208. // {
  209. // api: 'pauseRecording',
  210. // },
  211. // () => {
  212. // detailState.isPauseRecording = true
  213. // }
  214. // )
  215. evaluatStopPlay()
  216. }
  217. }
  218. })
  219. }