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

Commit 41e5ffc

Browse files
committed
修改 - 提取 playerMixin
1 parent 8ce40af commit 41e5ffc

File tree

5 files changed

+152
-114
lines changed

5 files changed

+152
-114
lines changed

src/base/base-confirm.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<transition name="confirm-fade">
3-
<div class="confirm" v-show="showFlag" @click.self="chooseCancel">
3+
<div class="confirm" v-show="showFlag" @click.stop.self="chooseCancel">
44
<div class="confirm-wrapper">
55
<div class="confirm-content">
66
<p class="text">{{ title }}</p>

src/common/js/mixin.js

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
* mixin 中方法均会被各个组件中的同名属性方法覆盖
33
*/
44

5-
import { mapGetters } from 'vuex'
5+
import { mapGetters, mapMutations } from 'vuex'
6+
import { playMode } from 'common/js/config'
7+
import { shuffle } from 'common/js/util'
68

79
export const playlistMixin = {
810
computed: {
@@ -31,3 +33,54 @@ export const playlistMixin = {
3133
}
3234
}
3335
}
36+
37+
export const playerMixin = {
38+
methods: {
39+
togglePlayMode () {
40+
const mode = (this.mode + 1) % 3
41+
this.setPlayMode(mode)
42+
43+
let list = null
44+
if (mode === playMode.random) {
45+
list = shuffle(this.sequenceList) // 打乱顺序
46+
} else {
47+
list = this.sequenceList
48+
}
49+
this.resetCurrentIndex(list)
50+
this.setPlaylist(list)
51+
/**
52+
* 经试验,playlist 的改变不会导致 currentSong 的重新计算(在 watch 中设定一
53+
* 个 console ),currentIndex 会导致触发 watcher ,即 currentSong 会重新计
54+
* 算。
55+
*/
56+
},
57+
// 保证 playlist 改变时不会改变当前播放歌曲
58+
resetCurrentIndex (list) {
59+
let index = list.findIndex(item => {
60+
return item.id === this.currentSong.id
61+
}) // 在修改后的 list 中找到当前播放的 index
62+
63+
this.setCurrentIndex(index)
64+
},
65+
66+
...mapMutations({
67+
setPlayingState: 'SET_PLAYING_STATE',
68+
setCurrentIndex: 'SET_CURRENT_INDEX',
69+
setPlayMode: 'SET_PLAY_MODE',
70+
setPlaylist: 'SET_PLAYLIST'
71+
})
72+
},
73+
74+
computed: {
75+
iconMode () {
76+
return this.mode === playMode.sequence ? 'icon-sequence' : this.mode === playMode.loop ? 'icon-loop' : 'icon-random'
77+
},
78+
79+
...mapGetters([
80+
'playlist',
81+
'mode',
82+
'sequenceList',
83+
'currentSong'
84+
])
85+
}
86+
}

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

Lines changed: 20 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
@enter="enter"
77
@after-enter="afterEnter"
88
@after-leave="afterLeave"
9-
><!-- 待解决 @leave="leave" 与 leave-active leave-to 冲突 -->
9+
type="transition"
10+
><!-- FIXME: @leave="leave" 与 leave-active leave-to 冲突 -->
1011
<div class="normal-player" v-show="fullScreen">
1112
<div class="background">
1213
<img
@@ -31,7 +32,7 @@
3132
@touchend.prevent="middleTouchEnd"
3233
>
3334
<div class="middle-album" ref="album">
34-
<div class="cd-wrapper" ref="cd">
35+
<div class="cd-wrapper" ref="cdWrapper">
3536
<div :class="['cd', rotateCD]">
3637
<img
3738
:src="currentSong.image"
@@ -79,7 +80,7 @@
7980
<!-- 播放控件 -->
8081
<div class="player-operators">
8182

82-
<div class="icon icon-left" @click="changeMode">
83+
<div class="icon icon-left" @click="togglePlayMode">
8384
<i :class="iconMode"></i>
8485
</div>
8586
<div :class="['icon', 'icon-left', disableBtn]">
@@ -152,14 +153,15 @@ import PartsPlaylist from 'components/parts-playlist/parts-playlist'
152153
import { mapGetters, mapMutations } from 'vuex'
153154
import { prefixStyle } from 'common/js/control-dom'
154155
import { playMode } from 'common/js/config'
155-
import { shuffle } from 'common/js/util'
156+
import { playerMixin } from 'common/js/mixin'
156157
import animations from 'create-keyframe-animation'
157158
import LyricParser from 'lyric-parser'
158159
159160
const transform = prefixStyle('transform')
160161
const transitionDuration = prefixStyle('transitionDuration')
161162
162163
export default {
164+
mixins: [playerMixin],
163165
data () {
164166
return {
165167
songReady: false, // 用于限制点击行为
@@ -261,33 +263,6 @@ export default {
261263
}
262264
},
263265
264-
changeMode () {
265-
const mode = (this.mode + 1) % 3
266-
this.setPlayMode(mode)
267-
268-
let list = null
269-
if (mode === playMode.random) {
270-
list = shuffle(this.sequenceList) // 打乱顺序
271-
} else {
272-
list = this.sequenceList
273-
}
274-
this.resetCurrentIndex(list)
275-
this.setPlaylist(list)
276-
/**
277-
* 经试验,playlist 的改变不会导致 currentSong 的重新计算(在 watch 中设定一
278-
* 个 console ),currentIndex 会导致触发 watcher ,即 currentSong 会重新计
279-
* 算。
280-
*/
281-
},
282-
// 保证 playlist 改变时不会改变当前播放歌曲
283-
resetCurrentIndex (list) {
284-
let index = list.findIndex(item => {
285-
return item.id === this.currentSong.id
286-
}) // 在修改后的 list 中找到当前播放的 index
287-
288-
this.setCurrentIndex(index)
289-
},
290-
291266
endPlay () {
292267
if (this.mode === playMode.loop) {
293268
this.loopPlay()
@@ -358,25 +333,28 @@ export default {
358333
}
359334
})
360335
// done 为 enter 钩子的回调函数,执行效果是调用下一阶段钩子,这里是 afterEnter
361-
animations.runAnimation(this.$refs.cd, 'move', done)
336+
animations.runAnimation(this.$refs.cdWrapper, 'move', done)
362337
},
363338
364339
afterEnter () {
365340
animations.unregisterAnimation('move')
366-
this.$refs.cd.style.animation = ''
341+
this.$refs.cdWrapper.style.animation = ''
367342
},
368343
369344
leave (el, done) {
345+
this.$refs.cdWrapper.style.transition = `all 0.4s`
346+
370347
const { x, y, scale } = this._getPositionAndScale()
348+
371349
// const transform = prefixStyle('transform')
372-
this.$refs.cd.style[transform] = `translate3d(${x}, ${y}, 0) scale(${scale})`
373-
this.$refs.cd.style.transition = `all 0.4s`
374-
this.$refs.cd.addEventListener('transitioned', done)
350+
this.$refs.cdWrapper.style[transform] = `translate3d(${x}, ${y}, 0) scale(${scale})`
351+
this.$refs.cdWrapper.addEventListener('transitionend', done)
352+
done()
375353
},
376354
377355
afterLeave () {
378-
this.$refs.cd.style.transition = ''
379-
this.$refs.cd.style[transform] = ''
356+
this.$refs.cdWrapper.style.transition = ''
357+
this.$refs.cdWrapper.style[transform] = ''
380358
},
381359
382360
middleTouchStart (evt) {
@@ -458,11 +436,7 @@ export default {
458436
},
459437
460438
...mapMutations({
461-
setFullScreen: 'SET_FULL_SCREEN',
462-
setPlayingState: 'SET_PLAYING_STATE',
463-
setCurrentIndex: 'SET_CURRENT_INDEX',
464-
setPlayMode: 'SET_PLAY_MODE',
465-
setPlaylist: 'SET_PLAYLIST'
439+
setFullScreen: 'SET_FULL_SCREEN'
466440
})
467441
},
468442
@@ -479,7 +453,8 @@ export default {
479453
watch: {
480454
// nextTick 防止报错 DOMException: The play() request was interrupted by a new load request (原因:异步请求)
481455
currentSong (newSong, oldSong) {
482-
if (newSong.id === oldSong.id) return // 防止暂停时切模式自动播放问题
456+
// 当列表清空时检验 !newSong.id
457+
if (!newSong.id || newSong.id === oldSong.id) return // 防止暂停时切模式自动播放问题
483458
484459
if (this.currentLyric) {
485460
this.currentLyric.stop() // 暂停之前 LyricParser 实例
@@ -512,18 +487,10 @@ export default {
512487
return this.currentTime / this.currentSong.duration
513488
},
514489
515-
iconMode () {
516-
return this.mode === playMode.sequence ? 'icon-sequence' : this.mode === playMode.loop ? 'icon-loop' : 'icon-random'
517-
},
518-
519490
...mapGetters([
520491
'fullScreen',
521-
'playlist',
522-
'currentSong',
523492
'playing',
524-
'currentIndex',
525-
'mode',
526-
'sequenceList'
493+
'currentIndex'
527494
])
528495
},
529496
@@ -757,7 +724,6 @@ export default {
757724
}
758725
&.normal-enter, &.normal-leave-to {
759726
opacity: 0;
760-
// Chrome 有一定几率出现只有一个类(top / bottom)被正常添加(进入动画),IOS 正常
761727
.parts-top {
762728
transform: translate3d(0, -100px, 0);
763729
}

0 commit comments

Comments
 (0)