import Vue from "vue";
import router from "@/router";
import Router from '@/router/index'
import axios from "axios";
import qs from "qs";
import { Notification, Loading, Message } from "element-ui";
// var instance = axios.create({
//   baseURL:'',
//   timeout:5000,
//   headers:{"Content-Type":"multipart/form-data"}
// });
//
// Vue.prototype.instance=instance;
//样式文件,需单独引入
import "element-ui/lib/theme-chalk/index.css";
Vue.prototype.$http = axios;
Vue.prototype.$notify = Notification;
Vue.prototype.$loading = Loading;
Vue.prototype.$message = Message;
Vue.prototype.$qs = qs;
let LoadingInstance,
    token = "";
// 环境设置
// 环境的切换
if (process.env.NODE_ENV == "development") {
    axios.defaults.baseURL = "http://cp.zgcareer.com";
} else if (process.env.NODE_ENV == "debug") {
    axios.defaults.baseURL = "http://cp.zgcareer.com";
} else if (process.env.NODE_ENV == "production") {
    axios.defaults.baseURL = "http://cp.zgcareer.com";
}
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
// axios.defaults.headers.common['token'] = localStorage.getItem("token");


// 设置请求超时
axios.defaults.timeout = 10000;
// 请求拦截器
axios.interceptors.request.use(
    config => {
        const token = localStorage.getItem("token");
        config.headers = {
            'Content-Type': 'application/x-www-form-urlencoded',
        };

        if (localStorage.getItem("token")) {
            config.params = { 'token': localStorage.getItem("token") }
        }
        // token && (config.headers.Authorization = token);
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);
// 响应拦截器
axios.interceptors.response.use(
    response => {
        LoadingInstance.close();
        // 如果返回的状态码为1,说明接口请求成功,可以正常拿到数据
        // 否则的话抛出错误
        if (response.data.code == 1) {
            return Promise.resolve(response);
        } else {
            console.log(response.data.code)
            switch (response.data.code) {

                case 0:
                    Notification.info({
                        title: "提示",
                        message: response.data.msg,
                        duration: 1500
                    });
                    break;
                case 401: //未登录
                    console.log(401)
                    Notification.error({
                        title: "错误",
                        message: response.data.msg,
                        duration: 1500
                    });
                    console.log(401, 1)
                    let test1 = localStorage.getItem("schoolSymbol");
                    localStorage.clear();
                    Router.push({
                        path: "/login/" + test1
                    });
                    break;
                case 500:
                    console.log(500, 2)
                    Notification.error({
                        title: "错误",
                        message: response.data.msg,
                        duration: 1500
                    });
                    console.log(500)
                    let test0 = localStorage.getItem("schoolSymbol");
                    localStorage.clear();
                    Router.push({
                        path: "/login/" + test0
                    });
                    break;

                default:
                    Notification.error({
                        title: "错误",
                        message: response.data.msg,
                        duration: 1500
                    });
                    let test4 = localStorage.getItem("schoolSymbol");
                    localStorage.clear();
                    Router.push({
                        path: "/login/" + test4
                    });
            }
            return Promise.reject(response);
        }
    },
    error => {
        LoadingInstance.close();
        // console.log(error.response.status)
        let test2 = localStorage.getItem("schoolSymbol");
        Router.push({
            path: "/login/" + test2
        });
        if (error.response.status) {
            Notification.error({
                title: "错误",
                message: "网络错误",
                duration: 1500
            });
            return Promise.reject(error.response);
        }

    }
);

/**
 * get方法,对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function get(url, params, loading) {
    //console.log(loading);
    // headers? axios.defaults.headers.get['XX-Token'] = headers['XX-Token']:"";
    if (
        loading == "" ||
        loading == undefined ||
        typeof loading == undefined ||
        loading
    ) {
        LoadingInstance = Loading.service({
            //加载loading
            fullscreen: true,
            lock: true,
            // spinner: 'el-icon-loading',
            text: "加载中...",
            background: "rgba(0, 0, 0, 0)"
        });
    }
    return new Promise((resolve, reject) => {
        axios
            .get(url, {
                params: params
            })
            .then(res => {
                resolve(res.data.data);
            })
            .catch(err => {
                reject(err.data);
            });
    });
}

/**
 * post方法,对应post请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 **/

export function post(url, params, loading) {
    if (loading != false) {
        LoadingInstance = Loading.service({
            fullscreen: true,
            lock: true,
            text: "加载中...",
            background: "rgba(0, 0, 0, 0)"
        });
    }
    return new Promise((resolve, reject) => {
        let that = this;
        axios
            .post(url, qs.stringify(Object.assign({}, params)))
            .then(res => {
                resolve(res.data.data);
            })
            .catch(err => {

                reject(err.data);
            });
    });
}

export function uploadFile(Url, data) {
    //上传图片的方法
    return new Promise((resolve, reject) => {
        let instance = axios.create({
            // http://cp.zgcareer.com
            baseURL: "http://cp.zgcareer.com",
            headers: {
                "Content-Type": "multipart/form-data"
            }
        });
        instance
            .post(Url, data)
            .then(res => {
                resolve(res.data.data);
            })
            .catch(error => {
                reject(error.data);
            });
    });
}
export function toast(message, type) {
    //上传图片的方法
    if (type == "error") {
        Notification.error({
            title: "错误",
            message: message,
            duration: 1500
        });
    } else {
        Notification.success({
            title: "提示",
            message: message,
            duration: 1500
        });
    }
}