From 721d4f768540463dce304dbb133f97bed0c490c7 Mon Sep 17 00:00:00 2001 From: Aqr-K <95741669+Aqr-K@users.noreply.github.com> Date: Mon, 24 Feb 2025 19:26:56 +0800 Subject: [PATCH 1/3] refactor: replace Vuex with Pinia --- src/api/index.ts | 14 ++- src/components/cards/MediaCard.vue | 6 +- src/components/cards/UserCard.vue | 15 +-- src/components/dialog/UserAddEditDialog.vue | 13 ++- src/layouts/components/DefaultLayout.vue | 7 +- src/layouts/components/UserProfile.vue | 20 ++-- src/main.ts | 13 +-- src/pages/appcenter.vue | 6 +- src/pages/dashboard.vue | 6 +- src/pages/login.vue | 56 ++++++----- src/router/index.ts | 8 +- src/store/auth.ts | 96 ------------------- src/store/index.ts | 19 ---- src/stores/auth.ts | 42 ++++++++ src/stores/index.ts | 16 ++++ src/stores/types.ts | 23 +++++ src/stores/user.ts | 62 ++++++++++++ .../dashboard/AnalyticsWeeklyOverview.vue | 7 +- src/views/discover/MediaDetailView.vue | 6 +- src/views/reorganize/DownloadingListView.vue | 11 ++- src/views/subscribe/SubscribeListView.vue | 15 +-- src/views/system/SearchBarView.vue | 9 +- src/views/user/UserProfileView.vue | 21 ++-- 23 files changed, 269 insertions(+), 222 deletions(-) delete mode 100644 src/store/auth.ts delete mode 100644 src/store/index.ts create mode 100644 src/stores/auth.ts create mode 100644 src/stores/index.ts create mode 100644 src/stores/types.ts create mode 100644 src/stores/user.ts diff --git a/src/api/index.ts b/src/api/index.ts index 89b8ba34..4ec94ee3 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,6 +1,6 @@ import axios from 'axios' import router from '@/router' -import store from '@/store' +import { useAuthStore } from '@/stores' // 创建axios实例 const api = axios.create({ @@ -9,10 +9,12 @@ const api = axios.create({ // 添加请求拦截器 api.interceptors.request.use(config => { + // 认证 Store + const authStore = useAuthStore() // 在请求头中添加token - const token = store.state.auth.token - if (token) config.headers.Authorization = `Bearer ${token}` - + if (authStore.token) { + config.headers.Authorization = `Bearer ${authStore.token}` + } return config }) @@ -26,8 +28,10 @@ api.interceptors.response.use( // 请求超时 return Promise.reject(new Error(error)) } else if (error.response.status === 403) { + // 认证 Store + const authStore = useAuthStore() // 清除登录状态信息 - store.dispatch('auth/logout') + authStore.logout() // token验证失败,跳转到登录页面 router.push('/login') } diff --git a/src/components/cards/MediaCard.vue b/src/components/cards/MediaCard.vue index e6f677f0..d42e96cf 100644 --- a/src/components/cards/MediaCard.vue +++ b/src/components/cards/MediaCard.vue @@ -11,6 +11,7 @@ import noImage from '@images/no-image.jpeg' import tmdbImage from '@images/logos/tmdb.png' import doubanImage from '@images/logos/douban-black.png' import bangumiImage from '@images/logos/bangumi.png' +import { useUserStore } from '@/stores' // 输入参数 const props = defineProps({ @@ -22,7 +23,8 @@ const props = defineProps({ // 从 provide 中获取全局设置 const globalSettings: any = inject('globalSettings') -const store = useStore() +// 用户 Store +const userStore = useUserStore() // 提示框 const $toast = useToast() @@ -340,7 +342,7 @@ async function getMediaSeasons() { // 查询订阅弹窗规则 async function queryDefaultSubscribeConfig() { // 非管理员不显示 - if (!store.state.auth.superUser) return false + if (!userStore.superUser) return false try { let subscribe_config_url = '' if (props.media?.type === '电影') subscribe_config_url = 'system/setting/DefaultMovieSubscribeConfig' diff --git a/src/components/cards/UserCard.vue b/src/components/cards/UserCard.vue index d3c515aa..6ddeaded 100644 --- a/src/components/cards/UserCard.vue +++ b/src/components/cards/UserCard.vue @@ -1,7 +1,7 @@