play.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. <view
  18. class="item fav dir-top-wrap main-center cross-center"
  19. :class="{active: isFav}"
  20. @click="handleFavorite"
  21. >
  22. <u-icon name="heart-fill" size="58rpx" :color="isFav?$colors.primaryColor:$colors.defaultColor" />
  23. <text>{{ episode.user_favorite_count }}</text>
  24. </view>
  25. <view
  26. class="item collect dir-top-wrap main-center cross-center"
  27. :class="{active: isCollect}"
  28. @click="handleCollect"
  29. >
  30. <u-icon name="star-fill" size="58rpx" :color="isCollect?$colors.primaryColor:$colors.defaultColor" />
  31. <text>{{ episode.user_collect_count }}</text>
  32. </view>
  33. <view class="item share dir-top-wrap main-center cross-center">
  34. <button open-type="share" />
  35. <u-icon name="share-fill" size="58rpx" :color="$colors.defaultColor" />
  36. <text>{{ episode.sahre_count }}</text>
  37. </view>
  38. </view>
  39. <!--视频播放-->
  40. <view class="video-box main-center cross-center" :style="{zIndex: isPlaying?0:998}">
  41. <!-- 控制按钮 - 播放 -->
  42. <view v-if="!isPlaying" class="play-layer main-center cross-center" @tap="handlePlay">
  43. <u-icon name="play-right-fill" size="100rpx" :color="$colors.defaultColor" />
  44. </view>
  45. <!-- 控制按钮 - 暂停 -->
  46. <view v-if="isPlaying" class="pause-layer" @tap="handlePause" />
  47. <!--进度条-->
  48. <view v-if="isPlaying" class="progress-container">
  49. <view class="progress" :style="{width: progress+'%'}" />
  50. </view>
  51. <video
  52. id="video"
  53. :show-play-btn="playBtn"
  54. :show-fullscreen-btn="fullscreenBtn"
  55. :controls="controls"
  56. object-fit="contain"
  57. style="width: 100%;height: 100%;"
  58. :poster="episode.cover_img"
  59. :src="src"
  60. @binderror="binderror"
  61. @timeupdate="timeupdate"
  62. />
  63. </view>
  64. <!--底部-->
  65. <view class="footer" :class="{episode: footerShow}">
  66. <view class="bar main-between cross-center" @click="footerShow = !footerShow">
  67. <view class="icon" />
  68. <view class="name">
  69. <u-text :text="episode.name" :color="$colors.infoColor" :lines="1" />
  70. <!-- <u-text text="321313" :color="$colors.infoColor" :lines="1" />-->
  71. </view>
  72. <view class="arrow">
  73. <u-icon name="arrow-up" :color="$colors.infoColor" size="32rpx" />
  74. </view>
  75. </view>
  76. <view class="episode-container dir-top-wrap">
  77. <scroll-view
  78. class="header-box dir-left-nowrap cross-center"
  79. scroll-x
  80. scroll-with-animation
  81. >
  82. <view
  83. v-for="(item,index) in episodes"
  84. :key="index"
  85. class="header-item"
  86. :class="{active: episodesIndex === index}"
  87. @click="episodesIndex=index"
  88. >{{ item.title }}</view>
  89. </scroll-view>
  90. <view class="content dir-left-wrap main-left">
  91. <view
  92. v-for="(item, index) in episodes[episodesIndex].lists"
  93. :key="index"
  94. class="episode-item"
  95. @click="handleSelectEpisode(item.index)"
  96. >
  97. <image :src="episode.cover_img" />
  98. <text>第{{ item.sort }}集</text>
  99. <view v-if="activeIndex === item.index" class="playing" />
  100. <view v-if="!item.is_free" class="lock main-center cross-center">
  101. <u-icon name="lock-fill" :color="$colors.defaultColor" size="46rpx" />
  102. </view>
  103. </view>
  104. </view>
  105. </view>
  106. </view>
  107. </template>
  108. <!--toast-->
  109. <view
  110. v-if="toast.show"
  111. class="toast dir-top-wrap main-center cross-center"
  112. :class="toast.status"
  113. >
  114. <u-icon
  115. :name="toast.status === 'success' ? 'checkmark-circle' : 'close-circle'"
  116. :color="toast.status === 'success' ? $colors.primaryColor : $colors.defaultColor"
  117. size="80rpx"
  118. />
  119. <text>{{ toast.text }}</text>
  120. </view>
  121. </view>
  122. </template>
  123. <script>
  124. export default {
  125. name: 'Play',
  126. data() {
  127. return {
  128. id: null,
  129. listId: null,
  130. isPlaying: false,
  131. videoContext: null,
  132. controls: false,
  133. fullscreenBtn: false,
  134. playBtn: false,
  135. progress: 0,
  136. episode: null,
  137. activeIndex: 0,
  138. loading: true,
  139. isCollect: false,
  140. isFav: false,
  141. toast: {
  142. status: 'success',
  143. text: '收藏成功',
  144. show: false
  145. },
  146. footerShow: false,
  147. episodesIndex: 0
  148. }
  149. },
  150. computed: {
  151. src() {
  152. if (this.episode) {
  153. return this.episode.lists[this.activeIndex].url
  154. }
  155. },
  156. episodes() {
  157. const list = []
  158. if (this.episode) {
  159. let temp = []
  160. this.episode.lists.forEach((obj, index) => {
  161. obj.index = index
  162. temp.push(obj)
  163. if (temp.length === 6 || index === (this.episode.lists.length - 1)) {
  164. const start = list.length ? (list.length * 6) + 1 : 1
  165. const end = (start - 1) + temp.length
  166. list.push({ title: `${start}集-${end}集`, lists: temp })
  167. temp = []
  168. }
  169. })
  170. }
  171. return list
  172. }
  173. },
  174. watch: {
  175. 'toast.show'(val) {
  176. if (val) {
  177. setTimeout(() => {
  178. this.toast.show = false
  179. }, 1000)
  180. }
  181. }
  182. },
  183. methods: {
  184. timeupdate({ detail }) {
  185. // currentTime, duration
  186. if (detail.duration) {
  187. this.progress = (detail.currentTime / detail.duration * 100).toFixed(2)
  188. }
  189. },
  190. handlePlay() {
  191. this.isPlaying = true
  192. this.videoContext.play()
  193. this.watched(this.id, this.listId)
  194. },
  195. handleSelectEpisode(index) {
  196. this.activeIndex = index
  197. this.footerShow = false
  198. this.$loading()
  199. setTimeout(() => {
  200. this.$hideLoading()
  201. this.isPlaying = true
  202. this.videoContext.play()
  203. this.watched(this.id, this.episode.lists[this.activeIndex].id)
  204. }, 1000)
  205. },
  206. handlePause() {
  207. if (!this.isPlaying) return
  208. this.isPlaying = false
  209. this.videoContext.pause()
  210. },
  211. getEpisode() {
  212. this.loading = true
  213. this.$api.episode.detail(this.id).then(res => {
  214. this.loading = false
  215. this.episode = res.data
  216. console.log('-->data', this.listId)
  217. if (this.listId) {
  218. this.episode.lists.forEach((obj, index) => {
  219. if (parseInt(this.listId) === parseInt(obj.id)) {
  220. this.activeIndex = index
  221. console.log('-->data', this.activeIndex)
  222. }
  223. })
  224. } else {
  225. this.listId = this.episode.lists[0].id
  226. }
  227. })
  228. },
  229. checkCollect() {
  230. this.$api.user.collect.check(this.id).then(res => {
  231. this.isCollect = res.data
  232. })
  233. },
  234. handleCollect() {
  235. const method = this.isCollect ? 'destroy' : 'add'
  236. const num = this.isCollect ? -1 : 1
  237. this.$api.user.collect[method](this.id).then(res => {
  238. if (res.data) {
  239. this.toast.show = true
  240. this.toast.status = this.isCollect ? 'cancel' : 'success'
  241. this.toast.text = this.isCollect ? '取消收藏' : '收藏成功'
  242. this.episode.user_collect_count += num
  243. this.isCollect = !this.isCollect
  244. }
  245. })
  246. },
  247. checkFavorite() {
  248. this.$api.user.favorite.check(this.id).then(res => {
  249. this.isFav = res.data
  250. })
  251. },
  252. handleFavorite() {
  253. const method = this.isFav ? 'destroy' : 'add'
  254. const num = this.isFav ? -1 : 1
  255. this.$api.user.favorite[method](this.id).then(res => {
  256. if (res.data) {
  257. this.toast.show = true
  258. this.toast.status = this.isFav ? 'cancel' : 'success'
  259. this.toast.text = this.isFav ? '取消喜欢' : '喜欢成功'
  260. this.episode.user_favorite_count += num
  261. this.isFav = !this.isFav
  262. }
  263. })
  264. },
  265. watched(id, list_id) {
  266. this.$api.user.episode.watched(id, list_id).then(res => {
  267. })
  268. },
  269. shared(id, list_id) {
  270. this.$api.episode.shared(id, list_id).then(res => {
  271. this.episode.share_count += 1
  272. })
  273. }
  274. },
  275. onLoad(options) {
  276. this.videoContext = uni.createVideoContext('video', this)
  277. this.id = options.id
  278. this.listId = options?.list_id
  279. this.getEpisode()
  280. this.checkCollect()
  281. this.checkFavorite()
  282. },
  283. onShareAppMessage(res) {
  284. if (res.from === 'button') { // 来自页面内分享按钮
  285. console.log(res.target)
  286. }
  287. let options = {
  288. title: '',
  289. path: `/pages/episode/play?id=${this.id}`
  290. }
  291. if (this.episode) {
  292. options = {
  293. title: this.episode.name,
  294. path: `/pages/episode/play?id=${this.id}`,
  295. imageUrl: this.episode.cover_img,
  296. desc: this.episode.name,
  297. success() {
  298. }
  299. }
  300. }
  301. return options
  302. }
  303. }
  304. </script>
  305. <style lang="scss" scoped>
  306. .play-container {
  307. font-size: 28rpx;
  308. .video-box{
  309. position: fixed;
  310. top: 0;
  311. left: 0;
  312. right: 0;
  313. bottom: 0;
  314. .play-layer{
  315. position: fixed;
  316. top: 0;
  317. left: 0;
  318. bottom: 0;
  319. right: 0;
  320. background: transparent;
  321. z-index: 999;
  322. }
  323. .pause-layer{
  324. position: absolute;
  325. top: 0;
  326. left: 0;
  327. bottom: 0;
  328. right: 0;
  329. background: transparent;
  330. z-index: 99;
  331. }
  332. video{
  333. position: absolute;
  334. z-index: 98;
  335. }
  336. .progress-container{
  337. width: 93vw;
  338. background: #fff;
  339. height: 10rpx;
  340. position: fixed;
  341. z-index: 100;
  342. bottom: 160rpx;
  343. .progress{
  344. height: 10rpx;
  345. background: linear-gradient(270deg, #6EEBE8 0%, #FF74B9 100%);
  346. }
  347. }
  348. }
  349. .view-num{
  350. position: fixed;
  351. right: 20rpx;
  352. top: 40rpx;
  353. color: #fff;
  354. font-size: 26rpx;
  355. z-index: 100;
  356. text{
  357. margin-left: 10rpx;
  358. }
  359. }
  360. .status-bar{
  361. position: fixed;
  362. bottom: 300rpx;
  363. right: 40rpx;
  364. color: $default-color;
  365. z-index: 100;
  366. .item{
  367. margin-bottom: 40rpx;
  368. &.share{
  369. position: relative;
  370. button{
  371. position: absolute;
  372. background: transparent;
  373. top: 0;
  374. left: 0;
  375. right: 0;
  376. bottom: 0;
  377. z-index: 1;
  378. &:after{
  379. content: unset;
  380. }
  381. }
  382. }
  383. &.active{
  384. color: $primary-color;
  385. }
  386. text{
  387. margin-top: 10rpx;
  388. }
  389. }
  390. }
  391. .footer{
  392. position: fixed;
  393. width: 95vw;
  394. height: 76rpx;
  395. background: rgba(24, 28, 47, 0.8);
  396. bottom: 50rpx;
  397. left: 50%;
  398. transform: translateX(-50%);
  399. font-size: 26rpx;
  400. color: $info-color;
  401. border-radius: 20rpx;
  402. z-index: 999;
  403. transition: .3s;
  404. &.episode{
  405. bottom: 700rpx;
  406. margin-top: -4rpx;
  407. border-bottom-left-radius: 0;
  408. border-bottom-right-radius: 0;
  409. .episode-container{
  410. display: flex;
  411. margin-top: -4rpx;
  412. border-bottom-left-radius: 20px;
  413. border-bottom-right-radius: 20px;
  414. }
  415. .bar{
  416. .arrow{
  417. transform: rotate(180deg);
  418. }
  419. }
  420. }
  421. .bar{
  422. padding: 0 20rpx;
  423. .icon{
  424. background: url("/static/image/video.png") no-repeat center;
  425. background-size: 70%;
  426. width: 80rpx;
  427. height: 80rpx;
  428. }
  429. .name{
  430. text-align: left;
  431. flex: 1;
  432. padding: 0 30rpx;
  433. }
  434. .arrow{
  435. transition: .3s;
  436. }
  437. }
  438. .episode-container{
  439. display: none;
  440. background: inherit;
  441. .header-box{
  442. white-space: nowrap;
  443. margin: 20rpx 0;
  444. .header-item{
  445. margin-right: 20rpx;
  446. border-radius: 20rpx;
  447. display: inline-block;
  448. width: 200rpx;
  449. border: 1rpx solid $default-color;
  450. text-align: center;
  451. padding: 10rpx 0;
  452. color: $default-color;
  453. &.active{
  454. border-color: $primary-color;
  455. color: $primary-color;
  456. }
  457. }
  458. }
  459. .content{
  460. margin-top: 20rpx;
  461. .episode-item{
  462. position: relative;
  463. width: calc((100% - #{40rpx}) / 3);
  464. margin-right: 20rpx;
  465. margin-bottom: 20rpx;
  466. overflow: hidden;
  467. border-radius: 18rpx;
  468. &:nth-child(3n){
  469. margin-right: 0;
  470. }
  471. .playing{
  472. position: absolute;
  473. top: 0;
  474. left: 0;
  475. bottom: 0;
  476. right: 0;
  477. background: rgba(0,0,0,.5) url("/static/image/playing.png") no-repeat center;
  478. background-size: 40rpx;
  479. z-index: 2;
  480. }
  481. image{
  482. width: 100%;
  483. height: 260rpx;
  484. }
  485. text{
  486. position: absolute;
  487. left: 0;
  488. bottom: 0;
  489. right: 0;
  490. color: $default-color;
  491. padding: 20rpx 0;
  492. text-align: center;
  493. background: rgba(0,0,0,.3);
  494. z-index: 1;
  495. }
  496. .lock{
  497. position: absolute;
  498. top: 0;
  499. left: 0;
  500. bottom: 0;
  501. right: 0;
  502. background: rgba(0,0,0,.5);
  503. z-index: 2;
  504. }
  505. }
  506. }
  507. }
  508. }
  509. .toast{
  510. position: fixed;
  511. width: 60vw;
  512. background: rgba(0,0,0,.5);
  513. height: 300rpx;
  514. top: 50%;
  515. left: 50%;
  516. transform: translate(-50%,-50%);
  517. border-radius: 20rpx;
  518. font-size: 36rpx;
  519. color: $default-color;
  520. &.success{
  521. color: $primary-color;
  522. }
  523. text{
  524. margin-top: 20rpx;
  525. }
  526. }
  527. }
  528. </style>