Browse Source

读取分谱

liushengqiang 1 year ago
parent
commit
387393f7ce

+ 7 - 1
src/constant/instruments.ts

@@ -204,7 +204,13 @@ export const getInstrumentName = (name = '') => {
   if (!name) return ''
   for(let key in instruments){
     const _key = key.toLocaleLowerCase().replace(/ /g, '')
-    if (_key.includes(name) || name.includes(_key)){
+    if (_key.includes(name)){
+      return instruments[key]
+    }
+  }
+  for(let key in instruments){
+    const _key = key.toLocaleLowerCase().replace(/ /g, '')
+    if (name.includes(_key)){
       return instruments[key]
     }
   }

+ 20 - 10
src/page-gym/musci-list/choosePartName/index.tsx

@@ -1,23 +1,33 @@
-import { computed, defineComponent, onMounted, ref } from "vue";
-import { Picker, Button, PickerOption, Icon } from "vant";
+import { PropType, computed, defineComponent, onMounted, ref, toRefs } from "vue";
+import { Picker, Button, PickerOption, Icon, showLoadingToast, closeToast, showToast } from "vant";
 import styles from "./index.module.less";
 import state from "/src/state";
 import { getInstrumentName } from "/src/constant/instruments";
+import request from "umi-request";
 
 export default defineComponent({
 	name: "choosePartName",
+	props: {
+		partListNames: {
+			type: Array as PropType<string[]>,
+			default: () => [],
+		},
+	},
 	emits: ["close"],
 	setup(props, { emit }) {
+		const { partListNames } = toRefs(props);
 		const selectIndex = ref(0);
 		const columns = computed(() => {
-			return state.partListNames.map((n: any, index: number) => {
-				const trackName = getInstrumentName(n);
-				const examSongName = n + (trackName ? `(${trackName})` : "");
-				return {
-					text: examSongName,
-					value: index,
-				};
-			});
+			return partListNames.value
+				.filter((text) => text.toLocaleUpperCase() !== "COMMON")
+				.map((n: any, index: number) => {
+					const trackName = getInstrumentName(n);
+					const examSongName = n + (trackName ? `(${trackName})` : "");
+					return {
+						text: examSongName,
+						value: index,
+					};
+				});
 		});
 		return () => (
 			<div class={styles.container}>

+ 28 - 2
src/page-gym/musci-list/index.tsx

@@ -1,4 +1,4 @@
-import { List, NoticeBar } from "vant";
+import { List, NoticeBar, closeToast, showLoadingToast, showToast } from "vant";
 import { defineComponent, reactive } from "vue";
 import styles from "./index.module.less";
 import iconMusic from "../header-top/image/music.png";
@@ -14,6 +14,7 @@ import { useRoute } from "vue-router";
 import iconBack from "./icons/icon-back.png";
 import VipModel from "./vipModel";
 import { postMessage } from "/src/utils/native-message";
+import request from "umi-request";
 
 export default defineComponent({
 	name: "detail-list",
@@ -32,6 +33,7 @@ export default defineComponent({
 			selectedPartIndex: 0,
 			vipShow: false,
 			partShow: false,
+			partListNames: [] as any[]
 		});
 		//获取曲谱列表
 		const getData = async () => {
@@ -57,9 +59,32 @@ export default defineComponent({
 			musicData.loading = false;
 		};
 
+		const getPartNames = async (xmlUrl: string) => {
+			const partNames: string[] = [];
+			showLoadingToast({
+				type: "loading",
+				overlay: true,
+			});
+			try {
+				const res = await request.get(xmlUrl, { mode: "cors" });
+				const xml = new DOMParser().parseFromString(res, "text/xml");
+				const parts: any = xml.getElementsByTagName("part-name");
+				for (const item of parts) {
+					if (item.textContent) {
+						partNames.push(item.textContent);
+					}
+				}
+				closeToast();
+			} catch (error) {
+				showToast("读取分谱信息失败,请重试");
+			}
+			return partNames;
+		};
+
 		/**  */
-		const handleClick = (item: any) => {
+		const handleClick = async (item: any) => {
 			musicData.row = { ...item };
+			musicData.partListNames = await getPartNames(item.xmlUrl);
 			// 选择声部
 			if (state.partListNames.length > 1) {
 				musicData.partShow = true;
@@ -120,6 +145,7 @@ export default defineComponent({
 					</List>
 					<Popup v-model:show={musicData.partShow} teleport="body" defaultStyle={false}>
 						<ChoosePartName
+							partListNames={musicData.partListNames}
 							onClose={(index: number) => {
 								if ((index ?? -1) > -1) {
 									musicData.selectedPartIndex = index;