play.vue 13 KB

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