play.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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 len = this.lists.length
  123. const indexArr = ['prev', 'current', 'next']
  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 = 3
  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.watched(this.id, this.currentEpisode.id)
  216. },
  217. // 暂停
  218. handlePause(item, isAll) {
  219. if (!this.isPlaying) return
  220. item.isPlaying = false
  221. this.isPlaying = false
  222. // 展厅其他的
  223. const indexArr = ['prev', 'current', 'next']
  224. const swiperKey = indexArr[this.swiperCurrent]
  225. indexArr.forEach(obj => {
  226. if (swiperKey !== obj || isAll) {
  227. const videoContext = uni.createVideoContext(`video${obj}`, this)
  228. videoContext.pause()
  229. }
  230. })
  231. },
  232. // 选择剧集
  233. handleSelectEpisode(index) {
  234. // 暂停上一个
  235. this.handlePause(this.currentEpisode, true)
  236. const item = this.lists[index]
  237. // 重置SwiperEpisode数据 切换播放
  238. this.swiperCurrent = 1
  239. this.initSwiperEpisode(item.id)
  240. },
  241. // 当前剧集购买记录
  242. async getBuyRecord() {
  243. await this.$api.user.episode.buyRecord(this.id).then(res => {
  244. this.buyRecord = res.data
  245. })
  246. },
  247. // 购买剧集
  248. async handleBuy() {
  249. await this.$api.user.episode.buyHandle(this.id, this.currentEpisode.id).then(async res => {
  250. this.$hideLoading()
  251. if (typeof res.overage !== 'undefined') {
  252. this.rechargeShow = true
  253. } else {
  254. this.$u.toast('购买成功')
  255. await this.getBuyRecord()
  256. this.handlePlay(this.currentEpisode)
  257. this.$api.user.info().then(res => {
  258. this.$store.dispatch('user/info', res.data)
  259. })
  260. }
  261. }).catch(() => {
  262. this.$hideLoading()
  263. })
  264. },
  265. // 滚动 Swiper
  266. handleSwiperChancge({ detail }) {
  267. this.swiperCurrent = detail.current
  268. },
  269. // 播放前检查剧集是否购买/免费
  270. checkBeforePlay(item) {
  271. // 剧集免费 不免费已购买 VIP观看是VIP
  272. if (item.is_free) {
  273. return true
  274. }
  275. if (!item.is_free && this.buyRecord.indexOf(item.id) !== -1) {
  276. return true
  277. }
  278. if (this.episode.is_vip_watch && this.userInfo.info.is_vip) {
  279. return true
  280. }
  281. return false
  282. },
  283. // 检查余额是否足够支付
  284. checkOverage(item) {
  285. return this.userInfo.info.integral >= item.sale_price
  286. },
  287. // 记录观看记录
  288. watched(id, list_id) {
  289. this.$api.user.episode.watched(id, list_id).then(res => {
  290. })
  291. },
  292. // 分享
  293. handleShared(id, list_id) {
  294. this.$api.episode.shared(id, list_id).then(res => {
  295. this.episode.share_count += 1
  296. })
  297. },
  298. handleCollectAndFavChange(data) {
  299. if (data.type === 'collect') {
  300. this.episode.user_collect_count += data.num
  301. } else {
  302. this.episode.user_favorite_count += data.num
  303. }
  304. },
  305. // // 初始化 Swiper 剧集
  306. initSwiperEpisode(listId) {
  307. let currentIndex = 0
  308. if (listId) {
  309. currentIndex = this.lists.findIndex(obj => {
  310. return parseInt(listId) === parseInt(obj.id)
  311. })
  312. }
  313. let prevIndex = currentIndex - 1
  314. let nextIndex = currentIndex + 1
  315. const len = this.lists.length
  316. if (parseInt(listId) === 0 || prevIndex < 0) {
  317. prevIndex = len - 1
  318. }
  319. //
  320. if (nextIndex >= len) {
  321. nextIndex = 0
  322. }
  323. this.swiperEpisode = {
  324. prev: this.lists[prevIndex],
  325. current: this.lists[currentIndex],
  326. next: this.lists[nextIndex]
  327. }
  328. console.log('-->swiper data', JSON.stringify(this.swiperEpisode))
  329. this.currentEpisode = this.lists[currentIndex]
  330. this.$nextTick(() => {
  331. this.handlePlay(this.currentEpisode)
  332. })
  333. },
  334. // 获取剧集详情
  335. getEpisode() {
  336. this.loading = true
  337. this.$api.episode.detail(this.id).then(res => {
  338. this.loading = false
  339. this.episode = res.data
  340. uni.setNavigationBarTitle({
  341. title: this.episode.name + (this.episode.status === 0 ? ' | 更新中' : '已完结')
  342. })
  343. this.episode.lists.forEach((obj, index) => {
  344. obj.isPlaying = false
  345. obj.index = index
  346. obj.progress = 0
  347. })
  348. this.lists = this.episode.lists
  349. // 初始化 Swiper 剧集
  350. this.initSwiperEpisode(this.listId)
  351. })
  352. }
  353. },
  354. async onLoad(options) {
  355. this.id = options.id
  356. this.listId = options?.list_id
  357. this.listId = this.listId ? this.listId : 0
  358. await this.getBuyRecord()
  359. this.getEpisode()
  360. },
  361. // 分享
  362. onShareAppMessage(res) {
  363. if (res.from === 'button') { // 来自页面内分享按钮
  364. console.log(res.target)
  365. }
  366. let options = {
  367. title: '',
  368. path: `/pages/episode/play?id=${this.id}`
  369. }
  370. if (this.episode) {
  371. options = {
  372. title: this.episode.name,
  373. path: `/pages/episode/play?id=${this.id}`,
  374. imageUrl: this.episode.cover_img,
  375. desc: this.episode.name,
  376. success: res => {
  377. this.handleShared(this.id, this.currentEpisode.id)
  378. }
  379. }
  380. }
  381. return options
  382. }
  383. }
  384. </script>
  385. <style lang="scss" scoped>
  386. .play-container {
  387. font-size: 28rpx;
  388. .video-box{
  389. position: fixed;
  390. top: 0;
  391. left: 0;
  392. right: 0;
  393. bottom: 0;
  394. .play-layer{
  395. position: fixed;
  396. top: 0;
  397. left: 0;
  398. // #ifdef MP-KUAISHOU | MP-WEIXIN
  399. bottom: 240rpx;
  400. // #endif
  401. // #ifdef MP-TOUTIAO
  402. bottom: 360rpx;
  403. // #endif
  404. right: 0;
  405. background: transparent;
  406. z-index: 999;
  407. }
  408. .pause-layer{
  409. position: absolute;
  410. top: 0;
  411. left: 0;
  412. // #ifdef MP-KUAISHOU | MP-WEIXIN
  413. bottom: 240rpx;
  414. // #endif
  415. // #ifdef MP-TOUTIAO
  416. bottom: 360rpx;
  417. // #endif
  418. right: 0;
  419. background: transparent;
  420. z-index: 99;
  421. }
  422. .swiper {
  423. width: 100%;
  424. height: 100vh;
  425. position: relative;
  426. z-index: 99;
  427. .swiper-item {
  428. width: 100%;
  429. height: calc(100vh - #{150rpx});
  430. }
  431. }
  432. .progress-container{
  433. width: 93vw;
  434. background: #fff;
  435. height: 10rpx;
  436. position: fixed;
  437. z-index: 100;
  438. bottom: 160rpx;
  439. .progress{
  440. height: 10rpx;
  441. background: linear-gradient(270deg, #6EEBE8 0%, #FF74B9 100%);
  442. }
  443. }
  444. }
  445. }
  446. </style>