diff --git a/src/App.vue b/src/App.vue index 85af715..ad45c7c 100644 --- a/src/App.vue +++ b/src/App.vue @@ -1,11 +1,10 @@ diff --git a/src/main.ts b/src/main.ts index 2425c0f..3db58d3 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,5 +1,46 @@ 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' -createApp(App).mount('#app') +const pinia = createPinia() + +const app = createApp(App) + +// 配置路由和状态管理 +app.use(router) +app.use(pinia) +app.use(ElementPlus) + +// 初始化userStore +const store = userStore() + +// 使用插件进行持久化,监听 store 变化并保存到本地存储 +store.$subscribe((mutation, state) => { + localStorage.setItem('authStore', JSON.stringify(state)); +}); + +// 监听 action 执行,登录和刷新令牌后保存状态到本地存储 +store.$onAction(({ name, store, args, after, onError }) => { + 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') diff --git a/src/store/userStore.ts b/src/store/userStore.ts index dca6cca..d28381c 100644 --- a/src/store/userStore.ts +++ b/src/store/userStore.ts @@ -1,16 +1,10 @@ import { defineStore } from 'pinia'; import { ref, computed } from 'vue'; import { authService } from '../api/authService'; -import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'; -import { createPinia } from 'pinia'; import { jwtDecode } from 'jwt-decode'; -// 创建 Pinia 实例并使用持久化插件 -const pinia = createPinia(); -pinia.use(piniaPluginPersistedstate); - -// 定义 auth store,包含用户认证相关状态和方法 -export const userStore = defineStore('auth', () => { +// 定义 store,包含用户认证相关状态和方法 +export const userStore = defineStore('user', () => { const accessToken = ref(''); const refreshToken = ref(''); const role = ref(''); @@ -65,26 +59,4 @@ export const userStore = defineStore('auth', () => { } return { accessToken, refreshTokenToken: refreshToken, role, username, userId, isLoggedIn, login, logout, refreshTokenMethod }; -}, { - persist: true, -}); - -// 使用插件进行持久化,监听 store 变化并保存到本地存储 -userStore().$subscribe((mutation, state) => { - localStorage.setItem('authStore', JSON.stringify(state)); -}); - -// 监听 action 执行,登录和刷新令牌后保存状态到本地存储 -userStore().$onAction(({ name, store, args, after, onError }) => { - 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) { - userStore().$patch(persistedState); -} \ No newline at end of file +}); \ No newline at end of file diff --git a/src/views/LoginPage.vue b/src/views/LoginPage.vue new file mode 100644 index 0000000..d964336 --- /dev/null +++ b/src/views/LoginPage.vue @@ -0,0 +1,52 @@ + + + + + \ No newline at end of file diff --git a/src/views/ManagePage.vue b/src/views/ManagePage.vue new file mode 100644 index 0000000..6346205 --- /dev/null +++ b/src/views/ManagePage.vue @@ -0,0 +1,11 @@ + + + + + \ No newline at end of file