glhcp/pc/utils/tools.js

59 lines
1.3 KiB
JavaScript

//节流
export const trottle = (func, time = 1000, context) => {
let previous = new Date(0).getTime()
return function(...args) {
let now = new Date().getTime()
if (now - previous > time) {
func.apply(context, args)
previous = now
}
}
}
//获取url后的参数 以对象返回
export function strToParams(str) {
var newparams = {}
for (let item of str.split('&')) {
newparams[item.split('=')[0]] = item.split('=')[1]
}
return newparams
}
//对象参数转为以?&拼接的字符
export function paramsToStr(params) {
let p = '';
if (typeof params == 'object') {
p = '?'
for (let props in params) {
p += `${props}=${params[props]}&`
}
p = p.slice(0, -1)
}
return p
}
/**
* @description 复制到剪切板
* @param value { String } 复制内容
* @return { Promise } resolve | reject
*/
export const copyClipboard = (value) => {
const elInput = document.createElement('input')
elInput.setAttribute('value', value)
document.body.appendChild(elInput)
elInput.select()
try{
if(document.execCommand('copy'))
return Promise.resolve()
else
throw new Error()
} catch(err) {
return Promise.reject(err)
} finally {
document.body.removeChild(elInput)
}
}