184 lines
5.3 KiB
JavaScript
184 lines
5.3 KiB
JavaScript
import { createStore, storeKey } from "vuex";
|
||
|
||
export default createStore({
|
||
state: {
|
||
// appVersion: "0.0.1",
|
||
// debugStr: "测试debug字符",
|
||
showSongDetail: false, //是否显示歌曲详情
|
||
keywords: "", //查询关键字
|
||
backSnaps: [], //返回快照
|
||
lastHistoryPos: -1,
|
||
lashHistoryLength: 0,
|
||
settings: {
|
||
currentRoute: "/discover/recommend", //当前路由
|
||
songId: 0, //歌曲id
|
||
playing: false, //是否播放
|
||
playMode: 0, //播放模式:0-3,顺序,循环,单曲,随机。
|
||
volume: 100, //音量,最大100
|
||
muted: false, //静音
|
||
lastPlayed: [], //最近播放
|
||
playingList: [], //当前播放
|
||
searchHistory: [], //搜索历史
|
||
},
|
||
caches: {},
|
||
theme: {
|
||
//主题覆盖变量
|
||
themeOverrides: {
|
||
common: {
|
||
primaryColor: "#5C18A0FF",
|
||
primaryColorHover: "#7536ADFF",
|
||
primaryColorPressed: "#690D7EFF",
|
||
primaryColorSuppl: "#511D77FF",
|
||
textColorBase: "#1F1F1FFF",
|
||
},
|
||
},
|
||
zmusic: {},
|
||
},
|
||
},
|
||
getters: {
|
||
//根据当前路由计算主菜单的选择项
|
||
mainMenuSelected: (state) => (menuOptions) => {
|
||
return menuOptions.find((item) => {
|
||
return state.settings.currentRoute.indexOf(item.key) > -1;
|
||
})?.key;
|
||
},
|
||
cache:
|
||
(state) =>
|
||
(key, expire = 0) => {
|
||
if (state.caches[key]) {
|
||
if (
|
||
state.caches[key].time &&
|
||
(expire <= 0 ||
|
||
Date.now() - state.caches[key].time < 1000 * expire)
|
||
)
|
||
return state.caches[key].data;
|
||
}
|
||
return null;
|
||
},
|
||
},
|
||
mutations: {
|
||
//载入settings设置
|
||
loadSettings(state) {
|
||
const l = localStorage.getItem("zmusic.settings");
|
||
if (l) state.settings = { ...state.settings, ...JSON.parse(l) };
|
||
},
|
||
//保存settings设置
|
||
saveSettings(state, settings) {
|
||
state.settings = { ...state.settings, ...settings };
|
||
saveLoaclSettings(state.settings);
|
||
},
|
||
|
||
//载入caches设置
|
||
loadCaches(state) {
|
||
const l = localStorage.getItem("zmusic.caches");
|
||
if (l) state.caches = { ...state.caches, ...JSON.parse(l) };
|
||
},
|
||
//保存caches设置
|
||
saveCaches(state, caches) {
|
||
state.caches = { ...state.caches, ...caches };
|
||
saveLoaclCaches(state.caches);
|
||
},
|
||
|
||
//保存当前路由
|
||
saveCurrentRoute(state, currentRoute) {
|
||
state.settings.currentRoute = currentRoute;
|
||
saveLoaclSettings(state.settings);
|
||
},
|
||
|
||
//载入backSnaps设置
|
||
loadBackSnaps(state) {
|
||
const l = localStorage.getItem("zmusic.backSnaps");
|
||
if (l) state.backSnaps = { ...state.backSnaps, ...JSON.parse(l) };
|
||
},
|
||
//保存backSnaps设置
|
||
saveBackSnaps(state, backSnaps) {
|
||
state.backSnaps = { ...state.backSnaps, ...backSnaps };
|
||
saveLoaclTheme(state.backSnaps);
|
||
},
|
||
saveSnaps(state, snap)
|
||
{
|
||
// state.backSnaps[snap.idx] =
|
||
},
|
||
|
||
//载入theme设置
|
||
loadTheme(state) {
|
||
const l = localStorage.getItem("zmusic.theme");
|
||
if (l) state.theme = { ...state.theme, ...JSON.parse(l) };
|
||
},
|
||
//保存theme设置
|
||
saveTheme(state, theme) {
|
||
state.theme = { ...state.theme, ...theme };
|
||
saveLoaclTheme(state.theme);
|
||
},
|
||
savePlayed(state, played) {
|
||
// console.log(played)
|
||
let p = state.settings.lastPlayed.filter(
|
||
(item, idx) => item.id !== played.id && idx < 99
|
||
);
|
||
p.unshift(played);
|
||
// console.log(p)
|
||
state.settings.lastPlayed = p;
|
||
saveLoaclSettings(state.settings);
|
||
},
|
||
addToPlayingList(state, song) {
|
||
//如果没有,设置为[]
|
||
if (!state.settings.playingList)
|
||
state.settings.playingList = [];
|
||
|
||
let f = state.settings.playingList.find((item) => {
|
||
return item.id === song.id;
|
||
});
|
||
if (!f) {
|
||
// console.log("add to playingList");
|
||
state.settings.playingList.unshift(song);
|
||
saveLoaclSettings(state.settings);
|
||
}
|
||
},
|
||
removePlayingList(state, song) {
|
||
// console.log('removePlayingList', song)
|
||
let idx = state.settings.playingList.findIndex(
|
||
(item) => item.id === song.id
|
||
);
|
||
// console.log(idx)
|
||
if (idx > -1) {
|
||
state.settings.playingList.splice(idx, 1);
|
||
saveLoaclSettings(state.settings);
|
||
}
|
||
},
|
||
// 搜索历史
|
||
addSearchHistory(state, history) {
|
||
let h = state.settings.searchHistory.filter(
|
||
(item) => item != history
|
||
);
|
||
h.unshift(history);
|
||
state.settings.searchHistory = h;
|
||
saveLoaclSettings(state.settings);
|
||
},
|
||
removeSearchHistory(state, history) {
|
||
state.settings.searchHistory = state.settings.searchHistory.filter(
|
||
(item) => item != history
|
||
);
|
||
saveLoaclSettings(state.settings);
|
||
},
|
||
clearSearchHistory(state) {
|
||
state.settings.searchHistory = []
|
||
saveLoaclSettings(state.settings);
|
||
},
|
||
},
|
||
actions: {},
|
||
modules: {},
|
||
});
|
||
|
||
function saveLoaclSettings(s) {
|
||
localStorage.setItem("zmusic.settings", JSON.stringify(s));
|
||
}
|
||
function saveLoaclCaches(s) {
|
||
localStorage.setItem("zmusic.caches", JSON.stringify(s));
|
||
}
|
||
function saveLoaclTheme(s) {
|
||
localStorage.setItem("zmusic.theme", JSON.stringify(s));
|
||
}
|
||
function saveLoaclBackSnaps(s) {
|
||
localStorage.setItem("zmusic.backSnaps", JSON.stringify(s));
|
||
}
|