util.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /**
  2. * 格式化日期格式 (用于兼容ios Date对象)
  3. */
  4. export const formatDate = (time) => {
  5. // 将xxxx-xx-xx的时间格式,转换为 xxxx/xx/xx的格式
  6. return time.replace(/\-/g, "/");
  7. }
  8. /**
  9. * 对象转URL参数
  10. * @param {object} obj
  11. * @return {string}
  12. */
  13. export const urlEncode = (obj = {}) => {
  14. const result = []
  15. for (const key in obj) {
  16. const item = obj[key]
  17. if (!item) {
  18. continue
  19. }
  20. if (isArray(item)) {
  21. item.forEach(val => {
  22. result.push(key + '=' + val)
  23. })
  24. } else {
  25. result.push(key + '=' + item)
  26. }
  27. }
  28. return result.join('&')
  29. }
  30. /**
  31. * URL参数转对象
  32. * @param {string} queryStr
  33. * @return {object}
  34. */
  35. export const urlDecode = (queryStr = '') => {
  36. var newObj = new Object()
  37. if (queryStr) {
  38. var strs = queryStr.split("&")
  39. for (var i = 0; i < strs.length; i++) {
  40. newObj[strs[i].split("=")[0]] = (strs[i].split("=")[1]) || ''
  41. }
  42. }
  43. return newObj
  44. }
  45. /**
  46. * 遍历对象
  47. */
  48. export const objForEach = (obj, callback) => {
  49. Object.keys(obj).forEach((key) => {
  50. callback(obj[key], key)
  51. })
  52. }
  53. /**
  54. * 是否在数组内
  55. */
  56. export const inArray = (search, array) => {
  57. for (var i in array) {
  58. if (array[i] == search) return true
  59. }
  60. return false
  61. }
  62. /**
  63. * 对Date的扩展,将 Date 转化为指定格式的String
  64. * 月(Y)、月(m)、日(d)、小时(H)、分(M)、秒(S) 可以用 1-2 个占位符,
  65. * 例子:
  66. * dateFormat('YYYY-mm-dd HH:MM:SS', new Date()) ==> 2020-01-01 08:00:00
  67. */
  68. export const dateFormat = (fmt, date) => {
  69. const opt = {
  70. "Y+": date.getFullYear().toString(), // 年
  71. "m+": (date.getMonth() + 1).toString(), // 月
  72. "d+": date.getDate().toString(), // 日
  73. "H+": date.getHours().toString(), // 时
  74. "M+": date.getMinutes().toString(), // 分
  75. "S+": date.getSeconds().toString() // 秒
  76. // 有其他格式化字符需求可以继续添加,必须转化成字符串
  77. };
  78. let ret
  79. for (let k in opt) {
  80. ret = new RegExp("(" + k + ")").exec(fmt)
  81. if (ret) {
  82. fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
  83. };
  84. };
  85. return fmt
  86. }
  87. /**
  88. * 判断是否为空对象
  89. * @param {*} object 源对象
  90. */
  91. export const isEmptyObject = (object) => {
  92. return Object.keys(object).length === 0
  93. }
  94. /**
  95. * 判断是否为对象
  96. * @param {*} object
  97. */
  98. export const isObject = (object) => {
  99. return Object.prototype.toString.call(object) === '[object Object]'
  100. }
  101. /**
  102. * 判断是否为数组
  103. * @param {*} array
  104. */
  105. export const isArray = (array) => {
  106. return Object.prototype.toString.call(array) === '[object Array]'
  107. }
  108. /**
  109. * 判断是否为空
  110. * @param {*} object 源对象
  111. */
  112. export const isEmpty = (value) => {
  113. if (isArray(value)) {
  114. return value.length === 0
  115. }
  116. if (isObject(value)) {
  117. return isEmptyObject(value)
  118. }
  119. return !value
  120. }
  121. /**
  122. * 判断对象中是否存在某个属性
  123. * @param {*} object 源对象
  124. */
  125. export const hasOwnProperty = (object, key) => {
  126. // 使用hasOwnProperty在支付宝小程序中报错
  127. // return isObject(object) && object.hasOwnProperty(key)
  128. return isObject(object) && object[key] !== undefined
  129. }
  130. /**
  131. * 对象深拷贝
  132. * @param {*} obj 源对象
  133. */
  134. export const cloneObj = (obj) => {
  135. let newObj = isArray(obj) ? [] : {};
  136. if (typeof obj !== 'object') {
  137. return;
  138. }
  139. for (let i in obj) {
  140. newObj[i] = typeof obj[i] === 'object' ? cloneObj(obj[i]) : obj[i];
  141. }
  142. return newObj
  143. }
  144. // 节流函数
  145. // 思路: 第一次先设定一个变量true,
  146. // 第二次执行这个函数时,会判断变量是否true,
  147. // 是则返回。当第一次的定时器执行完函数最后会设定变量为flase。
  148. // 那么下次判断变量时则为flase,函数会依次运行。
  149. export function throttle(fn, delay = 100) {
  150. // 首先设定一个变量,在没有执行我们的定时器时为null
  151. var timer = null
  152. return function() {
  153. // 当我们发现这个定时器存在时,则表示定时器已经在运行中,需要返回
  154. if (timer) return
  155. timer = setTimeout(() => {
  156. fn.apply(this, arguments)
  157. timer = null
  158. }, delay)
  159. }
  160. }
  161. // 防抖函数
  162. // 首次运行时把定时器赋值给一个变量, 第二次执行时,
  163. // 如果间隔没超过定时器设定的时间则会清除掉定时器,
  164. // 重新设定定时器, 依次反复, 当我们停止下来时,
  165. // 没有执行清除定时器, 超过一定时间后触发回调函数。
  166. // 参考文档:https://segmentfault.com/q/1010000021145192
  167. export function debounce(fn, delay = 100) {
  168. let timer
  169. return function() {
  170. const that = this
  171. const _args = arguments // 存一下传入的参数
  172. if (timer) {
  173. clearTimeout(timer)
  174. }
  175. timer = setTimeout(function() {
  176. fn.apply(that, _args)
  177. }, delay)
  178. }
  179. }
  180. /**
  181. * 数组交集
  182. * @param {Array} 数组1
  183. * @param {Array} 数组2
  184. * @return {Array}
  185. */
  186. export const arrayIntersect = (array1, array2) => {
  187. return array1.filter(val => array2.indexOf(val) > -1)
  188. }
  189. /**
  190. * 获取当前客户端的rpx比值
  191. * @return {Number}
  192. */
  193. export const rpx = () => {
  194. const { windowWidth } = uni.getSystemInfoSync()
  195. // #ifdef H5
  196. // 与pages.json文件中的 rpxCalcMaxDeviceWidth参数对应, 请勿修改
  197. const rpxCalcMaxDeviceWidth = 750
  198. // 与pages.json文件中的 rpxCalcBaseDeviceWidth参数对应, 请勿修改
  199. const rpxCalcBaseDeviceWidth = 560
  200. const calcWindowWidth = windowWidth > rpxCalcMaxDeviceWidth ? rpxCalcBaseDeviceWidth : windowWidth
  201. return calcWindowWidth / 750
  202. // #endif
  203. // #ifndef H5
  204. return windowWidth / 750
  205. // #endif
  206. }
  207. /**
  208. * 获取当前客户端的rpx比值
  209. * @return {Number}
  210. */
  211. export const rpx2px = (num) => {
  212. return num * rpx()
  213. }