复制则可使用、亲测有效
export function filterName(id, list) {
let item = list.filter(_ => id === _.id);
return item.length !== 0 ? item[0].name : "";
}
export function getViewInfo(refsData, listHeight = 160) {
if (!refsData || !listHeight) {
console.log("请输入viewData的参数($refs(引用信息),listHeight(数据高度))");
} else {
let json = {};
let clientHeight = refsData.clientHeight;
let fontSize = parseInt(
document.getElementsByTagName("html")[0].style.fontSize
);
let dataLength = clientHeight / ((listHeight / 16) * fontSize);
json.height = refsData.clientHeight;
json.fontSize = fontSize;
json.loadSize = Math.floor(dataLength * 2);
return json;
}
}
export function setCookieDay(cName, value, expiredays) {
let exdate = new Date();
exdate.setDate(exdate.getDate() + expiredays);
document.cookie =
cName +
"=" +
escape(value) +
(expiredays == null ? "" : ";expires=" + exdate.toGMTString());
}
export function setCookieSec(cName, value, expireseconds) {
let exdate = new Date();
exdate.setTime(exdate.getTime() + expireseconds * 1000);
document.cookie =
cName +
"=" +
escape(value) +
(expireseconds == null ? "" : ";expires=" + exdate.toGMTString());
}
export function getCookie(cName) {
if (document.cookie.length > 0) {
let cStart = document.cookie.indexOf(cName + "=");
if (cStart !== -1) {
cStart = cStart + cName.length + 1;
let cEnd = document.cookie.indexOf(";", cStart);
if (cEnd === -1) {
cEnd = document.cookie.length;
}
return unescape(document.cookie.substring(cStart, cEnd));
}
}
return "";
}
export function formatText(strText) {
return strText
.replace(/\r\n/g, "<br/>")
.replace(/\n/g, "<br/>")
.replace(/\s/g, " ");
}
export function formSubmit(params, url) {
let form = document.createElement("form");
form.style.display = "none";
form.action = url;
form.method = "post";
document.body.appendChild(form);
for (var key in params) {
let input = document.createElement("input");
input.type = "hidden";
input.name = key;
input.value = params[key];
form.appendChild(input);
}
form.submit();
form.remove();
}
export function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
export function getUUID() {
function S4() {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
return (
S4() +
S4() +
"-" +
S4() +
"-" +
S4() +
"-" +
S4() +
"-" +
S4() +
S4() +
S4()
);
}
export function detectDeviceType() {
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
navigator.userAgent
)
? "Mobile"
: "Desktop";
}
export function scrollToTop() {
const c = document.documentElement.scrollTop || document.body.scrollTop;
if (c > 0) {
window.requestAnimationFrame(scrollToTop);
window.scrollTo(0, c - c / 8);
}
}
export function copyToClipboard(str) {
try {
const el = document.createElement("input");
el.value = str;
el.style.position = "absolute";
el.style.left = "-9999px";
document.body.appendChild(el);
el.select();
document.execCommand("copy");
console.log("拷贝成功");
document.body.removeChild(el);
return true;
} catch (error) {
console.log(error);
return false;
}
}
export function debounce(func, delay = 200) {
let timer;
return function(...args) {
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(() => {
func.apply(this, args);
}, delay);
};
}
export function throttle(func, delay = 200) {
let lastTime;
let timer;
return function(...args) {
let nowTime = Date.now();
if (lastTime && nowTime - lastTime < delay) {
clearTimeout(timer);
timer = setTimeout(() => {
lastTime = nowTime;
func.apply(this, args);
}, delay);
} else {
lastTime = nowTime;
func.apply(this, args);
}
};
}
export function deepClone(obj) {
var newObj = obj instanceof Array ? [] : {};
for (var i in obj) {
newObj[i] = typeof obj[i] === "object" ? deepClone(obj[i]) : obj[i];
}
return newObj;
}