request.js 3.06 KB
Newer Older
duanledexianxianxian's avatar
duanledexianxianxian committed
1 2 3 4
import { notification } from 'antd';
import HttpRequest from './kim-request';
import config from '@/config';

duanledexianxianxian's avatar
duanledexianxianxian committed
5 6 7 8 9 10 11 12 13
const {
  baseUrl,
  apiPrefix,
  headers,
  resCodeKey,
  resMessageKey,
  successCode,
  isThrowError = true,
} = config;
duanledexianxianxian's avatar
duanledexianxianxian committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

/**
 * 异常处理程序
 */
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
31 32 33 34 35
/**
 * 组装url
 * @param {url} url
 * @param {配置参数} more
 */
duanledexianxianxian's avatar
duanledexianxianxian committed
36 37 38 39 40 41 42 43 44 45
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
46 47 48 49 50 51 52 53 54 55 56
const axios = new HttpRequest({
  baseUrl,
  headers: headers() || {},
  errorHandler,
});

/**
 * 正常返回结果处理
 * @param {返回请求数据} response
 * @param {配置项} more
 */
duanledexianxianxian's avatar
duanledexianxianxian committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70
const checkCode = (response, more) => {
  const { data } = response;
  //
  if (`${data[resCodeKey]}` !== `${successCode}`) {
    if (isThrowError) {
      const errorMessage = data[resMessageKey] || '后端接口返回异常';
      const error = new Error(data[resCodeKey]);
      error.code = response[resCodeKey];
      error.message = errorMessage;
      throw error;
    }
  }
  return data;
};
duanledexianxianxian's avatar
duanledexianxianxian committed
71 72 73 74 75 76 77 78 79 80

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
81
const get = (url, data, more = {}) =>
duanledexianxianxian's avatar
duanledexianxianxian committed
82 83 84 85 86 87 88 89
  request(
    `${mergeApi(url, more)}`,
    {
      method: 'get', // default
      params: data,
    },
    more,
  );
duanledexianxianxian's avatar
duanledexianxianxian committed
90 91

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

const put = (url, data, more = {}) =>
duanledexianxianxian's avatar
duanledexianxianxian committed
102 103 104 105 106 107 108 109
  request(
    `${mergeApi(url, more)}`,
    {
      method: 'put', // default
      data,
    },
    more,
  );
duanledexianxianxian's avatar
duanledexianxianxian committed
110 111

const del = (url, data, more = {}) =>
duanledexianxianxian's avatar
duanledexianxianxian committed
112 113 114 115 116 117 118 119
  request(
    `${mergeApi(url, more)}`,
    {
      method: 'delete', // default
      data,
    },
    more,
  );
duanledexianxianxian's avatar
duanledexianxianxian committed
120 121

const patch = (url, data, more = {}) =>
duanledexianxianxian's avatar
duanledexianxianxian committed
122 123 124 125 126 127 128 129 130
  request(
    `${mergeApi(url, more)}`,
    {
      method: 'patch', // default
      data,
      ...more,
    },
    more,
  );
duanledexianxianxian's avatar
duanledexianxianxian committed
131 132 133 134 135 136 137 138

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
139
  post(`${mergeApi(url, more)}`, formData, {
duanledexianxianxian's avatar
duanledexianxianxian committed
140 141 142 143 144 145 146 147 148 149 150 151 152
    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
153
export { request, get, post, put, del, patch, uploadFile };