import axios from 'axios'
import qs from 'qs'
import context from '@/main'

import store from '@/store/index'

// 服务接口地址
axios.defaults.baseURL = systemConfig.base_url
// 携带 cookie
axios.defaults.withCredentials = true

// 默认 post 请求，使用 multipart/form-data 形式
axios.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";

// 创建 axios 实例
const service = axios.create({
    timeout: 6000000, // 请求超时时间,
})

// 结果拦截
const userFailCodes = [206, 207] // 用户失效code
const loginFailCodes = [208, 212] // 登录失效code
const noDataCodes = [202, 205] // 无数据
service.interceptors.response.use(response => {
    let code = response.data.code
    if (code && loginFailCodes.indexOf(code) > -1) {
        context.config.globalProperties.$message({ message: '登录失效，请重新登录', type: 'error' })
        store.commit('resetState', {});
        setTimeout(function () {
            context.config.globalProperties.judgeLogin();
        },100)
    } else if (code && userFailCodes.indexOf(code) > -1) {
        context.config.globalProperties.$message({ message: '用户状态失效，请联系管理员', type: 'error' })
    } else if (code && noDataCodes.indexOf(code) > -1) {
        context.config.globalProperties.$message({ message: '未查询到相关数据', type: 'warning' })
    } else if (code !== 200) {
        context.config.globalProperties.$message({ message: response.data.content || '请求出错', type: 'error' })
    }
    return response
})

// 普通请求
function req(method, url, params) {
    return new Promise((resolve, reject) => {
        service({
            method: method,
            url,
            data: params,
            headers: {
                'token': store.state.token || ''
            }
        }).then(response => {
            resolve(response.data)
        }).catch(err => {
            reject(err)
        })
    })
}
// 表单请求
function formReq(method, url, params) {
    return new Promise((resolve, reject) => {
        service({
            method: method,
            url,
            data: qs.stringify(params),
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'token': store.state.token || ''
            }
        }).then(response => {
            resolve(response.data)
        }).catch(err => {
            reject(err)
        })
    })
}

// 请求方法
export default {
    get(url, params) {
        return req('GET', url, params);
    },
    post(url, params) {
        return req('POST', url, params);
    },
    put(url, params) {
        return req('PUT', url, params);
    },
    delete(url, params) {
        return req('DELETE', url, params);
    },
    getForm(url, params) {
        return formReq('GET', url, params)
    },
    postForm(url, params) {
        return formReq('POST', url, params)
    },
    putForm(url, params) {
        return formReq('PUT', url, params)
    },
    deleteForm(url, params) {
        return formReq('DELETE', url, params)
    },
    getFile(url, params) {
        return new Promise((resolve, reject) => {
            service({
                method: 'GET',
                url,
                params: params,
                contentType: false,
                processData: false,
                responseType: 'blob'
            }).then(res => {
                resolve(res)
            }).catch(err => {
                reject(err)
            })
        })
    },
    postWithFile(url, params) {
        return new Promise((resolve, reject) => {
            service({
                method: 'POST',
                url,
                data: params,
                contentType: false,
                processData: false,
                headers: {
                    'Content-Type': 'multipart/form-data',
                }
            }).then(res => {
                resolve(res)
            }).catch(err => {
                reject(err)
            })
        })
    }
}
