From 9f5942546dd4c21f81e1a9eeaed1c58418de69f8 Mon Sep 17 00:00:00 2001 From: wangqiao Date: Sun, 7 Sep 2025 15:26:01 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E5=B0=81=E8=A3=85=E5=92=8C=E7=94=A8=E6=88=B7=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composables/useDollarFetchRequest.ts | 95 +++++++++++----------------- 1 file changed, 37 insertions(+), 58 deletions(-) diff --git a/composables/useDollarFetchRequest.ts b/composables/useDollarFetchRequest.ts index 939b80a..332b289 100644 --- a/composables/useDollarFetchRequest.ts +++ b/composables/useDollarFetchRequest.ts @@ -1,77 +1,56 @@ -import { isArray } from "~/utils/utils"; +import { isArray } from '~/utils/utils' +import useUserStore from '~/stores/user' // import refreshToken from "~/utils/RefreshToken"; -type FetchType = typeof $fetch; -export type FetchOptions = Parameters[1]; +type FetchType = typeof $fetch +export type FetchOptions = Parameters[1] -const useClientRequest = async ( - url: string, - opts?: FetchOptions -) => { - const token = useCookie("token"); - const runtimeConfig = useRuntimeConfig(); +const useClientRequest = async (url: string, opts?: FetchOptions) => { + const token = useCookie('token') + const runtimeConfig = useRuntimeConfig() + const userStore = useUserStore() const defaultOptions: FetchOptions = { baseURL: runtimeConfig.public.apiBase, onRequest({ options }) { - options.headers = options.headers || 'application/json'; - if (token.value) { - options.headers.set("Authorization", `Bearer ${token.value}`); + options.headers = options.headers || 'application/json' + if (token.value || userStore.token) { + options.headers.set('Authorization', `Bearer ${token.value || userStore.token}`) } }, onResponse({ response }) { if (+response.status === 200 && +response._data.code !== 0) { - ElMessage.error(response._data.msg); + ElMessage.error(response._data.msg) } }, onResponseError({ response }) { - ElMessage.error( - isArray(response._data.data.msg) - ? response._data.data.msg[0] - : response._data.data.msg - ); + ElMessage.error(isArray(response._data.data.msg) ? response._data.data.msg[0] : response._data.data.msg) }, - }; - - // 明确转换返回类型 - const response = await $fetch(url, { ...defaultOptions, ...opts }); - return response as unknown as T; -}; - - // GET请求 - export const get = ( - endpoint: string, - config?: Omit - ): Promise => { - return useClientRequest(endpoint, { ...config, method: 'GET' }) - } - // POST请求 - export const post = ( - endpoint: string, - body?: any, - config?: Omit - ): Promise => { - return useClientRequest(endpoint, { ...config, method: 'POST', body }) - } + // 明确转换返回类型 + const response = await $fetch(url, { ...defaultOptions, ...opts }) + return response as unknown as T +} - - // DELETE请求 - export const del = ( - endpoint: string, - config?: Omit - ): Promise => { - return useClientRequest(endpoint, { ...config, method: 'DELETE' }) - } +// GET请求 +export const get = (endpoint: string, config?: Omit): Promise => { + return useClientRequest(endpoint, { ...config, method: 'GET' }) +} - // PUT请求 - export const put = ( - endpoint: string, - body?: any, - config?: Omit - ): Promise => { - console.log({ ...config, method: 'PUT', body }); - - return useClientRequest(endpoint, { ...config, method: 'PUT', body }) - } +// POST请求 +export const post = (endpoint: string, body?: any, config?: Omit): Promise => { + return useClientRequest(endpoint, { ...config, method: 'POST', body }) +} + +// DELETE请求 +export const del = (endpoint: string, config?: Omit): Promise => { + return useClientRequest(endpoint, { ...config, method: 'DELETE' }) +} + +// PUT请求 +export const put = (endpoint: string, body?: any, config?: Omit): Promise => { + console.log({ ...config, method: 'PUT', body }) + + return useClientRequest(endpoint, { ...config, method: 'PUT', body }) +}