Home.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <template>
  2. <el-row class="movie-list">
  3. <el-row>
  4. <el-col :md="12">
  5. <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  6. <site-notice />
  7. </el-row>
  8. </el-col>
  9. <el-col :md="12">
  10. <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  11. <el-carousel :interval="3000" height="300px">
  12. <el-carousel-item v-for="(item, index) in carouselList" :key="index">
  13. <router-link target="_blank" :to="`/vod/video/${item.videoId}`">
  14. <img class="carousel_image_type" :src="item.coverUrl" alt="img">
  15. </router-link>
  16. </el-carousel-item>
  17. </el-carousel>
  18. </el-row>
  19. </el-col>
  20. </el-row>
  21. <el-scrollbar style="width: 100%; height: 100vh">
  22. <el-row>
  23. <el-row
  24. v-if="dataList.length !== 0"
  25. v-infinite-scroll="load"
  26. infinite-scroll-disabled="loading"
  27. infinite-scroll-distance="10"
  28. >
  29. <!--电影列表-->
  30. <el-col :md="24">
  31. <el-col v-for="(item, index) in dataList" :key="index" :md="6" :sm="12" :xs="12">
  32. <video-card :video="item" />
  33. </el-col>
  34. </el-col>
  35. </el-row>
  36. <el-row v-else class="not-result">
  37. <el-col :span="12" :offset="6">
  38. <img src="@/assets/img/not-collection.png">
  39. <div>推荐数据正在计算中</div>
  40. </el-col>
  41. </el-row>
  42. </el-row>
  43. </el-scrollbar>
  44. </el-row>
  45. </template>
  46. <script>
  47. import VideoCard from '@/components/card/VideoCard'
  48. import SiteNotice from '@/components/card/SiteNotice'
  49. import { getBannerVideo, videoRecommend } from '@/api/video'
  50. export default {
  51. name: 'Index',
  52. components: { VideoCard, SiteNotice },
  53. data() {
  54. return {
  55. // 屏幕宽度, 为了控制分页条的大小
  56. screenWidth: document.body.clientWidth,
  57. nextId: 0,
  58. dataList: [],
  59. loading: false,
  60. max: 0,
  61. carouselList: []
  62. }
  63. },
  64. created() {
  65. this.videoRecommendWrapper(this.nextId)
  66. this.getHotVideoWrapper()
  67. // this.initServerSendEvent()
  68. },
  69. mounted() {
  70. // 当窗口宽度改变时获取屏幕宽度
  71. window.onresize = () => {
  72. return () => {
  73. window.screenWidth = document.body.clientWidth
  74. this.screenWidth = window.screenWidth
  75. }
  76. }
  77. },
  78. methods: {
  79. videoRecommendWrapper(nextId) {
  80. videoRecommend(nextId).then(resp => {
  81. if (resp.code === 0) {
  82. this.loading = false
  83. const respData = resp.data
  84. if (respData.length === 0) {
  85. this.$message(
  86. {
  87. message: '已经到底啦~~~',
  88. type: 'info',
  89. duration: 1000
  90. }
  91. )
  92. return
  93. }
  94. for (const item of respData) {
  95. this.dataList.push(item)
  96. }
  97. this.nextId++
  98. this.$message(
  99. {
  100. message: '已加载新数据~~~',
  101. type: 'info',
  102. duration: 1000
  103. }
  104. )
  105. } else {
  106. this.$message(
  107. {
  108. message: '获取数据失败, 请重新刷新页面',
  109. type: 'warning',
  110. duration: 1000
  111. }
  112. )
  113. }
  114. }).catch(error => {
  115. this.$message(
  116. {
  117. message: error.message,
  118. type: 'error',
  119. duration: 1000
  120. }
  121. )
  122. }).finally(() => {
  123. })
  124. },
  125. load() {
  126. this.loading = true
  127. setTimeout(() => {
  128. this.videoRecommendWrapper(this.nextId)
  129. }, 1000)
  130. },
  131. getHotVideoWrapper() {
  132. getBannerVideo().then(resp => {
  133. if (resp.code === 0) {
  134. this.carouselList = resp.data
  135. }
  136. }).catch(error => {
  137. this.$message(
  138. {
  139. message: error.message,
  140. type: 'error',
  141. duration: 1000
  142. }
  143. )
  144. }).finally(() => {
  145. })
  146. },
  147. initServerSendEvent() {
  148. if (typeof (EventSource) !== 'undefined') {
  149. const sseUrl = process.env.VUE_APP_SERVER_URL + '/api/data/video/hot'
  150. const source = new EventSource(sseUrl)
  151. source.addEventListener('test', function(e) {
  152. console.log(e)
  153. })
  154. const that = this
  155. source.onmessage = function(event) {
  156. console.log('update sse data')
  157. const dataList = JSON.parse(event.data)
  158. if (dataList.length !== 0) {
  159. that.carouselList = dataList
  160. }
  161. }
  162. } else {
  163. this.$message(
  164. {
  165. message: '抱歉,你的浏览器不支持 SSE...',
  166. type: 'warning',
  167. duration: 1000
  168. }
  169. )
  170. }
  171. }
  172. }
  173. }
  174. </script>
  175. <style scoped>
  176. /*处于手机屏幕时*/
  177. @media screen and (max-width: 768px){
  178. .movie-list {
  179. padding-top: 5px;
  180. padding-left: 0.5%;
  181. padding-right: 0.5%;
  182. }
  183. }
  184. .movie-list {
  185. padding-top: 5px;
  186. padding-bottom: 5px;
  187. padding-left: 5px;
  188. padding-right: 5px;
  189. }
  190. .not-result {
  191. padding-top: 100px;
  192. padding-bottom: 100px;
  193. text-align: center;
  194. }
  195. .el-carousel__item h3 {
  196. color: #475669;
  197. font-size: 18px;
  198. opacity: 0.75;
  199. line-height: 300px;
  200. margin: 0;
  201. }
  202. .carousel_image_type{
  203. width: 100%;
  204. }
  205. </style>