From 91e6327760936a128c9050db03b6c007cc3f861b Mon Sep 17 00:00:00 2001 From: Litrix2 Date: Sat, 21 Dec 2024 23:21:41 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=A6=84=20refactor:=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/index.ts | 27 ++++++++++++++++++++++++++- src/schemas/response.ts | 6 +++--- src/stores/user.ts | 34 +++++----------------------------- src/utils/user.ts | 12 ++++++++++++ src/views/GobangPlayPage.vue | 3 ++- 5 files changed, 48 insertions(+), 34 deletions(-) create mode 100644 src/utils/user.ts diff --git a/src/api/index.ts b/src/api/index.ts index 16057e7..eb1d11b 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,5 +1,8 @@ +import { userInfoResponseSchema } from '@/schemas'; import { useUserStore } from '@/stores/user'; -import axios from 'axios'; +import axios, { type AxiosError } from 'axios'; +import { ElMessage } from 'element-plus'; +import { errorMessage } from '../utils/api'; const baseURL = 'https://wzpmc.cn:18080/'; const axiosInstance = axios.create({ @@ -23,3 +26,25 @@ axiosInstance.interceptors.response.use((response) => { }); export default axiosInstance; export type RawResp = Record; +export async function getUserInfo(showErrorMessage: boolean, userID?: string) { + let url = '/api/user/info'; + if (userID) { + url += `/${userID}`; + } + const raw = await axiosInstance + .get(url) + .then((r) => r.data) + .catch((e: AxiosError) => { + if (!showErrorMessage) return; + ElMessage.error(errorMessage('获取用户信息失败', e.code, e.message)); + }); + if (!raw) return; + const resp = userInfoResponseSchema.parse(raw); + if (resp.type === 'error') { + if (showErrorMessage) { + ElMessage.error(errorMessage('获取用户信息失败', resp.code, resp.msg)); + } + return; + } + return resp; +} diff --git a/src/schemas/response.ts b/src/schemas/response.ts index 983268b..9251e48 100644 --- a/src/schemas/response.ts +++ b/src/schemas/response.ts @@ -38,17 +38,17 @@ export const idAndNameSchema = z.object({ name: z.string(), }); -const _authSchema = idAndNameSchema.extend({ +const authSchema = idAndNameSchema.extend({ permissions: idAndNameSchema.array(), }); export const userInfoResponseSchema = createResponseSchema( idAndNameSchema.extend({ avatar: z.nullable(z.string()), - auth: _authSchema, + auth: authSchema, club: z.nullable( idAndNameSchema.extend({ commit: z.string(), - auth: _authSchema, + auth: authSchema, }), ), }), diff --git a/src/stores/user.ts b/src/stores/user.ts index c908125..24260b2 100644 --- a/src/stores/user.ts +++ b/src/stores/user.ts @@ -1,14 +1,11 @@ -import axiosInstance, { type RawResp } from '@/api'; +import { getUserInfo } from '@/api'; import router from '@/router'; import type { UserInfo } from '@/schemas'; -import { type idAndNameSchema, userInfoResponseSchema } from '@/schemas'; -import { errorMessage } from '@/utils/api'; +import { type idAndNameSchema } from '@/schemas'; import type { MaybePromise } from '@/utils/types'; import { StorageSerializers, useLocalStorage } from '@vueuse/core'; -import { AxiosError } from 'axios'; -import { ElMessage } from 'element-plus'; import { defineStore } from 'pinia'; -import { computed, onUnmounted, ref, watch } from 'vue'; +import { computed, ref, watch } from 'vue'; import { z } from 'zod'; type Permission = z.infer; @@ -40,20 +37,8 @@ export const useUserStore = defineStore('user', () => { async function updateSelfUserInfo(showErrorMessage: boolean): Promise { initializing.value = true; try { - const raw = await axiosInstance - .get('/api/user/info') - .then((r) => r.data) - .catch((e: AxiosError) => { - if (!showErrorMessage) return; - ElMessage.error(errorMessage('获取用户信息失败', e.code, e.message)); - }); - const resp = userInfoResponseSchema.parse(raw); - if (resp.type === 'error') { - if (showErrorMessage) { - ElMessage.error(errorMessage('获取用户信息失败', resp.code, resp.msg)); - } - return false; - } + const resp = await getUserInfo(showErrorMessage); + if (!resp) return false; userInfo.value = resp.data; return true; } finally { @@ -89,12 +74,3 @@ export const useUserStore = defineStore('user', () => { }; }); export type LogoutInterceptor = () => MaybePromise; -export function useLogoutInterceptor(fn: LogoutInterceptor) { - const userStore = useUserStore(); - userStore.addLogoutInterceptor(fn); - const remove = () => { - userStore.removeLogoutInterceptor(fn); - }; - onUnmounted(remove); - return remove; -} diff --git a/src/utils/user.ts b/src/utils/user.ts new file mode 100644 index 0000000..7bc6018 --- /dev/null +++ b/src/utils/user.ts @@ -0,0 +1,12 @@ +import { useUserStore, type LogoutInterceptor } from '@/stores/user'; +import { onUnmounted } from 'vue'; + +export function useLogoutInterceptor(fn: LogoutInterceptor) { + const userStore = useUserStore(); + userStore.addLogoutInterceptor(fn); + const remove = () => { + userStore.removeLogoutInterceptor(fn); + }; + onUnmounted(remove); + return remove; +} diff --git a/src/views/GobangPlayPage.vue b/src/views/GobangPlayPage.vue index 8c01fc3..bf8aac3 100644 --- a/src/views/GobangPlayPage.vue +++ b/src/views/GobangPlayPage.vue @@ -235,8 +235,9 @@ import GobangSide from '@/components/gobang/GobangSide.vue'; import GobangUser from '@/components/gobang/GobangUser.vue'; import router from '@/router'; import { useMediaStore } from '@/stores/media'; -import { useLogoutInterceptor, useUserStore } from '@/stores/user'; +import { useUserStore } from '@/stores/user'; import { create2DArray, iter2DArray, zip } from '@/utils/array'; +import { useLogoutInterceptor } from '@/utils/user'; import { useGobangSocket, WinFace, type RoomDetail, type RoomId } from '@/views/GobangListPage.vue'; import { ElMessage, ElMessageBox, ElNotification, type Action } from 'element-plus'; import { storeToRefs } from 'pinia';