import Vue from 'vue'
import axios from 'axios'

// axios 配置
axios.defaults.timeout = 100000
// axios.defaults.baseURL = 'https://api.github.com';
Vue.prototype.Ajax = axios
// Add a request interceptor 请求
axios.interceptors.request.use(function (config) {
  
    config.headers = {Pragma: 'no-cache'}
    return config
}, function (error) {
    
    return Promise.reject(error)
})

// http response 拦截器 errorCode
axios.interceptors.response.use(
    response => {
        let data = response.data
        return data
    },
    error => {
        return Promise.reject(error)
    }
)

export default {
    Get(url, params, fn, errorFn) {
        axios.get(URL + url, {params: params}).then(fn).catch(errorFn)
    },
    Post(url, params, fn) {
        axios.post(URL + url, params).then(fn).catch(err => console.error(err))
    },
    Delete(url, params, fn) {
        axios.delete(URL + url, {params: params}).then(fn).catch(err => console.error(err))
    },
    uploadImg: URL + '/file/upload_pic',    //上传图片接口
    uploadFile: URL + '/file/upload_file',    //上传文件接口，不用登录的

    Down(url, name, type="application/pdf") {
        axios({
            method: 'get',
            url: URL + url,
            responseType: 'blob'
        }).then(data => {
            if (!data) return
            if (isIE) {
                var blob = new Blob([data], {type: type})
                if (window.navigator && window.navigator.msSaveOrOpenBlob) { // for IE
                    window.navigator.msSaveOrOpenBlob(blob, name)
                }
            } else {
                let url = window.URL.createObjectURL(new Blob([data]))
                let link = document.getElementById('download')
                link.href = url
                // link.target = "_blank"
                link.setAttribute('download', name)
                link.click()
            }
        }).catch(err => console.error(err))
    }
}

