Index.vue 5.2 KB

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