仰望星辰工作室

starmusic-mobile

starmusic-mobile/ components/music-list.vue 10.9 KB · 420 行 原始文件
1<template>
2 <view class="music-list">
3 <view v-if="!list.length && loading" class="empty">加载中...</view>
4 <view v-else-if="!list.length" class="empty">
5 <text class="empty-text">{{ emptyText }}</text>
6 </view>
7 <scroll-view v-else class="list-scroll" scroll-y @scrolltolower="handleLoadMore">
8 <view
9 v-for="(item, index) in visibleList"
10 :key="item.id || index"
11 class="music-item tv-item d-fade-up"
12 data-navid="music-item"
13 :class="{ batch: batchMode, selected: batchMode && isBatchSelected(item) }"
14 :style="{ '--delay': Math.min(index, 8) * 30 + 'ms' }"
15 @click="handleItemClick(item, index)"
16 @longpress="handleItemLongPress(item)"
17 >
18 <!-- 批量模式复选框 -->
19 <view v-if="batchMode" class="batch-check" @click.stop="toggleBatchSelect(item)">
20 <view class="batch-check-box" :class="{ checked: isBatchSelected(item) }">
21 <lx-icon v-if="isBatchSelected(item)" name="check" :size="20" color="#fff" />
22 </view>
23 </view>
24 <view class="item-left">
25 <image
26 v-if="getItemPic(item)"
27 class="item-img"
28 :src="getItemPic(item)"
29 mode="aspectFill"
30 @error="handlePicError(item)"
31 />
32 <view v-else class="item-img item-img-placeholder">
33 <lx-icon name="music" :size="40" color="#c0c4cc" />
34 </view>
35 </view>
36 <view class="item-main">
37 <text class="item-name" :class="{ active: isActive(item) }">{{ item.name }}</text>
38 <view class="item-sub">
39 <text class="item-singer">{{ item.singer }}</text>
40 <text v-if="item.albumName" class="item-album"> - {{ item.albumName }}</text>
41 </view>
42 </view>
43 <view class="item-right">
44 <text v-if="item.interval" class="item-interval">{{ item.interval }}</text>
45 <view v-if="showMore" class="more-btn" data-navid="music-more" @click.stop="handleMore(item)">
46 <lx-icon name="more" :size="36" color="#909399" />
47 </view>
48 </view>
49 </view>
50 <view v-if="hasMore" class="load-more">加载更多...</view>
51 </scroll-view>
52 </view>
53</template>
54
55<script>
56import { getPicUrl, getPicKey } from '../common/music/index.js'
57import LxIcon from './lx-icon/lx-icon.vue'
58
59/** 封面并发请求上限,避免一次搜索结果同时发出几十个请求 */
60const PIC_CONCURRENCY = 5
61
62/**
63 * 单批封面加载数量。
64 * 列表可能有上千首(如我的收藏),一次性排队会打爆网络,
65 * 因此只加载前 PIC_BATCH_SIZE 条,滚动到底部时再扩展一批。
66 */
67const PIC_BATCH_SIZE = 30
68
69/**
70 * 单次渲染的行数上限(滚动到底部再扩展一批)。
71 * 长列表(我的收藏可能有上千首)一次性渲染会长时间占用渲染线程,
72 * 首次进入页面时表现为"白屏 / 点不动",分批渲染可让首屏立刻可见。
73 */
74const RENDER_CHUNK = 40
75
76export default {
77 name: 'MusicList',
78 components: { LxIcon },
79 props: {
80 list: {
81 type: Array,
82 default: () => [],
83 },
84 loading: {
85 type: Boolean,
86 default: false,
87 },
88 hasMore: {
89 type: Boolean,
90 default: false,
91 },
92 emptyText: {
93 type: String,
94 default: '暂无歌曲',
95 },
96 showMore: {
97 type: Boolean,
98 default: false,
99 },
100 currentMusicId: {
101 type: String,
102 default: '',
103 },
104 batchMode: {
105 type: Boolean,
106 default: false,
107 },
108 batchSelectedIds: {
109 type: Array,
110 default: () => [],
111 },
112 /**
113 * 列表标识(如歌单 id / 搜索关键词)。
114 * 变化时把渲染行数收回首批:换歌单/换搜索结果时不该沿用上一个大列表的展开量。
115 * 列表自身的增删(同一标识)不会触发收回,避免浏览长列表时内容突然被截断。
116 */
117 listKey: {
118 type: String,
119 default: '',
120 },
121 },
122 emits: ['item-click', 'item-longpress', 'load-more', 'more', 'batch-toggle', 'batch-select-all', 'batch-cancel'],
123 data() {
124 return {
125 /** 懒加载到的封面:picKey -> url */
126 picMap: {},
127 /** 加载失败的封面:picKey -> true,用于回退占位图 */
128 picFailed: {},
129 /** 当前渲染行数上限(滚动到底部按 RENDER_CHUNK 递增) */
130 renderLimit: RENDER_CHUNK,
131 }
132 },
133 computed: {
134 /** 实际渲染的列表片段:长列表只渲染前 renderLimit 行 */
135 visibleList() {
136 return this.list.length > this.renderLimit ? this.list.slice(0, this.renderLimit) : this.list
137 },
138 },
139 watch: {
140 // 不使用 immediate:watch 的初始化早于 created,
141 // 此时队列字段尚未准备好,首次加载统一交给 mounted 触发
142 list() {
143 this.scheduleLoadPics()
144 },
145 listKey() {
146 this.renderLimit = RENDER_CHUNK
147 },
148 },
149 created() {
150 /** 待请求队列(picKey 去重后的 musicInfo) */
151 this.picQueue = []
152 /** 已入队的 picKey,防止重复排队 */
153 this.picQueued = new Set()
154 /** 当前并发数 */
155 this.picRunning = 0
156 /** 组件是否已销毁,销毁后停止写入 */
157 this.picDestroyed = false
158 /** 已排队的列表长度上界,滚动到底部时扩展 */
159 this.picBound = PIC_BATCH_SIZE
160 },
161 mounted() {
162 this.scheduleLoadPics()
163 },
164 beforeUnmount() {
165 this.picDestroyed = true
166 this.picQueue = []
167 this.picQueued.clear()
168 },
169 methods: {
170 /** 取列表项封面:优先用源自带的 img,其次用懒加载结果 */
171 getItemPic(item) {
172 // 已知加载失败(链接失效/防盗链)的直接走占位图
173 if (this.picFailed[getPicKey(item)]) return ''
174 if (item.img) return item.img
175 if (item.meta && item.meta.picUrl) return item.meta.picUrl
176 return this.picMap[getPicKey(item)] || ''
177 },
178
179 /**
180 * 图片加载失败时回退占位图
181 * tx/mg 的封面地址是按 albumId 拼接的,部分专辑并不存在对应图片,
182 * 这类坏链接只能在 image 报错时才发现。
183 */
184 handlePicError(item) {
185 this.picFailed = { ...this.picFailed, [getPicKey(item)]: true }
186 },
187
188 /**
189 * 把列表中缺封面的项加入请求队列
190 * 只处理 picBound 之前的项,避免长列表一次发出上千请求
191 */
192 scheduleLoadPics() {
193 if (this.picDestroyed || !this.list || !this.list.length) return
194 const bound = Math.min(this.list.length, this.picBound)
195 for (let i = 0; i < bound; i++) {
196 const item = this.list[i]
197 if (!item || !item.source) continue
198 // 源自带封面,无需请求
199 if (item.img || (item.meta && item.meta.picUrl)) continue
200 const key = getPicKey(item)
201 if (this.picQueued.has(key)) continue
202 this.picQueued.add(key)
203 this.picQueue.push(item)
204 }
205 this.runPicQueue()
206 },
207
208 /** 按并发上限消费队列 */
209 runPicQueue() {
210 while (this.picRunning < PIC_CONCURRENCY && this.picQueue.length) {
211 const item = this.picQueue.shift()
212 this.picRunning++
213 getPicUrl(item)
214 .then((url) => {
215 if (this.picDestroyed || !url) return
216 this.picMap = { ...this.picMap, [getPicKey(item)]: url }
217 })
218 .catch(() => {})
219 .finally(() => {
220 this.picRunning--
221 if (!this.picDestroyed) this.runPicQueue()
222 })
223 }
224 },
225
226 handleItemClick(item, index) {
227 if (this.batchMode) {
228 this.toggleBatchSelect(item)
229 return
230 }
231 this.$emit('item-click', item, index)
232 },
233 handleItemLongPress(item) {
234 this.$emit('item-longpress', item)
235 },
236 handleLoadMore() {
237 // 滚动到底部:扩展封面加载范围
238 this.picBound += PIC_BATCH_SIZE
239 this.scheduleLoadPics()
240 // 本地还有未渲染的行:先把这一批渲染出来,等全部渲染完再通知父组件加载下一页
241 if (this.renderLimit < this.list.length) {
242 this.renderLimit += RENDER_CHUNK
243 return
244 }
245 this.$emit('load-more')
246 },
247 handleMore(item) {
248 this.$emit('more', item)
249 },
250 isBatchSelected(item) {
251 return item && item.id && this.batchSelectedIds.includes(item.id)
252 },
253 toggleBatchSelect(item) {
254 if (!item || !item.id) return
255 const selected = [...this.batchSelectedIds]
256 const idx = selected.indexOf(item.id)
257 if (idx >= 0) {
258 selected.splice(idx, 1)
259 } else {
260 selected.push(item.id)
261 }
262 this.$emit('batch-toggle', selected)
263 },
264 isActive(item) {
265 return item.id && item.id === this.currentMusicId
266 },
267 },
268}
269</script>
270
271<style lang="scss" scoped>
272.music-list {
273 flex: 1;
274 min-height: 0;
275 display: flex;
276 flex-direction: column;
277}
278
279.list-scroll {
280 flex: 1;
281 height: 100%;
282}
283
284.empty {
285 padding: 120rpx 0;
286 text-align: center;
287 color: var(--text-muted, #909399);
288}
289
290.empty-text {
291 font-size: 28rpx;
292}
293
294.music-item {
295 display: flex;
296 align-items: center;
297 padding: 16rpx 24rpx;
298 background: var(--card-bg, #fff);
299 border-bottom: 1rpx solid #f0f0f0;
300}
301
302.music-item:active {
303 background: var(--fill-bg, #f7f8fa);
304}
305
306.item-left {
307 width: 88rpx;
308 height: 88rpx;
309 margin-right: 20rpx;
310 border-radius: 12rpx;
311 overflow: hidden;
312 flex-shrink: 0;
313}
314
315.item-img {
316 width: 88rpx;
317 height: 88rpx;
318}
319
320.item-img-placeholder {
321 display: flex;
322 align-items: center;
323 justify-content: center;
324 background: var(--fill-bg, #f2f3f5);
325}
326
327.item-main {
328 flex: 1;
329 min-width: 0;
330 display: flex;
331 flex-direction: column;
332}
333
334.item-name {
335 font-size: 30rpx;
336 color: #303133;
337 overflow: hidden;
338 text-overflow: ellipsis;
339 white-space: nowrap;
340}
341
342.item-name.active {
343 color: var(--theme-color, #31c27c);
344}
345
346.item-sub {
347 margin-top: 8rpx;
348 display: flex;
349 font-size: 24rpx;
350 color: var(--text-muted, #909399);
351 overflow: hidden;
352}
353
354.item-singer,
355.item-album {
356 overflow: hidden;
357 text-overflow: ellipsis;
358 white-space: nowrap;
359}
360
361.item-right {
362 display: flex;
363 align-items: center;
364 margin-left: 16rpx;
365 flex-shrink: 0;
366}
367
368.item-interval {
369 font-size: 24rpx;
370 color: var(--text-muted, #909399);
371}
372
373.more-btn {
374 margin-left: 16rpx;
375 display: flex;
376 align-items: center;
377 justify-content: center;
378 padding: 8rpx 12rpx;
379}
380
381.load-more {
382 padding: 24rpx 0;
383 text-align: center;
384 font-size: 26rpx;
385 color: var(--text-muted, #909399);
386}
387
388/* 批量修改模式样式 */
389.music-item.batch {
390 padding-left: 8rpx;
391}
392.music-item.selected {
393 background: rgba(var(--theme-color-rgb, 49, 194, 124), 0.06);
394}
395.batch-check {
396 width: 48rpx;
397 height: 48rpx;
398 display: flex;
399 align-items: center;
400 justify-content: center;
401 flex-shrink: 0;
402 margin-right: 12rpx;
403}
404.batch-check-box {
405 width: 36rpx;
406 height: 36rpx;
407 border-radius: 8rpx;
408 border: 3rpx solid #c0c4cc;
409 display: flex;
410 align-items: center;
411 justify-content: center;
412 background: #fff;
413 transition: all 0.15s ease;
414}
415.batch-check-box.checked {
416 background: var(--theme-color, #31c27c);
417 border-color: var(--theme-color, #31c27c);
418}
419
420</style>