request.js 4.42 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
sync  
duanledexianxianxian committed
5 6 7 8
/**
 * todo  还需处理国际化
 */

duanledexianxianxian's avatar
duanledexianxianxian committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
const codeMessage = {
  200: '服务器成功返回请求的数据。',
  201: '新建或修改数据成功。',
  202: '一个请求已经进入后台排队(异步任务)。',
  204: '删除数据成功。',
  400: '发出的请求有错误,服务器没有进行新建或修改数据的操作。',
  401: '用户没有权限(令牌、用户名、密码错误)。',
  403: '用户得到授权,但是访问是被禁止的。',
  404: '发出的请求针对的是不存在的记录,服务器没有进行操作。',
  406: '请求的格式不可得。',
  410: '请求的资源被永久删除,且不会再得到的。',
  422: '当创建一个对象时,发生一个验证错误。',
  500: '服务器发生错误,请检查服务器。',
  502: '网关错误。',
  503: '服务不可用,服务器暂时过载或维护。',
  504: '网关超时。',
};
duanledexianxianxian's avatar
duanledexianxianxian committed
26 27 28 29 30 31 32 33 34
const {
  baseUrl,
  apiPrefix,
  headers,
  resCodeKey,
  resMessageKey,
  successCode,
  isThrowError = true,
} = config;
duanledexianxianxian's avatar
duanledexianxianxian committed
35

duanledexianxianxian's avatar
duanledexianxianxian committed
36 37 38 39 40
/**
 * 组装url
 * @param {url} url
 * @param {配置参数} more
 */
duanledexianxianxian's avatar
duanledexianxianxian committed
41 42 43 44 45 46 47 48 49 50
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
51 52 53 54 55 56 57 58 59 60
const axios = new HttpRequest({
  baseUrl,
  headers: headers() || {},
});

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

duanledexianxianxian's avatar
duanledexianxianxian committed
78 79
const handleError = error => {
  const { response } = error;
duanledexianxianxian's avatar
sync  
duanledexianxianxian committed
80
  // 如果response存在
duanledexianxianxian's avatar
duanledexianxianxian committed
81
  if (response && response.status) {
duanledexianxianxian's avatar
sync  
duanledexianxianxian committed
82 83 84 85 86 87 88 89 90 91 92
    const errorText = codeMessage[response.status] || response.statusText;
    const {
      status,
      config: { url },
    } = response;
    notification.error({
      message: `请求错误 ${status}: ${url}`,
      description: errorText,
    });
  } else {
    const { code, message } = error;
duanledexianxianxian's avatar
duanledexianxianxian committed
93
    notification.error({
duanledexianxianxian's avatar
sync  
duanledexianxianxian committed
94 95 96
      key: `notification_${code ? message : code}`,
      message: '请求错误',
      description: message,
duanledexianxianxian's avatar
duanledexianxianxian committed
97 98
    });
  }
duanledexianxianxian's avatar
sync  
duanledexianxianxian committed
99 100 101 102 103
  if (isThrowError) {
    throw error;
  } else {
    return error;
  }
duanledexianxianxian's avatar
duanledexianxianxian committed
104 105
};

duanledexianxianxian's avatar
duanledexianxianxian committed
106 107 108 109 110 111
export default function request(url, options = {}, more = {}) {
  let newOptions = options;
  newOptions.url = url;
  if (more.headers) {
    newOptions = { ...options, headers: more.headers };
  }
duanledexianxianxian's avatar
duanledexianxianxian committed
112 113 114 115
  return axios
    .request(newOptions)
    .then(response => handleResponse(response, more))
    .catch(error => handleError(error));
duanledexianxianxian's avatar
duanledexianxianxian committed
116 117
}

duanledexianxianxian's avatar
duanledexianxianxian committed
118
const get = (url, data, more = {}) =>
duanledexianxianxian's avatar
duanledexianxianxian committed
119 120 121 122 123 124 125 126
  request(
    `${mergeApi(url, more)}`,
    {
      method: 'get', // default
      params: data,
    },
    more,
  );
duanledexianxianxian's avatar
duanledexianxianxian committed
127 128

const post = (url, data, more = {}) =>
duanledexianxianxian's avatar
duanledexianxianxian committed
129 130 131 132 133 134 135 136
  request(
    `${mergeApi(url, more)}`,
    {
      method: 'post', // default
      data,
    },
    more,
  );
duanledexianxianxian's avatar
duanledexianxianxian committed
137 138

const put = (url, data, more = {}) =>
duanledexianxianxian's avatar
duanledexianxianxian committed
139 140 141 142 143 144 145 146
  request(
    `${mergeApi(url, more)}`,
    {
      method: 'put', // default
      data,
    },
    more,
  );
duanledexianxianxian's avatar
duanledexianxianxian committed
147 148

const del = (url, data, more = {}) =>
duanledexianxianxian's avatar
duanledexianxianxian committed
149 150 151 152 153 154 155 156
  request(
    `${mergeApi(url, more)}`,
    {
      method: 'delete', // default
      data,
    },
    more,
  );
duanledexianxianxian's avatar
duanledexianxianxian committed
157 158

const patch = (url, data, more = {}) =>
duanledexianxianxian's avatar
duanledexianxianxian committed
159 160 161 162 163 164 165 166 167
  request(
    `${mergeApi(url, more)}`,
    {
      method: 'patch', // default
      data,
      ...more,
    },
    more,
  );
duanledexianxianxian's avatar
duanledexianxianxian committed
168 169 170 171 172 173 174 175

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
176
  post(`${mergeApi(url, more)}`, formData, {
duanledexianxianxian's avatar
duanledexianxianxian committed
177 178 179 180 181 182 183
    headers: {
      'Content-Type': 'multipart/form-data',
    },
    ...more,
  });
};

duanledexianxianxian's avatar
duanledexianxianxian committed
184 185 186 187 188 189 190
/**
 * 上传文件
 * @param {url} url
 * @param {data} data
 * @param {type} type
 * @param {more} more
 */
duanledexianxianxian's avatar
duanledexianxianxian committed
191 192 193 194 195 196
const uploadFile = (url, data, type = 'formData', more = {}) => {
  if (type === 'formData') {
    formDataUpload(url, data, more);
  }
};

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