import axios from "axios";
import qs from "qs";

axios.defaults.baseURL = process.env.VUE_APP_BASE_API //测试


//post请求头
axios.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded;charset=UTF-8";

// 表示跨域请求时是否需要使用凭证 允许跨域携带cookie信息
axios.defaults.withCredentials = false;

// 允许跨域
axios.defaults.headers.post["Access-Control-Allow-Origin-Type"] = "*";
//axios.defaults.headers.common["Authorization"] = 'Bearer 7b18686e-0d1f-483b-b3f2-814e4a60e467';//`Bearer ${localStorage.getItem('access_token')`;
axios.defaults.headers.common["Authorization"] = 'Bearer '+localStorage.getItem('Authorization')+''

//设置超时
axios.defaults.timeout = 15000;

axios.interceptors.request.use(
    config => {
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);

axios.interceptors.response.use(
    response => {
        if (response.status == 200) {
            return Promise.resolve(response);
        } else {
            return Promise.reject(response);
        }
    },
    error => {

        console.log(JSON.stringify(error), '请求异常', {
            confirmButtonText: '确定',
            callback: (action) => {
                console.log(action)
            }
        });
    }
);
export default {
    /**
     * @param {String} url
     * @param {Object} data
     * @returns Promise
     */
    post(url, data) {
        return new Promise((resolve, reject) => {
            axios({
                method: 'post',
                url,
                data: qs.stringify(data),
            })
                .then(res => {
                    resolve(res.data)
                })
                .catch(err => {
                    reject(err)
                });
        })
    },

    get(url, data,type) {
        if(type=='video'){
            axios.defaults.baseURL = process.env.VUE_APP_BASE_VIDEO_API
        }else{
            axios.defaults.baseURL = process.env.VUE_APP_BASE_API
        }
        return new Promise((resolve, reject) => {

            axios({
                method: 'get',
                url,
                params: data,
            })
                .then(res => {
                    resolve(res.data)
                })
                .catch(err => {
                    reject(err)
                })
        })
    },

    baseUrl: process.env.VUE_APP_BASE_API
};
