You need to sign in or sign up before continuing.
http.js 1.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
import axios from 'axios';
import qs from 'qs';
import Notification from '../ui/Notification';

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

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

const responseErrorStatus = response => {
    const data = response.data;
    if (data[STATUS] !== 0) {
        Notification.error(data[STATUSINFO]);
        return Promise.reject(data);
    }
    return data;
};

const responseNetError = error => {
    Notification.error('net error');
    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'
    }
});

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