index.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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" 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="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.subjectName : ""
  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 }}
  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 } 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. getSecondRPM,
  274. onDayjs(time) {
  275. return dayjs(time).format("YYYY-MM-DD HH:mm")
  276. },
  277. goBack() {
  278. if (browser().isApp) {
  279. this.setStatusBarTextColor(false)
  280. postMessage({ api: "setBarStatus", content: { status: 1, backButtonHidden: 0 } })
  281. postMessage({
  282. api: "goBack"
  283. })
  284. } else {
  285. this.$router.back()
  286. }
  287. },
  288. // 下载
  289. onDownload() {
  290. postMessage({
  291. api: "saveFile",
  292. content: {
  293. url: this.state.musicDetail.videoUrl
  294. }
  295. })
  296. },
  297. // 删除作品
  298. async onDelete() {
  299. try {
  300. await api_userMusicRemove({ id: this.id })
  301. setTimeout(() => {
  302. this.state.deleteStatus = false
  303. this.$toast("删除成功")
  304. }, 100)
  305. setTimeout(() => {
  306. this.goBack()
  307. }, 1200)
  308. } catch {
  309. //
  310. }
  311. },
  312. async getStarList() {
  313. try {
  314. if (this.state.isClick) return
  315. this.state.isClick = true
  316. const res = await api_userMusicStarPage({
  317. userMusicId: this.id,
  318. ...this.state.params
  319. })
  320. this.state.listState.loading = false
  321. const result = res.data || {}
  322. // 处理重复请求数据
  323. if (this.state.list.length > 0 && result.current === 1) {
  324. return
  325. }
  326. this.state.list = this.state.list.concat(result.rows || [])
  327. this.state.listState.finished = result.current >= result.pages
  328. this.state.params.page = result.current + 1
  329. this.state.listState.dataShow = this.state.list.length > 0
  330. this.state.isClick = false
  331. } catch {
  332. this.state.listState.dataShow = false
  333. this.state.listState.finished = true
  334. this.state.isClick = false
  335. }
  336. },
  337. // 初始化 媒体播放
  338. initMediaPlay() {
  339. const { playStaff, pauseStaff, updateProgressStaff } = this.staffMoveInstance()
  340. const id = this.state.playType === "Audio" ? "#audioMediaSrc" : "#videoMediaSrc"
  341. this.state._plrl = new Plyr(id, {
  342. controls: ["play", "progress", "current-time", "duration"],
  343. fullscreen: {
  344. enabled: false,
  345. fallback: false
  346. }
  347. })
  348. const player = this.state._plrl
  349. // 创建音波数据
  350. if (this.state.playType === "Audio") {
  351. const audioDom = document.querySelector("#audioMediaSrc")
  352. const canvasDom = document.querySelector("#audioVisualizer")
  353. const { pauseVisualDraw, playVisualDraw } = audioVisualDraw(audioDom, canvasDom)
  354. player.on("play", () => {
  355. playVisualDraw()
  356. })
  357. player.on("pause", () => {
  358. pauseVisualDraw()
  359. })
  360. }
  361. player.on("timeupdate", () => {
  362. this.plyrState.currentTime = player.currentTime
  363. })
  364. player.on("play", () => {
  365. this.plyrState.playIngShow = false
  366. playStaff()
  367. })
  368. player.on("pause", () => {
  369. this.plyrState.playIngShow = true
  370. pauseStaff()
  371. })
  372. player.on("ended", () => {
  373. if (this.plyrState.mediaTimeShow) return
  374. player.currentTime = 0
  375. if (!player.playing) {
  376. setTimeout(() => {
  377. updateProgressStaff(player.currentTime)
  378. }, 100)
  379. }
  380. })
  381. // 处理按压事件
  382. const handleStart = () => {
  383. if (this.isLandscapeScreen.value) {
  384. return
  385. }
  386. this.plyrState.duration = player.duration
  387. this.plyrState.mediaTimeShow = true
  388. }
  389. // 处理松开事件
  390. const handleEnd = () => {
  391. this.plyrState.mediaTimeShow = false
  392. // 暂停的时候调用
  393. if (!player.playing) {
  394. updateProgressStaff(player.currentTime)
  395. }
  396. }
  397. const progressDom = document.querySelector("#playMediaSection .plyr__controls .plyr__progress__container")
  398. progressDom.addEventListener("mousedown", handleStart)
  399. progressDom.addEventListener("touchstart", handleStart)
  400. progressDom.addEventListener("mouseup", handleEnd)
  401. progressDom.addEventListener("touchend", handleEnd)
  402. },
  403. // 初始化五线谱
  404. initStaff() {
  405. const src = `${vaildMusicScoreUrl()}/gym-music-score/#/simple-detail?id=${this.state.musicDetail.musicSheetId}&musicRenderType=${
  406. this.staffState.musicRenderType
  407. }&part-index=${this.staffState.partIndex}`
  408. //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}`;
  409. this.staffState.staffSrc = src
  410. window.addEventListener("message", event => {
  411. const { api, height } = event.data
  412. if (api === "api_musicPage") {
  413. this.staffState.isShow = true
  414. this.staffState.height = height + "px"
  415. }
  416. })
  417. },
  418. staffMoveInstance() {
  419. let isPause = true
  420. const requestAnimationFrameFun = () => {
  421. requestAnimationFrame(() => {
  422. this.$refs.staffDom?.contentWindow?.postMessage(
  423. {
  424. api: "api_playProgress",
  425. content: {
  426. currentTime: this.state._plrl.currentTime * this.staffState.speedRate
  427. }
  428. },
  429. "*"
  430. )
  431. if (!isPause) {
  432. requestAnimationFrameFun()
  433. }
  434. })
  435. }
  436. const playStaff = () => {
  437. // 没渲染不执行
  438. if (!this.staffState.isShow) return
  439. isPause = false
  440. this.$refs.staffDom?.contentWindow?.postMessage(
  441. {
  442. api: "api_play"
  443. },
  444. "*"
  445. )
  446. requestAnimationFrameFun()
  447. }
  448. const pauseStaff = () => {
  449. // 没渲染不执行
  450. if (!this.staffState.isShow) return
  451. isPause = true
  452. this.$refs.staffDom?.contentWindow?.postMessage(
  453. {
  454. api: "api_paused"
  455. },
  456. "*"
  457. )
  458. }
  459. const updateProgressStaff = currentTime => {
  460. // 没渲染不执行
  461. if (!this.staffState.isShow) return
  462. this.$refs.staffDom?.contentWindow?.postMessage(
  463. {
  464. api: "api_updateProgress",
  465. content: {
  466. currentTime: currentTime * this.staffState.speedRate
  467. }
  468. },
  469. "*"
  470. )
  471. }
  472. return {
  473. playStaff,
  474. pauseStaff,
  475. updateProgressStaff
  476. }
  477. },
  478. //点击改变播放状态
  479. handlerClickPlay(event) {
  480. // 原生 播放暂停按钮 点击的时候 不触发
  481. if (event?.target?.matches("button.plyr__control")) {
  482. return
  483. }
  484. if (this.state._plrl.playing) {
  485. this.state._plrl.pause()
  486. } else {
  487. this.state._plrl.play()
  488. }
  489. },
  490. handleScroll() {
  491. const height = window.scrollY || document.documentElement.scrollTop
  492. // 防止多次调用
  493. if (height > 0 && this.isScreenScroll === false) {
  494. this.isScreenScroll = true
  495. this.setStatusBarTextColor(false)
  496. }
  497. if (height <= 0) {
  498. this.isScreenScroll = false
  499. this.setStatusBarTextColor(true)
  500. }
  501. },
  502. // 设置导航栏颜色
  503. setStatusBarTextColor(isWhite) {
  504. postMessage({
  505. api: "setStatusBarTextColor",
  506. content: { statusBarTextColor: isWhite }
  507. })
  508. },
  509. handleVisibilitychange() {
  510. if (document.hidden) {
  511. this.state._plrl?.pause()
  512. }
  513. },
  514. handlerBack(event) {
  515. event.stopPropagation()
  516. this.verticalScreen()
  517. },
  518. landscapeScreen() {
  519. postMessage({
  520. api: "setRequestedOrientation",
  521. content: {
  522. orientation: 0
  523. }
  524. })
  525. this.isLandscapeScreen = true
  526. },
  527. verticalScreen() {
  528. postMessage({
  529. api: "setRequestedOrientation",
  530. content: {
  531. orientation: 1
  532. }
  533. })
  534. this.isLandscapeScreen = false
  535. },
  536. handlerLandscapeScreen(event) {
  537. event.stopPropagation()
  538. if (!this.isLandscapeScreen) {
  539. this.landscapeScreen()
  540. }
  541. }
  542. },
  543. mounted() {
  544. this.state.heightA = this.$refs.playBoxDom.offsetHeight
  545. document.addEventListener("scroll", this.handleScroll)
  546. document.addEventListener("visibilitychange", this.handleVisibilitychange)
  547. },
  548. destroyed() {
  549. this.state._plrl?.destroy()
  550. document.removeEventListener("scroll", this.handleScroll)
  551. document.removeEventListener("visibilitychange", this.handleVisibilitychange)
  552. },
  553. async created() {
  554. this.setStatusBarTextColor(true)
  555. postMessage({ api: "setBarStatus", content: { status: 0, backButtonHidden: 1 } })
  556. postMessage({ api: "getNavHeight" }, res => {
  557. const { content } = res
  558. const dpi = content.dpi || 2
  559. if (content.navHeight) {
  560. const navHeight = content.navHeight / dpi
  561. this.navBarHeight = navHeight
  562. }
  563. })
  564. /* 初始化请求 */
  565. try {
  566. const res = await api_userMusicDetail(this.id)
  567. this.state.musicDetail = res.data || {}
  568. try {
  569. const jsonConfig = JSON.parse(res.data.jsonConfig)
  570. jsonConfig.speedRate && (this.staffState.speedRate = jsonConfig.speedRate)
  571. jsonConfig.musicRenderType && (this.staffState.musicRenderType = jsonConfig.musicRenderType)
  572. jsonConfig.partIndex && (this.staffState.partIndex = jsonConfig.partIndex)
  573. } catch {}
  574. // 五线谱
  575. this.initStaff()
  576. this.getStarList()
  577. // 判断是视频还是音频
  578. if (res.data.videoUrl.lastIndexOf("mp4") !== -1) {
  579. this.state.playType = "Video"
  580. } else {
  581. this.state.playType = "Audio"
  582. }
  583. this.$nextTick(() => {
  584. this.initMediaPlay()
  585. })
  586. } catch (e) {
  587. this.staffState.isShow = true
  588. if (e.code === 999) {
  589. this.$dialog
  590. .alert({
  591. message: e.msg,
  592. theme: "round-button",
  593. confirmButtonColor: "#2DC7AA"
  594. })
  595. .then(() => {
  596. this.goBack()
  597. })
  598. }
  599. }
  600. }
  601. }
  602. </script>
  603. <style lang="less" scoped>
  604. @import url("./index.less");
  605. </style>