cyber/src/store/index.js
2025-02-14 23:16:09 +08:00

54 lines
1.6 KiB
JavaScript

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,
userInfo: {}
},
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;
}
},
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'],
})]
})
export default store;