index.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. scoreData.musicType = query.musicRenderType;
  184. state.isEvaluatReport = true;
  185. await getMusicDetail(resultData.musicalNotesPlayStats?.examSongId);
  186. // 从练习记录进入评测报告,默认显示五线谱
  187. // if (!query.musicRenderType) {
  188. // state.musicRenderType = EnumMusicRenderType.staff
  189. // }
  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) => include.includes(item.pitchAssessment.result))
  240. }
  241. }
  242. const setViewColor = () => {
  243. clearViewColor()
  244. const notes = filterNotes()
  245. // console.log(1111,notes)
  246. for (const note of notes) {
  247. const active = state.times[note.index]
  248. setTimeout(() => {
  249. if (useedid.value.includes(active.id)) {
  250. return
  251. }
  252. useedid.value.push(active.id)
  253. const svgEl = document.getElementById('vf-' + active.id)
  254. const stemEl = document.getElementById('vf-' + active.id + '-stem')
  255. const errType = scoreData.itemType === 'cadence' ? note.rhythmicAssessment.result : note.pitchAssessment.result
  256. // console.log(1111222,errType)
  257. const isNeedCopyElement = ['HIGH', 'LOW', 'EARLY', 'LATE'].includes(
  258. errType
  259. )
  260. stemEl?.classList.add(colorsClass[errType])
  261. svgEl?.classList.add(colorsClass[errType])
  262. if (svgEl && isNeedCopyElement) {
  263. stemEl?.classList.remove(colorsClass[errType])
  264. svgEl?.classList.remove(colorsClass[errType])
  265. let copySvg: any = null;
  266. // 五线谱
  267. if (scoreData.musicType === 'staff') {
  268. stemEl?.classList.add(colorsClass.RIGHT)
  269. svgEl?.classList.add(colorsClass.RIGHT)
  270. copySvg = svgEl.querySelector('.vf-notehead')!.cloneNode(true) as SVGSVGElement
  271. } else {
  272. //copySvg = svgEl.querySelector('.vf-numbered-note-head')!.cloneNode(true) as SVGSVGElement
  273. if (isNeedCopyElement) {
  274. svgEl?.classList.add(styles.inaccuracy)
  275. const targetId = errType === 'HIGH' ? 'topSvg' : errType === 'LOW' ? 'bottomSvg' : errType === 'EARLY' ? 'leftSvg' : errType === 'LATE' ? 'rightSvg' : ''
  276. copySvg = document.getElementById(targetId)!.cloneNode(true) as SVGSVGElement
  277. const { width, height } = svgEl.getBoundingClientRect() || {}
  278. // @ts-ignore
  279. let { x, y } = svgEl?.getBBox() || {}
  280. x = errType === 'HIGH' ? x + (width - 15)/2 + 1 : errType === 'LOW' ? x + (width - 15)/2 + 1 : errType === 'EARLY' ? x - Math.abs((width - 15)/2) - 12 : errType === 'LATE' ? x + width + 6 : x
  281. 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
  282. copySvg.setAttribute("x", x)
  283. copySvg.setAttribute("y", y)
  284. }
  285. // console.log(x,y,copySvg.getBoundingClientRect())
  286. // const rect = document.createElementNS("http://www.w3.org/2000/svg", "rect");
  287. // rect.setAttribute("x", 0 +'px');
  288. // rect.setAttribute("y", 0+'px');
  289. // rect.setAttribute("width", `50`);
  290. // rect.setAttribute("height", `50`);
  291. // rect.setAttribute("fill", "#FF4444");
  292. // svgEl.prepend(rect);
  293. }
  294. if (scoreData.musicType === 'staff') {
  295. copySvg.style.transform = getOffsetPosition(errType)
  296. //svgEl.style.opacity = '.7'
  297. if (stemEl) {
  298. //stemEl.style.opacity = '.7'
  299. }
  300. }
  301. copySvg.id = 'vf-' + active.id + '-copy'
  302. copySvg?.classList.add(colorsClass[errType])
  303. // stemEl?.classList.add(colorsClass.RIGHT)
  304. // @ts-ignore
  305. state.osmd?.container.querySelector('svg')!.insertAdjacentElement('afterbegin', copySvg)
  306. // svgEl?.parentElement?.appendChild(copySvg)
  307. }
  308. }, 300)
  309. }
  310. }
  311. const removeClass = (el?: HTMLElement | null) => {
  312. if (!el) return
  313. const classList = el.classList.values()
  314. for (const val of classList) {
  315. if (val?.indexOf('vf-') !== 0) {
  316. el.classList.remove(val)
  317. }
  318. }
  319. }
  320. const clearViewColor = () => {
  321. for (const id of useedid.value) {
  322. removeClass(document.getElementById('vf-' + id))
  323. removeClass(document.getElementById('vf-' + id + '-stem'))
  324. const qid = 'vf-' + id + '-copy'
  325. const copyEl = document.getElementById(qid)
  326. if (copyEl) {
  327. copyEl.remove()
  328. }
  329. }
  330. useedid.value = []
  331. }
  332. const setPathColor = () => {
  333. console.log(11111,detailData.musicalNotesPlayStats,scoreData.itemType)
  334. for (const note of detailData.musicalNotesPlayStats) {
  335. const active = state.times[note.index];
  336. const svgEl = active?.id ? document.getElementById("vf-" + active?.id) : null;
  337. switch (scoreData.itemType) {
  338. case "intonation":
  339. svgEl?.classList.add(colorsClass.pitch[note.pitchAssessment.result]);
  340. break;
  341. case "cadence":
  342. svgEl?.classList.add(colorsClass.rhythmic[note.rhythmicAssessment.result]);
  343. break;
  344. case "integrity":
  345. svgEl?.classList.add(colorsClass.pitch[note.pitchAssessment.result]);
  346. break;
  347. default:
  348. break;
  349. }
  350. }
  351. };
  352. const setMearureColor = () => {
  353. for (let key in detailData.userMeasureScore) {
  354. addMeasureScore(detailData.userMeasureScore[key], false);
  355. }
  356. };
  357. /** 渲染完成 */
  358. const handleRendered = (osmd: any) => {
  359. state.musicRendered = true;
  360. state.osmd = osmd;
  361. state.times = formateTimes(osmd);
  362. console.log("🚀 ~ state.times:", state.times);
  363. //setPathColor();
  364. setViewColor();
  365. // setMearureColor();
  366. api_cloudLoading();
  367. };
  368. onMounted(() => {
  369. window.addEventListener("resize", resetMusicScore);
  370. });
  371. onBeforeUnmount(() => {
  372. window.removeEventListener("resize", resetMusicScore);
  373. });
  374. watch(
  375. () => scoreData.itemType,
  376. () => {
  377. setViewColor();
  378. }
  379. );
  380. return () => (
  381. <div
  382. class={[styles.detail, state.setting.eyeProtection && "eyeProtection", styles.shareBox]}
  383. style={{ paddingLeft: detailData.paddingLeft }}
  384. >
  385. <Transition name="van-fade">
  386. {!state.musicRendered && (
  387. <div class={styles.skeleton}>
  388. <Skeleton class={styles.skeleton} row={8} />
  389. </div>
  390. )}
  391. </Transition>
  392. <div
  393. class={[styles.headHeight, detailData.headerHide && styles.headHide]}
  394. onClick={(e: Event) => e.stopPropagation()}
  395. >
  396. <Transition name="van-slide-down">
  397. {state.musicRendered && <ShareTop scoreData={scoreData} />}
  398. </Transition>
  399. </div>
  400. <div
  401. id="scrollContainer"
  402. class={[styles.container, !state.setting.displayCursor && "hideCursor"]}
  403. >
  404. {/* 曲谱渲染 */}
  405. {!detailData.isLoading && <MusicScore musicColor={'#ADADAD'} onRendered={handleRendered} />}
  406. {
  407. <div class={styles.arrowSvg}>
  408. <TopArrow />
  409. <BottomArrow />
  410. <LeftArrow />
  411. <RightArrow />
  412. </div>
  413. }
  414. </div>
  415. </div>
  416. );
  417. },
  418. });