play.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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="footer main-between cross-center">
  41. <view class="icon" />
  42. <view class="name">
  43. <u-text :text="episode.name" :color="$colors.infoColor" :lines="1" />
  44. </view>
  45. <view class="arrow">
  46. <u-icon name="arrow-up" :color="$colors.infoColor" size="32rpx" />
  47. </view>
  48. </view>
  49. <!--视频播放-->
  50. <view class="video-box main-center cross-center">
  51. <!-- 控制按钮 - 播放 -->
  52. <view v-if="!isPlaying" class="play-layer main-center cross-center" @tap="handlePlay">
  53. <u-icon name="play-right-fill" size="100rpx" :color="$colors.defaultColor" />
  54. </view>
  55. <!-- 控制按钮 - 暂停 -->
  56. <view v-if="isPlaying" class="pause-layer" @tap="handlePause" />
  57. <!--进度条-->
  58. <view v-if="isPlaying" class="progress-container">
  59. <view class="progress" :style="{width: progress+'%'}" />
  60. </view>
  61. <video
  62. id="video"
  63. :show-play-btn="playBtn"
  64. :show-fullscreen-btn="fullscreenBtn"
  65. :controls="controls"
  66. object-fit="contain"
  67. style="width: 100%;height: 100%;"
  68. :poster="episode.cover_img"
  69. :src="src"
  70. @timeupdate="timeupdate"
  71. />
  72. </view>
  73. </template>
  74. <!--toast-->
  75. <view
  76. v-if="toast.show"
  77. class="toast dir-top-wrap main-center cross-center"
  78. :class="toast.status"
  79. >
  80. <u-icon
  81. :name="toast.status === 'success' ? 'checkmark-circle' : 'close-circle'"
  82. :color="toast.status === 'success' ? $colors.primaryColor : $colors.defaultColor"
  83. size="80rpx"
  84. />
  85. <text>{{ toast.text }}</text>
  86. </view>
  87. </view>
  88. </template>
  89. <script>
  90. export default {
  91. name: 'Play',
  92. data() {
  93. return {
  94. id: null,
  95. listId: null,
  96. isPlaying: false,
  97. videoContext: null,
  98. controls: false,
  99. fullscreenBtn: false,
  100. playBtn: false,
  101. progress: 0,
  102. episode: null,
  103. activeIndex: 0,
  104. loading: true,
  105. isCollect: false,
  106. isFav: false,
  107. toast: {
  108. status: 'success',
  109. text: '收藏成功',
  110. show: false
  111. }
  112. }
  113. },
  114. computed: {
  115. src() {
  116. if (this.episode) {
  117. return this.episode.lists[this.activeIndex].url
  118. }
  119. }
  120. },
  121. watch: {
  122. 'toast.show'(val) {
  123. console.log('-->data1', val)
  124. if (val) {
  125. setTimeout(() => {
  126. this.toast.show = false
  127. }, 1000)
  128. }
  129. }
  130. },
  131. methods: {
  132. timeupdate({ detail }) {
  133. // currentTime, duration
  134. if (detail.duration) {
  135. this.progress = (detail.currentTime / detail.duration * 100).toFixed(2)
  136. }
  137. },
  138. handlePlay() {
  139. this.isPlaying = true
  140. this.videoContext.play()
  141. },
  142. handlePause() {
  143. if (!this.isPlaying) return
  144. this.isPlaying = false
  145. this.videoContext.pause()
  146. },
  147. getEpisode() {
  148. this.loading = true
  149. this.$api.episode.detail(this.id).then(res => {
  150. this.loading = false
  151. this.episode = res.data
  152. if (this.listId) {
  153. this.episode.lists.forEach((obj, index) => {
  154. if (parseInt(this.listId) === obj.id) {
  155. this.activeIndex = index
  156. }
  157. })
  158. } else {
  159. this.listId = this.episode.lists[0].id
  160. }
  161. this.watched(this.id, this.listId)
  162. })
  163. },
  164. checkCollect() {
  165. this.$api.user.collect.check(this.id).then(res => {
  166. this.isCollect = res.data
  167. })
  168. },
  169. handleCollect() {
  170. const method = this.isCollect ? 'destroy' : 'add'
  171. const num = this.isCollect ? -1 : 1
  172. this.$api.user.collect[method](this.id).then(res => {
  173. if (res.data) {
  174. this.toast.show = true
  175. this.toast.status = this.isCollect ? 'cancel' : 'success'
  176. this.toast.text = this.isCollect ? '取消收藏' : '收藏成功'
  177. this.episode.user_collect_count += num
  178. this.isCollect = !this.isCollect
  179. }
  180. })
  181. },
  182. checkFavorite() {
  183. this.$api.user.favorite.check(this.id).then(res => {
  184. this.isFav = res.data
  185. })
  186. },
  187. handleFavorite() {
  188. const method = this.isFav ? 'destroy' : 'add'
  189. const num = this.isFav ? -1 : 1
  190. this.$api.user.favorite[method](this.id).then(res => {
  191. if (res.data) {
  192. this.toast.show = true
  193. this.toast.status = this.isFav ? 'cancel' : 'success'
  194. this.toast.text = this.isFav ? '取消喜欢' : '喜欢成功'
  195. this.episode.user_favorite_count += num
  196. this.isFav = !this.isFav
  197. }
  198. })
  199. },
  200. watched(id, list_id) {
  201. this.$api.user.episode.watched(id, list_id).then(res => {
  202. })
  203. }
  204. },
  205. onLoad(options) {
  206. this.videoContext = uni.createVideoContext('video', this)
  207. this.id = options.id
  208. this.listId = options?.list_Id
  209. this.getEpisode()
  210. this.checkCollect()
  211. this.checkFavorite()
  212. }
  213. }
  214. </script>
  215. <style lang="scss" scoped>
  216. .play-container {
  217. font-size: 28rpx;
  218. .video-box{
  219. position: fixed;
  220. top: 0;
  221. left: 0;
  222. right: 0;
  223. bottom: 0;
  224. .play-layer{
  225. position: fixed;
  226. top: 0;
  227. left: 0;
  228. bottom: 0;
  229. right: 0;
  230. background: transparent;
  231. z-index: 999;
  232. }
  233. .pause-layer{
  234. position: absolute;
  235. top: 0;
  236. left: 0;
  237. bottom: 0;
  238. right: 0;
  239. background: transparent;
  240. z-index: 99;
  241. }
  242. video{
  243. position: absolute;
  244. z-index: 98;
  245. }
  246. .progress-container{
  247. width: 93vw;
  248. background: #fff;
  249. height: 10rpx;
  250. position: fixed;
  251. z-index: 100;
  252. bottom: 160rpx;
  253. .progress{
  254. height: 10rpx;
  255. background: linear-gradient(270deg, #6EEBE8 0%, #FF74B9 100%);
  256. }
  257. }
  258. }
  259. .view-num{
  260. position: fixed;
  261. right: 20rpx;
  262. top: 40rpx;
  263. color: #fff;
  264. font-size: 26rpx;
  265. z-index: 100;
  266. text{
  267. margin-left: 10rpx;
  268. }
  269. }
  270. .status-bar{
  271. position: fixed;
  272. bottom: 300rpx;
  273. right: 40rpx;
  274. color: $default-color;
  275. z-index: 100;
  276. .item{
  277. margin-bottom: 40rpx;
  278. &.share{
  279. position: relative;
  280. button{
  281. position: absolute;
  282. background: transparent;
  283. top: 0;
  284. left: 0;
  285. right: 0;
  286. bottom: 0;
  287. z-index: 1;
  288. &:after{
  289. content: unset;
  290. }
  291. }
  292. }
  293. &.active{
  294. color: $primary-color;
  295. }
  296. text{
  297. margin-top: 10rpx;
  298. }
  299. }
  300. }
  301. .footer{
  302. position: fixed;
  303. width: 95vw;
  304. height: 76rpx;
  305. background: rgba(24, 28, 47, 0.8);
  306. bottom: 50rpx;
  307. left: 50%;
  308. transform: translateX(-50%);
  309. font-size: 26rpx;
  310. color: $info-color;
  311. border-radius: 20rpx;
  312. padding: 0 20rpx;
  313. z-index: 100;
  314. .icon{
  315. background: url("/static/image/video.png") no-repeat center;
  316. background-size: 70%;
  317. width: 80rpx;
  318. height: 80rpx;
  319. }
  320. .name{
  321. text-align: left;
  322. flex: 1;
  323. padding: 0 30rpx;
  324. }
  325. }
  326. .toast{
  327. position: fixed;
  328. width: 60vw;
  329. background: rgba(0,0,0,.5);
  330. height: 300rpx;
  331. top: 50%;
  332. left: 50%;
  333. transform: translate(-50%,-50%);
  334. border-radius: 20rpx;
  335. font-size: 36rpx;
  336. color: $default-color;
  337. &.success{
  338. color: $primary-color;
  339. }
  340. text{
  341. margin-top: 20rpx;
  342. }
  343. }
  344. }
  345. </style>