import CryptoJS from 'crypto-js'; /** * @description: UUID生成 * @return {string} */ export const generateUUID = () => { const chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; let result = ""; for (let i = 32; i > 0; --i) { result += chars[Math.floor(Math.random() * chars.length)]; } return result; }; /** * @description: 检查某个对象是否缺失必要参数 * @param {object} paramsObj * @param {string[]} requiredParams * @return {string[]} 缺失的参数数组 */ export const checkMissingParams = (paramsObj: object, requiredParams: string[]): string[] => { const missingParams: string[] = []; requiredParams.forEach(param => { if (!paramsObj[param as keyof typeof paramsObj]) { missingParams.push(param); } }); return missingParams } /** * @description: 对象排序 * @param {object} data * @return {object} */ export const sortObj = (data: object) => { let arr = []; for (let key in data) { arr.push(key); } arr = arr.sort(); let newData = {}; for (var i in arr) { newData[arr[i]] = data[arr[i]]; } return newData; }; export const generateRandomIV = (length: number) => { const words = []; const now = Date.now(); // 使用性能计数器增加随机性(如果可用) // @ts-ignore const perfNow = (typeof performance !== 'undefined' && performance.now) ? performance.now() : 0; for (let i = 0; i < length; i += 4) { // 组合多个随机源以增加随机性 const random1 = Math.random() * 0xffffffff; const random2 = Math.random() * 0xffffffff; const timePart = (now + i) & 0xffffffff; const perfPart = (perfNow * 1000 + i) & 0xffffffff; // 使用异或运算组合多个随机源 let combined = (random1 ^ random2 ^ timePart ^ perfPart) >>> 0; // 如果长度不是 4 的倍数,需要处理最后一个不完整的 word if (i + 4 > length) { // 只使用需要的字节数 const remainingBytes = length - i; combined = combined >>> (32 - remainingBytes * 8); combined = combined << (32 - remainingBytes * 8); } words.push(combined); } return CryptoJS.lib.WordArray.create(words, length); } /** * @description: js异步转同步优化 * @param {*} promiseObj * @return {*} */ export const to = (promiseObj) => { return promiseObj .then((data) => { return [null, data]; }) .catch((err) => [err]); }; /** * @description: 获取unix时间戳(秒) * @return {number} */ export const unixTimestamp = () => { return Math.floor(Date.now() / 1000); }; /** * @description: 加密 * @param {*} value * @return {string} */ export const compile = (value) => { value = JSON.stringify(value); let result = String.fromCharCode(value.charCodeAt(0) + value.length); for (let i = 1; i < value.length; i++) { result += String.fromCharCode( value.charCodeAt(i) + value.charCodeAt(i - 1) ); } return escape(result); }; /** * @description:解密 * @param {string} value * @return {*} */ export const uncompile = (value) => { value = unescape(value); let result = String.fromCharCode(value.charCodeAt(0) - value.length); for (let i = 1; i < value.length; i++) { result += String.fromCharCode( value.charCodeAt(i) - result.charCodeAt(i - 1) ); } result = JSON.parse(result); return result; }; /** * @description: 节流函数 * @param {Function} func 要节流的函数 * @param {number} wait 等待时间(毫秒) * @return {Function} 节流后的函数 */ export const throttle = (func, wait = 2000) => { let lastCall = 0; let pendingPromise = null; return function executedFunction(...args) { const now = Date.now(); if (now - lastCall >= wait) { // 可以执行,重置时间 lastCall = now; pendingPromise = null; return func.apply(this, args); } else { // 被节流,返回一个被拒绝的 Promise if (!pendingPromise) { pendingPromise = Promise.reject(new Error("点击太快,请稍后再试")); } return pendingPromise; } }; };