/* * @Descripttion: * @Author: zenghua.wang * @Date: 2022-02-23 21:12:37 * @LastEditors: wzh 1048523306@qq.com * @LastEditTime: 2024-09-09 18:46:56 */ 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; }; /** * 合并列 * @param {*} param0 * @param {*} options * @param {*} rowList * @returns */ export const setSpan = ({ rowIndex, columnIndex }, options, rowList) => { const columnList = deepClone(options.columns).filter((item) => !item.hide); const conditions = []; const getColspan = (column, conditions) => { const len = rowList.length; const arr = []; for (let i = 0; i < len; i++) { let colspan = 1; for (let j = i + 1; j < len; j++) { const bool = conditions.every((col) => { return rowList[i][col] === rowList[j][col]; }); if (bool && rowList[i][column] === rowList[j][column]) { colspan += 1; arr[i] = colspan; arr[j] = 0; if (j === len - 1) i = j; } else { colspan = 1; if (!arr[i]) arr[i] = colspan; i = j; arr[j] = colspan; } } } return arr; }; if (rowList.length <= 1) { return { rowspan: 1, colspan: 1, }; } else { for (let i = 0, columnLen = columnList.length; i < columnLen; i++) { if (columnList[i].isColspan) { const arr = getColspan(columnList[i].prop, conditions); const index = options.selection ? (options.index ? i + 2 : i + 1) : options.index ? i + 1 : i; conditions.push(columnList[i].prop); if (columnIndex === index) { for (let j = 0, rowLen = rowList.length; j < rowLen; j++) { if (rowIndex === j) { return { rowspan: arr[j], colspan: arr[j] > 0 ? 1 : 0, }; } } } } } } }; /** * @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); };