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

duanledexianxianxian's avatar
sync  
duanledexianxianxian committed
6 7 8 9
/**
 * todo  还需处理国际化
 */

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

duanledexianxianxian's avatar
duanledexianxianxian committed
39 40 41 42 43
/**
 * 组装url
 * @param {url} url
 * @param {配置参数} more
 */
duanledexianxianxian's avatar
duanledexianxianxian committed
44 45 46 47 48 49 50 51 52 53
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
54 55 56 57
const headers = () => ({
  Authorization: store.get('token'),
});

duanledexianxianxian's avatar
duanledexianxianxian committed
58 59
const axios = new HttpRequest({
  baseUrl,
duanledexianxianxian's avatar
duanledexianxianxian committed
60
  headers: headers || {},
duanledexianxianxian's avatar
duanledexianxianxian committed
61 62 63 64 65 66 67
});

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

duanledexianxianxian's avatar
duanledexianxianxian committed
85 86
const handleError = error => {
  const { response } = error;
duanledexianxianxian's avatar
sync  
duanledexianxianxian committed
87
  // 如果response存在
duanledexianxianxian's avatar
duanledexianxianxian committed
88
  if (response && response.status) {
duanledexianxianxian's avatar
sync  
duanledexianxianxian committed
89 90 91 92 93 94 95 96 97 98 99
    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
100
    notification.error({
duanledexianxianxian's avatar
sync  
duanledexianxianxian committed
101 102 103
      key: `notification_${code ? message : code}`,
      message: '请求错误',
      description: message,
duanledexianxianxian's avatar
duanledexianxianxian committed
104 105
    });
  }
duanledexianxianxian's avatar
sync  
duanledexianxianxian committed
106 107 108 109 110
  if (isThrowError) {
    throw error;
  } else {
    return error;
  }
duanledexianxianxian's avatar
duanledexianxianxian committed
111 112
};

duanledexianxianxian's avatar
duanledexianxianxian committed
113 114 115 116 117 118
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
119 120 121 122
  return axios
    .request(newOptions)
    .then(response => handleResponse(response, more))
    .catch(error => handleError(error));
duanledexianxianxian's avatar
duanledexianxianxian committed
123 124
}

duanledexianxianxian's avatar
duanledexianxianxian committed
125
const get = (url, data, more = {}) =>
duanledexianxianxian's avatar
duanledexianxianxian committed
126 127 128 129 130 131 132 133
  request(
    `${mergeApi(url, more)}`,
    {
      method: 'get', // default
      params: data,
    },
    more,
  );
duanledexianxianxian's avatar
duanledexianxianxian committed
134 135

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

const put = (url, data, more = {}) =>
duanledexianxianxian's avatar
duanledexianxianxian committed
146 147 148 149 150 151 152 153
  request(
    `${mergeApi(url, more)}`,
    {
      method: 'put', // default
      data,
    },
    more,
  );
duanledexianxianxian's avatar
duanledexianxianxian committed
154 155

const del = (url, data, more = {}) =>
duanledexianxianxian's avatar
duanledexianxianxian committed
156 157 158 159 160 161 162 163
  request(
    `${mergeApi(url, more)}`,
    {
      method: 'delete', // default
      data,
    },
    more,
  );
duanledexianxianxian's avatar
duanledexianxianxian committed
164 165

const patch = (url, data, more = {}) =>
duanledexianxianxian's avatar
duanledexianxianxian committed
166 167 168 169 170 171 172 173 174
  request(
    `${mergeApi(url, more)}`,
    {
      method: 'patch', // default
      data,
      ...more,
    },
    more,
  );
duanledexianxianxian's avatar
duanledexianxianxian committed
175 176 177 178 179 180 181 182

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
183
  post(`${mergeApi(url, more)}`, formData, {
duanledexianxianxian's avatar
duanledexianxianxian committed
184 185 186 187 188 189 190
    headers: {
      'Content-Type': 'multipart/form-data',
    },
    ...more,
  });
};

duanledexianxianxian's avatar
duanledexianxianxian committed
191 192 193 194 195 196 197
/**
 * 上传文件
 * @param {url} url
 * @param {data} data
 * @param {type} type
 * @param {more} more
 */
duanledexianxianxian's avatar
duanledexianxianxian committed
198 199 200 201 202 203
const uploadFile = (url, data, type = 'formData', more = {}) => {
  if (type === 'formData') {
    formDataUpload(url, data, more);
  }
};

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