request.js 2.64 KB
Newer Older
duanledexianxianxian's avatar
duanledexianxianxian committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
import { notification } from 'antd';
import HttpRequest from './kim-request';
import config from '@/config';

const { baseUrl, apiPrefix, headers } = config;

/**
 * 异常处理程序
 */
const errorHandler = error => {
  const { response } = error;
  if (response && response.status) {
    const errorText = response.statusText;
    const { status, url, code } = response;
    notification.error({
      key: `notification_${code}`,
      message: `请求错误 ${status}: ${url}`,
      description: errorText,
    });
  }
};

duanledexianxianxian's avatar
duanledexianxianxian committed
23 24 25 26 27
/**
 * 组装url
 * @param {url} url
 * @param {配置参数} more
 */
duanledexianxianxian's avatar
duanledexianxianxian committed
28 29 30 31 32 33 34 35 36 37
const mergeApi = (url, more) => {
  if (more && more.apiPrefix && typeof more.apiPrefix === 'string') {
    return `${config.apiPrefix}${url}`;
  }
  if (apiPrefix && typeof apiPrefix === 'string') {
    return `${config.apiPrefix}${url}`;
  }
  return url;
};

duanledexianxianxian's avatar
duanledexianxianxian committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
const axios = new HttpRequest({
  baseUrl,
  headers: headers() || {},
  errorHandler,
});

/**
 * 正常返回结果处理
 * @param {返回请求数据} response
 * @param {配置项} more
 */
const checkCode = (response, more) => {};

export default function request(url, options = {}, more = {}) {
  let newOptions = options;
  newOptions.url = url;
  if (more.headers) {
    newOptions = { ...options, headers: more.headers };
  }
  return axios.request(newOptions).then(response => checkCode(response, more));
}

duanledexianxianxian's avatar
duanledexianxianxian committed
60
const get = (url, data, more = {}) =>
duanledexianxianxian's avatar
duanledexianxianxian committed
61 62 63 64 65 66 67 68
  request(
    `${mergeApi(url, more)}`,
    {
      method: 'get', // default
      params: data,
    },
    more,
  );
duanledexianxianxian's avatar
duanledexianxianxian committed
69 70

const post = (url, data, more = {}) =>
duanledexianxianxian's avatar
duanledexianxianxian committed
71 72 73 74 75 76 77 78
  request(
    `${mergeApi(url, more)}`,
    {
      method: 'post', // default
      data,
    },
    more,
  );
duanledexianxianxian's avatar
duanledexianxianxian committed
79 80

const put = (url, data, more = {}) =>
duanledexianxianxian's avatar
duanledexianxianxian committed
81 82 83 84 85 86 87 88
  request(
    `${mergeApi(url, more)}`,
    {
      method: 'put', // default
      data,
    },
    more,
  );
duanledexianxianxian's avatar
duanledexianxianxian committed
89 90

const del = (url, data, more = {}) =>
duanledexianxianxian's avatar
duanledexianxianxian committed
91 92 93 94 95 96 97 98
  request(
    `${mergeApi(url, more)}`,
    {
      method: 'delete', // default
      data,
    },
    more,
  );
duanledexianxianxian's avatar
duanledexianxianxian committed
99 100

const patch = (url, data, more = {}) =>
duanledexianxianxian's avatar
duanledexianxianxian committed
101 102 103 104 105 106 107 108 109
  request(
    `${mergeApi(url, more)}`,
    {
      method: 'patch', // default
      data,
      ...more,
    },
    more,
  );
duanledexianxianxian's avatar
duanledexianxianxian committed
110 111 112 113 114 115 116 117

const formDataUpload = (url, data, more = {}) => {
  const formData = new FormData();
  if (data) {
    Object.keys(data).forEach(key => {
      formData.append(key, data[key]);
    });
  }
duanledexianxianxian's avatar
duanledexianxianxian committed
118
  post(`${mergeApi(url, more)}`, formData, {
duanledexianxianxian's avatar
duanledexianxianxian committed
119 120 121 122 123 124 125 126 127 128 129 130 131
    headers: {
      'Content-Type': 'multipart/form-data',
    },
    ...more,
  });
};

const uploadFile = (url, data, type = 'formData', more = {}) => {
  if (type === 'formData') {
    formDataUpload(url, data, more);
  }
};

duanledexianxianxian's avatar
duanledexianxianxian committed
132
export { request, get, post, put, del, patch, uploadFile };