Skip to content
This repository was archived by the owner on May 11, 2021. It is now read-only.

Commit becbf74

Browse files
committed
完成 - search page 点击播放和歌手跳转
1 parent afab3ad commit becbf74

File tree

4 files changed

+111
-20
lines changed

4 files changed

+111
-20
lines changed

src/components/parts-search-result/parts-search-result.vue

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,22 @@
66
@scrollToEnd="searchMore"
77
>
88
<ul class="result-list">
9-
<li class="result-item" v-for="(item, index) of searchResult" :key="index">
9+
<li
10+
class="result-item"
11+
v-for="(item, index) of searchResult"
12+
:key="index"
13+
@click="selectItem(item)"
14+
>
1015
<div class="item-icon">
1116
<i :class="getIconClass(item)"></i>
1217
</div>
1318
<div class="item-name">
1419
<p class="text" v-html="getDisplayName(item)"></p>
1520
</div>
1621
</li>
17-
<!-- <BaseLoading v-show="hasMore"/> -->
22+
<BaseLoading v-show="hasMore" title=""/>
1823
</ul>
24+
<router-view/>
1925
</BaseScroll>
2026
</template>
2127

@@ -25,6 +31,8 @@ import BaseLoading from 'base/base-loading/base-loading'
2531
import { searchKey } from 'api/the-search'
2632
import { ERR_OK } from 'api/config'
2733
import { createSong } from 'common/js/normalize-song'
34+
import { mapMutations, mapActions } from 'vuex'
35+
import Artist from 'common/js/normalize-artist'
2836
2937
const TYPE_SINGER = 'singer'
3038
const PER_PAGE = 20 // 每一页的结果总数
@@ -33,8 +41,7 @@ export default {
3341
props: {
3442
query: {
3543
type: String,
36-
default: '',
37-
hasMore: true // 是否还有数据未请求
44+
default: ''
3845
},
3946
4047
showArtist: {
@@ -47,11 +54,26 @@ export default {
4754
return {
4855
page: 1,
4956
searchResult: [],
57+
hasMore: true, // 是否还有数据未请求
5058
pullUp: true
5159
}
5260
},
5361
5462
methods: {
63+
selectItem (item) {
64+
if (item.type === TYPE_SINGER) {
65+
const artist = new Artist({
66+
id: item.singermid,
67+
name: item.singername
68+
})
69+
70+
this.setArtist(artist)
71+
this.$router.push({ path: `/search/${artist.id}` })
72+
} else {
73+
this.insertSong(item)
74+
}
75+
},
76+
5577
searchMore () {
5678
if (!this.hasMore) return
5779
@@ -84,7 +106,7 @@ export default {
84106
},
85107
86108
search () {
87-
this.page = 1
109+
this.page = 1 // 未清空输入框就再次执行搜索
88110
this.hasMore = true
89111
this.$refs.scroll.scrollTo(0, 0)
90112
searchKey(this.query, this.page, this.showArtist, PER_PAGE).then(res => {
@@ -131,7 +153,15 @@ export default {
131153
})
132154
133155
return ret
134-
}
156+
},
157+
158+
...mapMutations({
159+
setArtist: 'SET_ARTIST'
160+
}),
161+
162+
...mapActions([
163+
'insertSong'
164+
])
135165
},
136166
137167
watch: {

src/components/the-artist/artist.vue

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ export default {
2828
},
2929
3030
methods: {
31+
selectArtist (artist) {
32+
// path 覆盖 params
33+
this.$router.push({ path: `/artist/${artist.id}` })
34+
this.setArtist(artist)
35+
},
36+
37+
handlePlaylist (playlist) {
38+
const bottom = playlist.length > 0 ? '60px' : ''
39+
40+
this.$refs.artist.style.bottom = bottom
41+
this.$refs.artistList.refresh()
42+
},
43+
3144
// handlePlayList (playlist) {
3245
// const bottom = playlist.length > 0 ? '60px' : ''
3346
// this.$refs.artist.style.bottom = bottom
@@ -93,19 +106,6 @@ export default {
93106
return [...hot, ...ret, ...other]
94107
},
95108
96-
selectArtist (artist) {
97-
// path 覆盖 params
98-
this.$router.push({ path: `/artist/${artist.id}` })
99-
this.setArtist(artist)
100-
},
101-
102-
handlePlaylist (playlist) {
103-
const bottom = playlist.length > 0 ? '60px' : ''
104-
105-
this.$refs.artist.style.bottom = bottom
106-
this.$refs.artistList.refresh()
107-
},
108-
109109
...mapMutations({
110110
setArtist: 'SET_ARTIST' // 将 `this.setArtist()` 映射为 `this.$store.commit('SET_ARTIST')`
111111
})

src/router/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@ export default new Router({
4848
},
4949
{
5050
path: '/search',
51-
component: Search
51+
component: Search,
52+
children: [
53+
{
54+
path: ':id',
55+
component: ArtistItem
56+
}
57+
]
5258
}
5359
]
5460
})

src/store/actions.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,58 @@ export const randomPlay = ({ commit }, { list }) => {
3636
commit(types.SET_FULL_SCREEN, true)
3737
commit(types.SET_PLAYING_STATE, true)
3838
}
39+
40+
export const insertSong = ({ commit, state }, song) => {
41+
/**
42+
* 为了在之后使用 Array.prototype.splice() 方法,这里使用拓展运算符浅复制数组
43+
* 原因:
44+
* 1. 因为在 JavaScript 中操作对象的实质是操作对象指针,那么在不复制数组的情况下使
45+
* 用数组 splice() 方法将会修改原数组。
46+
* 2. 第一点的这个行为与在 vuex 中必须通过mutation 来修改 state 的原则相悖。继而报
47+
* 错 Do not mutate vuex store state outside mutation handlers。
48+
*/
49+
50+
let playlist = [...state.playlist]
51+
let sequenceList = [...state.sequenceList]
52+
let currentIndex = state.currentIndex // 当前在 playlist 中的索引
53+
54+
// 记录当前歌曲
55+
let currentSong = playlist[currentIndex]
56+
// 判断当前歌曲是否已存在,若存在则返回索引
57+
let fqIndex = findIndex(playlist, song)
58+
// 因为是插入歌曲,所以索引 +1
59+
currentIndex++
60+
// 插入目标歌曲到当前索引位置
61+
playlist.splice(currentIndex, 0, song)
62+
// 查找结果是存在目标歌曲
63+
if (fqIndex > -1) {
64+
if (currentIndex > fqIndex) {
65+
// 若当前插入索引大于列表中的索引,即在原索引之后
66+
playlist.splice(fqIndex, 1)
67+
currentIndex-- // 因为原单项已被删除,所以 -1
68+
} else {
69+
// 若当前插入索引不大于列表中的索引,即在原索引之前
70+
playlist.splice(fqIndex + 1, 1)
71+
}
72+
}
73+
74+
let sequenceCurrentIndex = findIndex(sequenceList, currentSong) + 1
75+
76+
let fsIndex = findIndex(sequenceList, song)
77+
78+
sequenceList.splice(sequenceCurrentIndex, 0, song)
79+
80+
if (fsIndex > -1) {
81+
if (sequenceCurrentIndex > fsIndex) {
82+
sequenceList.splice(fsIndex, 1)
83+
} else {
84+
sequenceList.splice(fsIndex + 1, 1)
85+
}
86+
}
87+
88+
commit(types.SET_PLAYLIST, playlist)
89+
commit(types.SET_SEQUENCE_LIST, sequenceList)
90+
commit(types.SET_CURRENT_INDEX, currentIndex)
91+
commit(types.SET_FULL_SCREEN, true)
92+
commit(types.SET_PLAYING_STATE, true)
93+
}

0 commit comments

Comments
 (0)