common.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * 显示消息提示框
  3. * @param content 提示的标题
  4. */
  5. export function toast(content) {
  6. uni.showToast({
  7. icon: 'none',
  8. title: content
  9. })
  10. }
  11. /**
  12. * 显示模态弹窗
  13. * @param content 提示的标题
  14. */
  15. export function showConfirm(content) {
  16. return new Promise((resolve, reject) => {
  17. uni.showModal({
  18. title: '提示',
  19. content: content,
  20. cancelText: '取消',
  21. confirmText: '确定',
  22. success: function(res) {
  23. resolve(res)
  24. }
  25. })
  26. })
  27. }
  28. /**
  29. * 参数处理
  30. * @param params 参数
  31. */
  32. export function tansParams(params) {
  33. let result = ''
  34. for (const propName of Object.keys(params)) {
  35. const value = params[propName]
  36. var part = encodeURIComponent(propName) + "="
  37. if (value !== null && value !== "" && typeof(value) !== "undefined") {
  38. if (typeof value === 'object') {
  39. for (const key of Object.keys(value)) {
  40. if (value[key] !== null && value[key] !== "" && typeof(value[key]) !== 'undefined') {
  41. let params = propName + '[' + key + ']'
  42. var subPart = encodeURIComponent(params) + "="
  43. result += subPart + encodeURIComponent(value[key]) + "&"
  44. }
  45. }
  46. } else {
  47. result += part + encodeURIComponent(value) + "&"
  48. }
  49. }
  50. }
  51. return result
  52. }
  53. /**
  54. * 随机数
  55. * @param {Object} min
  56. * @param {Object} max
  57. */
  58. export function getRandomNum(min, max) {
  59. var range = max - min;
  60. var rand = Math.random()
  61. return (min + Math.round(range * rand))
  62. }
  63. export function getSameData(arr1, arr2, key) {
  64. const idMapping = arr1.reduce((acc, el, i) => {
  65. acc[el[key]] = i;
  66. return acc;
  67. }, {});
  68. arr2.forEach(i => {
  69. if (idMapping[i[key]]) { //重复值
  70. i.checked = true
  71. }
  72. })
  73. }
  74. /**
  75. * @param {Function} func
  76. * @param {number} wait
  77. * @param {boolean} immediate
  78. * @return {*}
  79. */
  80. export function debounce(func, wait, immediate) {
  81. let timeout, args, context, timestamp, result
  82. const later = function() {
  83. // 据上一次触发时间间隔
  84. const last = +new Date() - timestamp
  85. // 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
  86. if (last < wait && last > 0) {
  87. timeout = setTimeout(later, wait - last)
  88. } else {
  89. timeout = null
  90. // 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用
  91. if (!immediate) {
  92. result = func.apply(context, args)
  93. if (!timeout) context = args = null
  94. }
  95. }
  96. }
  97. return function(...args) {
  98. context = this
  99. timestamp = +new Date()
  100. const callNow = immediate && !timeout
  101. // 如果延时不存在,重新设定延时
  102. if (!timeout) timeout = setTimeout(later, wait)
  103. if (callNow) {
  104. result = func.apply(context, args)
  105. context = args = null
  106. }
  107. return result
  108. }
  109. }
  110. //搜索框关键词存储
  111. export function saveKeyWord(type, val) {
  112. let arr = []
  113. if (uni.getStorageSync(type)) {
  114. let arr = JSON.parse(uni.getStorageSync(type))
  115. if (arr.indexOf(val) != -1) {
  116. console.log('arr.indexOf(val)', arr.indexOf(val))
  117. arr.splice(arr.indexOf(val), 1)
  118. }
  119. arr.unshift(val)
  120. if (arr.length > 6) {
  121. arr.pop()
  122. }
  123. uni.setStorageSync(type, JSON.stringify(arr))
  124. return arr
  125. } else {
  126. arr.unshift(val)
  127. uni.setStorageSync(type, JSON.stringify(arr))
  128. return arr
  129. }
  130. }
  131. //价格保留两位小数点正则
  132. export function testPrice(val) {
  133. let reg = /^\d+(\.([0-9]|\d[0-9]))?$/
  134. return reg.test(val)
  135. }