|
|
|
@ -1,4 +1,5 @@
|
|
|
|
|
import { createStore } from 'vuex';
|
|
|
|
|
import createPersistedState from 'vuex-persistedstate';
|
|
|
|
|
|
|
|
|
|
const modules = Object.fromEntries(
|
|
|
|
|
Object.entries(import.meta.globEager('./**/*.js')).map((entry) => {
|
|
|
|
@ -17,37 +18,12 @@ const modules = Object.fromEntries(
|
|
|
|
|
const store = createStore({
|
|
|
|
|
strict: process.env.NODE_ENV !== 'production',
|
|
|
|
|
modules,
|
|
|
|
|
plugins: [
|
|
|
|
|
// 所有模块都不持久化到sessionStorage
|
|
|
|
|
createPersistedState({ key: 'store-cache', paths: [], storage: window.sessionStorage }),
|
|
|
|
|
// local模块持久化到localStorage
|
|
|
|
|
createPersistedState({ key: 'store-cache', paths: ['local'], storage: window.localStorage }),
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const storeCache = () => {
|
|
|
|
|
localStorage.setItem('storeCache', JSON.stringify(store.state.local));
|
|
|
|
|
// sessionStorage.setItem('storeCache', JSON.stringify(store.state));
|
|
|
|
|
console.info('[store] store cache');
|
|
|
|
|
};
|
|
|
|
|
const loadCache = () => {
|
|
|
|
|
let storeCache = sessionStorage.getItem('storeCache');
|
|
|
|
|
if (storeCache) {
|
|
|
|
|
let init = _.cloneDeep(store.state);
|
|
|
|
|
storeCache = Object.assign(init, JSON.parse(storeCache));
|
|
|
|
|
sessionStorage.removeItem('storeCache');
|
|
|
|
|
} else {
|
|
|
|
|
storeCache = { ...store.state };
|
|
|
|
|
}
|
|
|
|
|
let localCache = localStorage.getItem('storeCache');
|
|
|
|
|
if (localCache) {
|
|
|
|
|
storeCache.local = _.cloneDeep(store.state.local);
|
|
|
|
|
Object.assign(storeCache.local, JSON.parse(localCache));
|
|
|
|
|
}
|
|
|
|
|
console.info('[store] load cache', storeCache);
|
|
|
|
|
store.replaceState(storeCache);
|
|
|
|
|
};
|
|
|
|
|
window._open = window.open;
|
|
|
|
|
window.open = function () {
|
|
|
|
|
storeCache();
|
|
|
|
|
window._open.apply(window, arguments);
|
|
|
|
|
};
|
|
|
|
|
window.addEventListener('beforeunload', storeCache);
|
|
|
|
|
store.storeCache = storeCache;
|
|
|
|
|
store.loadCache = loadCache;
|
|
|
|
|
|
|
|
|
|
export default store;
|
|
|
|
|