import axios from 'axios'
import { Toast } from 'vant'

import { logout } from './auth'

// const token = getStorage("token");
// const BASE_URL = getServerUrl();

// const BASE_URL = 'http://183.66.101.58:10023/';
// const token = 'b7bb2db5-82e5-4976-a4f3-6654ae9a8baf';

// const BASE_URL = 'http://222.182.202.109:33400/';
const BASE_URL = process.env.VUE_APP_API
// const token = getToken() || ''

axios.defaults.timeout = 600000;
axios.defaults.baseURL = BASE_URL;
axios.defaults.headers.post['Content-Type'] = 'multipart/json;charset=UTF-8;';

// 图片的请求
let imgAxios = axios.create({
	baseURL: BASE_URL,
	timeout: 600000,
	headers: {
		'Content-Type': 'multipart/form-data',
		// 'token': `${ token }`
	}
});

let proxyPost = axios.create({
	baseURL: '/api',
	timeout: 600000
});

// 请求拦截
axios.interceptors.request.use((config) => {
	// if (config.logout) {
		// delete config.headers.token
	// } else {
		// config.headers.token = getToken() || ''
	// }
		if (config.method === 'post') {
			// config.data = {
			// 	...config.data
			// };
		} else if (config.method === 'get') {
			config.params = {
				...config.params
			};
		}
		return config;
	},
	(error) => {
		return Promise.error(error);
	});


// 响应的拦截
axios.interceptors.response.use(res => {
	// console.log('response: ', res.data?JSON.stringify(res.data):'null')
	if (res.status === 200) {
		if (res.data && res.data.status === 401) {
			logout()
			location.reload()
		}
		return Promise.resolve(res);
	} else {
		console.log('res: ', res)
		// Toast(res)
		return Promise.reject(res);
	}
},  (error) => {
    if (error.response && error.response.data) {
		const data = error.response.data;
		if (error.response.status == 502 || error.response.status == 500) {
			Toast('服务器异常');
			document.getElementsByClassName('overLay_xy')[0].style.display = 'none';
		}
		else if (data.code != 40003) {
			const msg = data.message || data.msg;
			if (msg && msg.length > 0) {
				Toast(data.message || data.msg);
			}
			if (data.code == '403') {
				logout()
				setTimeout(() => {
					location.reload()
				}, 1000);
			}
		}
    }
    return Promise.reject(error)
});


proxyPost.interceptors.request.use((config) => {
	// config.headers.token = getToken() || ''

	if (config.method === 'post') {
		// config.data = {
		// 	...config.data
		// };
	} else if (config.method === 'get') {
		config.params = {
			...config.params
		};
	}
	return config;
},
(error) => {
	return Promise.error(error);
});

// 响应的拦截
proxyPost.interceptors.response.use(res => {
	console.log('response: ', res.data?JSON.stringify(res.data):'null')
	if (res.status === 200) {
		if (res.data && res.data.status === 401) {
			logout()
			location.reload()
		}
		return Promise.resolve(res);
	} else {
		return Promise.reject(res);
	}
});


/* GET方法，对应GET请求 */
export function get(url, params) {
	return new Promise((resolve, reject) => {
		axios.get(url, {
			params: params
		}).then(res => {
			resolve(res.data);
		}).catch(err => {
			reject(err.data)
		})
	})
}
/* post方法，对应post请求 */
export function post(url, params) {
	return new Promise((resolve, reject) => {
		axios.post(url, params).then(res => {
				resolve(res.data);
			})
			.catch(err => {
				reject(err)
			})
	});
}

/* post IMg方法，对应post请求 */
export function postImg(url, params) {
	return new Promise((resolve, reject) => {
		imgAxios.post(url, params)
			.then(res => {
				resolve(res.data);
			})
			.catch(err => {
				reject(err.data)
			})
	});
}

export function put(url, params) {
	return new Promise((resolve, reject) => {
		axios.put(url, params)
			.then(res => {
				resolve(res.data);
			})
			.catch(err => {
				reject(err.data)
			})
	});
}

export function deleteRequest(url, params) {
	return new Promise((resolve, reject) => {
		axios.delete(url, params)
			.then(res => {
				resolve(res.data);
			})
			.catch(err => {
				reject(err.data)
			})
	});
}

export function Post(url, data, config) {
	return new Promise((resolve, reject) => {
		proxyPost.post(url, data, config)
			.then(res => {
				resolve(res.data);
			})
			.catch(err => {
				reject(err.data)
			})
	});
}

/* GET方法，对应GET请求 */
export function Get(url, params) {
	return new Promise((resolve, reject) => {
		proxyPost.get(url, {
			params: params
		}).then(res => {
			resolve(res.data);
		}).catch(err => {
			reject(err.data)
		})
	})
}

export const imgBaseUrl = BASE_URL + '/';
 
export default axios