http.js 1.5 KB
Newer Older
1 2
import axios from 'axios';
import qs from 'qs';
B
BingBlog 已提交
3
import Notification from '../component/Notification';
4 5 6 7 8 9 10 11 12

const STATUS = 'status';
const STATUSINFO = 'msg';

const instance = axios.create({
    baseURL: '/',
    timeout: 30000
});

B
BingBlog 已提交
13
// for better ux, don't send the error msg because there will be too mutch error
14 15
const responseErrorStatus = response => {
    const data = response.data;
B
BingBlog 已提交
16 17 18 19
    // if (data[STATUS] !== 0) {
    //     Notification.error(data[STATUSINFO]);
    //     return Promise.reject(data);
    // }
20 21 22
    return data;
};

B
BingBlog 已提交
23
// for better ux, don't send the error msg because there will be too mutch error
24
const responseNetError = error => {
B
BingBlog 已提交
25
    // Notification.error('net error');
26 27 28 29 30 31 32 33 34 35 36 37 38 39
    return Promise.reject(error);
};

// post from
const formInstance = axios.create({
    baseURL: '/',
    timeout: 3000,
    transformRequest: [data => qs.stringify(data)],
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json,application/vnd.ms-excel'
    }
});

B
BingBlog 已提交
40

41 42 43 44 45 46 47 48 49 50 51 52
formInstance.interceptors.response.use(responseErrorStatus, responseNetError);

instance.interceptors.response.use(responseErrorStatus, responseNetError);

export const makeService = (url, opt = {method: 'get'}) => (params = {}) => {
    if (opt.method === 'delete' || opt.method === 'get') {
        params = {params};
    }
    return instance[opt.method](url, params);
};

export const makeFormService = (url, method = 'post') => (params = {}) => formInstance[method](url, params);