| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <template>
- <div>
- <TopNav @tab-change="handleTabChange" />
- <div class="top-placeholder"></div>
- <van-pull-refresh v-model="isRefreshing" @refresh="onRefresh">
- <van-list
- v-model="isLoading"
- :finished="isFinished"
- finished-text="没有更多了"
- @load="onLoad"
- >
- <div v-if="currentTab === 'recommend'" class="video-container">
- <VideoCard
- v-for="item in dataList"
- :key="item.videoId"
- :video="item"
- @click="$router.push(`/video/${item.videoId}`)"
- />
- </div>
- <div v-else class="hot-list">
- <div
- v-for="item in hotList"
- :key="item.videoId"
- class="hot-item"
- @click="$router.push(`/video/${item.videoId}`)"
- >
- <van-card :title="item.title" :thumb="item.coverUrl">
- <template #desc>
- <div class="card-desc">
- <p class="author">UP主:{{ item.user ? item.user.screenName : '未知' }}</p>
- <p class="stats">{{ item.view }}播放 · {{ item.pubDateStr || '刚刚' }}</p>
- </div>
- </template>
- <template #footer>
- <van-icon name="ellipsis" size="18" color="#999" />
- </template>
- </van-card>
- </div>
- </div>
- </van-list>
- </van-pull-refresh>
- </div>
- </template>
- <script>
- import TopNav from '@/components/TopNav.vue'
- import VideoCard from '@/components/VideoCard.vue'
- import { getHotVideo, videoRecommend } from '@/api/vod'
- export default {
- name: 'Home',
- components: {
- TopNav,
- VideoCard
- },
- data() {
- return {
- currentTab: 'recommend', // 默认为推荐
- nextId: 0,
- hotList: [],
- dataList: [],
- isLoading: false,
- isFinished: false,
- isRefreshing: false
- }
- },
- methods: {
- handleTabChange(type) {
- this.currentTab = type
- this.onRefresh()
- },
- // 模拟从后端获取数据
- async onLoad() {
- if (this.currentTab === 'hot') {
- this.dataList = []
- this.nextId = 0
- getHotVideo().then((resp) => {
- if (resp.code === 0) {
- this.hotList = resp.data
- } else {
- this.$toast(resp.msg)
- }
- })
- this.isFinished = true
- return
- }
- videoRecommend(this.nextId)
- .then((resp) => {
- if (resp.code === 0) {
- const respData = resp.data
- if (respData.length === 0) {
- this.$toast('已经到底啦~~~')
- return
- }
- if (this.isRefreshing) {
- this.dataList = []
- this.isRefreshing = false
- }
- this.dataList.push(...respData)
- this.isLoading = false
- if (this.dataList.length >= 1000) {
- this.isFinished = true
- }
- this.nextId++
- } else {
- this.$toast('获取数据失败, 请重新刷新页面')
- }
- })
- .catch((error) => {
- this.$toast(error.message)
- })
- },
- onRefresh() {
- this.isFinished = false
- this.isLoading = true
- this.onLoad()
- }
- }
- }
- </script>
- <style scoped lang="less">
- .top-placeholder {
- height: 94px;
- }
- .video-container {
- display: flex;
- flex-wrap: wrap;
- justify-content: space-between;
- padding: 8px 12px;
- background-color: #f4f4f4;
- // 关键:控制 VideoCard 在两列布局中的宽度
- /deep/ .video-card {
- width: 48.5%; // 留出中间的间隙
- }
- }
- // 热门模式样式
- .hot-list {
- background-color: #fff;
- .hot-item {
- border-bottom: 1px solid #f0f0f0;
- ::v-deep .van-card {
- background-color: #fff;
- padding: 12px;
- .van-card__title {
- font-size: 14px;
- font-weight: 500;
- line-height: 1.4;
- max-height: 40px;
- display: -webkit-box;
- -webkit-line-clamp: 2;
- -webkit-box-orient: vertical;
- overflow: hidden;
- }
- .van-card__thumb {
- width: 120px;
- height: 75px;
- border-radius: 4px;
- overflow: hidden;
- }
- }
- }
- .card-desc {
- margin-top: 8px;
- font-size: 12px;
- color: #999;
- p {
- margin: 2px 0;
- }
- .author {
- color: #666;
- }
- }
- }
- </style>
|