tools.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import store from '@/store'
  2. /**
  3. * 节流
  4. * @param {Function} func 回调函数
  5. * @param {Number} wait 时间限制
  6. * @returns 返回函数表达式
  7. */
  8. export function throttle(func, wait) {
  9. let timeout
  10. return function() {
  11. const context = this
  12. const args = arguments
  13. if (!timeout) {
  14. timeout = setTimeout(function() {
  15. timeout = null
  16. func.apply(context, args)
  17. }, wait)
  18. }
  19. }
  20. }
  21. /**
  22. * 防抖
  23. * @param {Function} func 回调函数
  24. * @param {Number} wait 时间限制
  25. * @param {Boolean} immediate 是否立即执行回调 true:立即执行,false:等待wait秒后执行
  26. * @returns 返回函数表达式
  27. */
  28. export function debounce(func, wait, immediate = false) {
  29. let timeout, result
  30. return function() {
  31. const context = this
  32. const args = arguments
  33. if (timeout) clearTimeout(timeout)
  34. if (immediate) {
  35. const callNow = !timeout
  36. timeout = setTimeout(function() {
  37. timeout = null
  38. }, wait)
  39. if (callNow) result = func.apply(context, args)
  40. } else {
  41. timeout = setTimeout(function() {
  42. func.apply(context, args)
  43. }, wait)
  44. }
  45. return result
  46. }
  47. }
  48. /**
  49. * 生成hash字符串
  50. * @param {Number} hashLength 生成的hash值的字符长度
  51. * @returns hash字符串
  52. */
  53. export function createHash(hashLength) {
  54. if (!hashLength || typeof Number(hashLength) !== 'number') {
  55. return
  56. }
  57. var ar = [
  58. '1',
  59. '2',
  60. '3',
  61. '4',
  62. '5',
  63. '6',
  64. '7',
  65. '8',
  66. '9',
  67. '0',
  68. 'a',
  69. 'b',
  70. 'c',
  71. 'd',
  72. 'e',
  73. 'f',
  74. 'g',
  75. 'h',
  76. 'i',
  77. 'j',
  78. 'k',
  79. 'l',
  80. 'm',
  81. 'n',
  82. 'o',
  83. 'p',
  84. 'q',
  85. 'r',
  86. 's',
  87. 't',
  88. 'u',
  89. 'v',
  90. 'w',
  91. 'x',
  92. 'y',
  93. 'z'
  94. ]
  95. var hs = []
  96. var hl = Number(hashLength)
  97. var al = ar.length
  98. for (var i = 0; i < hl; i++) {
  99. hs.push(ar[Math.floor(Math.random() * al)])
  100. }
  101. return hs.join('')
  102. }
  103. // 通过a标签下载文件
  104. export function downLoadWithATag(href) {
  105. const downLink = document.createElement('a')
  106. downLink.href = href
  107. downLink.style.display = 'none'
  108. downLink.setAttribute('download', true)
  109. downLink.click()
  110. }
  111. export function downloadWithUrl(url, name, options = {}) {
  112. return fetch(url, {
  113. headers: {
  114. 'x-token': store.getters.token
  115. },
  116. ...options
  117. })
  118. .then((data) => data.blob())
  119. .then((res) => {
  120. const a = document.createElement('a')
  121. a.href = URL.createObjectURL(res)
  122. a.download = name
  123. a.click()
  124. })
  125. }
  126. // 通知
  127. export function notify(self, title, duration = 3000) {
  128. const h = self.$createElement
  129. self.$notify.success({
  130. title: '移除授权机构',
  131. message: h('i', { style: 'color: #333' }, title),
  132. duration
  133. })
  134. }