import axios from 'axios';
import { ElLoading, ElMessage } from 'element-plus'
const service = axios.create({
    // process.env.NODE_ENV === 'development' 来判断是否开发环境
    // easy-mock服务挂了，暂时不使用了
    baseURL: '/api',
    timeout: 50000
});
var loadinginstace: any = null
service.interceptors.request.use((config: any) => {
        // var access_token=''
        // if(JSON.parse(localStorage.getItem("token") || '')){
        //     access_token=JSON.parse(localStorage.getItem("token") || '').access_token
        // }
        config.headers={
            'content-type':'application/json; charset=utf-8',
            // 'Authorization':'bearer'+access_token
        }
        loadinginstace = ElLoading.service({ fullscreen: true })
        return config;
    },
    (error: any) => {
        console.log(error);
        return Promise.reject();
    }
);

service.interceptors.response.use(
    (response: any) => {
        if(loadinginstace.close){
            loadinginstace.close()
        }
        if (response.status === 200) {
            return response.data;
        } else if(response.data.error == 'invalid_token'){
            ElMessage.error('Please log in again')
        } else {
            Promise.reject();
        }
    },
    (error: any) => {
        if(loadinginstace.close){
            loadinginstace.close()
        }
        console.log(error);
        return Promise.reject();
    }
);

export default service;
