play.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. <episode-buttons :episode="episode" @change="handleCollectAndFavChange" />
  12. <!--视频播放-->
  13. <view class="video-box main-center cross-center" :style="{zIndex: isPlaying ? 0 : 998}">
  14. <!--视频容器-->
  15. <swiper
  16. class="swiper"
  17. circular
  18. :vertical="true"
  19. :current="swiperCurrent"
  20. @change="handleSwiperChancge"
  21. >
  22. <swiper-item
  23. v-for="(item, index) in swiperEpisode"
  24. :key="index"
  25. >
  26. <!-- #ifdef MP-TOUTIAO | MP-WEIXIN-->
  27. <!-- 控制按钮 - 播放 -->
  28. <view v-if="!item.isPlaying" class="play-layer main-center cross-center" @tap="handlePlay(item)">
  29. <u-icon name="play-right-fill" size="100rpx" :color="$colors.defaultColor" />
  30. </view>
  31. <!-- 控制按钮 - 暂停 -->
  32. <view v-if="item.isPlaying" class="pause-layer" @tap="handlePause(item, true)" />
  33. <!-- #endif-->
  34. <video
  35. v-if="Object.keys(item).length"
  36. :id="`video${index}`"
  37. :poster="episode.cover_img"
  38. :src="item.url"
  39. :style="{
  40. width:'100%',
  41. height: 'calc(100vh - 130rpx)',
  42. zIndex: 0
  43. }"
  44. :show-play-btn="video.playBtn"
  45. :show-center-play-btn="video.playBtn"
  46. :show-fullscreen-btn="video.fullscreenBtn"
  47. :controls="video.controls"
  48. :show-progress="video.progress"
  49. object-fit="contain"
  50. @timeupdate="timeupdate"
  51. @ended="ended"
  52. />
  53. </swiper-item>
  54. </swiper>
  55. </view>
  56. <!--底部-->
  57. <episode-part
  58. :episode="episode"
  59. :is-playing="isPlaying"
  60. :buy-record="buyRecord"
  61. :current-episode="currentEpisode"
  62. @selectEpisode="handleSelectEpisode"
  63. />
  64. <!--充值-->
  65. <recharge
  66. :show.sync="rechargeShow"
  67. type="play"
  68. mode="bottom"
  69. :episode="episode"
  70. :list="currentEpisode"
  71. />
  72. </template>
  73. </view>
  74. </template>
  75. <script>
  76. import { mapState } from 'vuex'
  77. import Recharge from '../../components/Recharge/index'
  78. import EpisodeButtons from './components/EpisodeButtons'
  79. import EpisodePart from './components/EpisodePart'
  80. export default {
  81. name: 'Play',
  82. components: { EpisodePart, EpisodeButtons, Recharge },
  83. data() {
  84. return {
  85. id: null, // 短剧ID
  86. listId: null, // 剧集ID
  87. isPlaying: false, // 是否播放
  88. progress: 0, // 进度条
  89. episode: {}, // 短剧信息
  90. lists: [], // 剧集信息
  91. loading: false, // 数据加载
  92. video: { // 视频配置
  93. controls: true,
  94. progress: true,
  95. fullscreenBtn: false,
  96. playBtn: false
  97. },
  98. buyRecord: [], // 购买记录
  99. rechargeShow: false, // 显示充值
  100. swiperCurrent: 1, // 当前滚动
  101. currentEpisode: {}, // 当前播放剧集
  102. swiperEpisode: { // swiper 剧集
  103. prev: {},
  104. current: {},
  105. next: {}
  106. }
  107. }
  108. },
  109. computed: {
  110. ...mapState({
  111. userInfo: seate => seate.user.info
  112. }),
  113. videoContext() {
  114. const indexArr = ['prev', 'current', 'next']
  115. const swiperKey = indexArr[this.swiperCurrent]
  116. return uni.createVideoContext(`video${swiperKey}`, this)
  117. }
  118. },
  119. watch: {
  120. // 进度条
  121. progress(val) {
  122. if (val >= 100) {
  123. const indexArr = ['prev', 'current', 'next']
  124. const len = this.lists.length
  125. const swiperKey = indexArr[this.swiperCurrent]
  126. if (this.swiperEpisode[swiperKey].sort === this.lists[len - 1].sort) {
  127. this.$u.toast('已全部播放完成')
  128. return 100
  129. }
  130. // 切换
  131. switch (this.swiperCurrent) {
  132. case 0:
  133. this.swiperCurrent = 1
  134. break
  135. case 1:
  136. this.swiperCurrent = 2
  137. break
  138. case 2:
  139. this.swiperCurrent = 0
  140. break
  141. }
  142. this.$forceUpdate()
  143. }
  144. },
  145. swiperCurrent(val) {
  146. const indexArr = ['prev', 'current', 'next']
  147. const swiperKey = indexArr[val]
  148. const dataIndex = this.lists.findIndex(val => {
  149. return this.swiperEpisode[swiperKey].sort === val.sort
  150. })
  151. const len = this.lists.length
  152. let prevIndex, currentIndex, nextIndex
  153. switch (swiperKey) {
  154. case 'prev':
  155. currentIndex = dataIndex + 1 >= len ? 0 : dataIndex + 1
  156. nextIndex = dataIndex - 1 < 0 ? len - 1 : dataIndex - 1
  157. this.swiperEpisode.current = this.lists[currentIndex]
  158. this.swiperEpisode.next = this.lists[nextIndex]
  159. break
  160. case 'current':
  161. prevIndex = dataIndex - 1 < 0 ? len - 1 : dataIndex - 1
  162. nextIndex = dataIndex + 1 >= len ? 0 : dataIndex + 1
  163. this.swiperEpisode.prev = this.lists[prevIndex]
  164. this.swiperEpisode.next = this.lists[nextIndex]
  165. break
  166. case 'next':
  167. prevIndex = dataIndex + 1 >= len ? 0 : dataIndex + 1
  168. currentIndex = dataIndex - 1 < 0 ? len - 1 : dataIndex - 1
  169. this.swiperEpisode.current = this.lists[currentIndex]
  170. this.swiperEpisode.prev = this.lists[prevIndex]
  171. break
  172. }
  173. // 暂停其他
  174. this.handlePause(this.currentEpisode, false)
  175. // 当前播放剧集
  176. this.currentEpisode = this.swiperEpisode[swiperKey]
  177. // 播放
  178. this.handlePlay(this.currentEpisode)
  179. },
  180. currentEpisode(val) {
  181. console.log('-->当前剧集', val.sort)
  182. }
  183. },
  184. methods: {
  185. // 播放进度
  186. timeupdate({ detail }) {
  187. // currentTime, duration
  188. if (detail.duration) {
  189. this.progress = (detail.currentTime / detail.duration * 100).toFixed(2)
  190. }
  191. },
  192. ended() {
  193. // #ifdef MP-KUAISHOU
  194. this.progress = 100
  195. // #endif
  196. },
  197. // 播放
  198. handlePlay(item) {
  199. this.currentEpisode = item
  200. // 检查是否购买
  201. if (!this.checkBeforePlay(item)) {
  202. // 余额是否购买
  203. if (!this.checkOverage(item)) {
  204. this.rechargeShow = true
  205. return
  206. }
  207. // 余额足够 直接购买
  208. this.handleBuy()
  209. return
  210. }
  211. this.isPlaying = true
  212. item.isPlaying = true
  213. // #ifdef MP-KUAISHOU
  214. setTimeout(() => {
  215. this.videoContext.play()
  216. }, 1000)
  217. // #endif
  218. // #ifdef MP-TOUTIAO | MP-WEIXIN
  219. this.videoContext.play()
  220. // #endif
  221. this.progress = 0
  222. this.watched(this.id, this.currentEpisode.id)
  223. },
  224. // 暂停
  225. handlePause(item, isAll) {
  226. if (!this.isPlaying) return
  227. item.isPlaying = false
  228. this.isPlaying = false
  229. // 展厅其他的
  230. const indexArr = ['prev', 'current', 'next']
  231. const swiperKey = indexArr[this.swiperCurrent]
  232. indexArr.forEach(obj => {
  233. if (swiperKey !== obj || isAll) {
  234. const videoContext = uni.createVideoContext(`video${obj}`, this)
  235. videoContext.pause()
  236. }
  237. })
  238. },
  239. // 选择剧集
  240. handleSelectEpisode(index) {
  241. // 暂停上一个
  242. this.handlePause(this.currentEpisode, true)
  243. const item = this.lists[index]
  244. // 重置SwiperEpisode数据 切换播放
  245. this.swiperCurrent = 1
  246. this.initSwiperEpisode(item.id)
  247. },
  248. // 当前剧集购买记录
  249. async getBuyRecord() {
  250. await this.$api.user.episode.buyRecord(this.id).then(res => {
  251. this.buyRecord = res.data
  252. })
  253. },
  254. // 购买剧集
  255. async handleBuy() {
  256. await this.$api.user.episode.buyHandle(this.id, this.currentEpisode.id).then(async res => {
  257. this.$hideLoading()
  258. if (typeof res.overage !== 'undefined') {
  259. this.rechargeShow = true
  260. } else {
  261. this.$u.toast('购买成功')
  262. await this.getBuyRecord()
  263. this.handlePlay(this.currentEpisode)
  264. this.$api.user.info().then(res => {
  265. this.$store.dispatch('user/info', res.data)
  266. })
  267. }
  268. }).catch(() => {
  269. this.$hideLoading()
  270. })
  271. },
  272. // 滚动 Swiper
  273. handleSwiperChancge({ detail }) {
  274. this.swiperCurrent = detail.current
  275. },
  276. // 播放前检查剧集是否购买/免费
  277. checkBeforePlay(item) {
  278. // 剧集免费 不免费已购买 VIP观看是VIP
  279. if (item.is_free) {
  280. return true
  281. }
  282. if (!item.is_free && this.buyRecord.indexOf(item.id) !== -1) {
  283. return true
  284. }
  285. if (this.episode.is_vip_watch && this.userInfo.info.is_vip) {
  286. return true
  287. }
  288. return false
  289. },
  290. // 检查余额是否足够支付
  291. checkOverage(item) {
  292. return this.userInfo.info.integral >= item.sale_price
  293. },
  294. // 记录观看记录
  295. watched(id, list_id) {
  296. this.$api.user.episode.watched(id, list_id).then(res => {
  297. })
  298. },
  299. // 分享
  300. handleShared(id, list_id) {
  301. this.$api.episode.shared(id, list_id).then(res => {
  302. this.episode.share_count += 1
  303. })
  304. },
  305. handleCollectAndFavChange(data) {
  306. if (data.type === 'collect') {
  307. this.episode.user_collect_count += data.num
  308. } else {
  309. this.episode.user_favorite_count += data.num
  310. }
  311. },
  312. // // 初始化 Swiper 剧集
  313. initSwiperEpisode(listId) {
  314. let currentIndex = 0
  315. if (listId) {
  316. currentIndex = this.lists.findIndex(obj => {
  317. return parseInt(listId) === parseInt(obj.id)
  318. })
  319. }
  320. let prevIndex = currentIndex - 1
  321. let nextIndex = currentIndex + 1
  322. const len = this.lists.length
  323. if (parseInt(listId) === 0 || prevIndex < 0) {
  324. prevIndex = len - 1
  325. }
  326. //
  327. if (nextIndex >= len) {
  328. nextIndex = 0
  329. }
  330. this.swiperEpisode = {
  331. prev: this.lists[prevIndex],
  332. current: this.lists[currentIndex],
  333. next: this.lists[nextIndex]
  334. }
  335. console.log('-->swiper data', JSON.stringify(this.swiperEpisode))
  336. this.currentEpisode = this.lists[currentIndex]
  337. this.$nextTick(() => {
  338. this.handlePlay(this.currentEpisode)
  339. })
  340. },
  341. // 获取剧集详情
  342. getEpisode() {
  343. this.loading = true
  344. this.$api.episode.detail(this.id).then(res => {
  345. this.loading = false
  346. this.episode = res.data
  347. uni.setNavigationBarTitle({
  348. title: this.episode.name + (this.episode.status === 0 ? ' | 更新中' : '已完结')
  349. })
  350. this.episode.lists.forEach((obj, index) => {
  351. obj.isPlaying = false
  352. obj.index = index
  353. obj.progress = 0
  354. })
  355. this.lists = this.episode.lists
  356. // 初始化 Swiper 剧集
  357. this.initSwiperEpisode(this.listId)
  358. })
  359. }
  360. },
  361. async onLoad(options) {
  362. this.id = options.id
  363. this.listId = options?.list_id
  364. this.listId = this.listId ? this.listId : 0
  365. await this.getBuyRecord()
  366. this.getEpisode()
  367. },
  368. // 分享
  369. onShareAppMessage(res) {
  370. if (res.from === 'button') { // 来自页面内分享按钮
  371. console.log(res.target)
  372. }
  373. let options = {
  374. title: '',
  375. path: `/pages/episode/play?id=${this.id}`
  376. }
  377. if (this.episode) {
  378. options = {
  379. title: this.episode.name,
  380. path: `/pages/episode/play?id=${this.id}`,
  381. imageUrl: this.episode.cover_img,
  382. desc: this.episode.name,
  383. success: res => {
  384. this.handleShared(this.id, this.currentEpisode.id)
  385. }
  386. }
  387. }
  388. return options
  389. }
  390. }
  391. </script>
  392. <style lang="scss" scoped>
  393. .play-container {
  394. font-size: 28rpx;
  395. .video-box{
  396. position: fixed;
  397. top: 0;
  398. left: 0;
  399. right: 0;
  400. bottom: 0;
  401. .play-layer{
  402. position: fixed;
  403. top: 0;
  404. left: 0;
  405. // #ifdef MP-KUAISHOU | MP-WEIXIN
  406. bottom: 240rpx;
  407. // #endif
  408. // #ifdef MP-TOUTIAO
  409. bottom: 360rpx;
  410. // #endif
  411. right: 0;
  412. background: transparent;
  413. z-index: 999;
  414. }
  415. .pause-layer{
  416. position: absolute;
  417. top: 0;
  418. left: 0;
  419. // #ifdef MP-KUAISHOU | MP-WEIXIN
  420. bottom: 240rpx;
  421. // #endif
  422. // #ifdef MP-TOUTIAO
  423. bottom: 360rpx;
  424. // #endif
  425. right: 0;
  426. background: transparent;
  427. z-index: 99;
  428. }
  429. .swiper {
  430. width: 100%;
  431. height: 100vh;
  432. position: relative;
  433. z-index: 99;
  434. .swiper-item {
  435. width: 100%;
  436. height: calc(100vh - #{150rpx});
  437. }
  438. }
  439. .progress-container{
  440. width: 93vw;
  441. background: #fff;
  442. height: 10rpx;
  443. position: fixed;
  444. z-index: 100;
  445. bottom: 160rpx;
  446. .progress{
  447. height: 10rpx;
  448. background: linear-gradient(270deg, #6EEBE8 0%, #FF74B9 100%);
  449. }
  450. }
  451. }
  452. }
  453. </style>