index.tsx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. import {
  2. PropType,
  3. computed,
  4. defineComponent,
  5. nextTick,
  6. onBeforeMount,
  7. onMounted,
  8. onUnmounted,
  9. reactive,
  10. ref,
  11. } from "vue";
  12. import styles from "./index.module.less";
  13. import icons from "./image/icons.json";
  14. import { FIGNER_INSTRUMENT_DATA, IFIGNER_INSTRUMENT_Note } from "/src/view/figner-preview";
  15. import {
  16. ITypeFingering,
  17. IVocals,
  18. getFingeringConfig,
  19. mappingVoicePart,
  20. subjectFingering,
  21. } from "/src/view/fingering/fingering-config";
  22. import { Howl } from "howler";
  23. import { storeData } from "/src/store";
  24. import { api_back, api_cloudLoading, api_setRequestedOrientation, api_setStatusBarVisibility, isSpecialShapedScreen } from "/src/helpers/communication";
  25. import Hammer from "hammerjs";
  26. import { Button, Icon, Loading, Popup, Progress, Space } from "vant";
  27. import GuideIndex from "./guide/guide-index";
  28. import { getQuery } from "/src/utils/queryString";
  29. import { browser } from "/src/utils";
  30. import { usePageVisibility } from "@vant/use";
  31. import { watch } from "vue";
  32. import icon_loading_img from "./image/icon_loading_img.png";
  33. export default defineComponent({
  34. name: "viewFigner",
  35. emits: ["close"],
  36. props: {
  37. isComponent: {
  38. type: Boolean,
  39. default: false,
  40. },
  41. subject: {
  42. type: String as PropType<IVocals>,
  43. default: "",
  44. },
  45. },
  46. setup(props, { emit }) {
  47. const query = getQuery();
  48. const browsInfo = browser();
  49. const code = mappingVoicePart(query.code, "INSTRUMENT");
  50. const subject = props.isComponent ? props.subject || "pan-flute" : code || "pan-flute";
  51. const data = reactive({
  52. loading: true,
  53. subject: subject as any,
  54. realKey: 0,
  55. notes: [] as IFIGNER_INSTRUMENT_Note[],
  56. tones: [] as IFIGNER_INSTRUMENT_Note[],
  57. activeTone: {} as IFIGNER_INSTRUMENT_Note,
  58. popupActiveTone: {} as IFIGNER_INSTRUMENT_Note,
  59. activeToneName: "",
  60. soundFonts: {} as any,
  61. viewIndex: 0,
  62. noteAudio: null as unknown as Howl,
  63. transform: {
  64. scale: 1,
  65. x: 0,
  66. y: 0,
  67. startScale: 1,
  68. startX: 0,
  69. startY: 0,
  70. transition: "",
  71. },
  72. tipShow: false,
  73. tips: [] as IFIGNER_INSTRUMENT_Note[],
  74. tnoteShow: false,
  75. loadingSoundFonts: true,
  76. loadingSoundProgress: 0,
  77. huaweiPad: navigator?.userAgent?.includes("UAWEIVRD-W09") ? true : false,
  78. paddingTop: '',
  79. paddingLeft:''
  80. });
  81. const fingerData = reactive({
  82. relationshipIndex: 0,
  83. subject: null as unknown as ITypeFingering,
  84. fingeringInfo: subjectFingering(data.subject),
  85. });
  86. const getAPPData = async (type: 'top' | 'left') => {
  87. const screenData = await isSpecialShapedScreen();
  88. if (screenData?.content) {
  89. // console.log("🚀 ~ screenData:", screenData.content);
  90. const { isSpecialShapedScreen, notchHeight } = screenData.content;
  91. if (isSpecialShapedScreen) {
  92. if (type === 'top'){
  93. data.paddingTop = 25 + "px";
  94. }
  95. if(type === 'left'){
  96. data.paddingLeft = 25 + "px";
  97. }
  98. }
  99. }
  100. };
  101. if (!props.isComponent && !browsInfo.ios && fingerData.fingeringInfo.orientation === 1) {
  102. getAPPData('top');
  103. }
  104. if (!props.isComponent && !browsInfo.ios && fingerData.fingeringInfo.orientation === 0) {
  105. getAPPData('left');
  106. }
  107. const getNotes = () => {
  108. const fignerData = FIGNER_INSTRUMENT_DATA[data.subject as keyof typeof FIGNER_INSTRUMENT_DATA];
  109. if (fignerData) {
  110. data.tones = fignerData.tones || [];
  111. if (data.tones.length) {
  112. data.activeTone = data.tones[0];
  113. data.popupActiveTone = data.tones[0];
  114. }
  115. data.tips = fignerData.tips || [];
  116. setNotes();
  117. setTimeout(() => {
  118. data.loading = false;
  119. }, 600);
  120. }
  121. };
  122. const setNotes = () => {
  123. const fignerData = FIGNER_INSTRUMENT_DATA[data.subject as keyof typeof FIGNER_INSTRUMENT_DATA];
  124. if (fignerData) {
  125. data.notes = fignerData[`list${data.activeTone.realName || ""}`];
  126. }
  127. };
  128. const getFingeringData = async () => {
  129. const subject: any = data.subject + (data.viewIndex === 0 ? "" : data.viewIndex);
  130. console.log("🚀 ~ subject:", subject);
  131. fingerData.subject = await getFingeringConfig(subject);
  132. };
  133. const createAudio = (url: string) => {
  134. return new Promise((resolve) => {
  135. const noteAudio = new Howl({
  136. src: url,
  137. loop: true,
  138. onload: () => {
  139. resolve(noteAudio);
  140. },
  141. });
  142. });
  143. };
  144. const getSounFonts = async () => {
  145. const pathname = /(192|localhost)/.test(location.origin) ? "/" : location.pathname;
  146. data.loadingSoundFonts = true;
  147. data.loadingSoundProgress = 0;
  148. for (let i = 0; i < data.notes.length; i++) {
  149. const note = data.notes[i];
  150. // console.log("🚀 ~ note:", i);
  151. let url = `${pathname}soundfonts/${data.subject}/`;
  152. url += note.realName;
  153. url += ".mp3";
  154. data.soundFonts[note.realKey] = await createAudio(url);
  155. data.loadingSoundProgress = Math.floor(((i + 1) / data.notes.length) * 100);
  156. }
  157. data.loadingSoundProgress = 100;
  158. data.loadingSoundFonts = false;
  159. api_cloudLoading();
  160. // console.log("🚀 ~ data.soundFonts:", data.soundFonts);
  161. };
  162. onBeforeMount(() => {
  163. getNotes();
  164. getFingeringData();
  165. getSounFonts();
  166. });
  167. const noteClick = (item: IFIGNER_INSTRUMENT_Note) => {
  168. if (data.noteAudio) {
  169. data.noteAudio.stop();
  170. if (data.realKey === item.realKey) {
  171. data.realKey = 0;
  172. data.noteAudio = null as unknown as Howl;
  173. return;
  174. }
  175. }
  176. data.realKey = item.realKey;
  177. data.noteAudio = data.soundFonts[item.realKey];
  178. data.noteAudio.play();
  179. };
  180. const handleStop = () => {
  181. if (data.noteAudio) {
  182. data.noteAudio.stop();
  183. data.realKey = 0;
  184. data.noteAudio = null as unknown as Howl;
  185. }
  186. };
  187. /** 返回 */
  188. const handleBack = () => {
  189. handleStop();
  190. if (props.isComponent) {
  191. console.log("关闭");
  192. emit("close");
  193. return;
  194. } else {
  195. // if (fingerData.fingeringInfo.orientation === 0) {
  196. // api_setRequestedOrientation(1);
  197. // }
  198. }
  199. // 不在APP中,
  200. if (!storeData.isApp) {
  201. window.close();
  202. return;
  203. }
  204. api_back();
  205. };
  206. onMounted(() => {
  207. loadElement();
  208. api_setStatusBarVisibility();
  209. });
  210. const loadElement = () => {
  211. const fingeringContainer = document.getElementById("fingeringContainer");
  212. // console.log("🚀 ~ fingeringContainer:", fingeringContainer);
  213. const mc = new Hammer.Manager(fingeringContainer as HTMLElement);
  214. mc.add(new Hammer.Pan({ threshold: 0, pointers: 0 }));
  215. mc.add(new Hammer.Pinch({ threshold: 0 })).recognizeWith([mc.get("pan")]);
  216. // mc.get("pan").set({ direction: Hammer.DIRECTION_ALL });
  217. // mc.get("pinch").set({ enable: true });
  218. mc.on("panstart pinchstart", function (ev) {
  219. data.transform.transition = "";
  220. });
  221. mc.on("panmove pinchmove", function (ev) {
  222. if (ev.type === "pinchmove") {
  223. // console.log("🚀 ~ ev:", ev.type, ev.scale, ev.deltaX, ev.deltaY);
  224. data.transform.scale = ev.scale * data.transform.startScale;
  225. data.transform.x = data.transform.startX + ev.deltaX;
  226. data.transform.y = data.transform.startY + ev.deltaY;
  227. }
  228. if (ev.type === "panmove") {
  229. // console.log("🚀 ~ ev:", ev.type, ev.deltaX, ev.deltaY);
  230. data.transform.x = data.transform.startX + ev.deltaX;
  231. data.transform.y = data.transform.startY + ev.deltaY;
  232. }
  233. });
  234. //
  235. mc.on("hammer.input", function (ev) {
  236. // console.log("🚀 ~ ev:", ev.type, ev.isFinal);
  237. if (ev.isFinal) {
  238. data.transform.startScale = data.transform.scale;
  239. data.transform.startX = data.transform.x;
  240. data.transform.startY = data.transform.y;
  241. }
  242. });
  243. };
  244. const resetElement = () => {
  245. data.transform.transition = "all 0.3s";
  246. nextTick(() => {
  247. data.transform.scale = 1;
  248. data.transform.x = 0;
  249. data.transform.y = 0;
  250. data.transform.startScale = 1;
  251. data.transform.startX = 0;
  252. data.transform.startY = 0;
  253. });
  254. };
  255. const pageVisible = usePageVisibility();
  256. watch(
  257. () => pageVisible.value,
  258. (val) => {
  259. if (val === "hidden") {
  260. console.log("页面隐藏停止播放");
  261. handleStop();
  262. }
  263. }
  264. );
  265. /** 课件播放 */
  266. const changePlay = (res: any) => {
  267. if (res?.data?.api === "setPlayState") {
  268. handleStop();
  269. }
  270. };
  271. const noteBoxRef = ref();
  272. const scrollNoteBox = (type: "left" | "right") => {
  273. const width = noteBoxRef.value.offsetWidth / 2;
  274. (noteBoxRef.value as unknown as HTMLElement).scrollBy({
  275. left: type === "left" ? -width : width,
  276. behavior: "smooth",
  277. });
  278. };
  279. /** 滚轮缩放 */
  280. const handleWheel = (e: WheelEvent) => {
  281. e.preventDefault();
  282. if (e.deltaY > 0) {
  283. data.transform.scale -= 0.1;
  284. if (data.transform.scale <= 0.5) {
  285. data.transform.scale = 0.5;
  286. }
  287. } else {
  288. data.transform.scale += 0.1;
  289. if (data.transform.scale >= 2) {
  290. data.transform.scale = 2;
  291. }
  292. }
  293. };
  294. onMounted(() => {
  295. window.addEventListener("message", changePlay);
  296. const fingeringContainer = document.getElementById("fingeringContainer");
  297. fingeringContainer?.addEventListener("wheel", handleWheel);
  298. });
  299. onUnmounted(() => {
  300. window.removeEventListener("message", changePlay);
  301. const fingeringContainer = document.getElementById("fingeringContainer");
  302. fingeringContainer?.removeEventListener("wheel", handleWheel);
  303. });
  304. return () => {
  305. const relationship = fingerData.subject?.relationship?.[data.realKey] || [];
  306. const rs: number[] = Array.isArray(relationship[1])
  307. ? relationship[fingerData.relationshipIndex]
  308. : relationship;
  309. const canTizhi = Array.isArray(relationship[1]);
  310. return (
  311. <div
  312. class={[
  313. styles.fingerBox,
  314. !query.modelType && fingerData.fingeringInfo.orientation === 1
  315. ? styles.fingerBottom
  316. : styles.fingerRight,
  317. ]}
  318. >
  319. <div class={[styles.head, styles.backHead, data.paddingTop && styles.paddingTop]}>
  320. <div class={styles.left}>
  321. <button class={[styles.backBtn]} onClick={() => handleBack()}>
  322. <img src={icons.icon_back} />
  323. </button>
  324. </div>
  325. </div>
  326. <div class={[styles.head, data.paddingTop && styles.paddingTop]}>
  327. <div class={styles.left}>
  328. {data.subject === "pan-flute" && (
  329. <div
  330. class={styles.baseBtn}
  331. onClick={() => {
  332. data.viewIndex++;
  333. if (data.viewIndex > 2) {
  334. data.viewIndex = 0;
  335. }
  336. getFingeringData();
  337. }}
  338. >
  339. 切换视图
  340. </div>
  341. )}
  342. </div>
  343. <div class={styles.rightBtn}>
  344. <div class={[styles.item]} onClick={() => resetElement()}>
  345. <img src={icons.icon_2_0} />
  346. <span>还原</span>
  347. </div>
  348. <div
  349. class={[styles.item]}
  350. onClick={() => {
  351. resetElement();
  352. data.tipShow = !data.tipShow;
  353. }}
  354. >
  355. <img src={icons.icon_2_1} />
  356. <span>使用说明</span>
  357. </div>
  358. </div>
  359. </div>
  360. <div class={styles.fingerContent}>
  361. <div class={styles.wrapFinger}>
  362. <div id="fingeringContainer" class={styles.boxFinger}>
  363. <div
  364. style={{
  365. transform: `translate3d(${data.transform.x}px,${data.transform.y}px,0px) scale(${data.transform.scale})`,
  366. transition: data.transform.transition,
  367. }}
  368. class={[styles.fingeringContainer]}
  369. >
  370. <div class={styles.imgs}>
  371. <img src={fingerData.subject?.json?.full} />
  372. {rs.map((key: number | string, index: number) => {
  373. const nk: string =
  374. typeof key === "string" ? key.replace("active-", "") : String(key);
  375. return <img data-index={nk} src={fingerData.subject?.json?.[nk]} />;
  376. })}
  377. <div
  378. id="finger-note-2"
  379. class={[styles.tizhi, canTizhi && styles.canDisplay]}
  380. onClick={() =>
  381. (fingerData.relationshipIndex = fingerData.relationshipIndex === 0 ? 1 : 0)
  382. }
  383. >
  384. 替指
  385. </div>
  386. </div>
  387. </div>
  388. </div>
  389. <div class={[styles.notes, data.paddingLeft && styles.paddingLeft]}>
  390. <Button class={styles.noteBtn} onClick={() => scrollNoteBox("left")}>
  391. <Icon name="arrow-left" />
  392. </Button>
  393. <div
  394. class={[
  395. styles.noteContent,
  396. browsInfo.ios ? "" : styles.noteContentWrap,
  397. data.huaweiPad && styles.huaweiPad,
  398. ]}
  399. >
  400. <div ref={noteBoxRef} class={styles.noteBox}>
  401. {data.notes.map((note: IFIGNER_INSTRUMENT_Note, index: number) => {
  402. const steps = new Array(Math.abs(note.step)).fill(1);
  403. return (
  404. <div
  405. id={index == 0 ? "finger-note-0" : ""}
  406. draggable={false}
  407. class={styles.note}
  408. onClick={() => noteClick(note)}
  409. >
  410. {data.realKey === note.realKey ? (
  411. <img draggable={false} src={icons.icon_btn_ylow} />
  412. ) : (
  413. <img draggable={false} src={icons.icon_btn_blue} />
  414. )}
  415. <div
  416. class={[styles.noteKey, data.realKey === note.realKey && styles.keyActive]}
  417. >
  418. {note.step > 0 ? steps.map((n) => <span class={styles.dot}></span>) : null}
  419. <div class={styles.noteName}>
  420. <sup>{note.mark && (note.mark === "rise" ? "#" : "b")}</sup>
  421. {note.key}
  422. </div>
  423. {note.step < 0 ? steps.map((n) => <span class={styles.dot}></span>) : null}
  424. </div>
  425. </div>
  426. );
  427. })}
  428. </div>
  429. </div>
  430. <Button class={styles.noteBtn} onClick={() => scrollNoteBox("right")}>
  431. <Icon name="arrow" />
  432. </Button>
  433. </div>
  434. </div>
  435. <div class={[styles.tips, data.tipShow ? "" : styles.tipHidden]}>
  436. <div class={styles.tipTitle}>
  437. <div class={styles.tipTitleName}>{fingerData.fingeringInfo.code}使用说明</div>
  438. <Button class={styles.tipClose} onClick={() => (data.tipShow = false)}>
  439. <Icon name="cross" color="#999" />
  440. </Button>
  441. </div>
  442. <div class={styles.tipContent}>
  443. {data.tips.map((tip, tipIndex) => (
  444. <div class={styles.tipItem}>
  445. <div class={styles.iconWrap}>
  446. <div class={styles.tipItemIcon}>{tipIndex + 1}</div>
  447. </div>
  448. <div>
  449. {tip.name}: {tip.realName}
  450. </div>
  451. </div>
  452. ))}
  453. </div>
  454. </div>
  455. {data.loadingSoundFonts && (
  456. <div class={styles.loading}>
  457. <div class={styles.loadingWrap}>
  458. <img class={styles.loadingIcon} src={icon_loading_img} />
  459. <Progress percentage={data.loadingSoundProgress} />
  460. <div class={styles.loadingTip}>加载中,请稍后…</div>
  461. </div>
  462. </div>
  463. )}
  464. </div>
  465. {!!data.tones.length && (
  466. <>
  467. {fingerData.fingeringInfo.name == "hulusi-flute" ? (
  468. <div
  469. id="finger-note-1"
  470. class={[styles.toggleBtn, styles.toggleBtnhulusi]}
  471. onClick={() => (data.tnoteShow = true)}
  472. >
  473. <div>
  474. 全按作
  475. <div class={[styles.noteKey, styles.hulusiNoteKey]}>
  476. {data.activeTone.step > 0 ? <span class={styles.dot}></span> : null}
  477. <div class={styles.noteName}>
  478. <sup>
  479. {data.activeTone.mark && (data.activeTone.mark === "rise" ? "#" : "b")}
  480. </sup>
  481. {data.activeTone.key}
  482. </div>
  483. {data.activeTone.step < 0 ? <span class={styles.dot}></span> : null}
  484. </div>
  485. </div>
  486. <img src={icons.icon_arrow} />
  487. </div>
  488. ) : (
  489. <div id="finger-note-1" class={styles.toggleBtn} onClick={() => (data.tnoteShow = true)}>
  490. <div>
  491. <sup>{data.activeTone.mark && (data.activeTone.mark === "rise" ? "#" : "b")}</sup>
  492. {data.activeTone.name}
  493. </div>
  494. <img src={icons.icon_arrow} />
  495. </div>
  496. )}
  497. </>
  498. )}
  499. <Popup
  500. class="tonePopup"
  501. v-model:show={data.tnoteShow}
  502. position={
  503. !query.modelType && fingerData.fingeringInfo.orientation === 1 ? "bottom" : "right"
  504. }
  505. >
  506. <div class={styles.tones}>
  507. <div class={styles.toneTitle}>
  508. <div class={styles.tipTitleName}>移调</div>
  509. <Button class={styles.tipClose} onClick={() => (data.tnoteShow = false)}>
  510. <Icon name="cross" color="#999" />
  511. </Button>
  512. </div>
  513. <div style={{ flex: 1, overflow: "hidden" }}>
  514. <Space size={0} class={styles.toneContent}>
  515. {data.tones.map((tone: IFIGNER_INSTRUMENT_Note) => {
  516. const steps = new Array(Math.abs(tone.step)).fill(1);
  517. return (
  518. <Button
  519. round
  520. plain
  521. type={data.popupActiveTone.realName === tone.realName ? "primary" : "default"}
  522. onClick={() => {
  523. data.popupActiveTone = tone;
  524. setNotes();
  525. }}
  526. >
  527. {fingerData.fingeringInfo.name == "hulusi-flute" ? (
  528. <div style={{ display: "flex", alignItems: "center" }}>
  529. 全按作
  530. <div class={[styles.noteKey, styles.hulusiNoteKey]}>
  531. {tone.step > 0 ? <span class={styles.dot}></span> : null}
  532. <div class={styles.noteName} style={{ fontSize: "0.25rem" }}>
  533. <sup>{tone.mark && (tone.mark === "rise" ? "#" : "b")}</sup>
  534. {tone.key}
  535. </div>
  536. {tone.step < 0 ? <span class={styles.dot}></span> : null}
  537. </div>
  538. </div>
  539. ) : (
  540. <div class={styles.noteName}>
  541. <sup>{tone.mark && (tone.mark === "rise" ? "#" : "b")}</sup>
  542. {tone.name}
  543. </div>
  544. )}
  545. </Button>
  546. );
  547. })}
  548. </Space>
  549. </div>
  550. <Space size={0} class={styles.toneAction}>
  551. <Button type="primary" round plain onClick={() => (data.tnoteShow = false)}>
  552. 取消
  553. </Button>
  554. <Button
  555. type="primary"
  556. round
  557. onClick={() => {
  558. data.activeTone = data.popupActiveTone;
  559. setNotes();
  560. data.tnoteShow = false;
  561. }}
  562. >
  563. 确定
  564. </Button>
  565. </Space>
  566. </div>
  567. </Popup>
  568. {!data.loading && !data.loadingSoundFonts && <GuideIndex showGuide={false} list={["finger"]} />}
  569. </div>
  570. );
  571. };
  572. },
  573. });