import { notification } from 'antd'; import HttpRequest from './kim-request'; import config from '@/config'; /** * todo 还需处理国际化 */ const codeMessage = { 200: '服务器成功返回请求的数据。', 201: '新建或修改数据成功。', 202: '一个请求已经进入后台排队(异步任务)。', 204: '删除数据成功。', 400: '发出的请求有错误,服务器没有进行新建或修改数据的操作。', 401: '用户没有权限(令牌、用户名、密码错误)。', 403: '用户得到授权,但是访问是被禁止的。', 404: '发出的请求针对的是不存在的记录,服务器没有进行操作。', 406: '请求的格式不可得。', 410: '请求的资源被永久删除,且不会再得到的。', 422: '当创建一个对象时,发生一个验证错误。', 500: '服务器发生错误,请检查服务器。', 502: '网关错误。', 503: '服务不可用,服务器暂时过载或维护。', 504: '网关超时。', }; const { baseUrl, apiPrefix, headers, resCodeKey, resMessageKey, successCode, isThrowError = true, } = config; /** * 组装url * @param {url} url * @param {配置参数} more */ 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; }; const axios = new HttpRequest({ baseUrl, headers: headers() || {}, }); /** * 正常返回结果处理 * @param {返回请求数据} response * @param {配置项} more */ const handleResponse = (response, more) => { const { data } = response; // if (`${data[resCodeKey]}` !== `${successCode}`) { if (isThrowError) { const errorMessage = data[resMessageKey] || '后端接口返回异常'; let error = new Error(); error = { ...error, ...data }; error.code = data[resCodeKey]; error.message = errorMessage; return Promise.reject(error); } } // success return data; }; const handleError = error => { const { response } = error; // 如果response存在 if (response && response.status) { const errorText = codeMessage[response.status] || response.statusText; const { status, config: { url }, } = response; notification.error({ message: `请求错误 ${status}: ${url}`, description: errorText, }); } else { const { code, message } = error; notification.error({ key: `notification_${code ? message : code}`, message: '请求错误', description: message, }); } if (isThrowError) { throw error; } else { return error; } }; 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 => handleResponse(response, more)) .catch(error => handleError(error)); } const get = (url, data, more = {}) => request( `${mergeApi(url, more)}`, { method: 'get', // default params: data, }, more, ); const post = (url, data, more = {}) => request( `${mergeApi(url, more)}`, { method: 'post', // default data, }, more, ); const put = (url, data, more = {}) => request( `${mergeApi(url, more)}`, { method: 'put', // default data, }, more, ); const del = (url, data, more = {}) => request( `${mergeApi(url, more)}`, { method: 'delete', // default data, }, more, ); const patch = (url, data, more = {}) => request( `${mergeApi(url, more)}`, { method: 'patch', // default data, ...more, }, more, ); const formDataUpload = (url, data, more = {}) => { const formData = new FormData(); if (data) { Object.keys(data).forEach(key => { formData.append(key, data[key]); }); } post(`${mergeApi(url, more)}`, formData, { headers: { 'Content-Type': 'multipart/form-data', }, ...more, }); }; /** * 上传文件 * @param {url} url * @param {data} data * @param {type} type * @param {more} more */ const uploadFile = (url, data, type = 'formData', more = {}) => { if (type === 'formData') { formDataUpload(url, data, more); } }; export { request, get, post, put, del, patch, uploadFile };