cyber/src/store/index.js

65 lines
1.9 KiB
JavaScript
Raw Normal View History

2025-02-14 23:14:54 +08:00
import { createStore } from 'vuex';
import createPersistedStatePlugin from '../plugins/vuexLocalStorage';
import api from "../utils/axios.js";
import {getDomain} from "../utils/getDomain.js";
const store = createStore({
state: {
theme: localStorage.getItem('theme') || 'dark',
loading: {},
token: null,
2025-02-17 22:33:00 +08:00
userInfo: {},
editStore: {},
demos: {},
2025-02-14 23:14:54 +08:00
},
mutations: {
toggleTheme(state) {
state.theme = state.theme === 'dark' ? 'light' : 'dark'
},
setToken(state, token) {
state.token = token;
},
setUserInfo(state, obj) {
if (obj === {} || !obj) {
state.userInfo = {};
}
state.userInfo = {...state.userInfo, ...obj};
},
startLoading(state, text) {
state.loading.isLoading = {isLoading: true, text: '请稍后...'};
if (text) {
state.loading.text = text;
}
},
stopLoading(state) {
state.loading.isLoading = false;
2025-02-17 22:33:00 +08:00
},
setDemoValue(state, obj) {
state.demos[obj.demo] = {...state.demos[obj.demo], ...obj.value}
},
saveEdit(state, obj) {
state.editStore = {...state.editStore, ...obj};
},
setLogTemp(state, arr) {
state.log = arr;
2025-02-14 23:14:54 +08:00
}
},
getters: {
currentTheme: state => state.theme,
hasUserInfo: state => !!state.userInfo.uid,
profileImage: state => {
if (state.userInfo.profile) {
return `https://${getDomain()}/data/user/profile/` + state.userInfo.profile;
}else {
return `https://${getDomain()}/data/user/profile/default.jpg`
}
}
},
plugins: [createPersistedStatePlugin({
key: 'cyberStorage',
whitelist: ['theme', 'userInfo', 'demos', 'editStore'],
2025-02-14 23:14:54 +08:00
})]
})
export default store;