//Http配置
import Axios from 'axios'
import {
	ElMessage
} from 'element-plus'
import router from "../../router"
import TokenManager from "./token"
Axios.defaults.headers['channel'] = 'bms';

/**
 * 拦截http请求
 */
Axios.interceptors.request.use(config => {
	const tokenStr = TokenManager.getToken();
	if (tokenStr != null) config.headers['token'] = JSON.parse(TokenManager.getToken()).token;
	return config;
});


/**
 * 拦截http响应
 */
Axios.interceptors.response.use(res => {
		switch (res.data.status) { //状态码
			case 200:
				return res.data;
			case 300: //警告
				ElMessage.error(res.data.msg);
				break;
			case 400: //无权限
				ElMessage.error("请先登录");
				router.replace("/login");
				break;
			case 500: //服务器错误
				ElMessage.error("服务器开小差了");
				break;
			default: //无状态码
				ElMessage.error("请求状态异常");
				break;
		}
		throw "stop";
	},
	error => {
		console.log(error);
		throw "stop";
	});
export default Axios
