
function ViewImage(imgList, e) {
	uni.previewImage({
		urls: imgList,
		current: e.currentTarget.dataset.url
	});
}

function formatTime(time) {
	if (typeof time !== 'number' || time < 0) {
		return time
	}

	var hour = parseInt(time / 3600)
	time = time % 3600
	var minute = parseInt(time / 60)
	time = time % 60
	var second = time

	return ([hour, minute, second]).map(function(n) {
		n = n.toString()
		return n[1] ? n : '0' + n
	}).join(':')
}
// 判断是否为手机号
function isPhoneNumber(pone) {
	var myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
	if (!myreg.test(pone)) {
		return false;
	} else {
		return true;
	}
}
//密码判断
function isPassword(p) {
	var myreg = /^[a-zA-Z0-9]{6,12}$/;
	if (!myreg.test(p)) {
		return false;
	} else {
		return true;
	}
}
//判断验证码
function isCaptcha(p) {
	var myreg = /^[0-9A-Za-z]{4,12}$/;
	if (!myreg.test(p)) {
		return false;
	} else {
		return true;
	}
}



//判断用户名
function isUserName(p) {
	var myreg = /^[a-zA-Z0-9]{1,20}$/;
	if (!myreg.test(p)) {
		return false;
	} else {
		return true;
	}
}

//判断身份证
function isIdCard(value) {
	let myReg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
	if (!myReg.test(value)) {
		return false;
	} else {
		return true;
	}
}


const isEmpty = function(a) {
	return typeof(a) == 'undefined' || a == null || a == '' || a == 'undefined';
}



function formatLocation(longitude, latitude) {
	if (typeof longitude === 'string' && typeof latitude === 'string') {
		longitude = parseFloat(longitude)
		latitude = parseFloat(latitude)
	}

	longitude = longitude.toFixed(2)
	latitude = latitude.toFixed(2)

	return {
		longitude: longitude.toString().split('.'),
		latitude: latitude.toString().split('.')
	}
}

var dateUtils = {
	UNITS: {
		'年': 31557600000,
		'月': 2629800000,
		'天': 86400000,
		'小时': 3600000,
		'分钟': 60000,
		'秒': 1000
	},
	humanize: function(milliseconds, date) {
		var humanize = '';
		var _format = function(number) {
			return (number < 10 ? ('0' + number) : number);
		};
		for (var key in this.UNITS) {
			if (milliseconds >= this.UNITS[key]) {
				switch (key) {
					case '小时':
						humanize = _format(date.getHours()) + ':' + _format(date.getMinutes());
						break;
					default:
						humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前';
						break;
				}
				break;
			}
		}
		return humanize || '刚刚';
	},
	format: function(dateStr) {
		var date = this.parse(dateStr)
		var diff = Date.now() - date.getTime();
		if (diff < this.UNITS['天']) {
			return this.humanize(diff, date);
		}
		var _format = function(number) {
			return (number < 10 ? ('0' + number) : number);
		};
		return date.getFullYear() + '-' + _format(date.getMonth() + 1) + '-' + _format(date.getDate()) + '-' +
			_format(date.getHours()) + ':' + _format(date.getMinutes());
	},
	formatHms: function(dateStr) {
		var date = this.parse(dateStr)
		var _format = function(number) {
			return (number < 10 ? ('0' + number) : number);
		};
		return _format(date.getHours()) + ':' + _format(date.getMinutes());
	},
	formatYMD: function(dateStr) {
		var date = this.parse(dateStr)
		var _format = function(number) {
			return (number < 10 ? ('0' + number) : number);
		};
		return date.getFullYear() + '-' + _format(date.getMonth() + 1) + '-' + _format(date.getDate());
	},
	parse: function(str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串，转化为一个Date对象
		if (!str) {
			return new Date();
		}
		var a = str.split(/[^0-9]/);
		return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
	},
	/**************************************时间格式化处理************************************/
	dateFtt(fmt, date) { //author: meizz   
		var o = {
			"M+": date.getMonth() + 1, //月份   
			"d+": date.getDate(), //日   
			"H+": date.getHours(), //小时   
			"m+": date.getMinutes(), //分   
			"s+": date.getSeconds(), //秒   
			"q+": Math.floor((date.getMonth() + 3) / 3), //季度   
			"S": date.getMilliseconds() //毫秒   
		};
		if (/(y+)/.test(fmt))
			fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
		for (var k in o)
			if (new RegExp("(" + k + ")").test(fmt))
				fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
		return fmt;
	},

};

module.exports = {
	formatTime: formatTime,
	isPhoneNumber: isPhoneNumber,
	isUserName: isUserName,
	isPassword: isPassword,
	isCaptcha: isCaptcha,
	formatLocation: formatLocation,
	dateUtils: dateUtils,
	isEmpty: isEmpty,
	isIdCard: isIdCard,
	ViewImage: ViewImage,
}
