carry b705bbfa7d Add 'frontend/' from commit '94f710850fa74ad3aaca5b65318f2fec9e1a2cdf'
git-subtree-dir: frontend
git-subtree-mainline: 3f114b2cc3ad1bfe7399c1c78b54da47a88a3638
git-subtree-split: 94f710850fa74ad3aaca5b65318f2fec9e1a2cdf
2025-02-17 17:45:26 +08:00

47 lines
1.3 KiB
TypeScript

import { createApp } from 'vue'
import { createPinia } from 'pinia'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import './style.css'
import App from './App.vue'
import router from './router'
import apiClient from './api/axiosInstance'
import { userStore } from './store/userStore'
const pinia = createPinia()
const app = createApp(App)
// 配置路由和状态管理
app.use(router)
app.use(pinia)
app.use(ElementPlus)
// 初始化userStore
const store = userStore()
// 使用插件进行持久化,监听 store 变化并保存到本地存储
store.$subscribe((_, state) => {
localStorage.setItem('authStore', JSON.stringify(state));
});
// 监听 action 执行,登录和刷新令牌后保存状态到本地存储
store.$onAction(({ name, store, after }) => {
if (name === 'login' || name === 'refreshTokenMethod') {
after(() => {
localStorage.setItem('authStore', JSON.stringify(store.$state));
});
}
});
// 初始化时从本地存储恢复状态
const persistedState = JSON.parse(localStorage.getItem('authStore') || '{}');
if (persistedState.accessToken && persistedState.refreshTokenToken && persistedState.role && persistedState.username && persistedState.userId !== null) {
store.$patch(persistedState);
}
// 设置axios拦截器
apiClient
app.mount('#app')