utils.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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').replace(/&nbsp;/g,'&ensp;&ensp;')
  149. }
  150. function getNowDate() {
  151. return dayjs().format("YYYYMMDD")
  152. }
  153. /**
  154. * 计算小数
  155. * @param f
  156. * @param digit
  157. * @returns {number}
  158. */
  159. function formatNum(f, digit = 5) {
  160. var m = Math.pow(10, digit);
  161. return parseInt(f * m, 10) / m;
  162. }
  163. function formatSize(fileSize) {
  164. let newSize = fileSize + "b";
  165. if (fileSize > 1024) {
  166. newSize = (fileSize / 1024).toFixed(1);
  167. fileSize = newSize + "kb"
  168. }
  169. if (newSize > 1024) {
  170. newSize = (newSize / 1024).toFixed(1);
  171. fileSize = newSize + "mb"
  172. }
  173. if (newSize > 1024) {
  174. newSize = (newSize / 1024).toFixed(1);
  175. fileSize = "大于1gb"
  176. }
  177. return fileSize
  178. }
  179. /**
  180. * 判断是否邮箱
  181. * @param email
  182. * @returns {boolean}
  183. */
  184. function isEmail(email) {
  185. if (!email.trim()) {
  186. return false;
  187. }
  188. const reg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
  189. if (!reg.test(email)) {
  190. return false
  191. }
  192. return true
  193. }
  194. /**
  195. * 扩展获取字典项方法,使用storage 缓存失效来实现在一定时间内不重复加载字典
  196. */
  197. async function getDict(dictType){
  198. // let data = storage.getItem(dictType);
  199. // if (data){
  200. // return data;
  201. // }else{
  202. // const dictData = await Api.getDict(dictType);
  203. // storage.setItem(dictType,dictData.data,DICT_CACHE_TIME)
  204. // return dictData.data;
  205. // }
  206. }
  207. export {
  208. throttle,
  209. getDataSet,
  210. getEventParam,
  211. toast,
  212. showLoading,
  213. isMobile,
  214. getUuid,
  215. isNumber,
  216. formatTime,
  217. formatYMD,
  218. formatYMD2,
  219. handleRichTextImgAuto,
  220. getNowDate,
  221. isIdCard
  222. }