-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathmain.js
120 lines (114 loc) · 2.93 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios';
import VueAxios from 'vue-axios';
import Vuex from 'vuex'
Vue.config.productionTip = false
// 用 axios 进行 Ajax 请求
Vue.use(VueAxios, axios);
// Vuex 进行状态管理
Vue.use(Vuex);
const store = new Vuex.Store({
state: {
musicData: [],
skinColor: localStorage.skinColor || '#B72712',
isShowIndex: true,
isPlaying: false,
isAnimation: false,
isShowAsideMenu: false,
isShowMiniMusic: false,
isShowAbout: false,
linkBorderIndex: '',
DOM: {},
audio: {
name: '',
src: '',
musicImgSrc: '',
index: 0
}
},
mutations: {
del(state, index) {
if (state.musicData.length === 0) {
return;
}
state.musicData.splice(index, 1);
},
play(state, flag) {
state.isPlaying = flag;
},
addMusic(state, payload) {
for (let music of state.musicData) {
if (JSON.stringify(music) === JSON.stringify(payload)) {
return;
}
}
state.musicData.unshift(payload);
},
toggleMusic(state, index) {
state.audio.name = state.musicData[index].name;
state.audio.src = state.musicData[index].src;
state.audio.musicImgSrc = state.musicData[index].musicImgSrc;
state.audio.index = index;
},
playMusic(state, payload) {
state.audio.name = payload.name;
state.audio.src = payload.src;
state.audio.musicImgSrc = payload.imgSrc;
state.isPlaying = true;
},
findDOM(state, payload) {
state.DOM[payload.name] = payload.dom;
},
showIndex(state, flag) {
state.isShowIndex = flag;
},
showAsideMenu(state, flag) {
state.isShowAsideMenu = flag;
},
showMiniMusic(state, flag) {
state.isShowMiniMusic = flag;
},
showAbout(state, flag) {
state.isShowAbout = flag;
},
changeLinkBorderIndex(state, index) {
state.linkBorderIndex = index;
},
changeSkinColor(state, color) {
state.skinColor = color;
}
},
actions: {
getData({ commit,state }) {
if (localStorage.musics !== '[]' && localStorage.musics) {
state.musicData = JSON.parse(localStorage.musics);
return;
}
return new Promise((resolve, reject) => {
Vue.axios.get('/api/music-data')
.then (res => {
if (res.data.errno === 0) {
state.musicData = res.data.musicData;
localStorage.musics = JSON.stringify(state.musicData);
}
})
.then(() => {
commit('toggleMusic',0)
});
resolve();
});
}
}
});
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
store,
template: '<App/>',
components: { App }
})