// 防抖 只有在用户停止触发事件一段时间后才执行事件处理函数 export function debounce(fn, delay) { let timer = null; return function (...args) { if (timer) clearTimeout(timer); timer = setTimeout(() => { fn.apply(this, args); }, delay); }; } // 节流 确保在一段时间内只执行一次事件处理函数 export function throttle(fn, delay) { let lastTime = 0; // 记录上一次执行的时间 return function (...args) { const now = Date.now(); if (now - lastTime >= delay) { lastTime = now; fn.apply(this, args); } }; } /** * 验证手机号格式 以13/15/17/18/19开头 * @param {Number} phone 手机号码 */ export function isValidPhone(phone) { const regex = /^1[35789]\d{9}$/; return regex.test(phone.trim()); } /** * 验证密码格式:6-16位数字+字母组合,需包含至少1位字母 * @param {String} password 密码 */ export function isValidPassword(password) { const pwd = password.trim(); // 检查长度 if (pwd.length < 6 || pwd.length > 16) { uni.$u.toast('密码长度应大于6小于16'); return false; } // 检查是否只包含字母和数字 if (!/^[a-zA-Z0-9]+$/.test(pwd)) { uni.$u.toast('密码只能使用字母和数字,不能使用其他字符'); return false; } // 检查是否包含至少1个字母 if (!/[a-zA-Z]/.test(pwd)) { uni.$u.toast('密码中至少包含1个字母'); return false; } return true; } /** * 手机号脱敏 * @param {*} phone 手机号 * @returns */ export function phonePrivacy(phone) { if (phone) { return String(phone).replace(/(\d{3})\d{4}(\d{4})/, '$1****$2'); } else { return ''; } } /** * 安全区距离 * @returns 顶部和底部安全距离 */ export function getSafeArea() { const info = uni.getSystemInfoSync() // 顶部安全距离(状态栏高度) const top = info.statusBarHeight || 0 // 底部安全距离(iOS 刘海屏有值,安卓一般为 0) const bottom = info.safeArea ? info.screenHeight - info.safeArea.bottom : 0 return { top, bottom } }