index.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <template>
  2. <view class="page">
  3. <!-- 顶部导航 -->
  4. <view class="nav-bar">
  5. <view class="nav-btn" @click="goBack">
  6. <text class="nav-icon">←</text>
  7. </view>
  8. <text class="nav-title">专辑</text>
  9. <view class="nav-btn" />
  10. </view>
  11. <!-- 骨架屏加载状态 -->
  12. <view v-if="loading && albumList.length === 0" class="skeleton-grid">
  13. <view v-for="i in 6" :key="i" class="skeleton-card">
  14. <view class="skeleton-cover"></view>
  15. <view class="skeleton-info">
  16. <view class="skeleton-title"></view>
  17. <view class="skeleton-meta"></view>
  18. </view>
  19. </view>
  20. </view>
  21. <!-- 专辑列表 - 使用 v-show 避免 scroll-top 初始化的 DOM 未就绪问题 -->
  22. <scroll-view scroll-y class="album-list" @scrolltolower="loadMore" :style="{ display: (loading && albumList.length === 0) ? 'none' : 'block' }" :scroll-top="scrollTop">
  23. <view v-if="albumList.length === 0 && !loading" class="empty">
  24. <text class="empty-icon">📚</text>
  25. <text class="empty-text">暂无专辑</text>
  26. <text class="empty-hint">创建你的第一个专辑吧</text>
  27. </view>
  28. <view v-else class="album-grid">
  29. <view
  30. v-for="item in albumList"
  31. :key="item.id"
  32. class="album-card"
  33. @click="goToAlbumDetail(item.id)"
  34. >
  35. <view class="album-cover" :style="{ background: getCoverGradient(item) }">
  36. <text class="cover-icon">🎵</text>
  37. <view class="album-count">{{ item.subscriberCount || 0 }}人订阅</view>
  38. </view>
  39. <view class="album-info">
  40. <text class="album-title">{{ item.name }}</text>
  41. <text class="album-desc">{{ item.description || '暂无描述' }}</text>
  42. </view>
  43. </view>
  44. </view>
  45. <view v-if="loading" class="loading">
  46. <text>加载中...</text>
  47. </view>
  48. <view v-if="!hasMore && albumList.length > 0" class="no-more">
  49. <text>没有更多了</text>
  50. </view>
  51. </scroll-view>
  52. </view>
  53. </template>
  54. <script setup lang="ts">
  55. import { ref, onMounted } from 'vue';
  56. import { get } from '../../utils/request';
  57. // scroll-top 用于避免 scrollTop 错误
  58. const scrollTop = ref(0);
  59. interface Album {
  60. id: number;
  61. name: string;
  62. description?: string;
  63. coverUrl?: string;
  64. subscriberCount: number;
  65. }
  66. const albumList = ref<Album[]>([]);
  67. const page = ref(1);
  68. const pageSize = 20;
  69. const total = ref(0);
  70. const loading = ref(false);
  71. const hasMore = ref(true);
  72. // 音色封面颜色映射
  73. const voiceColors: Record<string, string[]> = {
  74. default: ['#4f46e5', '#818cf8'],
  75. red: ['#ef4444', '#f87171'],
  76. orange: ['#f97316', '#fb923c'],
  77. green: ['#22c55e', '#4ade80'],
  78. blue: ['#3b82f6', '#60a5fa'],
  79. purple: ['#a855f7', '#c084fc'],
  80. };
  81. // 获取封面渐变色
  82. function getCoverGradient(item: Album): string {
  83. // 使用 ID 映射到不同的颜色
  84. const colorKeys = Object.keys(voiceColors);
  85. const colorKey = colorKeys[item.id % colorKeys.length] || 'default';
  86. const colors = voiceColors[colorKey];
  87. return `linear-gradient(135deg, ${colors[0]} 0%, ${colors[1]} 100%)`;
  88. }
  89. // 返回
  90. function goBack() {
  91. const pages = getCurrentPages();
  92. if (pages.length > 1) {
  93. uni.navigateBack();
  94. } else {
  95. uni.switchTab({ url: '/pages/index/index' });
  96. }
  97. }
  98. // 跳转专辑详情
  99. function goToAlbumDetail(albumId: number) {
  100. uni.navigateTo({ url: `/pages/album/index?id=${albumId}` });
  101. }
  102. // 获取专辑列表(使用书籍 API)
  103. async function fetchAlbumList(isLoadMore = false) {
  104. if (loading.value) return;
  105. if (!isLoadMore) {
  106. page.value = 1;
  107. }
  108. loading.value = true;
  109. try {
  110. // 调用专辑列表 API
  111. const result = await get<{ albums: any[] }>('/book-generator/albums');
  112. const books = result.albums || [];
  113. // 转换为 Album 格式
  114. albumList.value = books.map((book: any) => ({
  115. id: book.id,
  116. name: book.title,
  117. description: book.description,
  118. subscriberCount: book.completedChapters || 0,
  119. }));
  120. total.value = books.length;
  121. hasMore.value = false;
  122. } catch (error) {
  123. console.error('获取专辑列表失败:', error);
  124. } finally {
  125. loading.value = false;
  126. }
  127. }
  128. // 加载更多
  129. function loadMore() {
  130. if (hasMore.value && !loading.value) {
  131. page.value++;
  132. fetchAlbumList(true);
  133. }
  134. }
  135. onMounted(async () => {
  136. await fetchAlbumList();
  137. });
  138. </script>
  139. <style scoped>
  140. .page {
  141. min-height: 100vh;
  142. background: #f5f5f5;
  143. }
  144. .nav-bar {
  145. display: flex;
  146. align-items: center;
  147. justify-content: space-between;
  148. padding: 44rpx 32rpx 24rpx;
  149. background: #ffffff;
  150. position: sticky;
  151. top: 0;
  152. z-index: 100;
  153. }
  154. .nav-btn {
  155. width: 64rpx;
  156. height: 64rpx;
  157. display: flex;
  158. align-items: center;
  159. justify-content: center;
  160. }
  161. .nav-icon {
  162. font-size: 36rpx;
  163. color: #333;
  164. }
  165. .nav-title {
  166. font-size: 32rpx;
  167. font-weight: 600;
  168. color: #333;
  169. }
  170. .album-list {
  171. height: calc(100vh - 100rpx);
  172. padding: 24rpx;
  173. }
  174. /* 骨架屏 */
  175. .skeleton-grid {
  176. display: flex;
  177. flex-wrap: wrap;
  178. gap: 20rpx;
  179. padding: 24rpx;
  180. }
  181. .skeleton-card {
  182. width: calc(50% - 10rpx);
  183. background: #ffffff;
  184. border-radius: 16rpx;
  185. overflow: hidden;
  186. }
  187. .skeleton-cover {
  188. width: 100%;
  189. height: 200rpx;
  190. background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
  191. background-size: 200% 100%;
  192. animation: shimmer 1.5s infinite;
  193. }
  194. .skeleton-info {
  195. padding: 20rpx;
  196. }
  197. .skeleton-title {
  198. height: 28rpx;
  199. width: 80%;
  200. background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
  201. background-size: 200% 100%;
  202. animation: shimmer 1.5s infinite;
  203. border-radius: 8rpx;
  204. margin-bottom: 12rpx;
  205. }
  206. .skeleton-meta {
  207. height: 22rpx;
  208. width: 50%;
  209. background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
  210. background-size: 200% 100%;
  211. animation: shimmer 1.5s infinite;
  212. border-radius: 8rpx;
  213. }
  214. @keyframes shimmer {
  215. 0% { background-position: 200% 0; }
  216. 100% { background-position: -200% 0; }
  217. }
  218. /* 专辑列表 */
  219. .album-grid {
  220. display: flex;
  221. flex-wrap: wrap;
  222. gap: 20rpx;
  223. }
  224. .album-card {
  225. width: calc(50% - 10rpx);
  226. background: #ffffff;
  227. border-radius: 16rpx;
  228. overflow: hidden;
  229. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.06);
  230. }
  231. .album-cover {
  232. position: relative;
  233. width: 100%;
  234. height: 200rpx;
  235. display: flex;
  236. align-items: center;
  237. justify-content: center;
  238. }
  239. .cover-icon {
  240. font-size: 80rpx;
  241. opacity: 0.6;
  242. }
  243. .album-count {
  244. position: absolute;
  245. bottom: 12rpx;
  246. right: 12rpx;
  247. background: rgba(0, 0, 0, 0.6);
  248. color: #ffffff;
  249. font-size: 22rpx;
  250. padding: 4rpx 12rpx;
  251. border-radius: 8rpx;
  252. }
  253. .album-info {
  254. padding: 20rpx;
  255. }
  256. .album-title {
  257. font-size: 28rpx;
  258. font-weight: 500;
  259. color: #1f2937;
  260. display: block;
  261. overflow: hidden;
  262. text-overflow: ellipsis;
  263. white-space: nowrap;
  264. margin-bottom: 8rpx;
  265. }
  266. .album-desc {
  267. font-size: 22rpx;
  268. color: #9ca3af;
  269. display: -webkit-box;
  270. -webkit-line-clamp: 2;
  271. -webkit-box-orient: vertical;
  272. overflow: hidden;
  273. }
  274. /* 空状态 */
  275. .empty {
  276. display: flex;
  277. flex-direction: column;
  278. align-items: center;
  279. justify-content: center;
  280. padding: 200rpx 0;
  281. }
  282. .empty-icon {
  283. font-size: 120rpx;
  284. margin-bottom: 24rpx;
  285. }
  286. .empty-text {
  287. font-size: 32rpx;
  288. color: #6b7280;
  289. margin-bottom: 12rpx;
  290. }
  291. .empty-hint {
  292. font-size: 26rpx;
  293. color: #9ca3af;
  294. }
  295. /* 加载状态 */
  296. .loading,
  297. .no-more {
  298. text-align: center;
  299. padding: 32rpx;
  300. }
  301. .loading text,
  302. .no-more text {
  303. font-size: 24rpx;
  304. color: #9ca3af;
  305. }
  306. </style>