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

const BASE_URL = process.env.VUE_APP_API

// 图片的请求
let signAxios = axios.create({
	baseURL: BASE_URL,
	timeout: 600000,
	headers: {
		'Content-Type': 'multipart/json;charset=UTF-8;',
	}
});

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


// 响应的拦截
signAxios.interceptors.response.use(res => {
	if (res.status === 200) {
		if (res.data && res.data.status === 401) {
			removeToken()
			location.reload()
		}
		return Promise.resolve(res);
	} else {
		return Promise.reject(res);
	}
},  (error) => {
    if (error.response && error.response.data) {
		const data = error.response.data;
		if (data.code != 40003) {
			const msg = data.message || data.msg;
			if (msg && msg.length > 0) {
				Toast(data.message || data.msg);
			}
			if (data.code == '403') {
				removeToken()
				setTimeout(() => {
					location.reload()
				}, 1000);
			}
		}
    }
    return Promise.reject(error)
});
 
export default signAxios