123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441 |
- import OHeader from '@/components/o-header'
- import OSticky from '@/components/o-sticky'
- import { defineComponent, nextTick, onMounted, onUnmounted, reactive, ref, watch } from 'vue'
- import styles from './music-detail.module.less'
- import { Button, Image, Picker, Popup, Skeleton } from 'vant'
- import iconBg from './images/music-img-default.png'
- import iconDownload from './images/icon-download.png'
- import iconChange from './images/icon-change.png'
- import iconMusic from './images/icon-music.png'
- import { postMessage } from '@/helpers/native-message'
- import request from '@/helpers/request'
- import { state } from '@/state'
- import { useRoute } from 'vue-router'
- import Plyr from 'plyr'
- import 'plyr/dist/plyr.css'
- import deepClone from '@/helpers/deep-clone'
- import StaffChange from './staff-change'
- import Download from './download'
- import { svgtopng } from './formatSvgToImg'
- import requestOrigin from 'umi-request'
- import { getInstrumentName } from '@/constant/instruments'
- import { formatXML, getCustomInfo, onlyVisible } from './instrument'
- export default defineComponent({
- name: 'music-detail',
- setup() {
- const route = useRoute()
- const audioRef = ref()
- const player = ref<any>(null)
- const partColumns = ref<any>([])
- const staffData = reactive({
- details: {} as any,
- status: false,
- open: false,
- audioReady: false,
- iframeSrc: '',
- isComberRender: false,
- musicXml: [] as any,
- instrumentName: '',
- iframeRef: null as any,
- imgs: [] as any,
- radio: 'staff' as any,
- partList: [] as any[],
- partNames: [] as any[],
- selectedPartName: '' as any,
- selectedPartIndex: 0,
- partXmlIndex: 0
- })
- const loading = ref(false)
- const downloadStatus = ref(false)
- const showImg = ref([] as any)
- watch(
- () => staffData.radio,
- (val: string) => {
- if (val == 'first') {
- showImg.value = deepClone(staffData.details.musicFirstSvg?.split(',') || [])
- } else if (val == 'fixed') {
- showImg.value = deepClone(staffData.details.musicJianSvg?.split(',') || [])
- } else {
- showImg.value = deepClone(staffData.details.musicImg?.split(',') || [])
- }
- }
- )
- const musicIframeLoad = async () => {
- const iframeRef: any = document.getElementById('staffIframeRef')
- if (iframeRef && iframeRef.contentWindow.renderXml) {
- const res = await requestOrigin.get(staffData.details.xmlFileUrl, { mode: 'cors' })
- const parseXmlInfo = getCustomInfo(res)
- const xml = formatXML(parseXmlInfo.parsedXML)
- if (staffData.isComberRender) {
- iframeRef.contentWindow.renderXml(xml, staffData.partXmlIndex, staffData.isComberRender)
- } else {
- const currentXml = onlyVisible(xml, staffData.partXmlIndex)
- iframeRef.contentWindow.renderXml(
- currentXml,
- staffData.partXmlIndex,
- staffData.isComberRender
- )
- }
-
- }
- }
- const resetRender = async () => {
- const iframeRef: any = document.getElementById('staffIframeRef')
- if (iframeRef && iframeRef.contentWindow.renderXml) {
- loading.value = true
-
-
-
-
-
-
- const res = await requestOrigin.get(staffData.details.xmlFileUrl, { mode: 'cors' })
- const parseXmlInfo = getCustomInfo(res)
- const xml = formatXML(parseXmlInfo.parsedXML)
- if (staffData.isComberRender) {
- iframeRef.contentWindow.renderXml(xml, staffData.partXmlIndex, staffData.isComberRender)
- } else {
- const currentXml = onlyVisible(xml, staffData.partXmlIndex)
- iframeRef.contentWindow.renderXml(currentXml, 0, staffData.isComberRender)
- }
- }
- }
- const resetRenderPage = (type: string, xmlUrl: string) => {
- const iframeRef: any = document.getElementById('staffIframeRef')
- if (iframeRef && iframeRef.contentWindow.renderXml) {
- iframeRef.contentWindow.resetRenderPage(type, xmlUrl)
- }
- }
- const renderStaff = async () => {
- try {
-
- staffData.iframeSrc = `${location.origin}${location.pathname}osmd/index.html`
- } catch (error) {
-
- }
- }
- const getPartNames = async (xmlUrl: string) => {
- const partNames: string[] = []
- try {
- const res = await requestOrigin.get(xmlUrl, { mode: 'cors' })
- const xml: any = new DOMParser().parseFromString(res, 'text/xml')
- for (const item of xml.getElementsByTagName('part-name')) {
- if (item.textContent) {
- partNames.push(item.textContent)
- }
- }
- } catch (error) {
-
- }
- return partNames.filter((text: string) => text.toLocaleUpperCase() !== 'COMMON') || []
- }
- const toDetail = async (row: any) => {
- if (row.musicSheetType === 'SINGLE') {
- loading.value = false
- return
- }
- staffData.partNames = await getPartNames(row.xmlFileUrl)
- let partList = row.background || []
- partList = partList.filter(
- (item: any) => !item.track?.toLocaleUpperCase()?.includes('COMMON')
- )
- partColumns.value = partList.map((item: any, index: number) => {
- const instrumentName = getInstrumentName(item.track)
- const xmlIndex = staffData.partNames.findIndex((name: any) => name === item.track)
- return {
- text: item.track + (instrumentName ? `(${instrumentName})` : ''),
- instrumentName: instrumentName,
- xmlIndex,
- value: index
- }
- })
-
- const defaultShowStaff = partColumns.value[staffData.selectedPartIndex]
- staffData.selectedPartName = defaultShowStaff.instrumentName
- staffData.partXmlIndex = defaultShowStaff.xmlIndex
- }
- const getMusicDetail = async () => {
- loading.value = true
- try {
- if (!route.query.id) return
- const { data } = await request.get(
- state.platformApi + '/musicSheet/detail/' + route.query.id
- )
- staffData.details = data || {}
- showImg.value = staffData.details.musicImg?.split(',') || []
- staffData.isComberRender = data.musicSubject === '1'
- nextTick(async () => {
- if (data.audioFileUrl) {
- initAudio()
- } else {
- await toDetail(staffData.details)
- renderStaff()
- }
- })
- } catch (e) {
-
- console.log(e)
- }
- }
- const initAudio = async () => {
- const controls = [
-
- 'play',
- 'progress',
- 'captions',
-
- 'current-time',
- 'duration'
- ]
- player.value = new Plyr(audioRef.value, {
- controls: controls
- })
- player.value.on('ready', () => {
- staffData.audioReady = true
- player.value.muted = false
- nextTick(async () => {
-
-
-
-
- await toDetail(staffData.details)
- renderStaff()
- })
- })
- }
-
- const openView = async (item: any) => {
- const src = `${location.origin}/orchestra-music-score/?id=${item.id}&part-index=${staffData.selectedPartIndex}`
- console.log('🚀 ~ src:', src)
- postMessage({
- api: 'openAccompanyWebView',
- content: {
- url: src,
- orientation: 0,
- isHideTitle: true,
- statusBarTextColor: false,
- isOpenLight: true
- }
- })
- }
- const onSubmit = () => {
- player.value?.pause()
- openView(staffData.details)
- }
- const showLoading = async (e: any) => {
- if (e.data?.api === 'musicStaffRender') {
- try {
- const osmdImg = e.data.osmdImg
- const imgs: any = []
- for (let i = 0; i < osmdImg.length; i++) {
- const img: any = await svgtopng(osmdImg[i].img, osmdImg[i].width, osmdImg[i].height)
- imgs.push(img)
- }
- showImg.value = imgs
- } catch (e) {
-
- }
- loading.value = e.data.loading
- }
- }
- onMounted(async () => {
- await getMusicDetail()
- window.addEventListener('message', showLoading)
- })
- onUnmounted(() => {
- window.removeEventListener('message', showLoading)
- })
- return () => (
- <div class={styles.musicDetail}>
- <OSticky mode="sticky" position="top">
- <OHeader border={false} background={'transparent'} />
- </OSticky>
- <div class={styles.musicContainer}>
- <div class={styles.musicInfos}>
- <div class={styles.musicImg}>
- <Image src={iconBg} />
- </div>
- <div class={styles.info}>
- <p class={styles.names}>
- {staffData.details.musicSheetName}
- {staffData.details.musicSheetType === 'CONCERT' && staffData.selectedPartName
- ? `(${staffData.selectedPartName})`
- : ''}
- </p>
- <p class={styles.author}>{staffData.details.composer}</p>
- </div>
- </div>
- <div class={styles.showImgContainer}>
- {
- }
- {loading.value && (
- <>
- <Skeleton title row={7} />
- </>
- )}
- <iframe
- id="staffIframeRef"
- style={{
- opacity: loading.value ? 0 : 1,
- width: '100%',
- height: '100%'
- }}
- src={staffData.iframeSrc}
- onLoad={musicIframeLoad}
- ></iframe>
- {}
- {
- }
- </div>
- </div>
- {staffData.details.id && (
- <OSticky position="bottom" varName="--footer-height">
- <div class={styles.bottomStyle} style={{ background: '#fff' }}>
- {staffData.details?.audioFileUrl && (
- <div
- class={[styles.audio, styles.collectCell]}
- style={{ opacity: staffData.audioReady ? 1 : 0 }}
- >
- <audio id="player" controls ref={audioRef} style={{ height: '40px' }}>
- <source src={staffData.details?.audioFileUrl} type="audio/mp3" />
- </audio>
- </div>
- )}
- <div class={styles.footers}>
- <div class={styles.iconGroup}>
- <div
- class={styles.icon}
- onClick={() => {
- if (loading.value) return
- downloadStatus.value = true
- }}
- >
- <img src={iconDownload} />
- <span>下载</span>
- </div>
- {staffData.details?.musicSheetType === 'CONCERT' ? (
- <div
- class={styles.icon}
- onClick={() => {
- if (loading.value) return
- staffData.open = true
- }}
- >
- <img src={iconMusic} />
- <span>声轨</span>
- </div>
- ) : (
- <div
- class={styles.icon}
- onClick={() => {
- if (loading.value) return
- staffData.status = true
- }}
- >
- <img src={iconChange} />
- <span>转谱</span>
- </div>
- )}
- </div>
- <Button
- round
- block
- type="primary"
- disabled={loading.value}
- color={'#FF8057'}
- onClick={onSubmit}
- >
- 开始练习
- </Button>
- </div>
- </div>
- </OSticky>
- )}
- <Popup
- v-model:show={staffData.status}
- teleport="body"
- closeable
- style={{ width: '80%' }}
- class={styles.staffChange}
- round
- >
- <StaffChange
- radio={staffData.radio}
- onClose={() => (staffData.status = false)}
- onChange={(type: string) => {
-
- staffData.radio = type
- staffData.status = false
- if (type == 'first') {
- loading.value = true
- resetRenderPage('first', staffData.details.xmlFileUrl)
- } else if (type == 'fixed') {
- loading.value = true
- resetRenderPage('fixed', staffData.details.xmlFileUrl)
- } else {
- loading.value = true
- resetRenderPage('staff', staffData.details.xmlFileUrl)
- }
- }}
- />
- </Popup>
- <Popup v-model:show={downloadStatus.value} position="bottom" round>
- {downloadStatus.value && (
- <Download
- imgList={JSON.parse(JSON.stringify(showImg.value))}
- musicSheetName={staffData.details.musicSheetName}
- />
- )}
- </Popup>
- <Popup teleport="body" position="bottom" round v-model:show={staffData.open}>
- <Picker
- columns={partColumns.value}
- onConfirm={(value) => {
- staffData.open = false
- staffData.selectedPartIndex = value.selectedValues[0]
- staffData.selectedPartName = value.selectedOptions[0].instrumentName
- staffData.partXmlIndex = value.selectedOptions[0].xmlIndex
-
- nextTick(() => {
- resetRender()
- })
- }}
- onCancel={() => (staffData.open = false)}
- />
- </Popup>
- </div>
- )
- }
- })
|