require.js 2.2 KB
import Vue from 'vue';
import axios from 'axios'
import Qs from 'qs'
import { getToken } from '@/axios/token'
import { Message } from 'element-ui';
axios.defaults.baseURL='http://192.168.1.108:5000';


// axios.defaults.baseURL = process.env.VUE_APP_REQUEST_URL;
// axios.defaults.baseURL = process.env.VUE_APP_IONICURL;
// if(process.env.NODE_ENV=="development"){
//     console.log('BASE_API=development='+process.env.NODE_ENV);
//     axios.defaults.baseURL='http://192.168.1.35:5001';
// }else if(process.env.NODE_ENV == "production"){
//     console.log('BASE_API=production='+process.env.NODE_ENV);
//     axios.defaults.baseURL='http://192.168.1.35:5001';
// }
axios.defaults.transformRequest = function (data, headers) {
    let ContentType = headers['Content-Type'] || headers.common['Content-Type'] || headers.post['Content-Type'] || 'application/json';
    if (ContentType === "application/json") {
        return JSON.stringify(data);
    }

    if (ContentType === "application/x-www-form-urlencoded") {
        return Qs.stringify(data);
    }
    return data;
};


axios.interceptors.request.use((config)=>{
    // config.headers['token'] = getToken();
    return config;
})






// 添加响应拦截器
axios.interceptors.response.use((response)=>{
    console.log('response=='+response);
     let {status} = response;
    if(status == 200 || status == 204 || status == 201){
        response.status = 200;
    }
    // 对响应数据做点什么
    // let {code,msg} = response.data;
    // if(code != 20000){
    //     Message({
    //         message: msg || '网络错误',
    //         type: 'warning',
    //         duration:2000
    //       });
    // }
    return response
}, err=>{
    // 对响应错误做点什么
    // Message({
    //     message: '服务器不给力!!!',
    //     type: 'error',
    //     duration:2000
    //   });
    console.log(err);
    return Promise.reject(err);
})
// const api = {
//     axios: axios,
//     get: (url, params) => axios.get(url, {params:params}).then(handlerResponse),
//     post: (url, data, config) =>
//       axios
//       .post(url, data, Object.assign({}, baseConfig, config))
//       .then(handlerResponse)
//   }

Vue.prototype.$axios = axios;