index.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. import { Skeleton } from "vant";
  2. import { defineComponent, onBeforeMount, onBeforeUnmount, onMounted, reactive, Transition, watch, ref } from "vue";
  3. import { formateTimes } from "../../helpers/formateMusic";
  4. import state, { isRhythmicExercises, getMusicDetail, EnumMusicRenderType } from "../../state";
  5. import { setGlobalData } from "../../utils";
  6. import MusicScore, { resetMusicScore } from "../../view/music-score";
  7. import styles from "./index.module.less";
  8. import {
  9. api_cloudLoading,
  10. api_setStatusBarVisibility,
  11. isSpecialShapedScreen,
  12. } from "/src/helpers/communication";
  13. import { getQuery } from "/src/utils/queryString";
  14. import { mappingVoicePart, subjectFingering } from "/src/view/fingering/fingering-config";
  15. import { api_musicPracticeRecordDetail, sysMusicScoreAccompanimentQueryPage } from "../api";
  16. import { getMusicSheetDetail } from "/src/utils/baseApi"
  17. import ShareTop from "./component/share-top";
  18. import { addMeasureScore } from "/src/view/evaluating";
  19. import TopArrow from "./component/note/topArrow";
  20. import BottomArrow from "./component/note/bottomArrow";
  21. import LeftArrow from "./component/note/leftArrow";
  22. import RightArrow from "./component/note/rightArrow";
  23. const colorsClass: any = {
  24. RIGHT: styles.right, // 正确
  25. WRONG: styles.wrong, // 错误
  26. NOT_PLAYED: styles.notPlay, // 未演奏
  27. EARLY: styles.cadence_fast, // 节奏快
  28. LATE: styles.cadence_slow, // 节奏慢
  29. HIGH: styles.intonation_high, // 音准高
  30. LOW: styles.intonation_low, // 音准低
  31. DURATION_INSUFFICIENT: styles.integrity_wrong // 完整性(时值)不足
  32. }
  33. // const colorsClass: any = {
  34. // /** 音准 */
  35. // pitch: {
  36. // /** 高了 */
  37. // HIGH: styles.intonation_high,
  38. // /** 正常 */
  39. // RIGHT: styles.intonation_right,
  40. // /** 低了 */
  41. // LOW: styles.intonation_low,
  42. // /** 未演奏 */
  43. // NOT_PLAYED: styles.notPlay,
  44. // /** 错误 */
  45. // WRONG: styles.intonation_wrong,
  46. // /** 时值不足 */
  47. // DURATION_INSUFFICIENT: styles.integrity_wrong
  48. // },
  49. // /** 节奏 */
  50. // rhythmic: {
  51. // /** 过早 */
  52. // EARLY: styles.cadence_fast,
  53. // /** 正常 */
  54. // RIGHT: styles.cadence_right,
  55. // /** 过迟 */
  56. // LATE: styles.cadence_slow,
  57. // /** 未演奏 */
  58. // NOT_PLAYED: styles.notPlay,
  59. // /** 错误 */
  60. // WRONG: styles.cadence_wrong
  61. // }
  62. // }
  63. export default defineComponent({
  64. name: "music-list",
  65. setup() {
  66. const query: any = getQuery();
  67. const useedid = ref<string[]>([])
  68. const scoreData = reactive({
  69. videoFilePath: "", // 回放视频路径
  70. cadence: 0,
  71. integrity: 0,
  72. intonation: 0,
  73. score: 0,
  74. heardLevel: "",
  75. itemType: "intonation",
  76. musicType: 'staff',
  77. });
  78. const detailData = reactive({
  79. isLoading: true,
  80. paddingLeft: "",
  81. headerHide: false,
  82. musicalNotesPlayStats: [] as any[],
  83. userMeasureScore: {} as any,
  84. });
  85. const getAPPData = async () => {
  86. const screenData = await isSpecialShapedScreen();
  87. if (screenData?.content) {
  88. const { isSpecialShapedScreen, notchHeight } = screenData.content;
  89. if (isSpecialShapedScreen) {
  90. detailData.paddingLeft = 25 + "px";
  91. }
  92. }
  93. // 普通webview 没有获取异性屏的方法
  94. detailData.paddingLeft = 20 + "px";
  95. };
  96. onBeforeMount(() => {
  97. getAPPData();
  98. api_setStatusBarVisibility();
  99. });
  100. // console.log(route.params, query)
  101. /** 获取曲谱数据 */
  102. const getMusicInfo = (res: any) => {
  103. const index = state.partIndex;
  104. const musicInfo = {
  105. ...res.data,
  106. ...res.data.background[index],
  107. };
  108. // console.log("🚀 ~ musicInfo:", musicInfo);
  109. setState(musicInfo, index);
  110. setCustom();
  111. detailData.isLoading = false;
  112. };
  113. const setState = (data: any, index: number) => {
  114. // console.log("🚀 ~ data:", data)
  115. state.scrollContainer = "scrollContainer";
  116. state.detailId = data.id;
  117. state.xmlUrl = data.xmlFileUrl;
  118. state.partIndex = index;
  119. state.subjectId = data.musicSubject;
  120. state.categoriesId = data.categoriesId;
  121. state.categoriesName = data.musicTagNames;
  122. state.enableEvaluation = data.canEvaluate ? true : false;
  123. state.examSongId = data.id + "";
  124. state.examSongName = data.musicSheetName;
  125. // 解析扩展字段
  126. if (data.extConfigJson) {
  127. try {
  128. state.extConfigJson = JSON.parse(data.extConfigJson as string);
  129. } catch (error) {
  130. console.error("解析扩展字段错误:", error);
  131. }
  132. }
  133. state.isOpenMetronome = data.mp3Type === "MP3_METRONOME" ? true : false;
  134. state.needTick = data.isOpenMetronome;
  135. state.isShowFingering = data.showFingering ? true : false;
  136. state.music = data.audioFileUrl;
  137. state.accompany = data.metronomeUrl || data.metronomeUrl;
  138. state.midiUrl = data.midiUrl;
  139. state.parentCategoriesId = data.musicTag;
  140. state.playMode = data.audioType === "MP3" ? "MP3" : "MIDI";
  141. state.originSpeed = state.speed = data.speed;
  142. state.track = data.track;
  143. state.enableNotation = data.notation ? true : false;
  144. // 映射声部ID
  145. state.subjectId = mappingVoicePart(state.subjectId as any, "ORCHESTRA");
  146. // console.log("🚀 ~ state.subjectId:", state.subjectId);
  147. // 是否打击乐
  148. state.isPercussion =
  149. state.subjectId == 23 ||
  150. state.subjectId == 113 ||
  151. state.subjectId == 121 ||
  152. isRhythmicExercises();
  153. // 设置指法
  154. state.fingeringInfo = subjectFingering(state.subjectId);
  155. // console.log("🚀 ~ state.fingeringInfo:", state.fingeringInfo, state.subjectId, state.track)
  156. // state.isOpenPrepare = true
  157. };
  158. const setCustom = () => {
  159. if (state.extConfigJson.multitrack) {
  160. setGlobalData("multitrack", state.extConfigJson.multitrack);
  161. }
  162. };
  163. onMounted(async () => {
  164. const res = await api_musicPracticeRecordDetail(query.id);
  165. state.partIndex = Number(res?.data?.partIndex);
  166. let resultData = {} as any;
  167. try {
  168. resultData = JSON.parse(res?.data?.scoreData);
  169. } catch (error) {
  170. console.error("解析评测结果:", error);
  171. }
  172. // console.log("🚀 ~ resultData:", resultData);
  173. // @ts-ignore
  174. // resultData.musicalNotesPlayStats?.notesData.forEach((item) => item.rhythmicAssessment.result = 'EARLY')
  175. detailData.musicalNotesPlayStats = resultData.musicalNotesPlayStats?.notesData || [];
  176. detailData.userMeasureScore = resultData.userMeasureScore || {};
  177. scoreData.heardLevel = res.data?.heardLevel;
  178. scoreData.cadence = res.data?.cadence;
  179. scoreData.integrity = res.data?.integrity;
  180. scoreData.intonation = res.data?.intonation;
  181. scoreData.score = res.data?.score;
  182. scoreData.videoFilePath = res.data?.videoFilePath || res.data?.recordFilePath;
  183. state.isEvaluatReport = true;
  184. await getMusicDetail(resultData.musicalNotesPlayStats?.examSongId);
  185. // 从练习记录进入评测报告,默认显示五线谱
  186. // if (!query.musicRenderType) {
  187. // state.musicRenderType = EnumMusicRenderType.staff
  188. // }
  189. scoreData.musicType = query.musicRenderType ? query.musicRenderType : state.musicRenderType;
  190. detailData.isLoading = false;
  191. // Promise.all([
  192. // getMusicSheetDetail(resultData.musicalNotesPlayStats?.examSongId),
  193. // ]).then((values) => {
  194. // getMusicInfo(values[0]);
  195. // });
  196. });
  197. const getOffsetPosition = (type: keyof typeof colorsClass): string => {
  198. // 五线谱
  199. if (scoreData.musicType === 'staff') {
  200. switch (type) {
  201. case 'EARLY':
  202. return 'translateX(-3px)'
  203. case 'LATE':
  204. return 'translateX(3px)'
  205. case 'HIGH':
  206. return 'translateY(-2px)'
  207. case 'LOW':
  208. return 'translateY(2px)'
  209. default:
  210. return ''
  211. }
  212. } else {
  213. switch (type) {
  214. case 'EARLY':
  215. return 'translateX(-3px)'
  216. case 'LATE':
  217. return 'translateX(3px)'
  218. case 'HIGH':
  219. return 'translateY(-2px)'
  220. case 'LOW':
  221. return 'translateY(-10px)'
  222. default:
  223. return ''
  224. }
  225. }
  226. }
  227. const filterNotes = () => {
  228. let include = ['RIGHT', 'WRONG', 'NOT_PLAYED']
  229. if (scoreData.itemType === 'intonation') { // 音准
  230. include.push(...['HIGH', 'LOW', 'DURATION_INSUFFICIENT'])
  231. } else if (scoreData.itemType === 'cadence') { // 节奏
  232. include.push(...['EARLY', 'LATE'])
  233. } else if (scoreData.itemType === 'integrity') { // 完整性
  234. include = ['DURATION_INSUFFICIENT', 'RIGHT', 'NOT_PLAYED']
  235. }
  236. if (scoreData.itemType === 'cadence') {
  237. return detailData.musicalNotesPlayStats.filter((item: any) => include.includes(item.rhythmicAssessment.result))
  238. } else {
  239. return detailData.musicalNotesPlayStats.filter((item: any) => {
  240. let result = item.pitchAssessment.result
  241. if (scoreData.itemType === 'integrity') {
  242. result = (result === 'HIGH' || result === 'LOW' || result === 'WRONG') ? 'RIGHT' : result
  243. }
  244. return include.includes(result)
  245. })
  246. }
  247. }
  248. const setViewColor = () => {
  249. clearViewColor()
  250. const notes = filterNotes()
  251. // console.log(1111,notes)
  252. for (const note of notes) {
  253. const active = state.times[note.index]
  254. setTimeout(() => {
  255. if (useedid.value.includes(active.id)) {
  256. return
  257. }
  258. useedid.value.push(active.id)
  259. const svgEl = document.getElementById('vf-' + active.id)
  260. const stemEl = document.getElementById('vf-' + active.id + '-stem')
  261. let errType = scoreData.itemType === 'cadence' ? note.rhythmicAssessment.result : note.pitchAssessment.result
  262. // console.log(1111222,errType)
  263. const isNeedCopyElement = scoreData.itemType === 'integrity' ? false : ['HIGH', 'LOW', 'EARLY', 'LATE'].includes(
  264. errType
  265. )
  266. if (scoreData.itemType === 'integrity') {
  267. errType = errType = (note.pitchAssessment.result === 'HIGH' || note.pitchAssessment.result === 'LOW' || note.pitchAssessment.result === 'WRONG') ? 'RIGHT' : errType
  268. }
  269. stemEl?.classList.add(colorsClass[errType])
  270. svgEl?.classList.add(colorsClass[errType])
  271. if (svgEl && isNeedCopyElement) {
  272. stemEl?.classList.remove(colorsClass[errType])
  273. svgEl?.classList.remove(colorsClass[errType])
  274. let copySvg: any = null;
  275. // 五线谱
  276. if (scoreData.musicType === 'staff') {
  277. stemEl?.classList.add(colorsClass.RIGHT)
  278. svgEl?.classList.add(colorsClass.RIGHT)
  279. copySvg = svgEl.querySelector('.vf-notehead')!.cloneNode(true) as SVGSVGElement
  280. } else {
  281. //copySvg = svgEl.querySelector('.vf-numbered-note-head')!.cloneNode(true) as SVGSVGElement
  282. if (isNeedCopyElement) {
  283. svgEl?.classList.add(styles.inaccuracy)
  284. const targetId = errType === 'HIGH' ? 'topSvg' : errType === 'LOW' ? 'bottomSvg' : errType === 'EARLY' ? 'leftSvg' : errType === 'LATE' ? 'rightSvg' : ''
  285. copySvg = document.getElementById(targetId)!.cloneNode(true) as SVGSVGElement
  286. const { width, height } = svgEl.getBoundingClientRect() || {}
  287. // @ts-ignore
  288. let { x, y } = svgEl?.getBBox() || {}
  289. x = errType === 'HIGH' ? x + (width - 15)/2 + 2 : errType === 'LOW' ? x + (width - 15)/2 + 2 : errType === 'EARLY' ? x - Math.abs((width - 15)/2) - 12 : errType === 'LATE' ? x + width + 6 : x
  290. y = errType === 'HIGH' ? y - Math.abs((height-10)/2) - 10 : errType === 'LOW' ? y + height + 8 : errType === 'EARLY' ? y + (height - 10)/2 : errType === 'LATE' ? y + (height - 10)/2 : y
  291. copySvg.setAttribute("x", x)
  292. copySvg.setAttribute("y", y)
  293. }
  294. // console.log(x,y,copySvg.getBoundingClientRect())
  295. // const rect = document.createElementNS("http://www.w3.org/2000/svg", "rect");
  296. // rect.setAttribute("x", 0 +'px');
  297. // rect.setAttribute("y", 0+'px');
  298. // rect.setAttribute("width", `50`);
  299. // rect.setAttribute("height", `50`);
  300. // rect.setAttribute("fill", "#FF4444");
  301. // svgEl.prepend(rect);
  302. }
  303. if (scoreData.musicType === 'staff') {
  304. copySvg.style.transform = getOffsetPosition(errType)
  305. //svgEl.style.opacity = '.7'
  306. if (stemEl) {
  307. //stemEl.style.opacity = '.7'
  308. }
  309. }
  310. copySvg.id = 'vf-' + active.id + '-copy'
  311. copySvg?.classList.add(colorsClass[errType])
  312. // stemEl?.classList.add(colorsClass.RIGHT)
  313. // @ts-ignore
  314. state.osmd?.container.querySelector('svg')!.insertAdjacentElement('afterbegin', copySvg)
  315. // svgEl?.parentElement?.appendChild(copySvg)
  316. }
  317. }, 300)
  318. }
  319. }
  320. const removeClass = (el?: HTMLElement | null) => {
  321. if (!el) return
  322. const classList = el.classList.values()
  323. for (const val of classList) {
  324. if (val?.indexOf('vf-') !== 0) {
  325. el.classList.remove(val)
  326. }
  327. }
  328. }
  329. const clearViewColor = () => {
  330. for (const id of useedid.value) {
  331. removeClass(document.getElementById('vf-' + id))
  332. removeClass(document.getElementById('vf-' + id + '-stem'))
  333. const qid = 'vf-' + id + '-copy'
  334. const copyEl = document.getElementById(qid)
  335. if (copyEl) {
  336. copyEl.remove()
  337. }
  338. }
  339. useedid.value = []
  340. }
  341. const setPathColor = () => {
  342. console.log(11111,detailData.musicalNotesPlayStats,scoreData.itemType)
  343. for (const note of detailData.musicalNotesPlayStats) {
  344. const active = state.times[note.index];
  345. const svgEl = active?.id ? document.getElementById("vf-" + active?.id) : null;
  346. switch (scoreData.itemType) {
  347. case "intonation":
  348. svgEl?.classList.add(colorsClass.pitch[note.pitchAssessment.result]);
  349. break;
  350. case "cadence":
  351. svgEl?.classList.add(colorsClass.rhythmic[note.rhythmicAssessment.result]);
  352. break;
  353. case "integrity":
  354. svgEl?.classList.add(colorsClass.pitch[note.pitchAssessment.result]);
  355. break;
  356. default:
  357. break;
  358. }
  359. }
  360. };
  361. const setMearureColor = () => {
  362. for (let key in detailData.userMeasureScore) {
  363. addMeasureScore(detailData.userMeasureScore[key], false);
  364. }
  365. };
  366. /** 渲染完成 */
  367. const handleRendered = (osmd: any) => {
  368. state.musicRendered = true;
  369. state.osmd = osmd;
  370. state.times = formateTimes(osmd);
  371. console.log("🚀 ~ state.times:", state.times);
  372. // @ts-ignore
  373. const beams = Array.from(new Set(document.getElementsByClassName('vf-beam')))
  374. beams.forEach((item: any) => {
  375. item.classList.add(styles.beam)
  376. })
  377. //setPathColor();
  378. setViewColor();
  379. // setMearureColor();
  380. api_cloudLoading();
  381. };
  382. onMounted(() => {
  383. window.addEventListener("resize", resetMusicScore);
  384. });
  385. onBeforeUnmount(() => {
  386. window.removeEventListener("resize", resetMusicScore);
  387. });
  388. watch(
  389. () => scoreData.itemType,
  390. () => {
  391. setViewColor();
  392. }
  393. );
  394. return () => (
  395. <div
  396. class={[styles.detail, state.setting.eyeProtection && "eyeProtection", styles.shareBox]}
  397. style={{ paddingLeft: detailData.paddingLeft }}
  398. >
  399. <Transition name="van-fade">
  400. {!state.musicRendered && (
  401. <div class={styles.skeleton}>
  402. <Skeleton class={styles.skeleton} row={8} />
  403. </div>
  404. )}
  405. </Transition>
  406. <div
  407. class={[styles.headHeight, detailData.headerHide && styles.headHide]}
  408. onClick={(e: Event) => e.stopPropagation()}
  409. >
  410. <Transition name="van-slide-down">
  411. {state.musicRendered && <ShareTop scoreData={scoreData} />}
  412. </Transition>
  413. </div>
  414. <div
  415. id="scrollContainer"
  416. class={[styles.container, !state.setting.displayCursor && "hideCursor"]}
  417. >
  418. {/* 曲谱渲染 */}
  419. {!detailData.isLoading && <MusicScore musicColor={'#ADADAD'} onRendered={handleRendered} />}
  420. {
  421. <div class={styles.arrowSvg}>
  422. <TopArrow />
  423. <BottomArrow />
  424. <LeftArrow />
  425. <RightArrow />
  426. </div>
  427. }
  428. </div>
  429. </div>
  430. );
  431. },
  432. });