Add resquest header
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import axios, { AxiosRequestConfig, AxiosResponse, AxiosInstance } from 'axios';
|
import axios, { AxiosRequestConfig, AxiosResponse, AxiosInstance } from 'axios';
|
||||||
import { formatMessage } from 'umi-plugin-locale';
|
import { formatMessage } from 'umi-plugin-locale';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
|
import md5 from 'md5';
|
||||||
import { globalState } from '@/services/common';
|
import { globalState } from '@/services/common';
|
||||||
import { isDev, dispatch } from './utils';
|
import { isDev, dispatch } from './utils';
|
||||||
import config from '@/config';
|
import config from '@/config';
|
||||||
@@ -44,6 +45,7 @@ function replaceId(obj: any) {
|
|||||||
|
|
||||||
async function filter<T>(
|
async function filter<T>(
|
||||||
method: keyof AxiosInstance,
|
method: keyof AxiosInstance,
|
||||||
|
url: string,
|
||||||
...args: any[]
|
...args: any[]
|
||||||
): Promise<AppResp<T>> {
|
): Promise<AppResp<T>> {
|
||||||
let ret;
|
let ret;
|
||||||
@@ -57,8 +59,12 @@ async function filter<T>(
|
|||||||
} else {
|
} else {
|
||||||
instance.defaults.headers = _.omit(instance.defaults.headers, 'token');
|
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 } =
|
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);
|
replaceId(data);
|
||||||
if (headers && headers.token) {
|
if (headers && headers.token) {
|
||||||
dispatch({ type: 'user/updateToken', payload: headers.token });
|
dispatch({ type: 'user/updateToken', payload: headers.token });
|
||||||
|
|||||||
Reference in New Issue
Block a user