• axios


    export default {
      get: function (url, params, response) {
        return apiAxios('GET', url, params, response)
      },
      post: function (url, params, response) {
        url = process.env.NODE_ENV != 'production' ? 'pc' + url : url;
        return postOri(url, params, {},response)
      },
      postBlob: function (url, params, response) {
        return postBlob('POST', url, params, response)
      },
      put: function (url, params, response) {
        return apiAxios('PUT', url, params, response)
      },
      delete: function (url, params, response) {
        return apiAxios('DELETE', url, params, response)
      },
      posth5: function (url, params, response) {
        return h5Axios('POST', url, params, response)
      },
      getdat: function (url, params, response) {
        return datAxios('GET', url, params, response)
      },
      postdat: function (url, params, response) {
        return datAxios('POST', url, params, response)
      },
      env:process.env.NODE_ENV != 'production' ? 'pc': '',
    }
    
    
    
    import axios from 'axios'
    import qs from 'qs'
    import {
      Message
    } from 'element-ui';
    import Vue from 'vue'
    import router from '../router'
    axios.interceptors.request.use(
      config => {
          config.headers.lan = (localStorage.getItem('lanCode') || 'cn');
          if(config.method=='post'){
            config.transformRequest = [function (data,type) {
              data = qs.stringify(data)
              return data;}
            ];
          }
          console.log(config,'config')
        return config;
      },
      err => {
        return Promise.reject(err);
    });
    let http = axios.create({
      withCredentials: true,
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
        'lan': localStorage.getItem('lanCode') || 'cn'
      },
      transformRequest: [function (data,type) {
        data = qs.stringify(data)
        return data;
      }]
    });
    let datHttp = axios.create({
      withCredentials: true,
      headers: {
        'Content-Type': 'application/json;charset=utf-8',
        'lan': localStorage.getItem('lanCode') || 'cn'
      },
      transformRequest: [function (data,type) {
        return data;
      }]
    });
    function postOri(url, data = {}, config = {
      "headers": {
       'Content-Type': 'application/json;charset=utf-8',
       'lan': localStorage.getItem('lanCode') || 'cn'
      },
     },response) {
      return new Promise((resolve, reject) => {
       axios.post(url, data, config)
       .then(function (res) {
        let data = res.data;
        if (data.code == 1001) {
          Message.warning(data.message);
          localStorage.clear();
          sessionStorage.clear();
          // Router.push({path:'/login'});
          setTimeout(_ => {
            router.replace({
              path: '/login' //自己修改
            })
          }, 100);
          return;
        }
        if (res.status == 200) {
          response(data);
        }
         
      }).catch(function (err) {
        if (err.response && err.response.status == 500) {
          Message.warning('系统异常');
          response(err);
        }
      })
      })
     }
    axios.interceptors.request 请求之前进来拦截
    import axios from 'axios'
    import qs from 'qs'
    import {
    Message
    } from 'element-ui';
    import Vue from 'vue'
    import router from '../router'
    axios.interceptors.request.use(
    config => {
    config.headers.lan = (localStorage.getItem('lanCode') || 'cn');
    if(config.method=='post'){
    config.transformRequest = [function (data,type) {
    data = qs.stringify(data)
    return data;}
    ];
    }
    console.log(config,'config')
    return config;
    },
    err => {
    return Promise.reject(err);
    });
    let http = axios.create({
    withCredentials: true,
    headers: {
    'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
    'lan': localStorage.getItem('lanCode') || 'cn'
    },
    transformRequest: [function (data,type) {
    data = qs.stringify(data)
    return data;
    }]
    });
  • 相关阅读:
    如何更专业的使用Chrome开发者工具
    Javascript中的Object对象
    【leetcode】 Remove Duplicates from Sorted List
    Windows上x86程序正常但x64程序崩溃问题
    Microsoft source-code annotation language (SAL) 相关
    Visual Studio 2013 编译CEF步骤
    C++中调用Python脚本
    MFCButton Memory leak(内存泄露问题)
    快速排序
    插入排序
  • 原文地址:https://www.cnblogs.com/guidan/p/10489277.html
Copyright © 2020-2023  润新知