starmusic-mobile
1<template>
2 <view :class="['page', tvCls]" :style="themeStyle">
3 <!-- 自定义背景图(绘制在页面内容之下) -->
4 <view v-if="bgStyle" class="bg-image" :style="bgStyle" />
5 <tv-nav />
6 <!-- 搜索栏 -->
7 <search-bar v-model="searchText" @search="handleSearch" @focus="handleFocus" @blur="handleBlur" />
8
9 <!-- 搜索联想下拉(输入时推荐候选词) -->
10 <view v-if="tipVisible" class="tip-layer">
11 <view class="tip-mask" @click="handleHideTip" />
12 <view v-if="tipList.length" class="tip-list" data-nav-scope>
13 <view v-for="(word, index) in tipList" :key="index" class="tip-item" data-navid="tip-item" @click="handleTipClick(word)">
14 <lx-icon name="search" :size="26" color="#c0c4cc" />
15 <text class="tip-text">{{ word }}</text>
16 </view>
17 <view v-if="tipLoading" class="tip-item tip-loading">
18 <text class="tip-text">加载中...</text>
19 </view>
20 </view>
21 </view>
22
23 <!-- 搜索平台切换(首页与搜索结果页均可见) -->
24 <view v-if="searchSources.length" class="search-type-bar">
25 <view
26 v-for="source in searchSources"
27 :key="source.id"
28 class="type-item d-press-sm"
29 :class="{ active: currentSource === source.id }"
30 @click="handleSourceChange(source.id)"
31 >
32 {{ source.name }}
33 </view>
34 <!-- 滑块指示条 -->
35 <view
36 class="type-indicator"
37 :style="{ transform: 'translateX(' + typeIndicatorX + 'rpx)', width: typeIndicatorWidth + 'rpx' }"
38 />
39 </view>
40
41 <!-- 空白视图(热搜/历史) -->
42 <view v-if="!searched" class="blank-view">
43 <view v-if="historyList.length" class="history-section d-fade-up">
44 <view class="section-header">
45 <text class="section-title">搜索历史</text>
46 <text class="clear-btn d-press-sm" @click="handleClearHistory">清空</text>
47 </view>
48 <view class="word-wrap">
49 <view
50 v-for="word in historyList"
51 :key="word"
52 class="word-item d-press-sm"
53 @click="handleWordClick(word)"
54 >
55 <text class="word-text">{{ word }}</text>
56 <view class="word-remove d-press-sm" @click.stop="handleRemoveWord(word)">
57 <lx-icon name="close" :size="22" color="#909399" />
58 </view>
59 </view>
60 </view>
61 </view>
62
63 <view v-if="hotList.length" class="hot-section d-fade-up" style="--delay: 60ms">
64 <view class="section-header">
65 <view class="hot-title">
66 <lx-icon name="trend" :size="30" color="theme" />
67 <text class="section-title">热搜榜</text>
68 </view>
69 </view>
70 <view class="hot-list">
71 <view
72 v-for="(word, index) in hotList"
73 :key="word"
74 class="hot-row tv-item d-press"
75 @click="handleWordClick(word)"
76 >
77 <text class="hot-rank" :class="{ top3: index < 3 }">{{ index + 1 }}</text>
78 <text class="hot-word">{{ word }}</text>
79 </view>
80 </view>
81 </view>
82
83 <view v-if="!historyList.length && !hotList.length" class="welcome d-fade-up">
84 <lx-icon name="music" :size="72" color="#c0c4cc" />
85 <text class="welcome-text">搜索你想听的音乐</text>
86 </view>
87 </view>
88
89 <!-- 搜索结果 -->
90 <music-list
91 v-else
92 :list="resultList"
93 :list-key="searchText"
94 :loading="loading"
95 :has-more="hasMore"
96 :current-music-id="currentMusicId"
97 empty-text="未找到相关歌曲,试试其他关键词"
98 @item-click="handleMusicClick"
99 @load-more="handleLoadMore"
100 />
101
102 <!-- 底部播放栏 -->
103 <view class="bottom-bar">
104 <player-bar />
105 </view>
106
107 <!-- 底部导航(液态玻璃,自绘替代原生 tabBar) -->
108 <app-tab-bar />
109 </view>
110</template>
111
112<script>
113import SearchBar from '../../components/search-bar.vue'
114import MusicList from '../../components/music-list.vue'
115import PlayerBar from '../../components/player-bar.vue'
116import LxIcon from '../../components/lx-icon/lx-icon.vue'
117import * as searchApi from '../../common/search.js'
118import * as player from '../../common/player.js'
119import { normalizeMusicInfo, addListMusics, LIST_IDS } from '../../common/playlist.js'
120import event from '../../common/event.js'
121import { deviceClass } from '../../common/tv.js'
122import AppTabBar from '../../components/app-tab-bar.vue'
123
124export default {
125 components: { SearchBar, MusicList, PlayerBar, AppTabBar, LxIcon },
126 data() {
127 return {
128 searchText: '',
129 searched: false,
130 loading: false,
131 resultList: [],
132 historyList: [],
133 hotList: [],
134 searchSources: [],
135 currentSource: 'kw',
136 // 类型滑块指示
137 typeIndicatorX: 0,
138 typeIndicatorWidth: 0,
139 page: 1,
140 allPage: 1,
141 hasMore: false,
142 currentMusicId: '',
143 // 搜索联想(输入候选词)
144 tipList: [],
145 tipVisible: false,
146 tipLoading: false,
147 // 遥控设备端 UI 类名(电视/车机为 'tv-ui ...',手机为空)
148 tvCls: '',
149 }
150 },
151 watch: {
152 // 输入内容变化时触发联想(防抖在 requestTips 内)
153 searchText(val) {
154 // 程序化赋值(搜索/点击候选词等)不触发联想
155 if (this._skipTipWatch) {
156 this._skipTipWatch = false
157 return
158 }
159 this.requestTips(val)
160 },
161 // 平台栏首帧渲染后测量滑块位置(searchSources 在 onLoad 中赋值)
162 searchSources(val) {
163 if (val && val.length) setTimeout(() => this.measureTypeIndicator(), 100)
164 },
165 },
166 onLoad() {
167 player.initPlayer()
168 this.tvCls = deviceClass()
169 this.searchSources = searchApi.getSearchSources()
170 this.currentSource = this.searchSources[0] ? this.searchSources[0].id : 'kw'
171 this.historyList = searchApi.getSearchHistory()
172 this.loadHotSearch()
173
174 const state = player.getPlayState()
175 if (state.playMusicInfo) {
176 this.currentMusicId = state.playMusicInfo.id
177 }
178 this.bindEvents()
179 },
180 onUnload() {
181 this.unbindEvents()
182 if (this._tipTimer) clearTimeout(this._tipTimer)
183 },
184 onBackPress() {
185 // 遥控/实体返回键:先收起联想下拉,而不是直接退出应用
186 if (this.tipVisible) {
187 this.handleHideTip()
188 return true
189 }
190 return false
191 },
192 methods: {
193 bindEvents() {
194 this._offMusicInfo = event.on('playerMusicInfoChanged', (data) => {
195 this.currentMusicId = data.musicInfo ? data.musicInfo.id : ''
196 })
197 },
198 unbindEvents() {
199 if (this._offMusicInfo) this._offMusicInfo()
200 },
201 handleFocus() {
202 // 输入框聚焦时,若已有搜索结果则保留;空输入时回到空白视图
203 if (!this.searchText) this.searched = false
204 this.tipFocused = true
205 // 重新聚焦时若已有候选词则再次显示
206 if (this.searchText.trim() && this.tipList.length) this.tipVisible = true
207 },
208 handleBlur() {
209 this.tipFocused = false
210 // 延迟隐藏,给点击候选词留出时间
211 setTimeout(() => {
212 if (!this.tipFocused) this.tipVisible = false
213 }, 300)
214 },
215 handleHideTip() {
216 this.tipVisible = false
217 this.tipList = []
218 this._tipSeq = (this._tipSeq || 0) + 1 // 使进行中的联想请求过期
219 },
220 // 输入联想:200ms 防抖请求候选词
221 requestTips(keyword) {
222 const text = String(keyword || '').trim()
223 if (this._tipTimer) {
224 clearTimeout(this._tipTimer)
225 this._tipTimer = null
226 }
227 this._tipSeq = (this._tipSeq || 0) + 1
228 const seq = this._tipSeq
229 if (!text) {
230 this.tipList = []
231 this.tipVisible = false
232 this.tipLoading = false
233 return
234 }
235 this.tipVisible = true
236 this.tipLoading = true
237 this._tipTimer = setTimeout(() => {
238 this._tipTimer = null
239 // 联想源固定用酷我(与参考项目一致,目前仅酷我启用了搜索建议)
240 searchApi.getMusicTip('kw', text).then((list) => {
241 if (seq !== this._tipSeq) return // 丢弃过期结果
242 this.tipList = list || []
243 this.tipLoading = false
244 if (!this.tipFocused) this.tipVisible = false
245 })
246 }, 200)
247 },
248 handleTipClick(word) {
249 this.handleSearch(word)
250 },
251 async loadHotSearch() {
252 try {
253 const result = await searchApi.getHotSearch(this.currentSource)
254 this.hotList = result.list
255 } catch (err) {
256 this.hotList = []
257 }
258 },
259 handleSearch(text) {
260 // 点击搜索 / 候选词时,抑制联想 watch 以及收起联想下拉
261 this._skipTipWatch = true
262 this.tipVisible = false
263 this._tipSeq = (this._tipSeq || 0) + 1
264 if (this._tipTimer) {
265 clearTimeout(this._tipTimer)
266 this._tipTimer = null
267 }
268 this.searchText = text
269 this.searched = true
270 this.resultList = []
271 this.page = 1
272 this.allPage = 1
273 searchApi.addHistoryWord(text)
274 this.historyList = searchApi.getSearchHistory()
275 this.loadSearch()
276 },
277 handleWordClick(word) {
278 this.searchText = word
279 this._skipTipWatch = true
280 this.handleSearch(word)
281 },
282 /** 从历史中移除单条(chip 上的 × 直接删除,按钮较小不易误触) */
283 handleRemoveWord(word) {
284 this.historyList = searchApi.removeHistoryWord(word)
285 uni.showToast({ title: '已删除', icon: 'none' })
286 },
287 handleClearHistory() {
288 uni.showModal({
289 title: '清空搜索历史',
290 content: '确定清空所有搜索历史?',
291 success: (res) => {
292 if (res.confirm) {
293 this.historyList = searchApi.clearHistoryList()
294 }
295 },
296 })
297 },
298 /** 测量类型栏滑块位置(px → rpx) */
299 measureTypeIndicator() {
300 const query = uni.createSelectorQuery().in(this)
301 query
302 .selectAll('.type-item')
303 .boundingClientRect((rects) => {
304 if (!rects || !rects.length) return
305 const activeIndex = this.searchSources.findIndex((s) => s.id === this.currentSource)
306 const target = rects[activeIndex >= 0 ? activeIndex : 0]
307 if (!target) return
308 const sys = uni.getSystemInfoSync()
309 const ratio = 750 / (sys.windowWidth || 375)
310 this.typeIndicatorX = Math.round((target.left - (rects[0] ? rects[0].left : 0)) * ratio)
311 this.typeIndicatorWidth = Math.round(target.width * ratio)
312 })
313 .exec()
314 },
315 handleSourceChange(sourceId) {
316 if (this.currentSource === sourceId) return
317 this.currentSource = sourceId
318 this.resultList = []
319 this.page = 1
320 this.allPage = 1
321 // 首页(未搜索)切换平台时刷新该平台的热搜词;搜索态则重新搜索
322 if (this.searched) {
323 this.loadSearch()
324 } else {
325 this.loadHotSearch()
326 }
327 // 滑块平滑移动到新位置(下一帧测量)
328 setTimeout(() => this.measureTypeIndicator(), 30)
329 },
330 async loadSearch() {
331 if (!this.searchText) return
332 this.loading = true
333 try {
334 const result = await searchApi.search(this.searchText, this.page, this.currentSource)
335 this.resultList = [...this.resultList, ...result.list]
336 this.allPage = result.allPage || 1
337 this.hasMore = this.page < this.allPage
338 } catch (err) {
339 uni.showToast({ title: err.message || '搜索失败', icon: 'none' })
340 } finally {
341 this.loading = false
342 }
343 },
344 handleLoadMore() {
345 if (!this.hasMore || this.loading) return
346 this.page++
347 this.loadSearch()
348 },
349 handleMusicClick(item, index) {
350 const musicInfo = normalizeMusicInfo(item)
351 // 搜索结果整体作为播放队列:从点击的这首开始播,播完继续播后续搜索结果
352 const list = this.resultList.map((m) => normalizeMusicInfo(m))
353 // 兜底:未拿到索引时按原列表定位
354 let startIndex = typeof index === 'number' ? index : this.resultList.indexOf(item)
355 if (!(startIndex >= 0 && startIndex < list.length)) startIndex = 0
356 // 仍记录到试听列表(去重置顶),便于之后回听
357 addListMusics(LIST_IDS.DEFAULT, [musicInfo], 'top')
358 player.playListByList(list, startIndex)
359 },
360 },
361}
362</script>
363
364<style lang="scss" scoped>
365.page {
366 display: flex;
367 flex-direction: column;
368 height: 100vh;
369 background: #f5f6f8;
370 /* 大屏(平板/桌面)时内容居中并限制宽度,避免列表拉满全宽 */
371 width: 100%;
372 max-width: 1100px;
373 margin: 0 auto;
374}
375
376.search-type-bar {
377 position: relative;
378 display: flex;
379 padding: 16rpx 24rpx 0;
380 background: var(--card-bg, #fff);
381 /* 半透明底 + 背景模糊:仅背景图开启时生效(变量由 background.js 注入),否则 none 跳过合成 */
382 backdrop-filter: var(--glass-blur, none);
383 -webkit-backdrop-filter: var(--glass-blur, none);
384 overflow-x: auto;
385 overflow-y: hidden;
386}
387
388.type-item {
389 position: relative;
390 z-index: 1;
391 padding: 12rpx 28rpx;
392 margin-right: 16rpx;
393 border-radius: 30rpx;
394 font-size: 26rpx;
395 color: var(--text-secondary, #606266);
396 background: var(--fill-bg, #f2f3f5);
397 white-space: nowrap;
398 transition: color 0.25s ease, background-color 0.25s ease;
399}
400
401.type-item.active {
402 background: rgba(var(--theme-color-rgb, 49, 194, 124), 0.12);
403 color: var(--theme-color-dark, #1ba56a);
404 font-weight: 600;
405}
406
407/* 滑块指示条:平滑横移 */
408.type-indicator {
409 position: absolute;
410 bottom: 0;
411 left: 24rpx;
412 height: 6rpx;
413 border-radius: 3rpx;
414 background: var(--theme-color, #31c27c);
415 transition: transform 0.3s cubic-bezier(0.25, 0.1, 0.25, 1), width 0.3s cubic-bezier(0.25, 0.1, 0.25, 1);
416 z-index: 0;
417}
418
419.blank-view {
420 flex: 1;
421 overflow-y: auto;
422 padding: 24rpx;
423}
424
425.section-header {
426 display: flex;
427 align-items: center;
428 justify-content: space-between;
429 margin-bottom: 20rpx;
430}
431
432.section-title {
433 font-size: 30rpx;
434 font-weight: 600;
435 color: #303133;
436}
437
438.clear-btn {
439 font-size: 24rpx;
440 color: var(--theme-color, #31c27c);
441}
442
443.word-wrap {
444 display: flex;
445 flex-wrap: wrap;
446}
447
448/* 历史词:白底胶囊 + 右侧删除按钮 */
449.word-item {
450 display: flex;
451 align-items: center;
452 max-width: 100%;
453 padding: 12rpx 12rpx 12rpx 26rpx;
454 margin: 0 16rpx 16rpx 0;
455 border-radius: 30rpx;
456 background: var(--card-bg, #fff);
457 font-size: 26rpx;
458 color: #303133;
459 box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.03);
460}
461
462.word-text {
463 max-width: 400rpx;
464 overflow: hidden;
465 text-overflow: ellipsis;
466 white-space: nowrap;
467}
468
469.word-remove {
470 margin-left: 10rpx;
471 width: 34rpx;
472 height: 34rpx;
473 border-radius: 50%;
474 display: flex;
475 align-items: center;
476 justify-content: center;
477 flex-shrink: 0;
478}
479
480/* 热搜榜:标题 + 一行一条的排名列表 */
481.hot-title {
482 display: flex;
483 align-items: center;
484}
485
486.hot-title .lx-icon {
487 margin-right: 10rpx;
488}
489
490.hot-list {
491 display: flex;
492 flex-direction: column;
493}
494
495.hot-row {
496 display: flex;
497 align-items: center;
498 padding: 22rpx 8rpx;
499 border-radius: 12rpx;
500}
501
502.hot-rank {
503 width: 56rpx;
504 flex-shrink: 0;
505 text-align: center;
506 font-size: 30rpx;
507 font-weight: 600;
508 color: var(--text-muted, #909399);
509}
510
511.hot-rank.top3 {
512 color: var(--theme-color, #31c27c);
513}
514
515.hot-word {
516 flex: 1;
517 min-width: 0;
518 margin-left: 16rpx;
519 font-size: 30rpx;
520 color: #303133;
521 overflow: hidden;
522 text-overflow: ellipsis;
523 white-space: nowrap;
524}
525
526.history-section {
527 margin-bottom: 40rpx;
528}
529
530.welcome {
531 padding: 200rpx 0;
532 display: flex;
533 flex-direction: column;
534 align-items: center;
535 color: var(--text-muted, #909399);
536}
537
538.welcome-text {
539 margin-top: 24rpx;
540 font-size: 30rpx;
541}
542
543.bottom-bar {
544 padding-bottom: env(safe-area-inset-bottom);
545}
546
547/* 搜索联想下拉 */
548.tip-layer {
549 position: fixed;
550 top: 104rpx;
551 left: 0;
552 right: 0;
553 bottom: 0;
554 z-index: 99;
555 pointer-events: none;
556}
557.tip-mask {
558 position: absolute;
559 top: 0;
560 left: 0;
561 right: 0;
562 bottom: 0;
563 background: rgba(0, 0, 0, 0.2);
564 pointer-events: auto;
565}
566.tip-list {
567 position: absolute;
568 top: 0;
569 left: 24rpx;
570 right: 24rpx;
571 background: var(--card-bg-strong, #fff);
572 border-radius: 16rpx;
573 box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.12);
574 overflow: hidden;
575 pointer-events: auto;
576 max-height: 540rpx;
577 /* 超出滚动 */
578}
579.tip-item {
580 display: flex;
581 align-items: center;
582 padding: 22rpx 24rpx;
583 border-bottom: 1rpx solid #f5f6f8;
584}
585.tip-item:last-child {
586 border-bottom: none;
587}
588.tip-text {
589 margin-left: 14rpx;
590 font-size: 28rpx;
591 color: #303133;
592}
593.tip-item.tip-loading {
594 color: var(--text-muted, #909399);
595}
596
597</style>