Add resquest header
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import axios, { AxiosRequestConfig, AxiosResponse, AxiosInstance } from 'axios';
|
||||
import { formatMessage } from 'umi-plugin-locale';
|
||||
import _ from 'lodash';
|
||||
import md5 from 'md5';
|
||||
import { globalState } from '@/services/common';
|
||||
import { isDev, dispatch } from './utils';
|
||||
import config from '@/config';
|
||||
@@ -44,6 +45,7 @@ function replaceId(obj: any) {
|
||||
|
||||
async function filter<T>(
|
||||
method: keyof AxiosInstance,
|
||||
url: string,
|
||||
...args: any[]
|
||||
): Promise<AppResp<T>> {
|
||||
let ret;
|
||||
@@ -57,8 +59,12 @@ async function filter<T>(
|
||||
} else {
|
||||
instance.defaults.headers = _.omit(instance.defaults.headers, 'token');
|
||||
}
|
||||
const matches = url.match(/(?:[/\da-zA-Z]+)/) || [''];
|
||||
const timestamp = Date.now();
|
||||
instance.defaults.headers.ts = `${timestamp}`;
|
||||
instance.defaults.headers.sig = md5(`${timestamp}${matches[0]}LimitSaltLOL8080`);
|
||||
const { status, data, headers } =
|
||||
await (instance[method] as (...args: any[]) => Promise<AxiosResponse<AppResp>>)(...args);
|
||||
await (instance[method] as (...args: any[]) => Promise<AxiosResponse<AppResp>>)(url, ...args);
|
||||
replaceId(data);
|
||||
if (headers && headers.token) {
|
||||
dispatch({ type: 'user/updateToken', payload: headers.token });
|
||||
|
||||
Reference in New Issue
Block a user