/** * Created by 叶子 on 2017/7/30. * http通用工具函数 */ import axios from 'axios'; import { message } from 'antd'; import config from '../const'; function checkURL(url) { if (/(http|ws|https)/.test(url)) { return url; } return `${config.BASE_API}${url}`; } /** * 公用get请求 * @param url 接口地址 * @param msg 接口异常提示 * @param params 请求参数 * @param headers 接口所需header配置 */ export const get = ({ url, headers, params, msg = '接口异常' }) => axios .get(checkURL(url), { headers, params }) .then(res => res.data) .catch(err => { console.log(err); message.warn(msg); return Promise.reject(err); }); /** * 公用post请求 * @param url 接口地址 * @param data 接口参数 * @param msg 接口异常提示 * @param headers 接口所需header配置 */ export const post = ({ url, data, msg = '接口异常', headers={} }) => axios .post(checkURL(url), data, headers) .then(res => res.data) .catch(err => { console.log(err); message.warn(msg); return Promise.reject(err); }); /** * * @param url * @param data * @param msg * @param headers * @returns {Promise | never>} */ export const put = ({ url, data, msg = '接口异常', headers }) => axios.put(checkURL(url), data, headers) .then(resp => resp.data) .catch(err => { console.log('err : ', url, ' err: ', err); return Promise.reject(err); }); export const del = ({ url, data, headers }) => axios.delete(checkURL(url), { data, headers }) .then(resp => resp.data) .catch(err => { return Promise.reject(err); });