index.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // 防抖 只有在用户停止触发事件一段时间后才执行事件处理函数
  2. export function debounce(fn, delay) {
  3. let timer = null;
  4. return function (...args) {
  5. if (timer) clearTimeout(timer);
  6. timer = setTimeout(() => {
  7. fn.apply(this, args);
  8. }, delay);
  9. };
  10. }
  11. // 节流 确保在一段时间内只执行一次事件处理函数
  12. export function throttle(fn, delay) {
  13. let lastTime = 0; // 记录上一次执行的时间
  14. return function (...args) {
  15. const now = Date.now();
  16. if (now - lastTime >= delay) {
  17. lastTime = now;
  18. fn.apply(this, args);
  19. }
  20. };
  21. }
  22. /**
  23. * 验证手机号格式 以13/15/17/18/19开头
  24. * @param {Number} phone 手机号码
  25. */
  26. export function isValidPhone(phone) {
  27. const regex = /^1[35789]\d{9}$/;
  28. return regex.test(phone.trim());
  29. }
  30. /**
  31. * 验证密码格式:6-16位数字+字母组合,需包含至少1位字母
  32. * @param {String} password 密码
  33. */
  34. export function isValidPassword(password) {
  35. const pwd = password.trim();
  36. // 检查长度
  37. if (pwd.length < 6 || pwd.length > 16) {
  38. uni.$u.toast('密码长度应大于6小于16');
  39. return false;
  40. }
  41. // 检查是否只包含字母和数字
  42. if (!/^[a-zA-Z0-9]+$/.test(pwd)) {
  43. uni.$u.toast('密码只能使用字母和数字,不能使用其他字符');
  44. return false;
  45. }
  46. // 检查是否包含至少1个字母
  47. if (!/[a-zA-Z]/.test(pwd)) {
  48. uni.$u.toast('密码中至少包含1个字母');
  49. return false;
  50. }
  51. return true;
  52. }
  53. /**
  54. * 手机号脱敏
  55. * @param {*} phone 手机号
  56. * @returns
  57. */
  58. export function phonePrivacy(phone) {
  59. if (phone) {
  60. return String(phone).replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
  61. } else {
  62. return '';
  63. }
  64. }
  65. /**
  66. * 安全区距离
  67. * @returns 顶部和底部安全距离
  68. */
  69. export function getSafeArea() {
  70. const info = uni.getSystemInfoSync()
  71. // 顶部安全距离(状态栏高度)
  72. const top = info.statusBarHeight || 0
  73. // 底部安全距离(iOS 刘海屏有值,安卓一般为 0)
  74. const bottom = info.safeArea
  75. ? info.screenHeight - info.safeArea.bottom
  76. : 0
  77. return { top, bottom }
  78. }