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