httpx.js 2.12 KB
import axios from "axios";
import { MessageBox } from 'element-ui'


const httpx = axios.create({
    timeout: 8000  // 请求超时时间
});


/****** 数据请求之前******/
httpx.interceptors.request.use(config => {
    console.log("数据请求之前.....")
    return config;
}, error => {
    //请求错误处理
    const txt = `本页数据请求之前中存在错误 :${error}
                     地址:${httpx.baseURL}
                     您访问的页面资源可能不存在,或者不支持跨域,请核实,
                     点击任意空白处继续或刷新浏览器,问题频繁出现请联系管理员。`
    MessageBox.alert(txt, "提示")
    Promise.reject(error)
});


/****** respone拦截器==>对响应做处理 ******/
httpx.interceptors.response.use(response => {
    console.log("数据请求成功之后...")
    console.log(response)
    if (response.data.code === 200) return response.data;

    const txt = `本页数据请求成功之后返回结果 :${response.data.msg}
                     地址:【${response.config.url}
                     请求方式:【${response.config.method}
                     您访问的页面资源可能不存在,或者不支持跨域,请核实,
                     点击任意空白处继续或刷新浏览器,问题频繁出现请联系管理员。`
    MessageBox.alert(txt, "提示");
}, error => {
    var msg = JSON.stringify(error);
    const txt = `本页数据请求成功之后中存在错误 :${msg}
                     地址:【${httpx.baseURL}
                     您访问的页面资源可能不存在,或者不支持跨域,请核实,
                     点击任意空白处继续或刷新浏览器,问题频繁出现请联系管理员。`
    MessageBox.alert(txt, "提示")
    return Promise.reject(error)
});

/**
 * 请求案例:
 *  回调函数 嵌套回调焊 methods对应的方法加 async 
    let res = await service({
        url: newUlr,
        method: "get",
    })
    2:单个请求
    service({
        url: newUlr,
        method: "get",
    }).then((res) => {
    
    })
 */

export default httpx