vue配置开发环境和测试环境

154 阅读1分钟

构建打包环境

在vue.config.js文件中设置代理

module.exports = {
    productionSourceMap:false,
    outputDir:process.env.NODE_ENV == 'production'?'dist':'test', //构建打包目录
    devServer:{
        proxy:'http://test.haitao.api.dxanm.com'
    }
}

创建.env.test文件

NODE_ENV = 'uat'

创建baseUrl.js工具文件

"use strict";

const baseUrl = ()=>{
    let env = process.env.NODE_ENV
    console.log(env)
    switch(env){
        case 'production':
            return 'http://haitao.api.dxanm.com'; //正式地址
        case 'uat':
            return 'http://test.haitao.api.dxanm.com'; //测试地址
        default:
            return window.location.origin;
    }
} 


export default baseUrl

在axios封装文件中

import axios from 'axios';
import baseUrl from '../utils/baseUrl';


const config = {
    baseURL: baseUrl() || "",
    timeout: 60 * 1000, // Timeout
    withCredentials: true, // Check cross-site Access-Control
  };
 
const _axios = axios.create(config);
//http request 拦截器
_axios.interceptors.request.use(
    config => {
        // var token = sessionStorage.getItem("token", token);
        if (token) {
            //将token放到请求头发送给服务器,将tokenkey放在请求头中
            // config.headers.token = token;
        } else {
            // if (config.url.indexOf('login') == -1) {
            //     // location.href = '/login'
            // }
        }
        return config;
    },
    error => {
        return Promise.reject(err);
    }
);


//http response 拦截器
_axios.interceptors.response.use(
    response => {
        if (response.data.code === 403) {
            Message.error(response.data.msg);
            location.href = '/login'
            //其余错误状态处理 
        } else {
            return response;
        }
    },
    error => {
        // location.href = "/login"
        return Promise.reject(error)
    }
)


/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */

export function get(url, params) {
    return new Promise((resolve, reject) => {
        _axios.get(url, {
            p