utils.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. * 判断是否是身份证号
  93. * @param idCard
  94. * @returns {boolean}
  95. */
  96. function isIdCard(idCard){
  97. if (!idCard) {
  98. return false;
  99. }
  100. let regIDCard = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
  101. return regIDCard.test(idCard);
  102. }
  103. /**
  104. * 随机生成uuid - 引入时间戳 (36位字符串)
  105. * @returns {string}
  106. */
  107. function getUuid() {
  108. let d = new Date().getTime();
  109. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  110. const r = (d + Math.random() * 16) % 16 | 0;
  111. d = Math.floor(d / 16);
  112. return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
  113. });
  114. }
  115. /**
  116. * 是否数字
  117. * @param value
  118. * @returns {boolean}
  119. */
  120. function isNumber(value) {
  121. return typeof value === 'number' && !isNaN(value);
  122. }
  123. /**
  124. * 格式化标准时间 YYYY-MM-DD HH:mm:ss
  125. * @param str
  126. * @returns {string}
  127. */
  128. function formatTime(str) {
  129. return dayjs(str).format("YYYY-MM-DD HH:mm:ss")
  130. }
  131. /**
  132. * 格式化日月年 YYYY-MM-DD YYYY年MM月DD日
  133. * @param str
  134. * @returns {string}
  135. */
  136. function formatYMD(str) {
  137. return dayjs(str).format("YYYY-MM-DD")
  138. }
  139. function formatYMD2(str) {
  140. return dayjs(str).format("YYYY年MM月DD日")
  141. }
  142. /**
  143. * 处理小程序richtext 显示图片自适应问题
  144. * @param content
  145. * @returns {*}
  146. */
  147. function handleRichTextImgAuto(content) {
  148. return content.replace(/\<img/gi, '<img style="max-width:100%;height:auto')
  149. }
  150. function getNowDate() {
  151. return dayjs().format("YYYYMMDD")
  152. }
  153. export {
  154. throttle,
  155. getDataSet,
  156. getEventParam,
  157. toast,
  158. showLoading,
  159. isMobile,
  160. getUuid,
  161. isNumber,
  162. formatTime,
  163. formatYMD,
  164. formatYMD2,
  165. handleRichTextImgAuto,
  166. getNowDate,
  167. isIdCard
  168. }