helper.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import CryptoJS from 'crypto-js';
  2. /**
  3. * @description: UUID生成
  4. * @return {string}
  5. */
  6. export const generateUUID = () => {
  7. const chars =
  8. "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  9. let result = "";
  10. for (let i = 32; i > 0; --i) {
  11. result += chars[Math.floor(Math.random() * chars.length)];
  12. }
  13. return result;
  14. };
  15. /**
  16. * @description: 检查某个对象是否缺失必要参数
  17. * @param {object} paramsObj
  18. * @param {string[]} requiredParams
  19. * @return {string[]} 缺失的参数数组
  20. */
  21. export const checkMissingParams = (paramsObj: object, requiredParams: string[]): string[] => {
  22. const missingParams: string[] = [];
  23. requiredParams.forEach(param => {
  24. if (!paramsObj[param as keyof typeof paramsObj]) {
  25. missingParams.push(param);
  26. }
  27. });
  28. return missingParams
  29. }
  30. /**
  31. * @description: 对象排序
  32. * @param {object} data
  33. * @return {object}
  34. */
  35. export const sortObj = (data: object) => {
  36. let arr = [];
  37. for (let key in data) {
  38. arr.push(key);
  39. }
  40. arr = arr.sort();
  41. let newData = {};
  42. for (var i in arr) {
  43. newData[arr[i]] = data[arr[i]];
  44. }
  45. return newData;
  46. };
  47. export const generateRandomIV = (length: number) => {
  48. const words = [];
  49. const now = Date.now();
  50. // 使用性能计数器增加随机性(如果可用)
  51. // @ts-ignore
  52. const perfNow = (typeof performance !== 'undefined' && performance.now) ? performance.now() : 0;
  53. for (let i = 0; i < length; i += 4) {
  54. // 组合多个随机源以增加随机性
  55. const random1 = Math.random() * 0xffffffff;
  56. const random2 = Math.random() * 0xffffffff;
  57. const timePart = (now + i) & 0xffffffff;
  58. const perfPart = (perfNow * 1000 + i) & 0xffffffff;
  59. // 使用异或运算组合多个随机源
  60. let combined = (random1 ^ random2 ^ timePart ^ perfPart) >>> 0;
  61. // 如果长度不是 4 的倍数,需要处理最后一个不完整的 word
  62. if (i + 4 > length) {
  63. // 只使用需要的字节数
  64. const remainingBytes = length - i;
  65. combined = combined >>> (32 - remainingBytes * 8);
  66. combined = combined << (32 - remainingBytes * 8);
  67. }
  68. words.push(combined);
  69. }
  70. return CryptoJS.lib.WordArray.create(words, length);
  71. }
  72. /**
  73. * @description: js异步转同步优化
  74. * @param {*} promiseObj
  75. * @return {*}
  76. */
  77. export const to = (promiseObj) => {
  78. return promiseObj
  79. .then((data) => {
  80. return [null, data];
  81. })
  82. .catch((err) => [err]);
  83. };
  84. /**
  85. * @description: 获取unix时间戳(秒)
  86. * @return {number}
  87. */
  88. export const unixTimestamp = () => {
  89. return Math.floor(Date.now() / 1000);
  90. };
  91. /**
  92. * @description: 加密
  93. * @param {*} value
  94. * @return {string}
  95. */
  96. export const compile = (value) => {
  97. value = JSON.stringify(value);
  98. let result = String.fromCharCode(value.charCodeAt(0) + value.length);
  99. for (let i = 1; i < value.length; i++) {
  100. result += String.fromCharCode(
  101. value.charCodeAt(i) + value.charCodeAt(i - 1)
  102. );
  103. }
  104. return escape(result);
  105. };
  106. /**
  107. * @description:解密
  108. * @param {string} value
  109. * @return {*}
  110. */
  111. export const uncompile = (value) => {
  112. value = unescape(value);
  113. let result = String.fromCharCode(value.charCodeAt(0) - value.length);
  114. for (let i = 1; i < value.length; i++) {
  115. result += String.fromCharCode(
  116. value.charCodeAt(i) - result.charCodeAt(i - 1)
  117. );
  118. }
  119. result = JSON.parse(result);
  120. return result;
  121. };
  122. /**
  123. * @description: 节流函数
  124. * @param {Function} func 要节流的函数
  125. * @param {number} wait 等待时间(毫秒)
  126. * @return {Function} 节流后的函数
  127. */
  128. export const throttle = (func, wait = 2000) => {
  129. let lastCall = 0;
  130. let pendingPromise = null;
  131. return function executedFunction(...args) {
  132. const now = Date.now();
  133. if (now - lastCall >= wait) {
  134. // 可以执行,重置时间
  135. lastCall = now;
  136. pendingPromise = null;
  137. return func.apply(this, args);
  138. } else {
  139. // 被节流,返回一个被拒绝的 Promise
  140. if (!pendingPromise) {
  141. pendingPromise = Promise.reject(new Error("点击太快,请稍后再试"));
  142. }
  143. return pendingPromise;
  144. }
  145. };
  146. };