util.js 1.1 KB

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