starmusic-mobile
1<template>
2 <view class="page">
3 <!-- 列表切换 -->
4 <view class="list-tabs">
5 <view
6 v-for="tab in tabs"
7 :key="tab.id"
8 class="tab-item"
9 :class="{ active: currentTab === tab.id }"
10 @click="handleTabChange(tab.id)"
11 >
12 {{ tab.name }}<text v-if="tab.count" class="tab-count">({{ tab.count }})</text>
13 </view>
14 </view>
15
16 <!-- 歌曲列表 -->
17 <view class="list-container">
18 <music-list
19 :list="currentList"
20 :current-music-id="currentMusicId"
21 empty-text="列表为空,去搜索添加歌曲吧"
22 :show-more="true"
23 :batch-mode="batchMode"
24 :batch-selected-ids="batchSelectedIds"
25 @item-click="handleMusicClick"
26 @item-longpress="handleItemLongPress"
27 @more="handleMore"
28 @batch-toggle="handleBatchToggle"
29 />
30 </view>
31
32 <!-- 播放全部 -->
33 <view v-if="currentList.length" class="play-all-bar">
34 <view class="play-all-btn" @click="handlePlayAll">
35 <lx-icon name="play-all" :size="32" color="#fff" />
36 <text>播放全部</text>
37 </view>
38 </view>
39
40 <!-- 批量操作栏 -->
41 <view v-if="batchMode" class="batch-bar">
42 <view class="batch-info">已选 {{ batchSelectedIds.length }} 首</view>
43 <view class="batch-actions">
44 <view class="batch-btn" @click="handleBatchDelete">
45 <lx-icon name="close" :size="24" color="#f56c6c" />
46 <text>删除</text>
47 </view>
48 <view class="batch-btn" @click="handleBatchCollect">
49 <lx-icon name="heart" :size="24" color="#ff4d4f" />
50 <text>{{ currentTab === 'love' ? '取消收藏' : '收藏' }}</text>
51 </view>
52 <view class="batch-btn exit" @click="exitBatchMode">退出</view>
53 </view>
54 </view>
55
56 <!-- 操作弹层 -->
57 <view v-if="showActions" class="mask" @click="showActions = false">
58 <view class="action-sheet" @click.stop>
59 <view class="sheet-title">{{ actionTarget ? actionTarget.name : '' }}</view>
60 <view class="action-item" @click="handleAction('play')">播放</view>
61 <view class="action-item" @click="handleAction('remove')">从列表移除</view>
62 <view class="action-item" @click="handleAction('collect')">
63 {{ actionTarget && isLoved(actionTarget) ? '取消收藏' : '收藏' }}
64 </view>
65 <view class="action-item cancel" @click="showActions = false">取消</view>
66 </view>
67 </view>
68
69 <!-- 底部播放栏 -->
70 <view class="bottom-bar">
71 <player-bar />
72 </view>
73 </view>
74</template>
75
76<script>
77import MusicList from '../../components/music-list.vue'
78import PlayerBar from '../../components/player-bar.vue'
79import LxIcon from '../../components/lx-icon/lx-icon.vue'
80import * as playlist from '../../common/playlist.js'
81import * as player from '../../common/player.js'
82import event from '../../common/event.js'
83
84export default {
85 components: { MusicList, PlayerBar, LxIcon },
86 data() {
87 return {
88 tabs: [
89 { id: 'default', name: '试听列表' },
90 { id: 'love', name: '我的收藏' },
91 ],
92 currentTab: 'default',
93 currentList: [],
94 currentMusicId: '',
95 showActions: false,
96 actionTarget: null,
97 actionIndex: -1,
98 batchMode: false,
99 batchSelectedIds: [],
100 }
101 },
102 onLoad() {
103 playlist.initList()
104 this.loadList()
105 const state = player.getPlayState()
106 if (state.playMusicInfo) this.currentMusicId = state.playMusicInfo.id
107 this.bindEvents()
108 },
109 onShow() {
110 this.loadList()
111 },
112 onUnload() {
113 this.unbindEvents()
114 },
115 methods: {
116 loadList() {
117 this.currentList = playlist.getList(this.currentTab)
118 },
119 bindEvents() {
120 this._offList = event.on('listUpdated', () => {
121 this.loadList()
122 })
123 this._offMusicInfo = event.on('playerMusicInfoChanged', (data) => {
124 this.currentMusicId = data.musicInfo ? data.musicInfo.id : ''
125 })
126 },
127 unbindEvents() {
128 if (this._offList) this._offList()
129 if (this._offMusicInfo) this._offMusicInfo()
130 },
131 handleTabChange(tabId) {
132 this.currentTab = tabId
133 this.loadList()
134 },
135 handleMusicClick(item, index) {
136 player.playListByList(this.currentList, index)
137 },
138 handlePlayAll() {
139 if (!this.currentList.length) return
140 player.playListByList(this.currentList, 0)
141 },
142 handleItemLongPress(item) {
143 if (this.batchMode) {
144 // 批量模式下长按直接切换选中状态
145 const selected = [...this.batchSelectedIds]
146 const idx = selected.indexOf(item.id)
147 if (idx >= 0) {
148 selected.splice(idx, 1)
149 } else {
150 selected.push(item.id)
151 }
152 this.batchSelectedIds = selected
153 return
154 }
155 // 非批量模式:进入批量修改模式,并选中当前歌曲
156 this.batchMode = true
157 this.batchSelectedIds = item.id ? [item.id] : []
158 },
159 handleMore(item) {
160 this.handleItemLongPress(item)
161 },
162 handleAction(action) {
163 if (!this.actionTarget) return
164 const item = this.actionTarget
165 const index = this.actionIndex
166 this.showActions = false
167 if (action === 'play') {
168 player.playListByList(this.currentList, index)
169 } else if (action === 'remove') {
170 playlist.removeListMusics(this.currentTab, [item.id])
171 } else if (action === 'collect') {
172 const isLoved = this.isLoved(item)
173 if (isLoved) {
174 playlist.removeListMusics('love', [item.id])
175 } else {
176 playlist.addListMusics('love', [item])
177 }
178 }
179 },
180 handleBatchToggle(selectedIds) {
181 this.batchSelectedIds = selectedIds
182 },
183 handleBatchDelete() {
184 if (!this.batchSelectedIds.length) return
185 playlist.removeListMusics(this.currentTab, this.batchSelectedIds)
186 this.batchMode = false
187 this.batchSelectedIds = []
188 },
189 handleBatchCollect() {
190 if (!this.batchSelectedIds.length) return
191 const list = this.currentList
192 for (const id of this.batchSelectedIds) {
193 const item = list.find((m) => m.id === id)
194 if (!item) continue
195 const isLoved = this.isLoved(item)
196 if (this.currentTab === 'love') {
197 if (isLoved) playlist.removeListMusics('love', [id])
198 } else {
199 if (!isLoved) playlist.addListMusics('love', item)
200 }
201 }
202 this.batchMode = false
203 this.batchSelectedIds = []
204 },
205 exitBatchMode() {
206 this.batchMode = false
207 this.batchSelectedIds = []
208 },
209 isLoved(item) {
210 return playlist.checkListContainsMusic('love', item.id)
211 },
212 },
213}
214</script>
215
216<style lang="scss" scoped>
217 .page {
218 display: flex;
219 flex-direction: column;
220 height: 100vh;
221 background: #f5f6f8;
222 }
223
224 .list-tabs {
225 display: flex;
226 padding: 16rpx 24rpx;
227 background: #fff;
228 }
229
230 .tab-item {
231 padding: 12rpx 32rpx;
232 margin-right: 16rpx;
233 border-radius: 30rpx;
234 font-size: 26rpx;
235 color: #606266;
236 background: #f2f3f5;
237 }
238
239 .tab-item.active {
240 background: #31c27c;
241 color: #fff;
242 }
243
244 .tab-count {
245 font-size: 22rpx;
246 }
247
248 .list-container {
249 flex: 1;
250 display: flex;
251 flex-direction: column;
252 overflow: hidden;
253 }
254
255 .play-all-bar {
256 padding: 16rpx 24rpx calc(env(safe-area-inset-bottom) + 16rpx);
257 }
258
259 .play-all-btn {
260 height: 84rpx;
261 display: flex;
262 align-items: center;
263 justify-content: center;
264 border-radius: 42rpx;
265 background: #31c27c;
266 color: #fff;
267 font-size: 30rpx;
268 }
269
270 .play-all-btn text {
271 margin-left: 8rpx;
272 }
273
274 .mask {
275 position: fixed;
276 top: 0;
277 left: 0;
278 right: 0;
279 bottom: 0;
280 background: rgba(0, 0, 0, 0.5);
281 z-index: 999;
282 }
283
284 .action-sheet {
285 position: fixed;
286 left: 0;
287 right: 0;
288 bottom: 0;
289 background: #fff;
290 border-radius: 24rpx 24rpx 0 0;
291 padding-bottom: calc(env(safe-area-inset-bottom));
292 z-index: 1000;
293 }
294
295 .sheet-title {
296 padding: 32rpx;
297 font-size: 28rpx;
298 color: #909399;
299 border-bottom: 1rpx solid #f5f6f8;
300 overflow: hidden;
301 text-overflow: ellipsis;
302 white-space: nowrap;
303 }
304
305 .action-item {
306 padding: 28rpx 32rpx;
307 font-size: 30rpx;
308 color: #303133;
309 text-align: center;
310 border-bottom: 1rpx solid #f5f6f8;
311 }
312
313 .action-item.cancel {
314 color: #909399;
315 border-bottom: none;
316 }
317
318 /* 批量修改栏 */
319 .batch-bar {
320 position: fixed;
321 left: 0;
322 right: 0;
323 bottom: 0;
324 background: #fff;
325 border-top: 1rpx solid #e5e6eb;
326 box-shadow: 0 -4rpx 16rpx rgba(0, 0, 0, 0.06);
327 padding: 12rpx 24rpx calc(env(safe-area-inset-bottom) + 12rpx);
328 display: flex;
329 align-items: center;
330 justify-content: space-between;
331 z-index: 998;
332 }
333 .batch-info {
334 font-size: 26rpx;
335 color: #303133;
336 font-weight: 500;
337 }
338 .batch-actions {
339 display: flex;
340 align-items: center;
341 gap: 24rpx;
342 }
343 .batch-btn {
344 display: flex;
345 align-items: center;
346 padding: 8rpx 16rpx;
347 border-radius: 8rpx;
348 background: #f5f6f8;
349 font-size: 24rpx;
350 color: #303133;
351 }
352 .batch-btn text {
353 margin-left: 6rpx;
354 }
355 .batch-btn.exit {
356 background: #e5e6eb;
357 color: #606266;
358 }
359
360 /* 底部播放栏 */
361 .bottom-bar {
362 padding-bottom: env(safe-area-inset-bottom);
363 }
364</style>