2025-04-01 18:07:18 +08:00
|
|
|
import dayjs from "dayjs";
|
|
|
|
|
2025-03-31 18:24:37 +08:00
|
|
|
export const setProperty = (prop: string, val: any, dom = document.documentElement) => {
|
2025-04-01 18:07:18 +08:00
|
|
|
dom.style.setProperty(prop, val);
|
2025-03-31 18:24:37 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export const mix = (color1: string, color2: string, weight: number = 0.5): string => {
|
2025-04-01 18:07:18 +08:00
|
|
|
let color = '#';
|
|
|
|
for (let i = 0; i <= 2; i++) {
|
|
|
|
const c1 = parseInt(color1.substring(1 + i * 2, 3 + i * 2), 16);
|
|
|
|
const c2 = parseInt(color2.substring(1 + i * 2, 3 + i * 2), 16);
|
|
|
|
const c = Math.round(c1 * weight + c2 * (1 - weight));
|
|
|
|
color += c.toString(16).padStart(2, '0');
|
|
|
|
}
|
|
|
|
return color;
|
2025-03-31 18:24:37 +08:00
|
|
|
};
|
2025-04-01 18:07:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {*} time 时间
|
|
|
|
* @param {*} fmStr 格式化格式 YYYY-MM-DD HH:mm:ss
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
export function format(time: Date | string, fmStr: string = "YYYY-MM-DD HH:mm:ss") {
|
|
|
|
if (!time) return "";
|
|
|
|
return dayjs(typeof time == 'string' ? new Date(time) : time).format(fmStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param fn 需要防抖的函数
|
|
|
|
* @param time 防抖时间
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
|
|
|
|
export const debounce = (fn: Function, time: number = 300) => {
|
|
|
|
let timer;
|
|
|
|
return function (...argu) {
|
|
|
|
if (timer) {
|
|
|
|
clearTimeout(timer);
|
|
|
|
timer = null;
|
|
|
|
}
|
|
|
|
timer = setTimeout(() => {
|
|
|
|
fn(...argu);
|
|
|
|
clearTimeout(timer);
|
|
|
|
timer = null;
|
|
|
|
}, time);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param fn 需要节流的函数
|
|
|
|
* @param time 需要节流的时间
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
export const throttle = (fn: Function, time: number = 300) => {
|
|
|
|
let flag = true
|
|
|
|
return function (...argu) {
|
|
|
|
if (!flag) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
flag = false
|
|
|
|
let timer = setTimeout(() => {
|
|
|
|
fn(...argu)
|
|
|
|
flag = true
|
|
|
|
clearTimeout(timer)
|
|
|
|
timer = null
|
|
|
|
}, time)
|
|
|
|
}
|
|
|
|
}
|