import axios from 'axios'
import { ElMessage } from 'element-plus'
// import store from '@/store'
import { getToken, removeToken } from '@/utils/auth'
import qs from 'qs'
import router from '@/router'

// create an axios instance

let baseURL = window.PLATFROM_CONFIG.baseUrl
    // alert(process.env.NODE_ENV)
    // alert(baseURL)
    // alert(process.env.VUE_APP_BASE_URL)

/**
 * @description 处理code异常
 * @param {*} code
 * @param {*} msg
 */
const handleCode = (code, msg) => {
    msg
    switch (code) {
        case '401':
            // ElMessage({
            //     message: '登录失效',0111111111111111111
            //     type: 'error',
            //     duration: 2 * 1000
            // })
            router
            // router.replace({ path: "/" });
            window.location.href = 'https://wx.ewoogi.com/manage/#/login'

            removeToken()
            return;
    }
}

const service = axios.create({
        baseURL: baseURL,
        // baseURL: process.env.NODE_ENV !== "development" ? baseURL : process.env.VUE_APP_BASE_URL,
        withCredentials: true, // send cookies when cross-domain requests
        timeout: 5000, // request timeout
        headers: {
            'Content-Type': 'application/json;charset=UTF-8',
        },
    })
    // request interceptor


service.interceptors.request.use(


    config => {
        // config.headers.accessToken = getToken()
        var tokenInfo = localStorage.getItem('accessToken')
        // console.log('tokenInfo')
        // console.log(tokenInfo)


        config.headers.accessToken = tokenInfo

        // config.headers.accessToken = getToken()
        getToken()


        if (
            config.data &&
            config.headers['Content-Type'] ===
            'application/x-www-form-urlencoded;charset=UTF-8'
        ) {
            config.data = qs.stringify(config.data);
        }


        // do something before request is sent

        // if (store.getters.token) {
        //     // let each request carry token
        //     // ['X-Token'] is a custom headers key
        //     // please modify it according to the actual situation
        //     config.headers['X-Token'] = getToken()
        // }

        if (config.method == 'get') {
            let ext_str = config.url.indexOf('?') == -1 ? '?' : '&';
            let obj_val = '';
            for (let k in config.data) {
                obj_val += k + "=" + config.data[k] + "&";
            }
            config.url += ext_str + obj_val;
        }
        // return config;



        return config
    },
    error => {
        // do something with request error
        console.log(error) // for debug
        return Promise.reject(error)
    }
)

// response interceptor
service.interceptors.response.use(
    /**
     * If you want to get http information such as headers or status
     * Please return  response => response
     */

    /**
     * Determine the request status by custom code
     * Here is just an example
     * You can also judge the status by HTTP Status Code
     */
    response => {
        const res = response.data
        handleCode(res.code, res.msg)



        return res

    },
    error => {
        // console.log('err' + error.response.data.msg) // for debug
        handleCode(error.code, error.msg)
        ElMessage
        // ElMessage({
        //     message: error.response.config.url === '/user/login?' ? error.response.data.msg : error.message,
        //     type: 'error',
        //     duration: 5 * 1000
        // })
        return Promise.reject(error)
    }

)

export default service