utils.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import dayjs from "dayjs";
  2. /**
  3. * 节流函数
  4. * @param {Function} callback 需要被节流的函数
  5. * @param {Number} duration 距离上次执行超过多少毫秒才会执行被节流的函数
  6. * @returns
  7. */
  8. function throttle(callback, duration = 500) {
  9. // 最后执行函数时的时间戳
  10. let lastTime = 0
  11. return function () {
  12. // 获取当前时间戳
  13. const now = new Date().getTime()
  14. console.log(`上次时间:${lastTime} 这次时间${now} 时间差${now - lastTime}`)
  15. // 判断当前时间距离上一次执行函数的时间是否超过了duration设定的毫秒数
  16. if (now - lastTime >= duration) { // 超过了
  17. // 因为我们需要在 page 中做 this.setData(),所以需要借助 call()
  18. // 利用 call()方法,实现保留原函数的 this 指向,利用JavaScript的arguments对象实现动态接收参数
  19. callback.call(this, ...arguments);
  20. // callback(...arguments)
  21. // 更新最后执行函数时的时间戳
  22. lastTime = now;
  23. console.log("执行throttle")
  24. }
  25. // 没超过,啥也不干
  26. }
  27. }
  28. /**
  29. * 获取事件回调参数的自定义属性
  30. * @param {Object} event
  31. * @param {String} target
  32. */
  33. function getDataSet(event, target) {
  34. if (target) {
  35. return event.currentTarget.dataset[target]
  36. } else {
  37. return event.currentTarget.dataset
  38. }
  39. }
  40. /**
  41. * 获取自定义组件事件参数
  42. * @param {Object} event
  43. * @param {String} target
  44. * @returns {*}
  45. */
  46. function getEventParam(event, target) {
  47. if (target) {
  48. return event.detail[target]
  49. } else {
  50. return event.detail
  51. }
  52. }
  53. /**
  54. * 吐司
  55. * @param msg
  56. */
  57. function toast(msg, icon = 'none') {
  58. wx.showToast({
  59. title: msg,
  60. icon
  61. });
  62. }
  63. /**
  64. * 显示加载框
  65. * @param msg
  66. */
  67. function showLoading(msg) {
  68. if (msg == undefined) {
  69. msg = '加载中...'
  70. }
  71. wx.showLoading({
  72. title: msg,
  73. mask: true
  74. });
  75. }
  76. /**
  77. * 判断是否手机
  78. * @param phone
  79. * @returns {boolean}
  80. */
  81. function isMobile(phone) {
  82. if (!phone.trim()) {
  83. return false;
  84. }
  85. const reg = /^1[0-9]{10}$/;
  86. if (!reg.test(phone)) {
  87. return false
  88. }
  89. return true
  90. }
  91. /**
  92. * 随机生成uuid - 引入时间戳 (36位字符串)
  93. * @returns {string}
  94. */
  95. function getUuid() {
  96. let d = new Date().getTime();
  97. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  98. const r = (d + Math.random() * 16) % 16 | 0;
  99. d = Math.floor(d / 16);
  100. return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
  101. });
  102. }
  103. /**
  104. * 是否数字
  105. * @param value
  106. * @returns {boolean}
  107. */
  108. function isNumber(value) {
  109. return typeof value === 'number' && !isNaN(value);
  110. }
  111. /**
  112. * 格式化标准时间 YYYY-MM-DD HH:mm:ss
  113. * @param str
  114. * @returns {string}
  115. */
  116. function formatTime(str) {
  117. return dayjs(str).format("YYYY-MM-DD HH:mm:ss")
  118. }
  119. /**
  120. * 格式化日月年 YYYY-MM-DD YYYY年MM月DD日
  121. * @param str
  122. * @returns {string}
  123. */
  124. function formatYMD(str) {
  125. return dayjs(str).format("YYYY-MM-DD")
  126. }
  127. function formatYMD2(str) {
  128. return dayjs(str).format("YYYY年MM月DD日")
  129. }
  130. /**
  131. * 处理小程序richtext 显示图片自适应问题
  132. * @param content
  133. * @returns {*}
  134. */
  135. function handleRichTextImgAuto(content) {
  136. return content.replace(/\<img/gi, '<img style="max-width:100%;height:auto')
  137. }
  138. export {
  139. throttle,
  140. getDataSet,
  141. getEventParam,
  142. toast,
  143. showLoading,
  144. isMobile,
  145. getUuid,
  146. isNumber,
  147. formatTime,
  148. formatYMD,
  149. formatYMD2,
  150. handleRichTextImgAuto,
  151. }