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.
32 lines
1.2 KiB
32 lines
1.2 KiB
import { useFetch } from '#app';
|
|
import type { UseFetchOptions } from '#app';
|
|
import { isArray } from '~/utils/tool';
|
|
import {MODULES_API_URL, SERVER_API_URL, TENANT_ID} from "~/config";
|
|
|
|
export const useServerRequest = <T>(url: string, opts?: UseFetchOptions<T, unknown>) => {
|
|
// 获取 Cookie
|
|
const token = useCookie('token');
|
|
|
|
const defaultOptions: UseFetchOptions<unknown> = {
|
|
baseURL: SERVER_API_URL + url,
|
|
onRequest({ options }) {
|
|
options.headers = (options.headers || {}) as { [key: string]: string };
|
|
if (token.value) {
|
|
options.headers.Authorization = token.value;
|
|
}
|
|
options.headers.tenantid = `${TENANT_ID}`;
|
|
},
|
|
onResponse({ response }) {
|
|
if (+response.status === 0 && +response._data.code !== 0) {
|
|
process.client && ElMessage.error(response._data.message);
|
|
}
|
|
},
|
|
onResponseError({ response }) {
|
|
process.client &&
|
|
ElMessage.error(isArray(response._data.data.message) ? response._data.data.message[0] : response._data.data.message);
|
|
}
|
|
};
|
|
|
|
console.log('请求接口:', SERVER_API_URL + url)
|
|
return useFetch<T>(SERVER_API_URL + url, { ...defaultOptions, ...opts } as any);
|
|
};
|