var common = require('./common.js');

var storage = require('./storage.js');

var config = require('./config.js'); // 检查token是否失效 如失效 清除缓存 提示重新登录
// 402: 解析token出错  403: token格式错误  404: 公共声明中缺少必要字段  405: token验证失败

function _checkTokenValid(res) {
	if (
		res.data.code === 402 ||
		res.data.code === 403 ||
		res.data.code === 404 ||
		res.data.code === 405 ||
		res.data.code === 300 ||
		res.data.code === 302 ||
		res.data.code === 303 ||
		res.data.code === 304 ||
		res.data.code === 305 ||
		res.data.code === 306
	) {
		//wx.clearStorageSync();
		var shareCode = storage.getShareCode();
		storage.clearStorage();
		storage.setShareCode(shareCode);
		uni.reLaunch({
			url: '/pages/index/index'
		}); //wx.switchTab({url:'/pages/index/index'})
		// wx.navigateTo({
		//   url: '/pages/login/login',
		// })
		// wx.showModal({
		//   title: '错误',
		//   content: '鉴权认证过期，请重新登录',
		//   showCancel: false
		// })
		return false;
	}

	return true;
}

function getRequest(url, data, successCallBack, failCallBack) {
	if (!failCallBack) failCallBack = function() {};

	if (url.indexOf('?') > 0) {
		url = url + '&' + common.obj2UrlQuery(data);
	} else {
		url = '?' + common.obj2UrlQuery(data);
	}
	uni.request({
		url: url,
		method: 'GET',
		header: {
			'User-Agent':'Ali'
		},
		success: function(res) {
			uni.hideLoading();

			if (_checkTokenValid(res)) {
				successCallBack(res);
			}
		},
		fail: function(res) {
			uni.hideLoading();

			if (!(res.errMsg.indexOf('interrupted') != -1)) {
				uni.showModal({
					title: '错误',
					content: '网络错误' + res.errMsg,
					showCancel: false
				});
			}

			if (failCallBack) failCallBack(res);
		}
	});
}

function postRequest(url, data, successCallBack, failCallBack) {
	if (!failCallBack) failCallBack = function() {};
	uni.request({
		url: url,
		data: data,
		header: {
			'content-type': 'application/x-www-form-urlencoded',
			'User-Agent':'Ali'
		},
		method: 'POST',
		success: function(res) {
			//wx.hideLoading();
			if (_checkTokenValid(res)) {
				successCallBack(res);
			}
		},
		fail: function(res) {
			uni.hideLoading();

			if (!(res.errMsg.indexOf('interrupted') != -1)) {
				uni.showModal({
					title: '错误',
					content: '网络错误' + res.errMsg,
					showCancel: false
				});
			}

			failCallBack(res);
		}
	});
}

function getApi(url, data, successCallBack, failCallBack) {
	var token = storage.getUserToken(); //wx.getStorageSync(config.STORAGE_USER_TOKEN)
	data.token = token;
	data.v = config.APP_VERSION;
	//#ifdef MP-ALIPAY
	data.from = 'ali'
	//#endif
	getRequest(url, data, successCallBack, failCallBack);
}

function postApi(url, data, successCallBack, failCallBack) {
	var token = storage.getUserToken(); // wx.getStorageSync(config.STORAGE_USER_TOKEN)
	data.token = token;
	data.v = config.APP_VERSION;
	//#ifdef MP-ALIPAY
	data.from = 'ali'
	//#endif
	postRequest(url, data, successCallBack, failCallBack);
}
/**
 * 上报formid
 */

function reportFormId(formId) {
	const accountInfo = uni.getAccountInfoSync();
	var postData = {
		formId: formId,
		appid: accountInfo.miniProgram.appId
	};
	postApi(config.API_FORMID_REPORT, postData, function() {});
}

function getAppConfig() {
	//const accountInfo = uni.getAccountInfoSync();
	var appId =  uni.getAccountInfoSync().miniProgram.appId;
	const pData = {
		appid: appId,
		terminal: 'wx_app'
	};
	getApi(config.API_INDEX_APP_CONFIG, pData, function(resp) {
		if (resp.data.code === 200) {
			const appConfig = resp.data.data.appConfig;
			storage.setAppConfig(appConfig);
			uni.setNavigationBarTitle({
				title: appConfig.app_name
			});
		}
	});
}

module.exports = {
	getRequest: getRequest,
	postRequest: postRequest,
	getApi: getApi,
	postApi: postApi,
	reportFormId: reportFormId,
	getAppConfig: getAppConfig
};
