基于Java spring + vue3 + nuxt构建的内容管理系统
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.
 
 
 

31 lines
1.1 KiB

import {getBaseUrl, isArray} from '~/utils/tool';
import {MODULES_API_URL, TENANT_ID} from "~/config";
type FetchType = typeof $fetch;
export type FetchOptions = Parameters<FetchType>[1];
export const useClientRequest = <T = unknown>(url: string, opts?: FetchOptions) => {
const token = useCookie<string | undefined>('token');
const runtimeConfig = useRuntimeConfig();
const defaultOptions: FetchOptions = {
baseURL: getBaseUrl(),
onRequest({ options }) {
options.headers = (options.headers || {}) as { [key: string]: string };
if (token.value) {
options.headers.Authorization = token.value;
}
options.headers.Tenantd = '5';
},
onResponse({ response }) {
if (+response.status === 0 && +response._data.code !== 0) {
ElMessage.error(response._data.message);
}
},
onResponseError({ response }) {
ElMessage.error(isArray(response._data.data.message) ? response._data.data.message[0] : response._data.data.message);
}
};
return $fetch<T>(getBaseUrl() + url, { ...defaultOptions, ...opts });
};