阅读 8

笔记——axios二次封装使用

import axios from 'axios'import Qs from 'qs'import router from '../router/index'import {    Loading,    Message} from 'element-ui'import {    removeCookie} from "./auth";export default function (path, parmas, method = "GET", headerType = "json") {    let baseURL = ""    let headers = {}    let data = {}       if (process.env.NODE_ENV == "development") {        baseURL = "/api"    }     // 设置请求头    if (method == "post") {        if (headerType == "json") {            headers = {                'X-Requested-With': 'XMLHttpRequest',                'Content-Type': 'application/json;charset=UTF-8'            }            data = parmas        } else {            headers = {                'X-Requested-With': 'XMLHttpRequest',                'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'            }            data = Qs.stringify(parmas);        }    } else {        if (headerType !== "json") {            headers = {                'X-Requested-With': 'XMLHttpRequest',                'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'            }            data = Qs.stringify(parmas);            path = path + '?' + data            data = {};        } else {            headers = {                'X-Requested-With': 'XMLHttpRequest'            }            data = Qs.stringify(parmas);            path = path + '?' + data            data = {};        }    }    //   请求拦截    axios.interceptors.request.use(config => {        return config    }, error => {        return Promise.reject(error)    })    axios.interceptors.response.use(response => {        if (response.data.statusCode == 20009) {            // Message.error({ message: '登录过期,请重新登录' }); //total提示            removeCookie('admin-key');            removeCookie('un_name')            localStorage.clear();            window.location.reload();        } else if (response.data.statusCode == 50099) {            response.data.statusMsg = "服务中断,请稍后再试!"        } else if (response.status == 500) {            Message.error({                message: '网络异常,请检查网络连接'            }); //网络异常        }        return response;    }, error => {        // console.log(error.message)        if(error.message.indexOf('timeout') >= 0){            Message.error({                message: '请求超时,请稍后再试!'            }); //后台响应超时        }else if(error.response === undefined){            Message.error({                message: '服务中断,请稍后再试!'            }); //后台无响应         }        // console.log(error.response);        return Promise.resolve(error.response)    })    // 发送请求    return new Promise((resolve, reject) => {        let times = 5000        if (path == 'fileupload/batch') {            times = 50000        }        // console.log(path)        // console.log(headers)        axios({            baseURL,            method: method,            url: path,            data: data,            timeout: times,            headers: headers        })            .then(result => {                resolve(result.data);            })            .catch(err => {                reject(err);            });    });};复制代码

以上是axios.js中的代码,下面是api.js中的使用

import https from "@/utils/axios";//登录const LOGINURL = "/login";export const requestLogin = function (params) {    return https(LOGINURL, params, "post", "from");};复制代码

配置文件反向代理

proxyTable: {   '/api/**': {      target: 'http://192.168.0.137:9765/',      changeOrigin: true,      pathRewrite: {         '^/api': '/'      },    }},复制代码


关注下面的标签,发现更多相似文章
评论