123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- import store from '@/store'
- /**
- * 节流
- * @param {Function} func 回调函数
- * @param {Number} wait 时间限制
- * @returns 返回函数表达式
- */
- export function throttle(func, wait) {
- let timeout
- return function() {
- const context = this
- const args = arguments
- if (!timeout) {
- timeout = setTimeout(function() {
- timeout = null
- func.apply(context, args)
- }, wait)
- }
- }
- }
- /**
- * 防抖
- * @param {Function} func 回调函数
- * @param {Number} wait 时间限制
- * @param {Boolean} immediate 是否立即执行回调 true:立即执行,false:等待wait秒后执行
- * @returns 返回函数表达式
- */
- export function debounce(func, wait, immediate = false) {
- let timeout, result
- return function() {
- const context = this
- const args = arguments
- if (timeout) clearTimeout(timeout)
- if (immediate) {
- const callNow = !timeout
- timeout = setTimeout(function() {
- timeout = null
- }, wait)
- if (callNow) result = func.apply(context, args)
- } else {
- timeout = setTimeout(function() {
- func.apply(context, args)
- }, wait)
- }
- return result
- }
- }
- /**
- * 生成hash字符串
- * @param {Number} hashLength 生成的hash值的字符长度
- * @returns hash字符串
- */
- export function createHash(hashLength) {
- if (!hashLength || typeof Number(hashLength) !== 'number') {
- return
- }
- var ar = [
- '1',
- '2',
- '3',
- '4',
- '5',
- '6',
- '7',
- '8',
- '9',
- '0',
- 'a',
- 'b',
- 'c',
- 'd',
- 'e',
- 'f',
- 'g',
- 'h',
- 'i',
- 'j',
- 'k',
- 'l',
- 'm',
- 'n',
- 'o',
- 'p',
- 'q',
- 'r',
- 's',
- 't',
- 'u',
- 'v',
- 'w',
- 'x',
- 'y',
- 'z'
- ]
- var hs = []
- var hl = Number(hashLength)
- var al = ar.length
- for (var i = 0; i < hl; i++) {
- hs.push(ar[Math.floor(Math.random() * al)])
- }
- return hs.join('')
- }
- // 通过a标签下载文件
- export function downLoadWithATag(href) {
- const downLink = document.createElement('a')
- downLink.href = href
- downLink.style.display = 'none'
- downLink.setAttribute('download', true)
- downLink.click()
- }
- export function downloadWithUrl(url, name, options = {}) {
- return fetch(url, {
- headers: {
- 'x-token': store.getters.token
- },
- ...options
- })
- .then((data) => data.blob())
- .then((res) => {
- const a = document.createElement('a')
- a.href = URL.createObjectURL(res)
- a.download = name
- a.click()
- })
- }
- // 通知
- export function notify(self, title, duration = 3000) {
- const h = self.$createElement
- self.$notify.success({
- title: '移除授权机构',
- message: h('i', { style: 'color: #333' }, title),
- duration
- })
- }
|