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

const { serverAddress } = config

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

Vue.use(VueAxios, axios)


/* 常用ajax封装 （固定URL） */
export const ajax = function (type = "post", url, params) {
    let token = localStorage.getItem("token")
    if (type.toLowerCase() == 'get') {
        return new Promise((resole, reject) => {
            axios({
                method: type,
                url: serverAddress + url,
                params,
                paramsSerializer: function (params) {
                    return Qs.stringify(params, { arrayFormat: 'brackets' })
                },
                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);
            });
        });
    } else {
        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);
            });
        });
    }
}



// export const upload = function (url, data) {
//     let token = localStorage.getItem("token")
//     return new Promise((resole, reject) => {
//         axios({
//             method: 'POST',
//             url: serverAddress + url,
//             data,
//             headers: {
//                 'Content-Type': 'multipart/form-data',
//                 '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);
//         });
//     });
// }


