| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- <template>
- <view class="page">
- <!-- 顶部导航 -->
- <view class="nav-bar">
- <view class="nav-btn" @click="goBack">
- <text class="nav-icon">←</text>
- </view>
- <text class="nav-title">专辑</text>
- <view class="nav-btn" />
- </view>
- <!-- 骨架屏加载状态 -->
- <view v-if="loading && albumList.length === 0" class="skeleton-grid">
- <view v-for="i in 6" :key="i" class="skeleton-card">
- <view class="skeleton-cover"></view>
- <view class="skeleton-info">
- <view class="skeleton-title"></view>
- <view class="skeleton-meta"></view>
- </view>
- </view>
- </view>
- <!-- 专辑列表 - 使用 v-show 避免 scroll-top 初始化的 DOM 未就绪问题 -->
- <scroll-view scroll-y class="album-list" @scrolltolower="loadMore" :style="{ display: (loading && albumList.length === 0) ? 'none' : 'block' }" :scroll-top="scrollTop">
- <view v-if="albumList.length === 0 && !loading" class="empty">
- <text class="empty-icon">📚</text>
- <text class="empty-text">暂无专辑</text>
- <text class="empty-hint">创建你的第一个专辑吧</text>
- </view>
- <view v-else class="album-grid">
- <view
- v-for="item in albumList"
- :key="item.id"
- class="album-card"
- @click="goToAlbumDetail(item.id)"
- >
- <view class="album-cover" :style="{ background: getCoverGradient(item) }">
- <text class="cover-icon">🎵</text>
- <view class="album-count">{{ item.subscriberCount || 0 }}人订阅</view>
- </view>
- <view class="album-info">
- <text class="album-title">{{ item.name }}</text>
- <text class="album-desc">{{ item.description || '暂无描述' }}</text>
- </view>
- </view>
- </view>
- <view v-if="loading" class="loading">
- <text>加载中...</text>
- </view>
- <view v-if="!hasMore && albumList.length > 0" class="no-more">
- <text>没有更多了</text>
- </view>
- </scroll-view>
- </view>
- </template>
- <script setup lang="ts">
- import { ref, onMounted } from 'vue';
- import { get } from '../../utils/request';
- // scroll-top 用于避免 scrollTop 错误
- const scrollTop = ref(0);
- interface Album {
- id: number;
- name: string;
- description?: string;
- coverUrl?: string;
- subscriberCount: number;
- }
- const albumList = ref<Album[]>([]);
- const page = ref(1);
- const pageSize = 20;
- const total = ref(0);
- const loading = ref(false);
- const hasMore = ref(true);
- // 音色封面颜色映射
- const voiceColors: Record<string, string[]> = {
- default: ['#4f46e5', '#818cf8'],
- red: ['#ef4444', '#f87171'],
- orange: ['#f97316', '#fb923c'],
- green: ['#22c55e', '#4ade80'],
- blue: ['#3b82f6', '#60a5fa'],
- purple: ['#a855f7', '#c084fc'],
- };
- // 获取封面渐变色
- function getCoverGradient(item: Album): string {
- // 使用 ID 映射到不同的颜色
- const colorKeys = Object.keys(voiceColors);
- const colorKey = colorKeys[item.id % colorKeys.length] || 'default';
- const colors = voiceColors[colorKey];
- return `linear-gradient(135deg, ${colors[0]} 0%, ${colors[1]} 100%)`;
- }
- // 返回
- function goBack() {
- const pages = getCurrentPages();
- if (pages.length > 1) {
- uni.navigateBack();
- } else {
- uni.switchTab({ url: '/pages/index/index' });
- }
- }
- // 跳转专辑详情
- function goToAlbumDetail(albumId: number) {
- uni.navigateTo({ url: `/pages/album/index?id=${albumId}` });
- }
- // 获取专辑列表(使用书籍 API)
- async function fetchAlbumList(isLoadMore = false) {
- if (loading.value) return;
- if (!isLoadMore) {
- page.value = 1;
- }
- loading.value = true;
- try {
- // 调用专辑列表 API
- const result = await get<{ albums: any[] }>('/book-generator/albums');
- const books = result.albums || [];
- // 转换为 Album 格式
- albumList.value = books.map((book: any) => ({
- id: book.id,
- name: book.title,
- description: book.description,
- subscriberCount: book.completedChapters || 0,
- }));
- total.value = books.length;
- hasMore.value = false;
- } catch (error) {
- console.error('获取专辑列表失败:', error);
- } finally {
- loading.value = false;
- }
- }
- // 加载更多
- function loadMore() {
- if (hasMore.value && !loading.value) {
- page.value++;
- fetchAlbumList(true);
- }
- }
- onMounted(async () => {
- await fetchAlbumList();
- });
- </script>
- <style scoped>
- .page {
- min-height: 100vh;
- background: #f5f5f5;
- }
- .nav-bar {
- display: flex;
- align-items: center;
- justify-content: space-between;
- padding: 44rpx 32rpx 24rpx;
- background: #ffffff;
- position: sticky;
- top: 0;
- z-index: 100;
- }
- .nav-btn {
- width: 64rpx;
- height: 64rpx;
- display: flex;
- align-items: center;
- justify-content: center;
- }
- .nav-icon {
- font-size: 36rpx;
- color: #333;
- }
- .nav-title {
- font-size: 32rpx;
- font-weight: 600;
- color: #333;
- }
- .album-list {
- height: calc(100vh - 100rpx);
- padding: 24rpx;
- }
- /* 骨架屏 */
- .skeleton-grid {
- display: flex;
- flex-wrap: wrap;
- gap: 20rpx;
- padding: 24rpx;
- }
- .skeleton-card {
- width: calc(50% - 10rpx);
- background: #ffffff;
- border-radius: 16rpx;
- overflow: hidden;
- }
- .skeleton-cover {
- width: 100%;
- height: 200rpx;
- background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
- background-size: 200% 100%;
- animation: shimmer 1.5s infinite;
- }
- .skeleton-info {
- padding: 20rpx;
- }
- .skeleton-title {
- height: 28rpx;
- width: 80%;
- background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
- background-size: 200% 100%;
- animation: shimmer 1.5s infinite;
- border-radius: 8rpx;
- margin-bottom: 12rpx;
- }
- .skeleton-meta {
- height: 22rpx;
- width: 50%;
- background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
- background-size: 200% 100%;
- animation: shimmer 1.5s infinite;
- border-radius: 8rpx;
- }
- @keyframes shimmer {
- 0% { background-position: 200% 0; }
- 100% { background-position: -200% 0; }
- }
- /* 专辑列表 */
- .album-grid {
- display: flex;
- flex-wrap: wrap;
- gap: 20rpx;
- }
- .album-card {
- width: calc(50% - 10rpx);
- background: #ffffff;
- border-radius: 16rpx;
- overflow: hidden;
- box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.06);
- }
- .album-cover {
- position: relative;
- width: 100%;
- height: 200rpx;
- display: flex;
- align-items: center;
- justify-content: center;
- }
- .cover-icon {
- font-size: 80rpx;
- opacity: 0.6;
- }
- .album-count {
- position: absolute;
- bottom: 12rpx;
- right: 12rpx;
- background: rgba(0, 0, 0, 0.6);
- color: #ffffff;
- font-size: 22rpx;
- padding: 4rpx 12rpx;
- border-radius: 8rpx;
- }
- .album-info {
- padding: 20rpx;
- }
- .album-title {
- font-size: 28rpx;
- font-weight: 500;
- color: #1f2937;
- display: block;
- overflow: hidden;
- text-overflow: ellipsis;
- white-space: nowrap;
- margin-bottom: 8rpx;
- }
- .album-desc {
- font-size: 22rpx;
- color: #9ca3af;
- display: -webkit-box;
- -webkit-line-clamp: 2;
- -webkit-box-orient: vertical;
- overflow: hidden;
- }
- /* 空状态 */
- .empty {
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
- padding: 200rpx 0;
- }
- .empty-icon {
- font-size: 120rpx;
- margin-bottom: 24rpx;
- }
- .empty-text {
- font-size: 32rpx;
- color: #6b7280;
- margin-bottom: 12rpx;
- }
- .empty-hint {
- font-size: 26rpx;
- color: #9ca3af;
- }
- /* 加载状态 */
- .loading,
- .no-more {
- text-align: center;
- padding: 32rpx;
- }
- .loading text,
- .no-more text {
- font-size: 24rpx;
- color: #9ca3af;
- }
- </style>
|