Update token handling and request logging
This commit is contained in:
@ -1,5 +1,5 @@
|
|||||||
import { isArray } from "~/utils/utils";
|
import { isArray } from "~/utils/utils";
|
||||||
import refreshToken from "~/utils/RefreshToken";
|
// import refreshToken from "~/utils/RefreshToken";
|
||||||
|
|
||||||
type FetchType = typeof $fetch;
|
type FetchType = typeof $fetch;
|
||||||
export type FetchOptions = Parameters<FetchType>[1];
|
export type FetchOptions = Parameters<FetchType>[1];
|
||||||
@ -8,15 +8,15 @@ const useClientRequest = async <T = unknown>(
|
|||||||
url: string,
|
url: string,
|
||||||
opts?: FetchOptions
|
opts?: FetchOptions
|
||||||
) => {
|
) => {
|
||||||
// const token = useCookie<string | undefined>("token");
|
const token = useCookie<string | undefined>("token");
|
||||||
const runtimeConfig = useRuntimeConfig();
|
const runtimeConfig = useRuntimeConfig();
|
||||||
|
|
||||||
const defaultOptions: FetchOptions = {
|
const defaultOptions: FetchOptions = {
|
||||||
baseURL: runtimeConfig.public.apiBase,
|
baseURL: runtimeConfig.public.apiBase,
|
||||||
onRequest({ options }) {
|
onRequest({ options }) {
|
||||||
options.headers = options.headers || 'application/json';
|
options.headers = options.headers || 'application/json';
|
||||||
if (refreshToken.getToken().token) {
|
if (token.value) {
|
||||||
options.headers.set("Authorization", `Bearer ${refreshToken.getToken().token}`);
|
options.headers.set("Authorization", `Bearer ${token.value}`);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onResponse({ response }) {
|
onResponse({ response }) {
|
||||||
|
|||||||
@ -39,6 +39,9 @@ const useServerRequest = async <T>(
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
console.log(url + '-----' + 'defaultOptions----', defaultOptions);
|
||||||
|
console.log(url + '-----' + 'opts----', opts);
|
||||||
|
|
||||||
// return useFetch<T>(url, { ...defaultOptions, ...opts } as any);
|
// return useFetch<T>(url, { ...defaultOptions, ...opts } as any);
|
||||||
// 明确转换返回类型
|
// 明确转换返回类型
|
||||||
const response = await useFetch<T>(url, { ...defaultOptions, ...opts } as any);
|
const response = await useFetch<T>(url, { ...defaultOptions, ...opts } as any);
|
||||||
|
|||||||
@ -115,7 +115,6 @@
|
|||||||
// total: 0,
|
// total: 0,
|
||||||
// })
|
// })
|
||||||
|
|
||||||
|
|
||||||
const {data: pageRes, refresh: getPage } = await useAsyncData(`draw-page-list-${Date.now()}`, async () => {
|
const {data: pageRes, refresh: getPage } = await useAsyncData(`draw-page-list-${Date.now()}`, async () => {
|
||||||
const res = await page(pageReq)
|
const res = await page(pageReq)
|
||||||
return res.data
|
return res.data
|
||||||
|
|||||||
Reference in New Issue
Block a user