import Vue from "vue";
import axios from "axios";
import vueAxios from "vue-axios";
import qs from "querystring";
import ErrorPage from '../pages/errPages.vue'
import store from "../store/store";
// import router from '../router'
// import store from "../store/store";
import {
	Toast,
	Notify
} from "vant";

Vue.use(vueAxios, axios); // Vue.axios/this.axios/this.$http使用axios，一次封装方便协作规范
Vue.use(Toast);
let requestIndex = 0;

Toast.allowMultiple();
//防止b端调用加载
if (sessionStorage.getItem('isC') == 'c') {
	// 添加请求拦截器
	// console.log('xxxxxxxxxxxxxxxx1234567u8i')
	axios.interceptors.request.use(
		function(config) {
			// 在发送请求之前做些什么

			if (config.url.includes('/dz/qd/common/saveLog') || config.url.includes('/dz/eventLog/saveLog')) {

			} else {
				if (requestIndex < 0) {
					requestIndex = 0;
				}
				requestIndex++;
				// store.commit("increase", toastIndex);
				// console.log('加载中...');
				// 兰蔻老虎机去掉接口请求加载效果 
				if(sessionStorage.getItem('templateId') != 'dz903235512'){
					Toast.loading({
						duration: 0, // 持续展示 toast
						message: "加载中...",
						forbidClick: true
					});
				}
			}
			return config;
		},
		function(error) {
			// 对请求错误做些什么
			return Promise.reject(error);
		}
	);

	// 添加响应拦截器
	axios.interceptors.response.use(
		function(response) {
			// 对响应数据做点什么
			// store.commit("reduce", 1);
			// console.log(store.state.axiosCounter,'store.state.axiosCounter')
			// if (store.state.axiosCounter == 0) {
			// console.log('111111111111')
			requestIndex--;
			if (requestIndex <= 0) {
				Toast.clear();
			}
			// }
			return response;
		},
		function(error) {
			if (process.env.NODE_ENV != "development") {
				// location.href = 'https://3.cn/10-0hi80x?errorAjax=1';
			}
			// 对响应错误做点什么
			return Promise.reject(error);
		}
	);
}
export default {
	// post请求
	post(url, params) {
		let isLogin = url.indexOf('/user-info/login') != -1
		return new Promise((resolve, reject) => {
			axios({
					method: "post",
					url: url,
					headers: {
						"Content-Type": "application/json;charset=UTF-8",
						token: isLogin ? '' : store.state.token //42416b21-3a61-4695-9e40-ffa883996339
					},
					data: params
				})
				.then(res => {
					console.log(res, "request返回数据")
					if (res.status === 200) {
						// console.log(res, 'asdasd')
						if (res.data.resp_code == 50009) {
							//触发活动已结束，则刷新页面
							// location.reload();
							console.log('活动已结束')
							// Notify('活动已结束33')

						}
						if (res.data.resp_code == 1000 || res.data.resp_code == 1002) {
							if (isLogin) {
								// location.replace(process.env.ACTIVITY_HOST + "/errorPage");
							}

						}
						if (res.data.resp_code == 50012) {
							//触发活动已结束，则刷新页面
							// location.reload();
							resolve(res);

						} else {
							resolve(res);
						}
						//判断活动对象 1 是活动对象 2 不是活动对象
						// location.replace(process.env.ACTIVITY_HOST+"/errorPage");
						Toast.clear();
					}
				})
				.catch(error => {
					if (isLogin) {
						// location.replace(process.env.ACTIVITY_HOST + "/errorPage");
					} else {
						// location.replace("https://3.cn/10-0hi80x");
					}
					console.log(error)

					reject(error);
					Toast.clear();

				});
		});
	},

	get(url, params) {
		return new Promise((resolve, reject) => {
			axios(url, {
					params,
					headers: {
						"Content-Type": "application/json;charset=UTF-8",
						token: store.state.token //42416b21-3a61-4695-9e40-ffa883996339
					},
				})
				.then(res => {
					if (res.status === 200) {
						resolve(res);
					}
				})
				.catch(error => {
					// location.replace("https://3.cn/10-0hi80x");
					reject(error);
				});
		});
	},

	// post请求
	uploadFile(url, params) {
		console.log(params, 'asdsdasdsad11111111111')
		let isLogin = url.indexOf('/user-info/login') != -1
		return new Promise((resolve, reject) => {
			axios({
					method: "post",
					url: url,
					data: params,
					headers: {
						"content-type": "multipart/form-data",
						"token": isLogin ? '' : store.state.token //42416b21-3a61-4695-9e40-ffa883996339
					},
				})
				.then(res => {
					console.log(res, "12313123")
					if (res.status === 200) {
						// console.log(res, 'asdasd')
						if (res.data.resp_code == 50009) {
							//触发活动已结束，则刷新页面
							// location.reload();
							console.log('活动已结束')
							// Notify('活动已结束33')

						}
						if (res.data.resp_code == 1000 || res.data.resp_code == 1002) {
							if (isLogin) {
								// location.replace(process.env.ACTIVITY_HOST + "/errorPage");
							}

						}
						if (res.data.resp_code == 50012) {
							//触发活动已结束，则刷新页面
							// location.reload();
							resolve(res);

						} else {
							resolve(res);
						}
						//判断活动对象 1 是活动对象 2 不是活动对象
						// location.replace(process.env.ACTIVITY_HOST+"/errorPage");
						Toast.clear();
					}
				})
				.catch(error => {
					if (isLogin) {
						// location.replace(process.env.ACTIVITY_HOST + "/errorPage");
					} else {
						// location.replace("https://3.cn/10-0hi80x");
					}
					console.log(error)

					reject(error);
					Toast.clear();

				});
		});
	},
};
