uniapp常用工具函数

import store from '@/store/index'
export const phoneInfo = () => {
	// 微信小程序胶囊按钮
	const menuButtonInfo = uni.getMenuButtonBoundingClientRect();
	// 系统信息
	const mobileInfo = uni.getSystemInfoSync();
	// 状态栏高度
	const statusBarHeight = mobileInfo.statusBarHeight
	// 微信小程序胶囊按钮与状态栏间距
	const menuButtonMaginTopBottom = menuButtonInfo.top - mobileInfo.statusBarHeight;
	// 导航栏高度
	const menuButtonContainerHeight = menuButtonInfo.height + menuButtonMaginTopBottom * 2;
	// 自定义导航栏高度(状态栏高度 + 原生导航栏高度)
	const customNavBarHeight = statusBarHeight + menuButtonContainerHeight
	//胶囊宽度加上左右边距的新宽度【即自定义导航栏时,左边元素的margin-right的值】
	menuButtonInfo.extendWidth = menuButtonInfo.width + 2 * (mobileInfo.windowWidth - menuButtonInfo.right);
	//胶囊的右边距。用于设置返回的左边距。对称
	menuButtonInfo.marginRight = mobileInfo.windowWidth - menuButtonInfo.right;
	// 存储信息
	store.commit('SET_PHONEINFO', {
		// 导航栏高度
		navigationBarHeight: menuButtonContainerHeight,
		// 状态栏高度
		statusBarHeight,
		// 自定义导航栏高度(状态栏高度 + 原生导航栏高度)
		customNavBarHeight,
		// 微信小程序胶囊按钮信息 {width,height,top,left,right,bottom;及extendWidth, marginRight}
		menuButtonInfo
	})
}
export const throttle = (fn, wait) => {
	var prev = Date.now();
	return function() {
		var context = this;
		var args = arguments;
		var now = Date.now();
		if (now - prev > wait) {
			fn.apply(context, args)
			prev = Date.now()
		}
	}
}
export const debounce = (func, wait) => {
	let timeout;
	return function() {
		let context = this;
		let args = arguments;
		let later = () => {
			timeout = null;
			func.apply(context, args);
		};
		clearTimeout(timeout);
		timeout = setTimeout(later, wait);
	}
}

export function inspectPhone(phone) {
	const mPattern = /^1[23456789]\d{9}$/;
	return mPattern.test(phone)
}

export function formatDate(number, format) {
	let time = new Date(number)
	let newArr = []
	let formatArr = ['Y', 'M', 'D', 'h', 'm', 's']
	newArr.push(time.getFullYear())
	newArr.push(formatNumber(time.getMonth() + 1))
	newArr.push(formatNumber(time.getDate()))

	newArr.push(formatNumber(time.getHours()))
	newArr.push(formatNumber(time.getMinutes()))
	newArr.push(formatNumber(time.getSeconds()))

	for (let i in newArr) {
		format = format.replace(formatArr[i], newArr[i])
	}
	return format;
}

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

export function getWeek(day) {
	var today = new Date();
	var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
	today.setTime(targetday_milliseconds);
	var tYear = today.getFullYear();
	var tMonth = today.getMonth();
	var tDate = today.getDate();
	tMonth = tMonth + 1;
	tDate = tDate;
	var daytime = `${tYear}-${tMonth >= 10 ? tMonth : '0' + tMonth}-${tDate >= 10 ? tDate : '0' + tDate}`; //今日时间
	if (day == '-0') {
		return {
			top: '今天',
			day: tDate,
			daytime: daytime
		};
	} else {
		return {
			top: getWeekqq(daytime),
			day: tDate,
			daytime: daytime
		};
	}
}

function getWeekqq(dateString) {
	var weekDay = ["S", "M", "T", "W", "T", "F", "S"];
	var myDate = new Date(Date.parse(dateString));
	return weekDay[myDate.getDay()]
}

你可能感兴趣的:(javascript,前端,开发语言)