forked from gxwebsoft/websoft-cms
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
1.0 KiB
29 lines
1.0 KiB
import { useFetch, UseFetchOptions } from '#app';
|
|
import { isArray } from '~/utils/tool';
|
|
|
|
export const useServerRequest = <T>(url: string, opts?: UseFetchOptions<T, unknown>) => {
|
|
const token = useCookie<string | undefined>('token');
|
|
const runtimeConfig = useRuntimeConfig();
|
|
|
|
const defaultOptions: UseFetchOptions<unknown> = {
|
|
baseURL: runtimeConfig.public.apiBase,
|
|
onRequest({ options }) {
|
|
options.headers = (options.headers || {}) as { [key: string]: string };
|
|
if (token.value) {
|
|
options.headers.Authorization = token.value;
|
|
}
|
|
options.headers.tenantid = '10199'
|
|
},
|
|
onResponse({ response }) {
|
|
if (+response.status === 200 && +response._data.code !== 200) {
|
|
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);
|
|
}
|
|
};
|
|
|
|
return useFetch<T>(url, { ...defaultOptions, ...opts } as any);
|
|
};
|