util.js 1021 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. let _debounceTimeout = null,
  2. _throttleRunning = false
  3. /**
  4. * 防抖
  5. * @param {Function} 执行函数
  6. * @param {Number} delay 延时ms
  7. */
  8. export const debounce = (fn, delay=500) => {
  9. clearTimeout(_debounceTimeout);
  10. _debounceTimeout = setTimeout(() => {
  11. fn();
  12. }, delay);
  13. }
  14. /**
  15. * 节流
  16. * @param {Function} 执行函数
  17. * @param {Number} delay 延时ms
  18. */
  19. export const throttle = (fn, delay=500) => {
  20. if(_throttleRunning){
  21. return;
  22. }
  23. _throttleRunning = true;
  24. fn();
  25. setTimeout(() => {
  26. _throttleRunning = false;
  27. }, delay);
  28. }
  29. /**
  30. * toast
  31. */
  32. export const msg = (title = '', param={}) => {
  33. if(!title) return;
  34. uni.showToast({
  35. title,
  36. duration: param.duration || 1500,
  37. mask: param.mask || false,
  38. icon: param.icon || 'none'
  39. });
  40. }
  41. /**
  42. * 检查登录
  43. * @return {Boolean}
  44. */
  45. export const isLogin = (options={}) => {
  46. const userInfo = uni.getStorageSync('userInfo');
  47. if(userInfo){
  48. return true;
  49. }
  50. if(options.nav !== false){
  51. uni.navigateTo({
  52. url: '/pages/login'
  53. })
  54. }
  55. return false;
  56. }