You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.1 KiB

6 months ago
  1. //防抖 延迟执行
  2. export function debounce(fn, delay = 500) {
  3. let timer
  4. return function() {
  5. let args = arguments
  6. if (timer) clearTimeout(timer)
  7. timer = setTimeout(() => {
  8. timer = null
  9. fn.apply(this, args)
  10. }, delay)
  11. }
  12. }
  13. //节流 首次执行
  14. export function throttle(fn, delay = 1000) {
  15. let last = 0
  16. return function() {
  17. let now = new Date()
  18. if (now - last > delay) {
  19. fn.apply(this, arguments)
  20. last = now
  21. }
  22. }
  23. }
  24. //提示
  25. const toast = (msg, mask = true, icon = 'none', duration = 1500) => {
  26. uni.showToast({
  27. title: String(msg),
  28. icon: icon,
  29. mask: mask,
  30. duration: duration,
  31. })
  32. }
  33. //加载
  34. const loading = (msg = '努力加载中', mask = true) => {
  35. uni.showLoading({
  36. title: String(msg),
  37. mask: mask,
  38. })
  39. }
  40. //json参数转url参数
  41. const jsonToUrl = (path, param) => {
  42. let url = path
  43. let arr = []
  44. if (param) arr = Object.keys(param)
  45. if (arr.length && arr.length != 0) {
  46. let str = arr.map(key => `${key}=${decodeURIComponent(param[key])}`).join('&')
  47. url = `${path}${path ? '?' : ''}${str}`
  48. }
  49. return url
  50. }
  51. //url参数转json参数
  52. const urlToJson = (url) => {
  53. let path = url
  54. let param = {}
  55. if (url.indexOf('?') != -1) {
  56. path = url.substr(0, url.indexOf('?'))
  57. let arr = url.split("?")[1].split("&")
  58. arr.forEach(i=>{
  59. if (i.split("=")[1] && i.split("=")[1] != 'undefined') param[i.split("=")[0]] = i.split("=")[1]
  60. })
  61. }
  62. return { path, param }
  63. }
  64. //url获取指定参数
  65. const getQuery = (key) => {
  66. let query = window.location.search.substring(1);
  67. let vars = query.split("&");
  68. for (let i=0;i<vars.length;i++) {
  69. let pair = vars[i].split("=");
  70. if (pair[0] == key) {return pair[1];}
  71. }
  72. return(false)
  73. }
  74. //加法
  75. Number.prototype.add = function(arg) {
  76. return (Math.round(this * 100) + Math.round(arg * 100)) / 100
  77. }
  78. //减法
  79. Number.prototype.sub = function(arg) {
  80. arg = -arg
  81. return (Math.round(this * 100) + Math.round(arg * 100)) / 100
  82. }
  83. //乘法
  84. Number.prototype.mul = function(arg) {
  85. return (Math.round(this * 100) * Math.round(arg * 100)) / 10000
  86. }
  87. //除法
  88. Number.prototype.div = function(arg) {
  89. let d1, d2, n1, n2
  90. n1 = Number(this.toString().replace(".", ""))
  91. n2 = Number(arg.toString().replace(".", ""))
  92. try {d1 = this.toString().split(".")[1].length;} catch (e) {d1 = 0;}
  93. try {d2 = arg.toString().split(".")[1].length;} catch (e) {d2 = 0;}
  94. return toFixed((n1 / n2) * Math.pow(10, d2 - d1), 2)
  95. }
  96. //四舍五入保留小数
  97. function toFixed(arg, n) {
  98. if (n == 0) {
  99. return Math.round(arg)
  100. }
  101. try {
  102. var d, carryD, i,
  103. ds = arg.toString().split('.'),
  104. len = ds[1].length,
  105. b0 = '', k = 0
  106. if (len > n) {
  107. while(k < n && ds[1].substring(0, ++k) == '0') {
  108. b0 += '0'
  109. }
  110. d = Number(ds[1].substring(0, n))
  111. carryD = Math.round(Number('0.' + ds[1].substring(n, len)))
  112. len = (d + carryD).toString().length
  113. if (len > n) {
  114. return Number(ds[0]) + 1
  115. } else if (len == n) {
  116. return Number(ds[0] + '.' + (d + carryD))
  117. }
  118. return Number(ds[0] + '.' + b0 + (d + carryD))
  119. }
  120. } catch (e) {}
  121. return arg
  122. }
  123. //输出
  124. export default {
  125. toast,
  126. loading,
  127. jsonToUrl,
  128. urlToJson,
  129. getQuery,
  130. }