import Vue from 'vue';
import axios from 'axios';
import VueAxios from 'vue-axios';
import config from './config'

const { serverAddress } = config

// import DEV from "@/dev_config"; // 导入上线配置
// const { ONLINE_BASE_HREF, SERVER_HREF } = DEV;

Vue.use(VueAxios, axios)


/* ajax封装 */
export const ajax = function (type = "post", url, params) {
    let token = localStorage.getItem("token")
    return new Promise((resole, reject) => {
        axios({
            method: type,
            url: serverAddress + url,
            params,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
				'x-auth-token': token
            }
        }).then((res) => {
            switch(res.data.errCode) {
                case 'request_not_authorize': // 登录过期
                    let url = window.location.protocol + "//" + window.location.host + "/#/login"
                    window.location.replace(url)
                    break;
            }
            resole(res.data);
        }).catch((err) => {
            reject(err.data);
        });
    });
}


/* 测试服务的 ajax封装 */
export const ajaxTest = function (type = "post", url, params) {
    return new Promise((resole, reject) => {
        axios({
            method: type,
            url: "http://119.3.84.55:31288" + url,
            // url: url,
            params,
            headers: {
                'Content-Type': 'application/json',
            }
        }).then((res) => {
            resole(res.data);
        }).catch((err) => {
            reject(err.data);
        });
    });
}
