Add resquest header

This commit is contained in:
saplf
2019-09-29 19:36:43 +08:00
parent 4e3ecd36b6
commit 895c1969e5

View File

@@ -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 });