/* eslint-disable */
import Request from './request';
// import {
// 	refreshToken
// } from '@/api/login';
import indexConfig from '@/config/global.config.js';
import mUtil from '@/utils/index.js';
import store from '@/store';

const yghttp = new Request();

const debug_token =
	"eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ5Y184NjAzNDg2NyIsInVzZXJfaWQiOjEyLCJtb2JpbGUiOiIxMzQzNzEwNDgxMiIsIm5pY2tuYW1lIjoieWNfODYwMzQ4NjciLCJleHAiOjE3NDE0MDM3MDcsImlhdCI6MTcwOTg2NzcwN30.LwHRcGuKyUV9S4o_cW0QhqO9KYY6L-AW6d1nMzDLCCjDLyEsdHj1Us2EbVqEJgx1Wut-hjIDqZw0iwwszsW_Kw";
// request全局参数设置
yghttp.setConfig(config => {
	/* 设置全局配置 */
	config.baseUrl = indexConfig.baseUrl; /* 根域名不同 */
	const systemInfo = uni.getSystemInfoSync();
	const systemInfoHeaders = {
		'device-name': systemInfo.brand, // 设备名称
		width: systemInfo.screenWidth, // 屏幕宽度
		height: systemInfo.screenHeight, // 屏幕高度
		os: systemInfo.platform, // 客户端平台
		'os-version': systemInfo.system // 操作系统版本
	};
	config.header = {
		...config.header,
		...systemInfoHeaders
	};
	return config;
});

let isRefreshing = false;
let requests = [];

let goLogin = false;

const onLogin = (msg) => {
	if (goLogin) return;
	goLogin = true
	const pages = getCurrentPages();
	const route = pages[pages.length - 1].route;
	if (!route || route !== 'pages/login/index') {
		try {
			mUtil.toast(msg);
			uni.removeStorageSync('apiToken');
			setTimeout(() => {
				uni.navigateTo({
					url: '/pages/login/index',
					complete: () => {
						goLogin = false;
					}
				})
			}, 500)
		} catch (e) {
			//TODO handle the exception
			goLogin = false;
		}
	} else {
		goLogin = false;
	};
}


yghttp.interceptor.request(
	config => {
		if (config.url === '/activity/lottery') {
			// 设置点击抽奖的接口时间
			config.timeout = 5000
		}
		if (config.isToken == false) {

		} else {
			config.header['apiToken'] = debug_token || uni.getStorageSync('apiToken');
		}
		/* 请求之前拦截器 */

		// config.header['client'] = 'applet';
		config.header['client'] = 'app';
		// 单商户
		// config.header['merchant-id'] = uni.getStorageSync('merchantId') || 1;
		return config;
	},
	error => {
		return Promise.reject(error);
	}
);

// 刷新refreshToken
async function handleRefreshToken(refresh_token) {
	const params = {};
	params.group = mUtil.platformGroupFilter();
	params.refresh_token = refresh_token;
	await http.post(refreshToken, params).then(async r => {
		store.commit('login', r.data);
		isRefreshing = false;
	});
}
yghttp.interceptor.response(
	async response => {
			if (response.header['refresh_token']) {
				store.commit('login', response.header.refresh_token);
				uni.setStorageSync('apiToken', response.header.refresh_token);
			}
			/* 请求之后拦截器 */
			switch (response.data.code) {
				case 200:
					return response.data;
				case 400:
					mUtil.toast('错误的请求');
					return Promise.reject(response.data.message);
					break;
				case 500:
					mUtil.toast(response.data.msg);
					return Promise.reject(response.data.msg);
					break;
				case 5001:
					mUtil.toast(response.data.msg);
					return response.data;
				case 100001:
					onLogin(response.data.msg)
					return Promise.reject();
					break;
				case 100004:
					onLogin(response.data.msg)
					return Promise.reject();
					break;
				case 100007:
					mUtil.toast(response.data.msg);
					setTimeout(() => {
						let pages = getCurrentPages();
						let currentPage = pages[pages.length - 1];
						let url = currentPage.route; // 当前页面路径
						if (url != 'pages/government/realAttestation') {
							uni.navigateTo({
								url: '/pages/government/realAttestation'
							})
						}
					}, 500)
					return Promise.reject();
					break;
				default:
					mUtil.toast(response.data.msg);
					return Promise.reject(response.data.msg);
			}
		},
		error => {
			return Promise.reject(error);
		}
);

export {
	yghttp
};