index.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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, { handleSetSpeed, isRhythmicExercises } from "/src/state";
  7. import { storeData } from "../../store";
  8. import AudioList from "../../view/audio-list";
  9. import MusicScore, { resetMusicScore } from "../../view/music-score";
  10. import { sysMusicScoreAccompanimentQueryPage, sysMusicScoreCategoriesQueryTree } from "../api";
  11. import EvaluatModel from "../evaluat-model";
  12. import HeaderTop from "../header-top";
  13. import styles from "./index.module.less";
  14. import { api_openCamera, api_setStatusBarVisibility, isSpecialShapedScreen } from "/src/helpers/communication";
  15. import { getQuery } from "/src/utils/queryString";
  16. import Evaluating, { evaluatingData } from "/src/view/evaluating";
  17. import MeasureSpeed from "/src/view/plugins/measure-speed";
  18. import { mappingVoicePart, subjectFingering } from "/src/view/fingering/fingering-config";
  19. import Fingering from "/src/view/fingering";
  20. import store from "store";
  21. import HelperModel from "../helper-model";
  22. import RecordingTime from "../custom-plugins/recording-time";
  23. import ExerciseStatistics from "../custom-plugins/ExerciseStatistics";
  24. import HomeWork from "../custom-plugins/HomeWork";
  25. import EvaluatingWork from "../custom-plugins/EvaluatingWork";
  26. import VipModel from "../custom-plugins/vip-verify";
  27. import GuidePage from "../custom-plugins/guide-page";
  28. import { handleSetCustomRender, setCustomGradual, setCustomNoteRealValue } from "../custom-plugins/custom-gradual";
  29. import { getStorageSpeed, setGlobalData } from "/src/utils";
  30. //特殊教材分类id
  31. const classIds = [1, 30, 97]; // [大雅金唐, 竖笛教程, 声部训练]
  32. const classKey = "sysMusicScoreCategoriesList";
  33. export default defineComponent({
  34. name: "music-list",
  35. setup() {
  36. const route = useRoute();
  37. const query: any = {
  38. ...getQuery(),
  39. ...route.query,
  40. };
  41. const paramsId = route.params.id as string;
  42. const detailData = reactive({
  43. isLoading: true,
  44. paddingLeft: "",
  45. classList: [] as any,
  46. });
  47. const getAPPData = async () => {
  48. const screenData = await isSpecialShapedScreen();
  49. if (screenData?.content) {
  50. // console.log("🚀 ~ screenData:", screenData.content);
  51. const { isSpecialShapedScreen, notchHeight } = screenData.content;
  52. if (isSpecialShapedScreen) {
  53. detailData.paddingLeft = 25 + "px";
  54. }
  55. }
  56. };
  57. onBeforeMount(() => {
  58. getAPPData();
  59. });
  60. // console.log(route.params, query)
  61. /** 获取曲谱数据 */
  62. const getMusicInfo = (res: any) => {
  63. const index = query["part-index"] ? parseInt(query["part-index"] as string) : 0;
  64. const musicInfo = res.data[index] ? res.data[index] : res.data[0];
  65. // console.log("🚀 ~ musicInfo:", musicInfo);
  66. setState(musicInfo, index);
  67. setCustom();
  68. detailData.isLoading = false;
  69. state.partListNames = res.data.map((n: any) => n.track);
  70. };
  71. const getCategorId = (arr: any[]): any[] => {
  72. const list = [];
  73. if (!Array.isArray(arr)) return [];
  74. for (let i = 0; i < arr.length; i++) {
  75. list.push(arr[i].id);
  76. if (Array.isArray(arr[i][classKey])) {
  77. list.push(...getCategorId(arr[i][classKey]));
  78. }
  79. }
  80. return list;
  81. };
  82. const getIds = (arr: any[], _classids?: any[]): any[] => {
  83. const classids = _classids ? _classids : classIds;
  84. if (!Array.isArray(arr)) return [];
  85. const ids = [];
  86. for (let i = 0; i < arr.length; i++) {
  87. if (classids.includes(arr[i].id)) {
  88. ids.push(arr[i].id, ...getCategorId(arr[i][classKey]));
  89. } else {
  90. ids.push(...getIds(arr[i][classKey], classids));
  91. }
  92. }
  93. return ids;
  94. };
  95. /** 获取分类数据 */
  96. const getCategory = (res: any) => {
  97. detailData.classList = res.data;
  98. };
  99. const setState = (data: any, index: number) => {
  100. state.detailId = data.id;
  101. state.xmlUrl = data.xmlUrl;
  102. state.partIndex = index;
  103. state.subjectId = data.subjectId;
  104. state.categoriesId = data.categoriesId;
  105. state.categoriesName = data.categoriesName;
  106. state.enableEvaluation = data.enableEvaluation;
  107. state.examSongId = data.examSongId + "";
  108. state.examSongName = data.examSongName;
  109. // 解析扩展字段
  110. if (data.extConfigJson) {
  111. try {
  112. state.extConfigJson = JSON.parse(data.extConfigJson as string);
  113. } catch (error) {
  114. console.error("解析扩展字段错误:", error);
  115. }
  116. state.gradualTimes = state.extConfigJson.gradualTimes
  117. state.repeatedBeats = state.extConfigJson.repeatedBeats || 0
  118. }
  119. state.isOpenMetronome = !data.isOpenMetronome;
  120. state.needTick = data.isOpenMetronome;
  121. state.isShowFingering = data.isShowFingering;
  122. state.music = data.isOpenMetronome ? data.mp3Url : data.metronomeMp3Url;
  123. state.accompany = data.isOpenMetronome ? data.url : data.metronomeUrl;
  124. state.midiUrl = data.midiUrl;
  125. state.parentCategoriesId = data.parentCategoriesId;
  126. state.playMode = data.playMode;
  127. state.originSpeed = state.speed = data.speed;
  128. state.track = data.track;
  129. state.isOpenPrepare = true;
  130. // 映射声部ID
  131. state.subjectId = mappingVoicePart(state.subjectId, "GYM");
  132. // 是否打击乐
  133. state.isPercussion = state.subjectId == 23 || state.subjectId == 113 || state.subjectId == 121 || isRhythmicExercises();
  134. // 设置指法
  135. state.fingeringInfo = subjectFingering(state.subjectId);
  136. //小曲目 不需要计算音乐的节拍器
  137. if (getIds(detailData.classList, [41]).includes(data.parentCategoriesId)) {
  138. state.isOpenMetronome = false;
  139. }
  140. // 设置是否特殊曲谱
  141. state.isSpecialBookCategory = !getIds(detailData.classList).includes(state.parentCategoriesId)
  142. // console.log('state.isSpecialBookCategory', state.isSpecialBookCategory, state.parentCategoriesId);
  143. };
  144. const setCustom = () => {
  145. if (state.extConfigJson.multitrack) {
  146. setGlobalData("multitrack", state.extConfigJson.multitrack);
  147. }
  148. };
  149. onBeforeMount(() => {
  150. api_setStatusBarVisibility();
  151. const settting = store.get("musicscoresetting");
  152. if (settting) {
  153. state.setting = settting;
  154. if (state.setting.camera) {
  155. api_openCamera();
  156. }
  157. // console.log("🚀 ~ settting:", settting)
  158. }
  159. });
  160. onMounted(() => {
  161. (window as any).appName = "gym";
  162. Promise.all([sysMusicScoreCategoriesQueryTree(storeData.platformType === "WEB"), sysMusicScoreAccompanimentQueryPage(paramsId)]).then((values) => {
  163. getCategory(values[0]);
  164. getMusicInfo(values[1]);
  165. });
  166. });
  167. /** 渲染完成 */
  168. const handleRendered = (osmd: any) => {
  169. state.musicRendered = true;
  170. state.osmd = osmd;
  171. setCustomGradual();
  172. setCustomNoteRealValue();
  173. state.times = formateTimes(osmd);
  174. console.log("🚀 ~ state.times:", state.times);
  175. try {
  176. metronomeData.metro = new Metronome();
  177. metronomeData.metro.init(state.times);
  178. } catch (error) {}
  179. // 设置设置过的速度
  180. const storeSpeed = getStorageSpeed(state.examSongId)
  181. if (storeSpeed){
  182. handleSetSpeed(storeSpeed)
  183. }
  184. handleSetCustomRender()
  185. };
  186. /** 指法配置 */
  187. const fingerConfig = computed<any>(() => {
  188. if (state.setting.displayFingering && state.fingeringInfo?.name) {
  189. if (state.fingeringInfo.direction === "transverse") {
  190. return {
  191. container: {
  192. paddingBottom: state.fingeringInfo.height,
  193. },
  194. fingerBox: {
  195. height: state.fingeringInfo.height,
  196. },
  197. };
  198. } else {
  199. return {
  200. container: {
  201. paddingRight: state.fingeringInfo.width,
  202. },
  203. fingerBox: {
  204. position: "absolute",
  205. width: state.fingeringInfo.width,
  206. height: "100%",
  207. right: 0,
  208. top: 0,
  209. },
  210. };
  211. }
  212. }
  213. return {
  214. container: {},
  215. fingerBox: {},
  216. };
  217. });
  218. // 监听指法显示
  219. watch(
  220. () => state.setting.displayFingering,
  221. () => {
  222. if (state.fingeringInfo.direction === "vertical") {
  223. if (state.setting.displayFingering) {
  224. document.getElementById("musicAndSelection")?.style.removeProperty("--music-zoom");
  225. } else {
  226. nextTick(() => {
  227. resetMusicScore();
  228. });
  229. }
  230. }
  231. }
  232. );
  233. onMounted(() => {
  234. window.addEventListener("resize", resetMusicScore);
  235. });
  236. onBeforeUnmount(() => {
  237. window.removeEventListener("resize", resetMusicScore);
  238. });
  239. return () => (
  240. <div class={[styles.detail, state.setting.camera && styles.opencamera]} style={{ paddingLeft: detailData.paddingLeft }}>
  241. {!state.musicRendered && (
  242. <div class={styles.skeleton}>
  243. <Skeleton class={styles.skeleton} row={8} />
  244. </div>
  245. )}
  246. <div class={styles.headHeight}>
  247. <Transition name="van-slide-down">{state.musicRendered && <HeaderTop />}</Transition>
  248. </div>
  249. <div
  250. style={{ ...fingerConfig.value.container }}
  251. class={[styles.container, state.setting.eyeProtection && "eyeProtection", !state.setting.displayCursor && "hideCursor"]}
  252. >
  253. {/* 曲谱渲染 */}
  254. {!detailData.isLoading && <MusicScore key="musicscore" onRendered={handleRendered} />}
  255. {/* 播放 */}
  256. {!detailData.isLoading && <AudioList />}
  257. {/* 评测 */}
  258. {state.modeType === "evaluating" && (
  259. <>
  260. <Evaluating />
  261. {evaluatingData.rendered && <EvaluatModel />}
  262. </>
  263. )}
  264. {/* 指法 */}
  265. {state.setting.displayFingering && state.fingeringInfo?.name && (
  266. <div style={{ ...fingerConfig.value.fingerBox }}>
  267. <Fingering />
  268. </div>
  269. )}
  270. </div>
  271. {/* 插件模块 */}
  272. <div class="plugins-box">
  273. {state.musicRendered && (
  274. <>
  275. <MeasureSpeed />
  276. {/* 投屏 and 帮助 */}
  277. <HelperModel />
  278. {/* 统计训练时长 */}
  279. <RecordingTime />
  280. {/* 统计时长显示, 只有学生端显示 */}
  281. {storeData.platformType === "STUDENT" && <ExerciseStatistics />}
  282. {/* 课后训练作业 */}
  283. {query.workRecord && storeData.platformType === "STUDENT" && <HomeWork />}
  284. {/* 进度评测作业 */}
  285. {query.evaluatingRecord && storeData.platformType === "STUDENT" && <EvaluatingWork />}
  286. {/* 开通会员提示 */}
  287. {storeData.platformType === "STUDENT" && (query.workRecord || query.evaluatingRecord) && <VipModel />}
  288. {/* 引导 */}
  289. <GuidePage />
  290. </>
  291. )}
  292. </div>
  293. </div>
  294. );
  295. },
  296. });