Refactor request formatting and configuration

This commit is contained in:
wangqiao
2025-08-27 14:32:39 +08:00
parent 64d0696cb9
commit 9acc229704
2 changed files with 75 additions and 101 deletions

View File

@ -1,87 +1,53 @@
import { useFetch } from "#app";
import type { UseFetchOptions } from "#app";
import { isArray } from "~/utils/utils";
import { useFetch } from '#app'
import type { UseFetchOptions } from '#app'
import { isArray } from '~/utils/utils'
const useServerRequest = async <T>(
url: string,
opts?: UseFetchOptions<T, unknown>
) => {
const token = useToken();
const user = useUserInfo();
const runtimeConfig = useRuntimeConfig();
const useServerRequest = async <T>(url: string, opts?: UseFetchOptions<T, unknown>) => {
const token = useToken()
const user = useUserInfo()
const runtimeConfig = useRuntimeConfig()
const defaultOptions: UseFetchOptions<unknown> = {
baseURL: runtimeConfig.public.apiBase,
onRequest({ options }) {
options.headers = options.headers || "application/json";
options.headers = options.headers || 'application/json'
if (token.value) {
console.log('user----', user.value);
console.log('token----', "Bearer " + token.value)
// @ts-ignore
options.headers["authorization"] = "Bearer " + token.value;
options.headers['authorization'] = 'Bearer ' + token.value
}
console.log(url +'-----' +'options----', options);
},
onResponse({ response }) {
if (+response.status === 200 && +response._data.code !== 0) {
process.client && ElMessage.error(response._data.msg);
process.client && ElMessage.error(response._data.msg)
}
},
onResponseError({ response }) {
process.client &&
ElMessage.error(
isArray(response._data.data.msg)
? response._data.data.msg[0]
: response._data.data.msg
);
process.client && ElMessage.error(isArray(response._data.data.msg) ? response._data.data.msg[0] : response._data.data.msg)
},
};
console.log(url + '-----' + 'defaultOptions----', defaultOptions);
console.log(url + '-----' + 'opts----', opts);
}
// return useFetch<T>(url, { ...defaultOptions, ...opts } as any);
// 明确转换返回类型
const response = await useFetch<T>(url, { ...defaultOptions, ...opts } as any);
console.log(url +'-----' + 'response----', response.data.value);
return response.data.value as unknown as T;
};
const response = await useFetch<T>(url, { ...defaultOptions, ...opts } as any)
return response.data.value as unknown as T
}
// GET请求
export const get = <T = unknown>(
endpoint: string,
config?: Omit<FetchOptions, 'method'>
): Promise<T> => {
console.log(endpoint + '----' + 'config----', config);
return useServerRequest<T>(endpoint, { ...config, method: 'GET' })
}
// GET请求
export const get = <T = unknown>(endpoint: string, config?: Omit<FetchOptions, 'method'>): Promise<T> => {
return useServerRequest<T>(endpoint, { ...config, method: 'GET' })
}
// POST请求
export const post = <T = unknown>(
endpoint: string,
body?: any,
config?: Omit<FetchOptions, 'method' | 'body'>
): Promise<T> => {
return useServerRequest<T>(endpoint, { ...config, method: 'POST', body })
}
// POST请求
export const post = <T = unknown>(endpoint: string, body?: any, config?: Omit<FetchOptions, 'method' | 'body'>): Promise<T> => {
return useServerRequest<T>(endpoint, { ...config, method: 'POST', body })
}
// DELETE请求
export const del = <T = unknown>(
endpoint: string,
config?: Omit<FetchOptions, 'method'>
): Promise<T> => {
return useServerRequest<T>(endpoint, { ...config, method: 'DELETE' })
}
// DELETE请求
export const del = <T = unknown>(endpoint: string, config?: Omit<FetchOptions, 'method'>): Promise<T> => {
return useServerRequest<T>(endpoint, { ...config, method: 'DELETE' })
}
// PUT请求
export const put = <T = unknown>(
endpoint: string,
body?: any,
config?: Omit<FetchOptions, 'method' | 'body'>
): Promise<T> => {
return useServerRequest<T>(endpoint, { ...config, method: 'PUT', body })
}
// PUT请求
export const put = <T = unknown>(endpoint: string, body?: any, config?: Omit<FetchOptions, 'method' | 'body'>): Promise<T> => {
return useServerRequest<T>(endpoint, { ...config, method: 'PUT', body })
}