index.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. }