uni-app中使用axios

"use strict";
import axios from "axios";
import qs from "qs";







// Full config:  https://github.com/axios/axios#request-config
// axios.defaults.baseURL = process.env.baseURL || process.env.apiUrl || '';
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
// axios.defaults.headers.post["Content-Type"] =
// "application/x-www-form-urlencoded;charset=UTF-8";
const config = {
    baseURL: "", // 正式环境
    timeout: 60 * 1000, // Timeout,
    headers: {
        clientType: "2",
        "Content-Type": "application/json"
    }

    // withCredentials: true, // Check cross-site Access-Control
};

const _axios = axios.create(config);

_axios.interceptors.request.use(
    config => {
        // Do something before request is sent
        return config;
    },
    error => {
        // Do something with request error
        return Promise.reject(error);
    }
);

// Add a response interceptor
_axios.interceptors.response.use(
    response => {
        // Do something with response data
        return response;
    },
    error => {
        // Do something with response error
        switch (error.response.status) {
            case 400:
                console.log("错误请求");
                break;
            case 401:

                break;
            case 403:
                console.log("拒绝访问");
                break;
            case 404:
                console.log("请求错误,未找到该资源");
                break;
            case 405:
                console.log("请求方法未允许");
                break;
            case 408:
                console.log("请求超时");
                break;
            case 500:
                console.log("服务器端出错");
                break;
            case 501:
                console.log("网络未实现");
                break;
            case 502:
                console.log("网络错误");
                break;
            case 503:
                console.log("服务不可用");
                break;
            case 504:
                console.log("网络超时");
                break;
            case 505:
                console.log("http版本不支持该请求");
                break;
            default:
                console.log(error.message);
                break;
        }
        return Promise.reject(error);
    }
);
axios.defaults.adapter = function(config) { //自己定义个适配器,用来适配uniapp的语法
    return new Promise((resolve, reject) => {
        console.log(config)
        var settle = require('axios/lib/core/settle');
        var buildURL = require('axios/lib/helpers/buildURL');
        uni.request({
            method: config.method.toUpperCase(),
            url: config.baseURL + buildURL(config.url, config.params, config.paramsSerializer),
            header: config.headers,
            data: config.data,
            dataType: config.dataType,
            responseType: config.responseType,
            sslVerify: config.sslVerify,
            complete: function complete(response) {
                console.log("执行完成:", response)
                response = {
                    data: response.data,
                    status: response.statusCode,
                    errMsg: response.errMsg,
                    header: response.header,
                    config: config
                };

                settle(resolve, reject, response);
            }
        })
    })
}

};

uniapp中使用axios 需要配置适配器

你可能感兴趣的:(uni-app中使用axios)