play.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. <template>
  2. <view class="play-container">
  3. <u-loading-page
  4. :loading="loading"
  5. :bg-color="$colors.bgColor"
  6. :color="$colors.primaryColor"
  7. :loading-color="$colors.primaryColor"
  8. />
  9. <template v-if="!loading">
  10. <!--播放数据-->
  11. <view class="view-num main-left cross-center">
  12. <u-icon name="eye-fill" :color="isCollect?$colors.primaryColor:$colors.defaultColor" size="26rpx" />
  13. <text>{{ episode.user_watch_record_count }}</text>
  14. </view>
  15. <!--按钮-->
  16. <view class="status-bar dir-top-wrap main-center">
  17. <!--喜欢-->
  18. <view
  19. class="item fav dir-top-wrap main-center cross-center"
  20. :class="{active: isFav}"
  21. @click="handleFavorite"
  22. >
  23. <u-icon name="heart-fill" size="58rpx" :color="isFav?$colors.primaryColor:$colors.defaultColor" />
  24. <text>{{ episode.user_favorite_count }}</text>
  25. </view>
  26. <!--收藏-->
  27. <view
  28. class="item collect dir-top-wrap main-center cross-center"
  29. :class="{active: isCollect}"
  30. @click="handleCollect"
  31. >
  32. <u-icon name="star-fill" size="58rpx" :color="isCollect?$colors.primaryColor:$colors.defaultColor" />
  33. <text>{{ episode.user_collect_count }}</text>
  34. </view>
  35. <!--分享-->
  36. <view class="item share dir-top-wrap main-center cross-center">
  37. <button open-type="share" />
  38. <u-icon name="share-fill" size="58rpx" :color="$colors.defaultColor" />
  39. <text>{{ episode.sahre_count }}</text>
  40. </view>
  41. </view>
  42. <!--视频播放-->
  43. <view class="video-box main-center cross-center" :style="{zIndex: isPlaying?0:998}">
  44. <!--进度条-->
  45. <!-- <view v-if="isPlaying" class="progress-container">-->
  46. <!-- <view class="progress" :style="{width: progress+'%'}" />-->
  47. <!-- </view>-->
  48. <!--视频容器-->
  49. <swiper
  50. class="swiper"
  51. circular
  52. :vertical="true"
  53. :current="swiperCurrent"
  54. @change="handleSwiperChancge"
  55. >
  56. <swiper-item
  57. v-for="(item,index) in episode.lists"
  58. :key="index"
  59. >
  60. <!-- #ifdef MP-TOUTIAO | MP-WEIXIN-->
  61. <!-- 控制按钮 - 播放 -->
  62. <view v-if="!item.isPlaying" class="play-layer main-center cross-center" @tap="handlePlay(item)">
  63. <u-icon name="play-right-fill" size="100rpx" :color="$colors.defaultColor" />
  64. </view>
  65. <!-- 控制按钮 - 暂停 -->
  66. <view v-if="item.isPlaying" class="pause-layer" @tap="handlePause(item)" />
  67. <!-- #endif-->
  68. <video
  69. :id="`video${index}`"
  70. :poster="episode.cover_img"
  71. :src="item.url"
  72. style="width: 100%;height: 100%;"
  73. :show-play-btn="video.playBtn"
  74. :show-fullscreen-btn="video.fullscreenBtn"
  75. :controls="video.controls"
  76. :show-progress="video.progress"
  77. object-fit="contain"
  78. @timeupdate="timeupdate"
  79. />
  80. </swiper-item>
  81. </swiper>
  82. <!-- <video-->
  83. <!-- id="video"-->
  84. <!-- :show-play-btn="video.playBtn"-->
  85. <!-- :show-fullscreen-btn="video.fullscreenBtn"-->
  86. <!-- :controls="video.controls"-->
  87. <!-- object-fit="contain"-->
  88. <!-- style="width: 100%;height: 100%;"-->
  89. <!-- :poster="episode.cover_img"-->
  90. <!-- :src="src"-->
  91. <!-- @timeupdate="timeupdate"-->
  92. <!-- />-->
  93. </view>
  94. <!--底部-->
  95. <view class="footer" :class="{episode: footerShow}">
  96. <view class="bar main-between cross-center" @click="footerShow = !footerShow">
  97. <view class="icon" />
  98. <view class="name">
  99. <u-text :text="episode.name" :color="$colors.infoColor" :lines="1" />
  100. <!-- <u-text text="321313" :color="$colors.infoColor" :lines="1" />-->
  101. </view>
  102. <view class="arrow">
  103. <u-icon name="arrow-up" :color="$colors.infoColor" size="32rpx" />
  104. </view>
  105. </view>
  106. <view class="episode-container dir-top-wrap">
  107. <!--分集 横向滚动-->
  108. <scroll-view
  109. class="header-box dir-left-nowrap cross-center"
  110. scroll-x
  111. scroll-with-animation
  112. >
  113. <view
  114. v-for="(item,index) in episodes"
  115. :key="index"
  116. class="header-item"
  117. :class="{active: episodesIndex === index}"
  118. @click="episodesIndex=index"
  119. >{{ item.title }}</view>
  120. </scroll-view>
  121. <!--几集选择-->
  122. <view class="content dir-left-wrap main-left">
  123. <view
  124. v-for="(item, index) in episodes[episodesIndex].lists"
  125. :key="index"
  126. class="episode-item"
  127. @click="handleSelectEpisode(item.index)"
  128. >
  129. <image :src="episode.cover_img" />
  130. <text>第{{ item.sort }}集</text>
  131. <view v-if="currentEpisode.index === item.index && isPlaying" class="playing" />
  132. <view v-if="!item.is_free && buyRecord.indexOf(item.id) === -1 && !(episode.is_vip_watch && userInfo.info.is_vip)" class="lock main-center cross-center">
  133. <u-icon name="lock-fill" :color="$colors.defaultColor" size="46rpx" />
  134. </view>
  135. </view>
  136. </view>
  137. </view>
  138. </view>
  139. <!--toast-->
  140. <view
  141. v-if="toast.show"
  142. class="toast dir-top-wrap main-center cross-center"
  143. :class="toast.status"
  144. >
  145. <u-icon
  146. :name="toast.status === 'success' ? 'checkmark-circle' : 'close-circle'"
  147. :color="toast.status === 'success' ? $colors.primaryColor : $colors.defaultColor"
  148. size="80rpx"
  149. />
  150. <text>{{ toast.text }}</text>
  151. </view>
  152. <!--购买弹窗-->
  153. <u-modal
  154. :show="modal.show"
  155. :content="`确定购买【第${currentEpisode.sort}集】?`"
  156. show-cancel-button
  157. @confirm="handleBuy"
  158. @cancel="modal.show = false"
  159. />
  160. <!--充值-->
  161. <recharge
  162. :show.sync="rechargeShow"
  163. type="play"
  164. mode="bottom"
  165. :episode="episode"
  166. :list="currentEpisode"
  167. />
  168. </template>
  169. </view>
  170. </template>
  171. <script>
  172. import { mapState } from 'vuex'
  173. import Recharge from '../../components/Recharge/index'
  174. export default {
  175. name: 'Play',
  176. components: { Recharge },
  177. data() {
  178. return {
  179. id: null,
  180. listId: null,
  181. isPlaying: false,
  182. progress: 0,
  183. episode: null,
  184. loading: true,
  185. isCollect: false,
  186. isFav: false,
  187. video: {
  188. controls: true,
  189. // #ifdef MP-KUAISHOU | MP-TOUTIAO
  190. progress: false,
  191. // #endif
  192. // #ifdef MP-TOUTIAO | MP-WEIXIN
  193. progress: true,
  194. // #endif
  195. fullscreenBtn: false,
  196. playBtn: false
  197. },
  198. toast: {
  199. status: 'success',
  200. text: '收藏成功',
  201. show: false
  202. },
  203. footerShow: false,
  204. episodesIndex: 0,
  205. buyRecord: [],
  206. modal: {
  207. show: false
  208. },
  209. rechargeShow: false,
  210. swiperCurrent: 0,
  211. currentEpisode: null
  212. }
  213. },
  214. computed: {
  215. ...mapState({
  216. userInfo: seate => seate.user.info
  217. }),
  218. src() {
  219. if (!this.episode) return ''
  220. return this.currentEpisode.url
  221. },
  222. episodes() {
  223. const list = []
  224. if (this.episode) {
  225. let temp = []
  226. this.episode.lists.forEach((obj, index) => {
  227. temp.push(obj)
  228. if (temp.length === 6 || index === (this.episode.lists.length - 1)) {
  229. const start = list.length ? (list.length * 6) + 1 : 1
  230. const end = (start - 1) + temp.length
  231. list.push({ title: `${start}集-${end}集`, lists: temp })
  232. temp = []
  233. }
  234. })
  235. }
  236. return list
  237. },
  238. videoContext() {
  239. return uni.createVideoContext(`video${this.swiperCurrent}`, this)
  240. }
  241. },
  242. watch: {
  243. 'toast.show'(val) {
  244. if (val) {
  245. setTimeout(() => {
  246. this.toast.show = false
  247. }, 1000)
  248. }
  249. },
  250. progress(val) {
  251. if (val >= 100) {
  252. if ((this.swiperCurrent + 1) === this.episode.lists.length) {
  253. this.$u.toast('已全部播放完成')
  254. return 100
  255. }
  256. this.swiperCurrent += 1
  257. this.currentEpisode = this.episode.lists[this.swiperCurrent]
  258. this.$forceUpdate()
  259. this.$loading('下一集加载中...')
  260. setTimeout(() => {
  261. this.$hideLoading()
  262. this.handlePlay(this.currentEpisode)
  263. }, 1000)
  264. }
  265. }
  266. },
  267. methods: {
  268. // 播放进度
  269. timeupdate({ detail }) {
  270. // currentTime, duration
  271. if (detail.duration) {
  272. this.progress = (detail.currentTime / detail.duration * 100).toFixed(2)
  273. }
  274. },
  275. // 播放
  276. handlePlay(item) {
  277. this.currentEpisode = item
  278. // 检查是否购买
  279. if (!this.checkBeforePlay(item)) {
  280. // 余额是否购买
  281. if (!this.checkOverage(item)) {
  282. this.rechargeShow = true
  283. return
  284. }
  285. // 余额足够 直接购买
  286. this.handleBuy()
  287. return
  288. }
  289. console.log('-->data', item)
  290. item.isPlaying = true
  291. this.isPlaying = true
  292. this.videoContext.play()
  293. this.watched(this.id, this.currentEpisode.id)
  294. },
  295. // 暂停
  296. handlePause(item) {
  297. if (!this.isPlaying) return
  298. item.isPlaying = false
  299. this.isPlaying = false
  300. this.videoContext.pause()
  301. },
  302. // 选择剧集
  303. handleSelectEpisode(index) {
  304. // 暂停上一个
  305. this.handlePause(this.currentEpisode)
  306. // 切换播放
  307. this.swiperCurrent = index
  308. const item = this.episode.lists[index]
  309. this.currentEpisode = item
  310. // 检查是否购买
  311. if (!this.checkBeforePlay(item)) {
  312. // 余额是否购买
  313. if (!this.checkOverage(item)) {
  314. this.rechargeShow = true
  315. return
  316. }
  317. // 余额足够 直接购买
  318. this.handleBuy(index)
  319. // this.modal.show = true
  320. return
  321. }
  322. this.footerShow = false
  323. this.$loading()
  324. setTimeout(() => {
  325. this.$hideLoading()
  326. this.handlePlay(this.currentEpisode)
  327. this.watched(this.id, this.currentEpisode.id)
  328. }, 1000)
  329. },
  330. // 当前剧集购买记录
  331. async getBuyRecord() {
  332. await this.$api.user.episode.buyRecord(this.id).then(res => {
  333. this.buyRecord = res.data
  334. })
  335. },
  336. // 购买剧集
  337. async handleBuy() {
  338. // this.$loading('购买中...')
  339. await this.$api.user.episode.buyHandle(this.id, this.currentEpisode.id).then(async res => {
  340. this.$hideLoading()
  341. if (typeof res.overage !== 'undefined') {
  342. this.rechargeShow = true
  343. } else {
  344. this.$u.toast('购买成功')
  345. this.modal.show = false
  346. await this.getBuyRecord()
  347. this.handlePlay(this.currentEpisode)
  348. this.footerShow = false
  349. this.$api.user.info().then(res => {
  350. this.$store.dispatch('user/info', res.data)
  351. })
  352. }
  353. }).catch(() => {
  354. this.$hideLoading()
  355. })
  356. },
  357. handleSwiperChancge({ detail }) {
  358. // 暂停上一个
  359. this.handlePause(this.currentEpisode)
  360. console.log('-->data', detail)
  361. this.swiperCurrent = detail.current
  362. this.currentEpisode = this.episode.lists[this.swiperCurrent]
  363. // 播放
  364. this.handlePlay(this.currentEpisode)
  365. },
  366. // 播放前检查剧集是否购买/免费
  367. checkBeforePlay(item) {
  368. // 剧集免费 不免费已购买 VIP观看是VIP
  369. if (item.is_free) {
  370. return true
  371. }
  372. if (!item.is_free && this.buyRecord.indexOf(item.id) !== -1) {
  373. return true
  374. }
  375. if (this.episode.is_vip_watch && this.userInfo.info.is_vip) {
  376. return true
  377. }
  378. return false
  379. },
  380. // 检查余额是否足够支付
  381. checkOverage(item) {
  382. return this.userInfo.info.integral >= item.sale_price
  383. },
  384. // 检查是否收藏当前剧集
  385. checkCollect() {
  386. this.$api.user.collect.check(this.id).then(res => {
  387. this.isCollect = res.data
  388. })
  389. },
  390. // 收藏相关 处理
  391. handleCollect() {
  392. const method = this.isCollect ? 'destroy' : 'add'
  393. const num = this.isCollect ? -1 : 1
  394. this.$api.user.collect[method](this.id).then(res => {
  395. if (res.data) {
  396. this.toast.show = true
  397. this.toast.status = this.isCollect ? 'cancel' : 'success'
  398. this.toast.text = this.isCollect ? '取消收藏' : '收藏成功'
  399. this.episode.user_collect_count += num
  400. this.isCollect = !this.isCollect
  401. }
  402. })
  403. },
  404. // 检查是否喜欢当前短剧
  405. checkFavorite() {
  406. this.$api.user.favorite.check(this.id).then(res => {
  407. this.isFav = res.data
  408. })
  409. },
  410. // 处理 喜欢剧集
  411. handleFavorite() {
  412. const method = this.isFav ? 'destroy' : 'add'
  413. const num = this.isFav ? -1 : 1
  414. this.$api.user.favorite[method](this.id).then(res => {
  415. if (res.data) {
  416. this.toast.show = true
  417. this.toast.status = this.isFav ? 'cancel' : 'success'
  418. this.toast.text = this.isFav ? '取消喜欢' : '喜欢成功'
  419. this.episode.user_favorite_count += num
  420. this.isFav = !this.isFav
  421. }
  422. })
  423. },
  424. // 记录观看记录
  425. watched(id, list_id) {
  426. this.$api.user.episode.watched(id, list_id).then(res => {
  427. })
  428. },
  429. // 分享
  430. shared(id, list_id) {
  431. this.$api.episode.shared(id, list_id).then(res => {
  432. this.episode.share_count += 1
  433. })
  434. },
  435. // 获取剧集详情
  436. getEpisode() {
  437. this.loading = true
  438. this.$api.episode.detail(this.id).then(res => {
  439. this.loading = false
  440. this.episode = res.data
  441. this.episode.lists.forEach((obj, index) => {
  442. obj.isPlaying = false
  443. obj.index = index
  444. })
  445. if (this.listId) {
  446. this.episode.lists.forEach((obj, index) => {
  447. if (parseInt(this.listId) === parseInt(obj.id)) {
  448. this.currentEpisode = obj
  449. this.swiperCurrent = index
  450. }
  451. })
  452. } else {
  453. this.currentEpisode = this.episode.lists[0]
  454. }
  455. this.$nextTick(() => {
  456. this.handlePlay(this.currentEpisode)
  457. })
  458. })
  459. }
  460. },
  461. async onLoad(options) {
  462. this.id = options.id
  463. this.listId = options?.list_id
  464. await this.getBuyRecord()
  465. this.getEpisode()
  466. this.checkCollect()
  467. this.checkFavorite()
  468. },
  469. // 分享
  470. onShareAppMessage(res) {
  471. if (res.from === 'button') { // 来自页面内分享按钮
  472. console.log(res.target)
  473. }
  474. let options = {
  475. title: '',
  476. path: `/pages/episode/play?id=${this.id}`
  477. }
  478. if (this.episode) {
  479. options = {
  480. title: this.episode.name,
  481. path: `/pages/episode/play?id=${this.id}`,
  482. imageUrl: this.episode.cover_img,
  483. desc: this.episode.name,
  484. success: res => {
  485. this.shared(this.id, this.currentEpisode.id)
  486. }
  487. }
  488. }
  489. return options
  490. }
  491. }
  492. </script>
  493. <style lang="scss" scoped>
  494. .play-container {
  495. font-size: 28rpx;
  496. .video-box{
  497. position: fixed;
  498. top: 0;
  499. left: 0;
  500. right: 0;
  501. bottom: 0;
  502. .play-layer{
  503. position: fixed;
  504. top: 0;
  505. left: 0;
  506. bottom: 0;
  507. right: 0;
  508. background: transparent;
  509. z-index: 999;
  510. }
  511. .pause-layer{
  512. position: absolute;
  513. top: 0;
  514. left: 0;
  515. bottom: 0;
  516. right: 0;
  517. background: transparent;
  518. z-index: 99;
  519. }
  520. .swiper {
  521. width: 100%;
  522. height: 100vh;
  523. position: relative;
  524. z-index: 99;
  525. .swiper-item {
  526. width: 100%;
  527. height: 100%;
  528. }
  529. }
  530. .progress-container{
  531. width: 93vw;
  532. background: #fff;
  533. height: 10rpx;
  534. position: fixed;
  535. z-index: 100;
  536. bottom: 160rpx;
  537. .progress{
  538. height: 10rpx;
  539. background: linear-gradient(270deg, #6EEBE8 0%, #FF74B9 100%);
  540. }
  541. }
  542. }
  543. .view-num{
  544. position: fixed;
  545. right: 20rpx;
  546. top: 40rpx;
  547. color: #fff;
  548. font-size: 26rpx;
  549. z-index: 100;
  550. text{
  551. margin-left: 10rpx;
  552. }
  553. }
  554. .status-bar{
  555. position: fixed;
  556. bottom: 300rpx;
  557. right: 40rpx;
  558. color: $default-color;
  559. z-index: 999;
  560. .item{
  561. margin-bottom: 40rpx;
  562. &.share{
  563. position: relative;
  564. button{
  565. position: absolute;
  566. background: transparent;
  567. top: 0;
  568. left: 0;
  569. right: 0;
  570. bottom: 0;
  571. z-index: 1;
  572. &:after{
  573. content: unset;
  574. }
  575. }
  576. }
  577. &.active{
  578. color: $primary-color;
  579. }
  580. text{
  581. margin-top: 10rpx;
  582. }
  583. }
  584. }
  585. .footer{
  586. position: fixed;
  587. width: 95vw;
  588. height: 76rpx;
  589. background: rgba(24, 28, 47, 0.8);
  590. // #ifdef MP-KUAISHOU
  591. bottom: 150rpx;
  592. // #endif
  593. // #ifdef MP-TOUTIAO | MP-WEIXIN
  594. bottom: 100rpx;
  595. // #endif
  596. left: 50%;
  597. transform: translateX(-50%);
  598. font-size: 26rpx;
  599. color: $info-color;
  600. border-radius: 20rpx;
  601. z-index: 999;
  602. transition: .3s;
  603. &.episode{
  604. bottom: 700rpx;
  605. margin-top: -4rpx;
  606. border-bottom-left-radius: 0;
  607. border-bottom-right-radius: 0;
  608. .episode-container{
  609. display: flex;
  610. margin-top: -4rpx;
  611. border-bottom-left-radius: 20px;
  612. border-bottom-right-radius: 20px;
  613. }
  614. .bar{
  615. .arrow{
  616. transform: rotate(180deg);
  617. }
  618. }
  619. }
  620. .bar{
  621. padding: 0 20rpx;
  622. .icon{
  623. background: url("/static/image/video.png") no-repeat center;
  624. background-size: 70%;
  625. width: 80rpx;
  626. height: 80rpx;
  627. }
  628. .name{
  629. text-align: left;
  630. flex: 1;
  631. padding: 0 30rpx;
  632. }
  633. .arrow{
  634. transition: .3s;
  635. }
  636. }
  637. .episode-container{
  638. display: none;
  639. background: inherit;
  640. min-height: 620rpx;
  641. .header-box{
  642. white-space: nowrap;
  643. margin: 20rpx 0;
  644. .header-item{
  645. margin-right: 20rpx;
  646. border-radius: 20rpx;
  647. display: inline-block;
  648. width: 200rpx;
  649. border: 1rpx solid $default-color;
  650. text-align: center;
  651. padding: 10rpx 0;
  652. color: $default-color;
  653. &.active{
  654. border-color: $primary-color;
  655. color: $primary-color;
  656. }
  657. }
  658. }
  659. .content{
  660. margin-top: 20rpx;
  661. .episode-item{
  662. position: relative;
  663. width: calc((100% - #{40rpx}) / 3);
  664. margin-right: 20rpx;
  665. margin-bottom: 20rpx;
  666. overflow: hidden;
  667. border-radius: 18rpx;
  668. &:nth-child(3n){
  669. margin-right: 0;
  670. }
  671. .playing{
  672. position: absolute;
  673. top: 0;
  674. left: 0;
  675. bottom: 0;
  676. right: 0;
  677. background: rgba(0,0,0,.5) url("/static/image/playing.png") no-repeat center;
  678. background-size: 40rpx;
  679. z-index: 2;
  680. }
  681. image{
  682. width: 100%;
  683. height: 260rpx;
  684. }
  685. text{
  686. position: absolute;
  687. left: 0;
  688. bottom: 0;
  689. right: 0;
  690. color: $default-color;
  691. padding: 20rpx 0;
  692. text-align: center;
  693. background: rgba(0,0,0,.3);
  694. z-index: 1;
  695. }
  696. .lock{
  697. position: absolute;
  698. top: 0;
  699. left: 0;
  700. bottom: 0;
  701. right: 0;
  702. background: rgba(0,0,0,.5);
  703. z-index: 2;
  704. }
  705. }
  706. }
  707. }
  708. }
  709. .toast{
  710. position: fixed;
  711. width: 60vw;
  712. background: rgba(0,0,0,.5);
  713. height: 300rpx;
  714. top: 50%;
  715. left: 50%;
  716. transform: translate(-50%,-50%);
  717. border-radius: 20rpx;
  718. font-size: 36rpx;
  719. color: $default-color;
  720. &.success{
  721. color: $primary-color;
  722. }
  723. text{
  724. margin-top: 20rpx;
  725. }
  726. }
  727. }
  728. </style>