index.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // import Vue from 'vue'
  2. import mitt from 'mitt'
  3. /**
  4. * @description: 日期格式化
  5. * @param {*} timestamp 时间戳
  6. * @param {*} type 日期格式化类型
  7. * @param {*} hideYear 是否显示年 为true时 本年不显示 其他年份显示
  8. * @return {*}
  9. */
  10. export function dateFormatter (timestamp, type, hideYear = false) {
  11. const date = new Date(timestamp)
  12. const year = date.getFullYear()
  13. const month = date.getMonth() + 1 // 月份从 0 开始,需要加 1
  14. const month_0 = (`0${date.getMonth() + 1}`).slice(-2) // 月份从 0 开始,需要加 1,并补零
  15. const day = date.getDate()
  16. const day_0 = (`0${date.getDate()}`).slice(-2) // 补零
  17. const hours = (`0${date.getHours()}`).slice(-2) // 补零
  18. const minutes = (`0${date.getMinutes()}`).slice(-2) // 补零
  19. const seconds = (`0${date.getSeconds()}`).slice(-2) // 补零
  20. const currentYear = new Date().getFullYear()
  21. const currentMonth = new Date().getMonth() + 1
  22. const currentDay = new Date().getDate()
  23. // 当天 不显示 年月日
  24. const isCurrentDay = year === currentYear && month === currentMonth && day === currentDay
  25. // 默认格式 2023-11-04 13:23:02
  26. return `${isCurrentDay ? '' : (`${year}-${month_0}-${day_0} `)}${hours}:${minutes}:${seconds}`
  27. }
  28. /**
  29. * @description: 节流
  30. * @param {*} fun
  31. * @param {*} delay
  32. * @return {*}
  33. */
  34. export function _throttle (fn, delay = 1000) {
  35. let lastTime, timer
  36. return function () {
  37. const _this = this
  38. const args = arguments
  39. const nowTime = Date.now()
  40. if (lastTime && nowTime - lastTime < delay) {
  41. if (timer) clearTimeout(timer)
  42. timer = setTimeout(() => {
  43. lastTime = nowTime
  44. fn.apply(_this, args)
  45. }, delay)
  46. } else {
  47. lastTime = nowTime
  48. fn.apply(_this, args)
  49. }
  50. }
  51. }