import Vue from 'vue';
import axios from 'axios';
import router from '@/router'
axios.defaults.timeout = 65000;
axios.defaults.baseURL = '';
//http request 拦截器
axios.interceptors.request.use(
    config => {
        console.log(config)
        // const token = getCookie('名称');注意使用的时候需要引入cookie方法，推荐js-cookie
        console.log(window.localStorage.getItem("token"))
        var userJson = window.localStorage.getItem("token")
        if(!userJson){
            var userData = false
            var token;
        }else{
            var userData = JSON.parse(userJson)
            var token;
        }  
        if(userData && userData.access_token){
            token = userData.access_token
        }
        
        config.data = JSON.stringify(config.data)
        config.headers['Content-Type'] = 'application/json;charset=UTF-8'
        config.headers['Authorization'] = 'Basic c2FiZXI6c2FiZXJfc2VjcmV0'
        config.headers['Blade-Auth'] = token
        return config;
    },
    error => {
        return Promise.reject(err);
    }
);


//http response 拦截器
axios.interceptors.response.use(
    response => {
        if (response.data.code == 1011008 || response.data.code == 1011006) {
            // Toast.fail('登录过期，跳原生')
            let userData
            var u = navigator.userAgent;
            var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
            var isiOS = u.indexOf('Mac OS X') > -1
            try{
                if(isAndroid){
                    window.jsi.logout()
                }
                if(isiOS){
                    
                }
            }catch(err){
            }
        }
        // if (response.data.code != 200) {
        //     Toast.fail(response.data.message)
        // }
        
        switch (response.data.code) {
            case 32:
                response.msg = '请求错误(400)'
                break;
        }
        return response;
    },
    err => {
        console.log(err.response)
        if (err && err.response) {
            console.log(err.response)
            if(err.response.status == 500 || err.response.status == 503){
                Toast.fail('服务器错误')
                return
            }
            switch (err.response.data.code) {
                
                case 401:
                    Toast.fail('登录已失效，请重新登录')
                    router.push("/")

                    break;
                case 403:
                    err.message = '拒绝访问(403)';
                    break;
                case 404:
                    err.message = '请求出错(404)';
                    break;
                case 408:
                    err.message = '请求超时(408)';
                    break;
                case 500:
                    err.message = '服务器错误(500)';
                    break;
                case 501:
                    err.message = '服务未实现(501)';
                    break;
                case 502:
                    err.message = '网络错误(502)';
                    break;
                case 503:
                    err.message = '服务不可用(503)';
                    break;
                case 504:
                    err.message = '网络超时(504)';
                    break;
                case 505:
                    err.message = 'HTTP版本不受支持(505)';
                    break;
                default:
                    err.message = `${err.response.data.msg}`;
            } 
            // if(err.response.status == 404){
            //     router.push("/*")
            // }
            // if(err.response.status == 500 || err.response.status == 502){
            //     router.push("/500")
            // }
            // if(err.response.status == 401){
            //     router.push("/401")
            // }
        } else {
            err.message = '连接服务器失败!'
        }
        console.log(err.message)
        // Toast.fail(err.message)
        return Promise.reject(err);
    }
)

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

export function get(url, params = {}) {
    return new Promise((resolve, reject) => {
        if(process.env.NODE_ENV == "development"){
            var getUrl = "http://" + window.location.host + "" + url
        }else{
            var getUrl = "https://" + window.location.host + "/api" + url
        }
        axios.get(getUrl, {params:params})
            .then(response => {
                setTimeout(() => {
                }, 500)
                resolve(response.data);
            }, err => {
                reject(err)
            })
    })
}


/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function post(url, data = {},params,headers) {
    return new Promise((resolve, reject) => {
        if(process.env.NODE_ENV == "development"){
            var postUrl = "http://" + window.location.host + url
        }else{
            var postUrl = "https://" + window.location.host +'/api' + url
        }
        axios.post(postUrl, data = {},{
            params:params,
            headers:headers || null
        })
            .then(response => {
                setTimeout(() => {
                }, 500)
                resolve(response.data);
            }, err => {
                reject(err)
            })
    })
}

export default {
    post,
    get
}