个人对antd的axios封装

3 篇文章 0 订阅
2 篇文章 0 订阅
import axios from 'axios';
import qs from 'qs';
import {ant} from '../store';

const service = axios.create({
    // baseURL:'/',   //api的baseurl
    timeout: 5000    //请求超时时间
});


service.interceptors.request.use(config => {
        let token = localStorage.getItem('token');
        token ? config.headers.token = token : config.headers.token = '1111';
        return config;
    },
    error => {
        Promise.reject(error)
    });


//response响应拦截器
service.interceptors.response.use(
    response => {
        // let res = response.data;
        // if (res.code === 401) {
        //     window.location.href = '#/login';
        // }
        return Promise.resolve(response)
    },
    error => {
        //进行提示 或者弹窗
        return Promise.reject(error);
    }
)

export default function ajax(_url, _data, _options) {
    let options = null;
    let data = '';
    let params = '';
    if (Object.prototype.toString.call(_data) === "[object FormData]") {
        options = {
            method: 'POST',
            header: {'Content-Type': 'multipart/form-data'}
        };
        data = _data
    } else {
        options = _options || {
            method: 'POST',
            dataType: 'json',
            header: {
                'Content-type': 'application/x-www-form-urlencoded;charset=utf-8'
            }
        };
        if (options === 'GET')
            params = _data;
        else
            data = qs.stringify(_data, {indices: false});
    }

    return new Promise((resolve, reject) => {
        service({
            url: _url,
            method: options.method || options,
            headers: options.header,
            params: params,
            data: data,
            withCredentials: true,
            paramsSerializer: params => {
                return qs.stringify(params, {indices: false})
            }
        }).then(response => {
            if ((response.data && response.data.status) === 'success') {
                resolve(response.data);
            } else {
                failStatus(response.data.code, response.data.message)
            }

        }).catch(error => {
            failStatus(error.response);
            return reject(error)
        })
    })
}


const failStatus = function (status, message = '') {
    switch (status) {
        case 400:
            ant.Alert('fail', 'HTTP Status 400(请求错误)');
            break;
        case 401: {
            // ant.Alert('fail', '登录超时');
            break;
        }
        case 403:
            ant.Alert('fail', 'HTTP Status 403(用户无权限访问该资源,请求失败)');
            break;
        case 404:
            ant.Alert('fail', 'HTTP Status 404 (Not Found)');
            break;
        case 500:
            if (message) {
                ant.Alert('fail', message);
            } else {
                ant.Alert('fail', 'HTTP Status 500(服务器内部错误)');
            }
            break;
        case 502:
            ant.Alert('fail', 'HTTP Status 502(网关错误)');
            break;
        case 503:
            ant.Alert('fail', 'HTTP Status 503(服务不可用)');
            break;
        case 504:
            ant.Alert('fail', 'HTTP Status 504(网关超时)');
            break;
    }
};



针对antd弹出的简单封装

import {message, Modal} from 'antd';
export const Alert = (type, content) => {
    if (type === 'info') {
        message.info(content);
    }
    if (type === 'success') {
        message.success(content);
    }
    if (type === 'fail') {
        message.error(content);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值