Commit a95cf7e2 authored by Ylg's avatar Ylg

add request interceptors

parent 35cb260a
...@@ -8,6 +8,20 @@ interface ILoginParams { ...@@ -8,6 +8,20 @@ interface ILoginParams {
const request = extend({}); const request = extend({});
request.interceptors.request.use((url, options) => {
const token = JSON.parse(localStorage.getItem('user'))?.token;
const authHeader = {
Authorization: `Bearer ${token || ''}`,
'Content-Type': 'application/json',
'__tenant': '3a0a90fe-9a0d-70f4-200d-a80a41fb6195'
};
return {
url: url,
options: { ...options, interceptors: true, headers: authHeader },
};
});
request.interceptors.response.use(async (response): Promise<any> => { request.interceptors.response.use(async (response): Promise<any> => {
console.log('response', response); console.log('response', response);
if(response.status === 401) { if(response.status === 401) {
...@@ -23,10 +37,6 @@ request.interceptors.response.use(async (response): Promise<any> => { ...@@ -23,10 +37,6 @@ request.interceptors.response.use(async (response): Promise<any> => {
export function loginChatGLM(options: ILoginParams) { export function loginChatGLM(options: ILoginParams) {
return request('https://glm-mangement-api.baibaomen.com/api/app/account/login', { return request('https://glm-mangement-api.baibaomen.com/api/app/account/login', {
method: 'POST', method: 'POST',
headers: {
'Content-Type': 'application/json',
__tenant: '3a0a90fe-9a0d-70f4-200d-a80a41fb6195',
},
data: options, data: options,
}); });
} }
...@@ -34,6 +44,5 @@ export function loginChatGLM(options: ILoginParams) { ...@@ -34,6 +44,5 @@ export function loginChatGLM(options: ILoginParams) {
export function GetChatGLMToken(): Promise<string> { export function GetChatGLMToken(): Promise<string> {
return request('https://glm-mangement-api.baibaomen.com/user/token', { return request('https://glm-mangement-api.baibaomen.com/user/token', {
method: 'POST', method: 'POST',
headers: { Authorization: 'Bearer ' + JSON.parse(localStorage.getItem('user') || '')?.token },
}); });
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment