index.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. <template>
  2. <div
  3. class="creation"
  4. :style="{
  5. '--heightA': state.heightA + 'px',
  6. '--navBarHeight': navBarHeight + 'px'
  7. }"
  8. >
  9. <div class="creationBg"></div>
  10. <van-sticky>
  11. <van-nav-bar
  12. class="nav"
  13. :class="{ isScreenScroll: isScreenScroll }"
  14. :fixed="false"
  15. :title="state.musicDetail.musicSheetName"
  16. left-arrow
  17. @click-left="goBack"
  18. />
  19. </van-sticky>
  20. <div class="singer">演奏:{{ state.musicDetail.username }}</div>
  21. <van-sticky :offset-top="44 + navBarHeight" :z-index="200">
  22. <div class="playBox" data-html2canvas-ignore="true" ref="playBoxDom">
  23. <div
  24. :class="[
  25. 'playSection',
  26. plyrState.mediaTimeShow && 'mediaTimeShow',
  27. isLandscapeScreen && 'isLandscapeScreen',
  28. state.playType === 'Video' && 'videoType'
  29. ]"
  30. id="playMediaSection"
  31. @click="handlerClickPlay"
  32. >
  33. <!-- 横屏 -->
  34. <div v-if="isLandscapeScreen" class="backBox">
  35. <img class="backImg" :src="state.playType === 'Video'?require('./img/back1.png'):require('./img/back.png')" @click="handlerBack" />
  36. <div class="musicDetail">
  37. <div class="musicSheetName">
  38. <van-notice-bar :text="state.musicDetail.musicSheetName" background="none" />
  39. </div>
  40. <div class="username">演奏:{{ state.musicDetail.username }}</div>
  41. </div>
  42. </div>
  43. <!-- audio -->
  44. <div class="audioBox" v-if="state.playType === 'Audio'">
  45. <canvas class="audioVisualizer" id="audioVisualizer"></canvas>
  46. <audio
  47. crossorigin="anonymous"
  48. id="audioMediaSrc"
  49. :src="state.musicDetail.videoUrl"
  50. controls="false"
  51. preload="metadata"
  52. playsinline
  53. webkit-playsinline
  54. />
  55. <img src="./img/ty.png" class="tyBg" />
  56. <div class="audioBoxBg">
  57. <div :class="['audioPan', plyrState.playIngShow ? 'imgRotate' : '']">
  58. <van-image class="audioImg" :src="state.musicDetail.img || require('./img/music_bg.png')" />
  59. </div>
  60. <i class="audioPoint"></i>
  61. <i :class="['audioZhen', plyrState.playIngShow && 'active']"></i>
  62. </div>
  63. </div>
  64. <!-- video -->
  65. <video
  66. v-if="state.playType === 'Video'"
  67. id="videoMediaSrc"
  68. class="videoBox"
  69. :src="state.musicDetail.videoUrl"
  70. :data-poster="state.musicDetail.videoImg || require('./img/videoBg.png')"
  71. :poster="state.musicDetail.videoImg || require('./img/videoBg.png')"
  72. preload="metadata"
  73. playsinline
  74. webkit-playsinline
  75. x5-playsinline
  76. />
  77. <!-- 工具 -->
  78. <div :class="['playLarge', !plyrState.mediaTimeShow && plyrState.playIngShow && 'playIngShow']"></div>
  79. <div class="mediaTimeCon">
  80. <div class="mediaTime">
  81. <div>
  82. {{ getSecondRPM(plyrState.currentTime) }}
  83. </div>
  84. <div class="note">/</div>
  85. <div class="duration">
  86. {{ getSecondRPM(plyrState.duration) }}
  87. </div>
  88. </div>
  89. </div>
  90. <div class="landscapeScreen" @click="handlerLandscapeScreen"></div>
  91. <!-- 谱面 -->
  92. <div v-if="staffState.staffSrc" :class="['staffBoxCon', staffState.isShow && 'staffBoxShow']">
  93. <div
  94. class="staffBox"
  95. :style="{
  96. '--staffBoxHeight': staffState.height
  97. }"
  98. >
  99. <div class="mask"></div>
  100. <iframe ref="staffDom" class="staff" frameborder="0" :src="staffState.staffSrc"></iframe>
  101. </div>
  102. </div>
  103. </div>
  104. </div>
  105. </van-sticky>
  106. <div class="musicSection">
  107. <div class="avatarInfoBox">
  108. <div class="avatar">
  109. <van-image class="userLogo" :src="state.musicDetail.avatar" />
  110. <div class="infoCon">
  111. <div class="info">
  112. <span class="userName">{{ state.musicDetail.username }}</span>
  113. <img :src="require('./img/icon-member.png')" v-if="state.musicDetail.vipFlag" class="iconMember" />
  114. </div>
  115. <div class="sub">
  116. {{
  117. (state.musicDetail.subjectName || "") + " " + (state.musicDetail.currentGradeNum ? getGradeCh(state.musicDetail.currentGradeNum - 1) : "")
  118. }}
  119. </div>
  120. </div>
  121. </div>
  122. <div class="linkes">
  123. <img :src="require('./img/icon-zan.png')" class="iconZan" />
  124. <span>{{ state.musicDetail.likeNum }}</span>
  125. </div>
  126. </div>
  127. <textEllipsis class="textEllipsis" :text="state.musicDetail.desc || ''" />
  128. </div>
  129. <div class="likeSection">
  130. <div class="likeTitle">点赞记录</div>
  131. <van-list
  132. v-if="state.listState.dataShow"
  133. :finished="state.listState.finished"
  134. :finishedText="' '"
  135. @load="getStarList"
  136. :immediateCheck="false"
  137. >
  138. <van-cell
  139. v-for="(item, index) in state.list"
  140. :key="index"
  141. :class="['likeItem', index === state.list.length - 1 && 'likeItemLast']"
  142. :border="false"
  143. >
  144. <template #icon>
  145. <van-image :src="item.userAvatar" class="userLogo" />
  146. </template>
  147. <template #title>
  148. <div class="userInfo">
  149. <p class="name">{{ item.userName || `游客${item.userId}` }}</p>
  150. <p class="sub">
  151. {{ (item.subjectName || "") + " " + (item.currentGradeNum ? getGradeCh(item.currentGradeNum - 1) : "") }}
  152. </p>
  153. </div>
  154. </template>
  155. <template #default>
  156. <div class="time">
  157. {{ onDayjs(item.createTime) }}
  158. </div>
  159. </template>
  160. </van-cell>
  161. </van-list>
  162. <MEmpty class="mEmpty" v-else msg="暂无内容" style="margin-bottom: 0.5rem" />
  163. </div>
  164. <div class="upward" v-show="!isScreenScroll">
  165. <img :src="require('./img/upward.png')" />
  166. </div>
  167. <div class="bottomSection">
  168. <div class="bottomShare">
  169. <p @click="onDownload">
  170. <img :src="require('./img/icon-download.png')" />
  171. <span>下载</span>
  172. </p>
  173. <p @click="() => (state.shareStatus = true)">
  174. <img :src="require('./img/icon-share.png')" />
  175. <span>分享</span>
  176. </p>
  177. <p @click="() => (state.deleteStatus = true)">
  178. <img :src="require('./img/icon-delete.png')" />
  179. <span>删除</span>
  180. </p>
  181. </div>
  182. <img
  183. :src="require('./img/edit.png')"
  184. class="btnEdit"
  185. @click="
  186. () => {
  187. setStatusBarTextColor(false)
  188. $router.push({ path: '/creation-edit', query: { id: this.id } })
  189. }
  190. "
  191. />
  192. </div>
  193. <Loading v-if="!staffState.isShow"></Loading>
  194. <van-popup position="bottom" v-model="state.shareStatus" style=" background: transparent ">
  195. <ShareModel :musicDetail="state.musicDetail" @close="state.shareStatus = false" />
  196. </van-popup>
  197. <van-popup v-model="state.deleteStatus" round class="popupContainer">
  198. <p class="popupTit">温馨提示</p>
  199. <p class="popupContent">确认删除作品吗?</p>
  200. <div class="popupBtnGroup">
  201. <van-button round @click="() => (state.deleteStatus = false)">
  202. 取消
  203. </van-button>
  204. <van-button round type="primary" @click="onDelete">
  205. 确认
  206. </van-button>
  207. </div>
  208. </van-popup>
  209. </div>
  210. </template>
  211. <script>
  212. import dayjs from "dayjs"
  213. import { browser, getSecondRPM, getGradeCh} from "@/common/common"
  214. import { postMessage } from "@/helpers/native-message"
  215. import { api_userMusicDetail, api_userMusicRemove, api_userMusicStarPage } from "./api"
  216. import audioVisualDraw, { vaildMusicScoreUrl } from "./audioVisualDraw"
  217. import textEllipsis from "./textEllipsis"
  218. import Loading from "./loading"
  219. import "plyr/dist/plyr.css"
  220. import Plyr from "plyr"
  221. import MEmpty from "@/components/MEmpty"
  222. import ShareModel from "./share-model"
  223. export default {
  224. name: "creation",
  225. data() {
  226. return {
  227. id: this.$route.query.id,
  228. state: {
  229. deleteStatus: false,
  230. shareStatus: false,
  231. playType: "", // 播放类型
  232. musicDetail: {},
  233. isClick: false,
  234. list: [],
  235. listState: {
  236. dataShow: true, // 判断是否有数据
  237. loading: false,
  238. finished: false
  239. },
  240. params: {
  241. page: 1,
  242. rows: 20
  243. },
  244. _plrl: null,
  245. heightA: 0
  246. },
  247. plyrState: {
  248. duration: 0,
  249. currentTime: 0,
  250. mediaTimeShow: false,
  251. playIngShow: true
  252. },
  253. isLandscapeScreen: false,
  254. isScreenScroll: false,
  255. navBarHeight: 0,
  256. staffState: {
  257. staffSrc: "",
  258. isShow: false,
  259. height: "initial",
  260. speedRate: 1,
  261. musicRenderType: "staff",
  262. partIndex: 0
  263. }
  264. }
  265. },
  266. components: {
  267. textEllipsis,
  268. MEmpty,
  269. Loading,
  270. ShareModel
  271. },
  272. methods: {
  273. getGradeCh,
  274. getSecondRPM,
  275. onDayjs(time) {
  276. return dayjs(time).format("YYYY-MM-DD HH:mm")
  277. },
  278. goBack() {
  279. if (browser().isApp) {
  280. this.setStatusBarTextColor(false)
  281. postMessage({ api: "setBarStatus", content: { status: 1, backButtonHidden: 0 } })
  282. postMessage({
  283. api: "goBack"
  284. })
  285. } else {
  286. this.$router.back()
  287. }
  288. },
  289. // 下载
  290. onDownload() {
  291. postMessage({
  292. api: "saveFile",
  293. content: {
  294. url: this.state.musicDetail.videoUrl
  295. }
  296. })
  297. },
  298. // 删除作品
  299. async onDelete() {
  300. try {
  301. await api_userMusicRemove({ id: this.id })
  302. setTimeout(() => {
  303. this.state.deleteStatus = false
  304. this.$toast("删除成功")
  305. }, 100)
  306. setTimeout(() => {
  307. this.goBack()
  308. }, 1200)
  309. } catch {
  310. //
  311. }
  312. },
  313. async getStarList() {
  314. try {
  315. if (this.state.isClick) return
  316. this.state.isClick = true
  317. const res = await api_userMusicStarPage({
  318. userMusicId: this.id,
  319. ...this.state.params
  320. })
  321. this.state.listState.loading = false
  322. const result = res.data || {}
  323. // 处理重复请求数据
  324. if (this.state.list.length > 0 && result.current === 1) {
  325. return
  326. }
  327. this.state.list = this.state.list.concat(result.rows || [])
  328. this.state.listState.finished = result.current >= result.pages
  329. this.state.params.page = result.current + 1
  330. this.state.listState.dataShow = this.state.list.length > 0
  331. this.state.isClick = false
  332. } catch {
  333. this.state.listState.dataShow = false
  334. this.state.listState.finished = true
  335. this.state.isClick = false
  336. }
  337. },
  338. // 初始化 媒体播放
  339. initMediaPlay() {
  340. const { playStaff, pauseStaff, updateProgressStaff } = this.staffMoveInstance()
  341. const id = this.state.playType === "Audio" ? "#audioMediaSrc" : "#videoMediaSrc"
  342. this.state._plrl = new Plyr(id, {
  343. controls: ["play", "progress", "current-time", "duration"],
  344. fullscreen: {
  345. enabled: false,
  346. fallback: false
  347. }
  348. })
  349. const player = this.state._plrl
  350. // 创建音波数据
  351. if (this.state.playType === "Audio") {
  352. const audioDom = document.querySelector("#audioMediaSrc")
  353. const canvasDom = document.querySelector("#audioVisualizer")
  354. const { pauseVisualDraw, playVisualDraw } = audioVisualDraw(audioDom, canvasDom)
  355. player.on("play", () => {
  356. playVisualDraw()
  357. })
  358. player.on("pause", () => {
  359. pauseVisualDraw()
  360. })
  361. }
  362. player.on("timeupdate", () => {
  363. this.plyrState.currentTime = player.currentTime
  364. })
  365. player.on("play", () => {
  366. this.plyrState.playIngShow = false
  367. playStaff()
  368. })
  369. player.on("pause", () => {
  370. this.plyrState.playIngShow = true
  371. pauseStaff()
  372. })
  373. player.on("ended", () => {
  374. if (this.plyrState.mediaTimeShow) return
  375. player.currentTime = 0
  376. if (!player.playing) {
  377. setTimeout(() => {
  378. updateProgressStaff(player.currentTime)
  379. }, 100)
  380. }
  381. })
  382. // 处理按压事件
  383. const handleStart = () => {
  384. if (this.isLandscapeScreen.value) {
  385. return
  386. }
  387. this.plyrState.duration = player.duration
  388. this.plyrState.mediaTimeShow = true
  389. }
  390. // 处理松开事件
  391. const handleEnd = () => {
  392. this.plyrState.mediaTimeShow = false
  393. // 暂停的时候调用
  394. if (!player.playing) {
  395. updateProgressStaff(player.currentTime)
  396. }
  397. }
  398. const progressDom = document.querySelector("#playMediaSection .plyr__controls .plyr__progress__container")
  399. progressDom.addEventListener("mousedown", handleStart)
  400. progressDom.addEventListener("touchstart", handleStart)
  401. progressDom.addEventListener("mouseup", handleEnd)
  402. progressDom.addEventListener("touchend", handleEnd)
  403. },
  404. // 初始化五线谱
  405. initStaff() {
  406. const src = `${vaildMusicScoreUrl()}/gym-music-score/#/simple-detail?id=${this.state.musicDetail.musicSheetId}&musicRenderType=${
  407. this.staffState.musicRenderType
  408. }&part-index=${this.staffState.partIndex}`
  409. //const src = `http://192.168.3.122:3000/gym-music-score.html#/simple-detail?id=${state.musicDetail.musicSheetId}&musicRenderType=${staffState.musicRenderType}&part-index=${staffState.partIndex}`;
  410. this.staffState.staffSrc = src
  411. window.addEventListener("message", event => {
  412. const { api, height } = event.data
  413. if (api === "api_musicPage") {
  414. this.staffState.isShow = true
  415. this.staffState.height = height + "px"
  416. }
  417. })
  418. },
  419. staffMoveInstance() {
  420. let isPause = true
  421. const requestAnimationFrameFun = () => {
  422. requestAnimationFrame(() => {
  423. this.$refs.staffDom?.contentWindow?.postMessage(
  424. {
  425. api: "api_playProgress",
  426. content: {
  427. currentTime: this.state._plrl.currentTime * this.staffState.speedRate
  428. }
  429. },
  430. "*"
  431. )
  432. if (!isPause) {
  433. requestAnimationFrameFun()
  434. }
  435. })
  436. }
  437. const playStaff = () => {
  438. // 没渲染不执行
  439. if (!this.staffState.isShow) return
  440. isPause = false
  441. this.$refs.staffDom?.contentWindow?.postMessage(
  442. {
  443. api: "api_play"
  444. },
  445. "*"
  446. )
  447. requestAnimationFrameFun()
  448. }
  449. const pauseStaff = () => {
  450. // 没渲染不执行
  451. if (!this.staffState.isShow) return
  452. isPause = true
  453. this.$refs.staffDom?.contentWindow?.postMessage(
  454. {
  455. api: "api_paused"
  456. },
  457. "*"
  458. )
  459. }
  460. const updateProgressStaff = currentTime => {
  461. // 没渲染不执行
  462. if (!this.staffState.isShow) return
  463. this.$refs.staffDom?.contentWindow?.postMessage(
  464. {
  465. api: "api_updateProgress",
  466. content: {
  467. currentTime: currentTime * this.staffState.speedRate
  468. }
  469. },
  470. "*"
  471. )
  472. }
  473. return {
  474. playStaff,
  475. pauseStaff,
  476. updateProgressStaff
  477. }
  478. },
  479. //点击改变播放状态
  480. handlerClickPlay(event) {
  481. // 原生 播放暂停按钮 点击的时候 不触发
  482. if (event?.target?.matches("button.plyr__control")) {
  483. return
  484. }
  485. if (this.state._plrl.playing) {
  486. this.state._plrl.pause()
  487. } else {
  488. this.state._plrl.play()
  489. }
  490. },
  491. handleScroll() {
  492. const height = window.scrollY || document.documentElement.scrollTop
  493. // 防止多次调用
  494. if (height > 0 && this.isScreenScroll === false) {
  495. this.isScreenScroll = true
  496. this.setStatusBarTextColor(false)
  497. }
  498. if (height <= 0) {
  499. this.isScreenScroll = false
  500. this.setStatusBarTextColor(true)
  501. }
  502. },
  503. // 设置导航栏颜色
  504. setStatusBarTextColor(isWhite) {
  505. postMessage({
  506. api: "setStatusBarTextColor",
  507. content: { statusBarTextColor: isWhite }
  508. })
  509. },
  510. handleVisibilitychange() {
  511. if (document.hidden) {
  512. this.state._plrl?.pause()
  513. }
  514. },
  515. handlerBack(event) {
  516. event.stopPropagation()
  517. this.verticalScreen()
  518. },
  519. landscapeScreen() {
  520. postMessage({
  521. api: "setRequestedOrientation",
  522. content: {
  523. orientation: 0
  524. }
  525. })
  526. this.isLandscapeScreen = true
  527. },
  528. verticalScreen() {
  529. postMessage({
  530. api: "setRequestedOrientation",
  531. content: {
  532. orientation: 1
  533. }
  534. })
  535. this.isLandscapeScreen = false
  536. },
  537. handlerLandscapeScreen(event) {
  538. event.stopPropagation()
  539. if (!this.isLandscapeScreen) {
  540. this.landscapeScreen()
  541. }
  542. }
  543. },
  544. mounted() {
  545. this.state.heightA = this.$refs.playBoxDom.offsetHeight
  546. document.addEventListener("scroll", this.handleScroll)
  547. document.addEventListener("visibilitychange", this.handleVisibilitychange)
  548. },
  549. destroyed() {
  550. this.state._plrl?.destroy()
  551. document.removeEventListener("scroll", this.handleScroll)
  552. document.removeEventListener("visibilitychange", this.handleVisibilitychange)
  553. },
  554. async created() {
  555. this.setStatusBarTextColor(true)
  556. postMessage({ api: "setBarStatus", content: { status: 0, backButtonHidden: 1 } })
  557. postMessage({ api: "getNavHeight" }, res => {
  558. const { content } = res
  559. const dpi = content.dpi || 2
  560. if (content.navHeight) {
  561. const navHeight = content.navHeight / dpi
  562. this.navBarHeight = navHeight
  563. }
  564. })
  565. /* 初始化请求 */
  566. try {
  567. const res = await api_userMusicDetail(this.id)
  568. this.state.musicDetail = res.data || {}
  569. try {
  570. const jsonConfig = JSON.parse(res.data.jsonConfig)
  571. jsonConfig.speedRate && (this.staffState.speedRate = jsonConfig.speedRate)
  572. jsonConfig.musicRenderType && (this.staffState.musicRenderType = jsonConfig.musicRenderType)
  573. jsonConfig.partIndex && (this.staffState.partIndex = jsonConfig["part-index"])
  574. } catch {}
  575. // 五线谱
  576. this.initStaff()
  577. this.getStarList()
  578. // 判断是视频还是音频
  579. if (res.data.videoUrl.lastIndexOf("mp4") !== -1) {
  580. this.state.playType = "Video"
  581. } else {
  582. this.state.playType = "Audio"
  583. }
  584. this.$nextTick(() => {
  585. this.initMediaPlay()
  586. })
  587. } catch (e) {
  588. this.staffState.isShow = true
  589. if (e.code === 999) {
  590. this.$dialog
  591. .alert({
  592. message: e.msg,
  593. theme: "round-button",
  594. confirmButtonColor: "#2DC7AA"
  595. })
  596. .then(() => {
  597. this.goBack()
  598. })
  599. }
  600. }
  601. }
  602. }
  603. </script>
  604. <style lang="less" scoped>
  605. @import url("./index.less");
  606. </style>