Files
AlgerMusicPlayer/src/views/mv/index.vue
T

232 lines
5.8 KiB
Vue
Raw Normal View History

2023-12-27 18:21:01 +08:00
<template>
<div class="mv-list">
<div class="mv-list-title">
<h2>推荐MV</h2>
</div>
<n-scrollbar :size="100" @scroll="handleScroll">
<div v-loading="initLoading" class="mv-list-content" :class="setAnimationClass('animate__bounceInLeft')">
<div
v-for="(item, index) in mvList"
:key="item.id"
class="mv-item"
:class="setAnimationClass('animate__bounceIn')"
:style="getItemAnimationDelay(index)"
>
<div class="mv-item-img" @click="handleShowMv(item, index)">
<n-image class="mv-item-img-img" :src="getImgUrl(item.cover, '320y180')" lazy preview-disabled />
2023-12-27 18:21:01 +08:00
<div class="top">
<div class="play-count">{{ formatNumber(item.playCount) }}</div>
<i class="iconfont icon-videofill"></i>
</div>
</div>
<div class="mv-item-title">{{ item.name }}</div>
</div>
<div v-if="loadingMore" class="loading-more">加载中...</div>
<div v-if="!hasMore && !initLoading" class="no-more">没有更多了</div>
2023-12-27 18:21:01 +08:00
</div>
</n-scrollbar>
<mv-player
v-model:show="showMv"
:current-mv="playMvItem"
:is-prev-disabled="isPrevDisabled"
@next="playNextMv"
@prev="playPrevMv"
/>
2023-12-27 18:21:01 +08:00
</div>
</template>
<script setup lang="ts">
import { computed, onMounted, ref } from 'vue';
2023-12-27 18:21:01 +08:00
import { useStore } from 'vuex';
import { getTopMv } from '@/api/mv';
import MvPlayer from '@/components/MvPlayer.vue';
import { IMvItem } from '@/type/mv';
import { formatNumber, getImgUrl, setAnimationClass, setAnimationDelay } from '@/utils';
defineOptions({
name: 'Mv',
});
const showMv = ref(false);
const mvList = ref<Array<IMvItem>>([]);
const playMvItem = ref<IMvItem>();
const store = useStore();
const initLoading = ref(false);
const loadingMore = ref(false);
const currentIndex = ref(0);
const offset = ref(0);
const limit = ref(30);
const hasMore = ref(true);
2023-12-27 18:21:01 +08:00
const getItemAnimationDelay = (index: number) => {
const currentPageIndex = index % limit.value;
return setAnimationDelay(currentPageIndex, 30);
};
2023-12-27 18:21:01 +08:00
onMounted(async () => {
await loadMvList();
});
2023-12-27 18:21:01 +08:00
const handleShowMv = async (item: IMvItem, index: number) => {
store.commit('setIsPlay', false);
store.commit('setPlayMusic', false);
showMv.value = true;
currentIndex.value = index;
2023-12-27 18:21:01 +08:00
playMvItem.value = item;
};
2023-12-27 18:21:01 +08:00
const playPrevMv = async (setLoading: (value: boolean) => void) => {
try {
if (currentIndex.value > 0) {
const prevItem = mvList.value[currentIndex.value - 1];
await handleShowMv(prevItem, currentIndex.value - 1);
}
} finally {
setLoading(false);
}
};
const playNextMv = async (setLoading: (value: boolean) => void) => {
try {
if (currentIndex.value < mvList.value.length - 1) {
const nextItem = mvList.value[currentIndex.value + 1];
await handleShowMv(nextItem, currentIndex.value + 1);
} else if (hasMore.value) {
await loadMvList();
if (mvList.value.length > currentIndex.value + 1) {
const nextItem = mvList.value[currentIndex.value + 1];
await handleShowMv(nextItem, currentIndex.value + 1);
} else {
showMv.value = false;
}
} else {
showMv.value = false;
}
} catch (error) {
console.error('加载更多MV失败:', error);
showMv.value = false;
} finally {
setLoading(false);
2023-12-27 18:21:01 +08:00
}
};
const loadMvList = async () => {
if (!hasMore.value || loadingMore.value) return;
if (offset.value === 0) {
initLoading.value = true;
} else {
loadingMore.value = true;
}
try {
const res = await getTopMv(limit.value, offset.value);
if (offset.value === 0) {
mvList.value = res.data.data;
} else {
mvList.value.push(...res.data.data);
}
hasMore.value = res.data.data.length === limit.value;
offset.value += limit.value;
} catch (error) {
console.error('加载MV失败:', error);
} finally {
initLoading.value = false;
loadingMore.value = false;
}
};
const handleScroll = (e: Event) => {
const target = e.target as Element;
const { scrollTop, clientHeight, scrollHeight } = target;
const threshold = 100;
if (scrollHeight - (scrollTop + clientHeight) < threshold) {
loadMvList();
}
};
const isPrevDisabled = computed(() => currentIndex.value === 0);
2023-12-27 18:21:01 +08:00
</script>
<style scoped lang="scss">
.mv-list {
2024-11-28 23:45:44 +08:00
@apply relative h-full w-full;
2023-12-29 16:04:44 +08:00
&-title {
@apply text-xl font-bold;
}
2023-12-27 18:21:01 +08:00
&-content {
@apply grid gap-4 pb-28 mt-2 pr-4;
grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
2023-12-27 18:21:01 +08:00
}
.mv-item {
2023-12-29 16:04:44 +08:00
@apply p-2 rounded-lg;
2024-01-03 22:27:58 +08:00
background-color: #1f1f1f;
2023-12-27 18:21:01 +08:00
&-img {
2023-12-29 16:04:44 +08:00
@apply rounded-lg overflow-hidden relative;
aspect-ratio: 16/9;
2024-01-03 22:27:58 +08:00
line-height: 0;
2023-12-27 18:21:01 +08:00
&:hover img {
2023-12-29 16:04:44 +08:00
@apply hover:scale-110 transition-all duration-300 ease-in-out object-top;
2023-12-27 18:21:01 +08:00
}
&-img {
@apply w-full h-full object-cover rounded-lg overflow-hidden;
2023-12-27 18:21:01 +08:00
}
.top {
@apply absolute w-full h-full top-0 left-0 flex justify-center items-center transition-all duration-300 ease-in-out cursor-pointer;
2023-12-29 16:04:44 +08:00
background-color: #0000009b;
2023-12-27 18:21:01 +08:00
opacity: 0;
i {
2023-12-29 16:04:44 +08:00
font-size: 40px;
2023-12-27 18:21:01 +08:00
transition: all 0.5s ease-in-out;
opacity: 0;
}
&:hover {
@apply opacity-100;
}
&:hover i {
2023-12-29 16:04:44 +08:00
@apply transform scale-150 opacity-80;
2023-12-27 18:21:01 +08:00
}
.play-count {
position: absolute;
2024-01-03 22:27:58 +08:00
top: 20px;
2023-12-27 18:21:01 +08:00
left: 10px;
font-size: 14px;
}
}
}
&-title {
@apply p-2 text-sm text-white truncate;
}
}
}
2024-05-23 17:12:35 +08:00
.mobile {
.mv-list-content {
@apply px-4;
grid-template-columns: repeat(2, 1fr);
gap: 1rem;
2024-05-23 17:12:35 +08:00
}
}
.loading-more,
.no-more {
@apply col-span-full text-center py-4 text-gray-400;
}
</style>