index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * 获取路径参数
  3. * @param {*} url
  4. */
  5. export function getQueryObject (url) {
  6. url = url == null ? window.location.href : url
  7. const search = url.substring(url.lastIndexOf('?') + 1)
  8. const obj = {}
  9. const reg = /([^?&=]+)=([^?&=]*)/g
  10. search.replace(reg, (rs, $1, $2) => {
  11. const name = decodeURIComponent($1)
  12. let val = decodeURIComponent($2)
  13. val = String(val)
  14. obj[name] = val
  15. return rs
  16. })
  17. return obj
  18. }
  19. // 获取全局唯一id
  20. export function getUuid() {
  21. const s = [];
  22. const hexDigits = '0123456789abcdef';
  23. for (let i = 0; i < 36; i++) {
  24. s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  25. }
  26. s[14] = '4'; // bits 12-15 of the time_hi_and_version field to 0010
  27. s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
  28. s[8] = '-';
  29. s[13] = '-';
  30. s[18] = '-';
  31. s[23] = '-';
  32. const uid = s.join('');
  33. return uid;
  34. }
  35. /**
  36. * @param {Sting} input value
  37. * @returns {number} output value
  38. */
  39. export function byteLength (str) {
  40. // returns the byte length of an utf8 string
  41. let s = str.length
  42. for (var i = str.length - 1; i >= 0; i--) {
  43. const code = str.charCodeAt(i)
  44. if (code > 0x7f && code <= 0x7ff) s++
  45. else if (code > 0x7ff && code <= 0xffff) s += 2
  46. if (code >= 0xDC00 && code <= 0xDFFF) i--
  47. }
  48. return s
  49. }
  50. export function html2Text (val) {
  51. const div = document.createElement('div')
  52. div.innerHTML = val
  53. return div.textContent || div.innerText
  54. }
  55. export function toggleClass (element, className) {
  56. if (!element || !className) {
  57. return
  58. }
  59. let classString = element.className
  60. const nameIndex = classString.indexOf(className)
  61. if (nameIndex === -1) {
  62. classString += ' ' + className
  63. } else {
  64. classString =
  65. classString.substr(0, nameIndex) +
  66. classString.substr(nameIndex + className.length)
  67. }
  68. element.className = classString
  69. }
  70. export function debounce (func, wait, immediate) {
  71. let timeout, args, context, timestamp, result
  72. const later = function () {
  73. // 据上一次触发时间间隔
  74. const last = +new Date() - timestamp
  75. // 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
  76. if (last < wait && last > 0) {
  77. timeout = setTimeout(later, wait - last)
  78. } else {
  79. timeout = null
  80. // 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用
  81. if (!immediate) {
  82. result = func.apply(context, args)
  83. if (!timeout) context = args = null
  84. }
  85. }
  86. }
  87. return function (...args) {
  88. context = this
  89. timestamp = +new Date()
  90. const callNow = immediate && !timeout
  91. // 如果延时不存在,重新设定延时
  92. if (!timeout) timeout = setTimeout(later, wait)
  93. if (callNow) {
  94. result = func.apply(context, args)
  95. context = args = null
  96. }
  97. return result
  98. }
  99. }
  100. export function formatTime(time, fmt) {
  101. if (!time) return '';
  102. else {
  103. const date = new Date(time);
  104. const o = {
  105. 'M+': date.getMonth() + 1,
  106. 'd+': date.getDate(),
  107. 'H+': date.getHours(),
  108. 'm+': date.getMinutes(),
  109. 's+': date.getSeconds(),
  110. 'q+': Math.floor((date.getMonth() + 3) / 3),
  111. S: date.getMilliseconds(),
  112. };
  113. if (/(y+)/.test(fmt))
  114. fmt = fmt.replace(
  115. RegExp.$1,
  116. (date.getFullYear() + '').substr(4 - RegExp.$1.length)
  117. );
  118. for (const k in o) {
  119. if (new RegExp('(' + k + ')').test(fmt)) {
  120. fmt = fmt.replace(
  121. RegExp.$1,
  122. RegExp.$1.length === 1
  123. ? o[k]
  124. : ('00' + o[k]).substr(('' + o[k]).length)
  125. );
  126. }
  127. }
  128. return fmt;
  129. }
  130. }