const WEXIN_BASE_URL = 'https://api.mch.weixin.qq.com' // wexin
const WECARD_BASE_URL = 'https://open.wecard.qq.com' // wecard
const CODE_BASE_URL = 'https://chtech.ncjti.edu.cn/shuidian' // code
const AIR_BASE_URL = 'https://chtech.ncjti.edu.cn/air-conditioner-control' // air
const IP_BASE_URL = 'https://pv.sohu.com/cityjson' // ip

const myRequest = (options) => {
	// 域名 或 url 为空，则不执行任何操作
	if (options.host == '' || options.url == '') {
		return;
	}


	let header = options.header;
	if (typeof header == 'undefined' || header == '' || header == {}) {
		header = {
			'token': 'AqwxcdAxs4212pomk231qsxssaz'
		}
	} else {
		header['token'] = 'AqwxcdAxs4212pomk231qsxssaz';
	}
	// options.data['token'] = 'AqwxcdAxs4212pomk231qsxssaz';
	
	// console.log(header);
	// console.log(options.data);

	return new Promise((resolve, reject) => {
		let BASE_URL = '';

		if (options.host == 'wexin') {
			BASE_URL = WEXIN_BASE_URL;
		} else if (options.host == 'wecard') {
			BASE_URL = WECARD_BASE_URL;
		} else if (options.host == 'air') {
			BASE_URL = AIR_BASE_URL;
		} else if (options.host == 'code') {
			BASE_URL = CODE_BASE_URL;
		} else if (options.host == 'ip') {
			BASE_URL = IP_BASE_URL;
		} else {
			uni.showToast({
				title: '域名无效',
				icon: 'success'
			});
			return;
		}

		uni.request({
			url: BASE_URL + options.url,
			method: options.method || 'GET',
			header: header || {},
			data: options.data || {},
			success: (res) => {
				// console.log(res)
				if (res.statusCode == 200) {
					resolve(res)
				} else {
					uni.showToast({
						title: '请求数据失败，状态码：' + res.statusCode,
						icon: 'none'
					})
				}
			},
			fail: (err) => {
				uni.showToast({
					icon: 'none',
					title: err.errMsg,
					duration: 5000
				})
				reject(err)
			}
		})
	}).catch(
		err => {
			console.log("catch " + err);
		}
	)
}

/**
 * 时间计算
 */
const getDate = function getDate(type) {
	const date = new Date();

	let year = date.getFullYear();
	let month = date.getMonth() + 1;
	// let day = date.getDate();

	if (type === 'start_date') {
		year = year - 4;
	} else if (type === 'end_date') {
		year = year;
	} else if (type === 'get_year') {
		let years = []
		years.push(year + '年')
		years.push((year - 1) + '年')
		years.push((year - 2) + '年')
		years.push((year - 3) + '年')
		return years
	} else if (type === 'get_month') {
		return month
	}

	month = month > 9 ? month : '0' + month;


	// day = day > 9 ? day : '0' + day;

	return `${year}-${month}`;
}

/**
 * 时间生成订单号
 */
const getOrderId = function get_order_id(head) {
	const date = new Date()

	let year = date.getFullYear()
	let month = date.getMonth() + 1
	let day = date.getDate()
	let hour = date.getHours()
	let minute = date.getMinutes()
	let second = date.getSeconds()
	let millisecond = date.getMilliseconds()
	let len_mill = millisecond.toString().length

	month = month > 9 ? month : '0' + month
	day = day > 9 ? day : '0' + day
	second = second > 9 ? second : '0' + second
	if (len_mill == 1) {
		millisecond = (Math.floor(Math.random() * 9000) + 10000).toString() + millisecond
	} else if (len_mill == 2) {
		millisecond = (Math.floor(Math.random() * 900) + 1000).toString() + millisecond
	} else if (len_mill == 3) {
		millisecond = (Math.floor(Math.random() * 900) + 100).toString() + millisecond
	}

	return `${head}${year}${month}${day}${hour}${minute}${second}${millisecond}`
}

export {
	CODE_BASE_URL,
	myRequest,
	getDate,
	getOrderId
}
