index.tsx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. import { Skeleton } from "vant";
  2. import { computed, defineComponent, nextTick, onBeforeMount, onBeforeUnmount, onMounted, reactive, Transition, watch, watchEffect } from "vue";
  3. import { useRoute } from "vue-router";
  4. import { formateTimes } from "../../helpers/formateMusic";
  5. import Metronome, { metronomeData } from "../../helpers/metronome";
  6. import state, { isRhythmicExercises, musicscoresettingKey } from "../../state";
  7. import { storeData } from "../../store";
  8. import { setGlobalData } from "../../utils";
  9. import AudioList from "../../view/audio-list";
  10. import MusicScore, { resetMusicScore } from "../../view/music-score";
  11. import { sysMusicScoreAccompanimentQueryPage } from "../api";
  12. import EvaluatModel from "../evaluat-model";
  13. import HeaderTop from "../header-top";
  14. import styles from "./index.module.less";
  15. import { api_cloudLoading, api_openCamera, api_setStatusBarVisibility, isSpecialShapedScreen } from "/src/helpers/communication";
  16. import { getQuery } from "/src/utils/queryString";
  17. import Evaluating, { evaluatingData } from "/src/view/evaluating";
  18. import MeasureSpeed from "/src/view/plugins/measure-speed";
  19. import { mappingVoicePart, subjectFingering } from "/src/view/fingering/fingering-config";
  20. import Fingering from "/src/view/fingering";
  21. import store from "store";
  22. import Tick, { handleInitTick } from "/src/view/tick";
  23. import FollowPractice from "/src/view/follow-practice";
  24. import FollowModel from "../follow-model";
  25. import RecordingTime from "../custom-plugins/recording-time";
  26. import UnitTest from "../custom-plugins/unitTest";
  27. export default defineComponent({
  28. name: "music-list",
  29. setup() {
  30. const query: any = getQuery();
  31. const detailData = reactive({
  32. isLoading: true,
  33. paddingLeft: "",
  34. headerHide: false,
  35. });
  36. const getAPPData = async () => {
  37. const screenData = await isSpecialShapedScreen();
  38. if (screenData?.content) {
  39. // console.log("🚀 ~ screenData:", screenData.content);
  40. const { isSpecialShapedScreen, notchHeight } = screenData.content;
  41. if (isSpecialShapedScreen) {
  42. detailData.paddingLeft = 25 + "px";
  43. }
  44. }
  45. };
  46. onBeforeMount(() => {
  47. getAPPData();
  48. api_setStatusBarVisibility();
  49. const settting = store.get(musicscoresettingKey);
  50. if (settting) {
  51. state.setting = settting;
  52. if (state.setting.camera) {
  53. api_openCamera();
  54. }
  55. // console.log("🚀 ~ settting:", settting)
  56. }
  57. });
  58. // console.log(route.params, query)
  59. /** 获取曲谱数据 */
  60. const getMusicInfo = (res: any) => {
  61. const index = query["part-index"] ? parseInt(query["part-index"] as string) : 0;
  62. const musicInfo = {
  63. ...res.data,
  64. ...res.data.background[index],
  65. };
  66. // console.log("🚀 ~ musicInfo:", musicInfo);
  67. setState(musicInfo, index);
  68. setCustom();
  69. detailData.isLoading = false;
  70. };
  71. const setState = (data: any, index: number) => {
  72. // console.log("🚀 ~ data:", data)
  73. state.scrollContainer = "scrollContainer";
  74. state.detailId = data.id;
  75. state.xmlUrl = data.xmlFileUrl;
  76. state.partIndex = index;
  77. state.subjectId = data.musicSubject;
  78. state.categoriesId = data.categoriesId;
  79. state.categoriesName = data.musicTagNames;
  80. state.enableEvaluation = data.canEvaluate ? true : false;
  81. state.examSongId = data.id + "";
  82. state.examSongName = data.musicSheetName;
  83. // 解析扩展字段
  84. if (data.extConfigJson) {
  85. try {
  86. state.extConfigJson = JSON.parse(data.extConfigJson as string);
  87. } catch (error) {
  88. console.error("解析扩展字段错误:", error);
  89. }
  90. }
  91. state.isOpenMetronome = data.mp3Type === "MP3_METRONOME" ? true : false;
  92. state.needTick = data.isOpenMetronome;
  93. state.isShowFingering = data.showFingering ? true : false;
  94. state.music = data.audioFileUrl;
  95. state.accompany = data.metronomeUrl || data.metronomeUrl;
  96. state.midiUrl = data.midiUrl;
  97. state.parentCategoriesId = data.musicTag;
  98. state.playMode = data.audioType === "MP3" ? "mp3" : "midi";
  99. state.originSpeed = state.speed = data.speed;
  100. state.track = data.track;
  101. state.enableNotation = data.notation ? true : false;
  102. // 映射声部ID
  103. state.subjectId = mappingVoicePart(state.subjectId as any, "ORCHESTRA");
  104. console.log("🚀 ~ state.subjectId:", state.subjectId);
  105. // 是否打击乐
  106. state.isPercussion = state.subjectId == 23 || state.subjectId == 113 || state.subjectId == 121 || isRhythmicExercises();
  107. // 设置指法
  108. state.fingeringInfo = subjectFingering(state.subjectId);
  109. // console.log("🚀 ~ state.fingeringInfo:", state.fingeringInfo, state.subjectId, state.track)
  110. };
  111. const setCustom = () => {
  112. if (state.extConfigJson.multitrack) {
  113. setGlobalData("multitrack", state.extConfigJson.multitrack);
  114. }
  115. };
  116. onMounted(() => {
  117. (window as any).appName = "colexiu";
  118. Promise.all([sysMusicScoreAccompanimentQueryPage(query.id)]).then((values) => {
  119. getMusicInfo(values[0]);
  120. });
  121. });
  122. /** 渲染完成 */
  123. const handleRendered = (osmd: any) => {
  124. state.musicRendered = true;
  125. state.osmd = osmd;
  126. const saveSpeed = (store.get("speeds") || {})[state.examSongId];
  127. const bpm = (osmd as any).bpm || osmd.Sheet.userStartTempoInBPM;
  128. // state.originSpeed =
  129. // state.speed = saveSpeed || bpm || 100;
  130. state.times = formateTimes(osmd);
  131. console.log("🚀 ~ state.times:", state.times);
  132. try {
  133. metronomeData.metro = new Metronome();
  134. metronomeData.metro.init(state.times);
  135. } catch (error) {}
  136. // 设置节拍器
  137. if (state.needTick) {
  138. const beatLengthInMilliseconds = osmd?.Sheet?.SheetPlaybackSetting?.beatLengthInMilliseconds || (60 / bpm) * 1000;
  139. handleInitTick(beatLengthInMilliseconds, osmd?.Sheet?.SheetPlaybackSetting?.Rhythm?.Numerator || 4);
  140. }
  141. api_cloudLoading();
  142. };
  143. /** 指法配置 */
  144. const fingerConfig = computed<any>(() => {
  145. if (state.setting.displayFingering && state.fingeringInfo?.name) {
  146. if (state.fingeringInfo.direction === "transverse") {
  147. return {
  148. container: {
  149. paddingBottom: state.fingeringInfo.height,
  150. },
  151. fingerBox: {
  152. position: "absolute",
  153. width: "100%",
  154. height: state.fingeringInfo.height,
  155. left: 0,
  156. bottom: 0,
  157. background: "var(--container-background)",
  158. },
  159. };
  160. } else {
  161. return {
  162. container: {
  163. paddingRight: state.fingeringInfo.width,
  164. },
  165. fingerBox: {
  166. position: "absolute",
  167. width: state.fingeringInfo.width,
  168. height: "100%",
  169. right: 0,
  170. top: 0, //"calc(var(--header-height) / 2)",
  171. },
  172. };
  173. }
  174. }
  175. return {
  176. container: {},
  177. fingerBox: {},
  178. };
  179. });
  180. // 监听指法显示
  181. watch(
  182. () => state.setting.displayFingering,
  183. () => {
  184. if (state.fingeringInfo.direction === "vertical") {
  185. if (state.setting.displayFingering) {
  186. document.getElementById("musicAndSelection")?.style.removeProperty("--music-zoom");
  187. } else {
  188. nextTick(() => {
  189. resetMusicScore();
  190. });
  191. }
  192. }
  193. }
  194. );
  195. // 监听播放状态
  196. watch(
  197. () => state.playState,
  198. () => {
  199. detailData.headerHide = state.playState === "play" ? true : false;
  200. }
  201. );
  202. // 设置改变触发
  203. watch(
  204. [() => state.setting],
  205. () => {
  206. store.set(musicscoresettingKey, state.setting);
  207. },
  208. { deep: true }
  209. );
  210. onMounted(() => {
  211. window.addEventListener("resize", resetMusicScore);
  212. });
  213. onBeforeUnmount(() => {
  214. window.removeEventListener("resize", resetMusicScore);
  215. });
  216. return () => (
  217. <div
  218. class={[styles.detail, state.setting.eyeProtection && "eyeProtection"]}
  219. style={{ paddingLeft: detailData.paddingLeft, opacity: state.setting.camera ? `${state.setting.cameraOpacity / 100}` : "" }}
  220. onClick={(e: Event) => {
  221. if (state.playState === "play") {
  222. detailData.headerHide = !detailData.headerHide;
  223. }
  224. }}
  225. >
  226. {!state.musicRendered && (
  227. <div class={styles.skeleton}>
  228. <Skeleton class={styles.skeleton} row={8} />
  229. </div>
  230. )}
  231. <div class={[styles.headHeight, detailData.headerHide && styles.headHide]} onClick={(e: Event) => e.stopPropagation()}>
  232. <Transition name="van-slide-down">{state.musicRendered && <HeaderTop />}</Transition>
  233. </div>
  234. <div id="scrollContainer" style={{ ...fingerConfig.value.container }} class={[styles.container, !state.setting.displayCursor && "hideCursor"]}>
  235. {/* 曲谱渲染 */}
  236. {!detailData.isLoading && <MusicScore onRendered={handleRendered} />}
  237. {/* 播放 */}
  238. {!detailData.isLoading && <AudioList />}
  239. {/* 评测 */}
  240. {state.modeType === "evaluating" && (
  241. <>
  242. <Evaluating />
  243. {evaluatingData.rendered && <EvaluatModel />}
  244. </>
  245. )}
  246. </div>
  247. {/* 指法 */}
  248. {state.setting.displayFingering && state.fingeringInfo?.name && (
  249. <div style={{ ...fingerConfig.value.fingerBox }}>
  250. <Fingering />
  251. </div>
  252. )}
  253. {/* 公用的插件 */}
  254. <div class="plugins-box">
  255. {state.musicRendered && (
  256. <>
  257. <MeasureSpeed />
  258. {/* 统计训练时长 */}
  259. {storeData.platformType === 'STUDENT' && <RecordingTime />}
  260. {/* 单元测验 和 课后训练 */}
  261. {storeData.platformType === 'STUDENT' && <UnitTest />}
  262. </>
  263. )}
  264. </div>
  265. {/* 节拍器 */}
  266. {state.needTick && <Tick />}
  267. {/* 跟练模式 */}
  268. {state.modeType === "follow" && (
  269. <>
  270. <FollowPractice />
  271. <FollowModel />
  272. </>
  273. )}
  274. </div>
  275. );
  276. },
  277. });