import axios from 'axios'
import { AxiosRequestConfig, AxiosResponse } from 'axios'
import { message, Modal } from 'ant-design-vue';
import router from '@/router';
const request = axios.create({
	baseURL: process.env.NODE_ENV === "development" ? "https://jpcj1.zzxcx.net/stage-api/" : "https://jpcj.zzxcx.net/prod-api/",
});
request.interceptors.request.use((config) => {
	console.log(config)
	const token = window.localStorage.getItem("token")
	// 是否需要设置 token
	if (config.headers.isLogin == '1' || !token) {
		config.headers["Authorization"] = ""; // 让每个请求携带自定义token 请根据实际情况自行修改
	}
	else {
		config.headers["Authorization"] = ("Bearer " + token);
	}

	return config;
}, error => {
	console.log(error)

});
request.interceptors.response.use((res) => {
	const code = res.data.code
	switch (code) {
		case 401:
			Modal.confirm({
				content:'登录失效,是否需要重新登录',
				onOk:()=>{
					router.push('/login')
				}
			})
			break;
		case 500:
			message.error('服务器错误')
			break;

	}


	return res.data

})
export default request