util.js 1.26 KB
// 手机正则
const REGEXPS = {
	"mobile": /^1\d{10}$/
}
// 验证手机
function checkMobile(str) {
	return REGEXPS.mobile.test(str);
}


function formatTime(date) {
	var year = date.getFullYear()
	var month = date.getMonth() + 1
	var day = date.getDate()

	var hour = date.getHours()
	var minute = date.getMinutes()
	var second = date.getSeconds()


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

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




/**
 * 获取屏幕剩余高度
 * useHeight 单位是rpx
 * 默认返回单位是rpx  可通过unit参数改为 px
 */
function getLastScreenHeight(useHeight = 0, unit = 'rpx') {
	let sysInfo = wx.getSystemInfoSync();
	let clientHeight = sysInfo.windowHeight;
	// 获取可使用窗口高度
	let clientWidth = sysInfo.windowWidth;
	// 算出比例
	let ratio = 750 / clientWidth;
	// 算出屏幕高度(单位rpx)
	let height = clientHeight * ratio;
	// 计算剩余高度
	let lastHeight = height - useHeight;
	// 可转换成px
	if (unit == 'px') {
		lastHeight = lastHeight / 750 * clientWidth
	}
	return lastHeight;
}

module.exports = {
	formatTime: formatTime,
	checkMobile: checkMobile,
	getLastScreenHeight: getLastScreenHeight
}