const formatTime = date => {
	const year = date.getFullYear()
	const month = date.getMonth() + 1
	const day = date.getDate()
	const hour = date.getHours()
	const minute = date.getMinutes()
	const second = date.getSeconds()

	return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
	n = n.toString()
	return n[1] ? n : '0' + n
}

// 验证是否为整数
const e_checkNumber = strnumber => {
	return /^[1-9]\d*$|^0$/.test(strnumber);
}

// 验证手机号码
const e_checkPhone = strtel => {
	var strIsMobile = /^(?:13\d|14\d|15\d|16\d|17\d|18\d|19\d)\d{5}(\d{3}|\*{3})$/; //手机号码验证规则
	var strIsPhone = /^((0\d{2,3})(-)?)?(\d{7,8})(-(\d{3,}))?$/; //座机验证规则
	var strPhone = strtel; //获得用户填写的号码值 赋值给变量dianhua
	// if (!strIsMobile.test(strPhone) && !strIsPhone.test(strPhone)) { //如果用户输入的值不同时满足手机号和座机号的正则
	// 	return false;
	// }
	if (!strIsMobile.test(strPhone)) { //如果用户输入的值不满足手机号的正则
		return false;
	}
	return true;
}

// 验证邮箱
const e_checkEMail = stremail => {
	return /^\w+([-.]\w+)*@([\w-]){1,}(\.([\w]){1,}){1,}$/.test(stremail);
}

// 验证身份证
const e_checkIDCard = strIDCard => {
	if (15 == strIDCard.length)
		return (/^([0-9]){15,15}$/.test(strIDCard));
	if (18 == strIDCard.length)
		return (/^([0-9]){17,17}([0-9xX]){1,1}$/.test(strIDCard));
	return false;
}

/**
 * 获取密码强度等级
 * 0-密码过短(没有达到最小长度)
 * 1-密码安全性差
 * 2-密码安全性良好
 * 3-密码安全性强
 * @param {string} strPwd 
 * @param {number} minLength 
 */
const e_getPwdLevel = (strPwd, minLength) => {
	minLength = minLength || 6;
	if (strPwd.length < minLength) {
		return 0;
	}
	let level = 0;
	if (strPwd.match(/[a-z]/ig)) {
		level++;
	}
	if (strPwd.match(/[0-9]/ig)) {
		level++;
	}
	if (strPwd.match(/(.[^a-z0-9])/ig)) {
		level++;
	}
	if (strPwd.length < 6 && ls > 0) {
		level--;
	}
	return level;
}

// 是否登录验证
const loginVerification = fun => {
	let objUserInfo = uni.getStorageSync('userInfo');
	if (objUserInfo.openID) {
		typeof fun === 'function' && fun();
	} else {
		uni.showToast({
			title: '请先授权登录',
			icon: 'none'
		})
	}
}

const msg = (title, fun, duration = 2000, mask = true, icon = 'none') => {
	//统一提示方便全局修改
	if (Boolean(title) === false) {
		typeof fun === 'function' && fun();
		return;
	}
	uni.showToast({
		title: title,
		duration: duration,
		mask: mask,
		icon: icon,
		success() {
			setTimeout(function() {
				// 执行成功-回调方法
				typeof fun === 'function' && fun();
			}, duration)
		}
	})
}

/**
 * 生成唯一标识字符
 */
const Guid = () => {
    function Rfour() {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    }
    return (Rfour() + Rfour() + "-" + Rfour() + "-" + Rfour() + "-" + Rfour() + "-" + Rfour() + Rfour() + Rfour());
}

module.exports = {
	formatTime: formatTime,
	e_checkNumber: e_checkNumber,
	e_checkPhone: e_checkPhone,
	e_checkEMail: e_checkEMail,
	e_checkIDCard: e_checkIDCard,
	e_getPwdLevel: e_getPwdLevel,
	loginVerification: loginVerification,
	msg: msg,
	Guid: Guid
}