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

Commit 6d680dd

Browse files
committed
完成 - 添加 / 删除喜欢的歌曲的所有逻辑
1 parent 73265a6 commit 6d680dd

File tree

4 files changed

+40
-28
lines changed

4 files changed

+40
-28
lines changed

src/common/js/mixin.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@ export const playlistMixin = {
3636

3737
export const playerMixin = {
3838
methods: {
39+
toggleIsFavorite (song) {
40+
if (this.isFavoriteSong(song)) {
41+
this.deleteMyFavoriteSong(song)
42+
} else {
43+
this.saveMyFavoriteSong(song)
44+
}
45+
},
46+
47+
getFavoriteIcon (song) {
48+
return this.isFavoriteSong(song) ? 'icon-like' : 'icon-not-like'
49+
},
50+
51+
isFavoriteSong (song) {
52+
return this.myFavoriteSongs.findIndex(item => item.id === song.id) > -1
53+
},
54+
3955
togglePlayMode () {
4056
const mode = (this.mode + 1) % 3
4157
this.setPlayMode(mode)
@@ -68,7 +84,12 @@ export const playerMixin = {
6884
setCurrentIndex: 'SET_CURRENT_INDEX',
6985
setPlayMode: 'SET_PLAY_MODE',
7086
setPlaylist: 'SET_PLAYLIST'
71-
})
87+
}),
88+
89+
...mapActions([
90+
'saveMyFavoriteSong',
91+
'deleteMyFavoriteSong'
92+
])
7293
},
7394

7495
computed: {
@@ -80,7 +101,8 @@ export const playerMixin = {
80101
'playlist',
81102
'mode',
82103
'sequenceList',
83-
'currentSong'
104+
'currentSong',
105+
'myFavoriteSongs'
84106
])
85107
}
86108
}

src/components/app-player/app-player.vue

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
<i class="icon-next" @click="nextSong"></i>
9999
</div>
100100
<div class="icon icon-right">
101-
<i :class="['icon', hasFavorite]" @click="toggleFavorite"></i>
101+
<i :class="['icon', getFavoriteIcon(currentSong)]" @click="toggleIsFavorite(currentSong)"></i>
102102
</div>
103103
</div>
104104
</div>
@@ -174,8 +174,7 @@ export default {
174174
currentLyric: null,
175175
currentLineNum: 0,
176176
currentShow: 'cd',
177-
playingLyric: '',
178-
hasFavoriteSong: false
177+
playingLyric: ''
179178
}
180179
},
181180
@@ -215,16 +214,6 @@ export default {
215214
this.setFullScreen(true)
216215
},
217216
218-
toggleFavorite () {
219-
this.hasFavoriteSong = !this.hasFavoriteSong
220-
221-
if (this.hasFavoriteSong) {
222-
this.saveMyFavoriteSong(this.currentSong)
223-
} else {
224-
this.deleteMyFavoriteSong(this.currentSong)
225-
}
226-
},
227-
228217
showPlaylist () {
229218
this.$refs.playlist.showPlaylist()
230219
},
@@ -460,9 +449,7 @@ export default {
460449
}),
461450
462451
...mapActions([
463-
'savePlayedHistory',
464-
'saveMyFavoriteSong',
465-
'deleteMyFavoriteSong'
452+
'savePlayedHistory'
466453
])
467454
},
468455
@@ -501,10 +488,6 @@ export default {
501488
},
502489
503490
computed: {
504-
hasFavorite () {
505-
return this.hasFavoriteSong ? 'icon-like' : 'icon-not-like'
506-
},
507-
508491
rotateCD () {
509492
return this.playing ? 'play' : 'play pause'
510493
},

src/components/parts-playlist/parts-playlist.vue

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
>
3131
<i :class="['current', getCurrentIcon(song)]"></i>
3232
<span class="item-text">{{ song.name }}</span>
33-
<span class="like-btn">
34-
<i class="icon-not-like"></i>
33+
<span class="like-btn" @click.stop="toggleIsFavorite(song)">
34+
<i :class="getFavoriteIcon(song)"></i>
3535
</span>
3636
<span class="delete-btn" @click.stop="deleteSong(song, index)">
3737
<i class="icon-close"></i>
@@ -262,6 +262,9 @@ export default {
262262
}
263263
.like-btn {
264264
margin-right: 15px;
265+
.icon-like {
266+
color: $color-sub-theme;
267+
}
265268
}
266269
}
267270
}

src/components/the-user-center/the-user-center.vue

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,19 @@ export default {
6565
6666
methods: {
6767
addAllSongsToList () {
68-
if (this.switchIndex === 0) {
68+
let list = []
6969
70+
if (this.switchIndex === 0) {
71+
list = this.myFavoriteSongs.map(song => {
72+
return new Song(song)
73+
})
7074
} else {
71-
const list = this.playedHistory.map(song => {
75+
list = this.playedHistory.map(song => {
7276
return new Song(song)
7377
})
74-
75-
this.randomPlay({ list })
7678
}
79+
80+
this.randomPlay({ list })
7781
},
7882
7983
addSongToList (song, index) {

0 commit comments

Comments
 (0)