http.js 1.4 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

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

const instance = axios.create({
J
Jeff Wang 已提交
9 10
  baseURL: '/',
  timeout: 30000,
11 12
});

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

B
BingBlog 已提交
23
// for better ux, don't send the error msg because there will be too mutch error
24
const responseNetError = (error) => {
J
Jeff Wang 已提交
25 26
  // Notification.error('net error');
  return Promise.reject(error);
27 28 29 30
};

// post from
const formInstance = axios.create({
J
Jeff Wang 已提交
31 32 33 34 35 36 37
  baseURL: '/',
  timeout: 3000,
  transformRequest: [(data) => qs.stringify(data)],
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Accept': 'application/json,application/vnd.ms-excel',
  },
38 39
});

B
BingBlog 已提交
40

41 42 43 44 45
formInstance.interceptors.response.use(responseErrorStatus, responseNetError);

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

export const makeService = (url, opt = {method: 'get'}) => (params = {}) => {
J
Jeff Wang 已提交
46 47 48 49
  if (opt.method === 'delete' || opt.method === 'get') {
    params = {params};
  }
  return instance[opt.method](url, params);
50 51 52
};

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