295 lines
6.6 KiB
JavaScript
Raw Normal View History

2025-01-23 01:10:59 +00:00
/*
* @Descripttion:
* @Author: zenghua.wang
* @Date: 2022-02-23 21:12:37
2025-01-25 09:08:09 +00:00
* @LastEditors: zenghua.wang
* @LastEditTime: 2025-01-25 17:04:22
2025-01-23 01:10:59 +00:00
*/
import lodash from 'lodash';
import moment from 'moment';
import { Base64 } from 'js-base64';
/**
* @Title 防抖指在一定时间内多次触发同一个事件只执行最后一次操作
* @param {*} fn
* @param {*} delay
* @returns
*/
export function debounce(fn, delay) {
let timer = null;
return function (...args) {
clearTimeout(timer);
timer = setTimeout(() => {
fn.apply(this, args);
}, delay);
};
}
/**
* @Title 节流指在一定时间内多次触发同一个事件只执行第一次操作
* @param {*} fn
* @param {*} delay
* @returns
*/
export function throttle(fn, delay) {
let timer = null;
return function (...args) {
if (!timer) {
timer = setTimeout(() => {
fn.apply(this, args);
timer = null;
}, delay);
}
};
}
/**
* @Title 判断是否 empty,返回ture
* @param {*} val:null 'null' undefined 'undefined' 0 '0' "" 返回true
* @returns
*/
export const isEmpty = (val) => {
if (val && parseInt(val) === 0) return false;
if (typeof val === 'undefined' || val === 'null' || val == null || val === 'undefined' || val === undefined || val === '') {
return true;
} else if (typeof val === 'object' && Object.keys(val).length === 0) {
return true;
} else if (val instanceof Array && val.length === 0) {
return true;
} else {
return false;
}
};
/**
* @Title 深度拷贝对象
* @param {*} obj
* @returns
*/
export const deepClone = (obj = {}) => {
return lodash.cloneDeep(obj);
};
/**
* @Title 将number转换为px
* @param {*} val
* @returns
*/
export const setPx = (val) => {
if (isEmpty(val)) return '';
val = val + '';
if (val.indexOf('%') === -1) {
val = val + 'px';
}
return val;
};
/**
* @Tilte 设置属性默认值
* @param {*} options
* @param {*} prop
* @param {*} defaultVal
* @returns
*/
export const setDefaultOption = (options, prop, defaultVal) => {
return options[prop] === undefined ? defaultVal : options.prop;
};
/**
* 设置字典值
* @param {*} columns
* @param {*} key
* @param {*} data
* @returns
*/
export const setDicData = (columns, key, data = []) => {
if (isEmpty(data)) return;
const len = columns.length;
for (let i = 0; i < len; i++) {
if (columns[i]?.prop === key) {
columns[i]['dicData'] = data;
break;
}
}
};
/**
* 求字段lable
* @param {*} tree
* @returns
*/
export const setDicLabel = (dicData, value) => {
let label = value;
if (isEmpty(dicData)) return label;
const len = dicData.length;
for (let i = 0; i < len; i++) {
if (dicData[i]?.value === value) {
label = dicData[i].label;
break;
}
}
return label;
};
/**
* 加密
* @param {*} n
* @returns
*/
export const encode = (n, flag = false) => {
if (flag) {
return (
((e) => {
let t = e.length.toString();
for (let n = 10 - t.length; n > 0; n--) t = '0' + t;
return t;
})(n) +
((e) => {
const t = Base64.encode(e).split('');
for (let n = 0; n < Math.floor(e.length / 100 + 1); n++) t.splice(100 * n + 1, 0, 3);
return t.join('');
})(n)
);
}
return n;
};
/**
* 解密
* @param {*} e
* @returns
*/
export const decode = (e, flag = false) => {
if (flag) {
try {
const t = Number(e.substr(0, 10));
const n = e.substr(10).split('');
for (let i = 0, s = 0; s < Math.floor(t / 100) + 1; s++) {
n.splice(100 * s + 1 - i, 1);
i++;
}
const o = Base64.decode(n.join(''));
return o;
} catch (error) {
return e;
}
}
return e;
};
/**
* @Title 图片转base64
* @param {*} file
* @returns
*/
export const imageToBase64 = (file) => {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => {
resolve(reader.result);
};
reader.onerror = reject;
});
};
/**
* @Title bufferToBase64
* @param {*} buffer
* @returns
*/
export const bufferToBase64 = (buffer) => {
return 'data:image/jpeg;base64,' + window.btoa(new Uint8Array(buffer).reduce((data, byte) => data + String.fromCharCode(byte), ''));
};
/**
* @Title 将array转化为树
* @param tree
* @returns
*/
export const getTree = (tree = []) => {
tree.forEach((item) => {
delete item.children;
});
const map = {};
tree.forEach((item) => {
map[item.id] = item;
});
const arr = [];
tree.forEach((item) => {
const parent = map[item.parentId];
if (parent) {
(parent.children || (parent.children = [])).push(item);
} else {
arr.push(item);
}
});
return arr;
};
/**
* @Title 获取路由中的参数
* @param name
* @returns
*/
export const getUrlQuery = (name) => {
const url = window.location.href;
const hash = url.substring(url.indexOf('#') + 1);
const searchIndex = hash.indexOf('?');
const search = searchIndex !== -1 ? hash.substring(searchIndex + 1) : '';
const usp = new URLSearchParams(search);
return usp.get(name);
};
/**
* @Title 获取静态资源文件
* @param {*} url
* @returns
*/
export const getAssetsFile = (url) => {
return new URL(`../assets/images/${url}`, import.meta.url);
};
/**
* @Title 替换图片url字段值
* @param {*} url
* @returns
*/
export const setUploadField = (url) => {
if (isEmpty(url) || url.includes('http')) return null;
return url;
};
/**
* @Title: a链接方式文件下载
* @param {void} content:
* @param {void} fileName:
* @return {void}
*/
export const dowloadLink = (content, fileName) => {
const blob = new Blob([content]);
if ('download' in document.createElement('a')) {
const elink = document.createElement('a');
elink.download = fileName;
elink.style.display = 'none';
elink.href = URL.createObjectURL(blob);
document.body.appendChild(elink);
elink.click();
URL.revokeObjectURL(elink.href);
document.body.removeChild(elink);
} else {
navigator.msSaveBlob(blob, fileName);
}
};
/**
* @Title 模拟休眠
* @param {*} duration
* @returns
*/
export const sleep = (duration = 0) =>
new Promise((resolve) => {
setTimeout(resolve, duration);
});
/**
* 日期格式化
* @param {*} date
* @param {*} format
* @returns
*/
export const dateFormat = (datetime, type = 'yyyy-MM-dd') => {
return moment(datetime).format(type);
};
/**
* 上日///
*/
export const lastDate = (last = 0, date = 'month', type = 'yyyy-MM-dd') => {
if (date === 'day') {
return moment().subtract(last, 'day').endOf('day').format(type);
}
return moment().subtract(last, date).format(type);
};