import axios from "axios";
import { requestLogger, responseLogger } from "axios-logger";
import store from "@/store";
import { Toast } from "vant";
const request = axios.create({
	baseURL: "/prod-api" ,
});

request.interceptors.request.use((config) => {
	console.log(config)
	// 是否需要设置 token
	if (config.headers.isToken !== false) {
		config.headers["Authorization"] = "Bearer " + store.getters.getToken; // 让每个请求携带自定义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=wx67ca1b8c9816ef28&redirect_uri=https://jpcj-h5.zzxcx.net/home/test&response_type=code&scope=snsapi_userinfo&state=LOGIN#wechat_redirect`)
							break;
						case "test":
							location.replace(`https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx67ca1b8c9816ef28&redirect_uri=https://jpcj-h51.zzxcx.net/home/test&response_type=code&scope=snsapi_userinfo&state=LOGIN#wechat_redirect`)
							break;
						case "production":
							location.replace(`https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx67ca1b8c9816ef28&redirect_uri=https://jpcj-h5.zzxcx.net/home/test&response_type=code&scope=snsapi_userinfo&state=LOGIN#wechat_redirect`);
							break;
					}
				}, 2000)
				break;
			case 502:
				if (res.data.msg == "微信授权无效，请重新授权") {
					location.replace("https://jpcj-h5.zzxcx.net/home/test")
				}

				break;
		}

	}

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

export default request;
