import axios from "axios";
const request = axios.create({
	baseURL: import.meta.env.MODE === "production" ? "https://nbjk.zzxcx.net/nbjk-admin" : "https://nbjk1.zzxcx.net/nbjk-admin",
});

request.interceptors.request.use((config) => {
	console.log(config)
	let 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 => {

});

request.interceptors.response.use((res) => {
	if (res && res.data) {
		switch (res.data.code) {
			case 401:

				// Toast("登录失败,需要再次登录")
				window.setTimeout(() => {

					switch (import.meta.env.MODE) {
						case "development":
							location.replace(`https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx457ba48e0801c0b6&redirect_uri=https://nbjk1-h5.zzxcx.net/&response_type=code&scope=snsapi_userinfo&state=LOGIN#wechat_redirect`)
							break;
						case "test":
							location.replace(`https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx457ba48e0801c0b6&redirect_uri=https://nbjk1-h5.zzxcx.net/&response_type=code&scope=snsapi_userinfo&state=LOGIN#wechat_redirect`)
							break;
						case "production":
							location.replace(`https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx457ba48e0801c0b6&redirect_uri=https://nbjk-h5.zzxcx.net/&response_type=code&scope=snsapi_userinfo&state=LOGIN#wechat_redirect`);
							break;
					}
				}, 2000)
				break;
			case 502:
				if (res.data.msg == "微信授权无效，请重新授权") {
					location.replace("https://nbjk1-h5.zzxcx.net/")
				}

				break;
			default:
				break;
		}

	}

	// console.log(res)
	return res;
});

export default request;
