d8ea009be5644c7164c4b5ae51cd349e5525e465.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. System.register(["cc"], function (_export, _context) {
  2. "use strict";
  3. var _cclegacy, Utils, _crd;
  4. _export("Utils", void 0);
  5. return {
  6. setters: [function (_cc) {
  7. _cclegacy = _cc.cclegacy;
  8. }],
  9. execute: function () {
  10. _crd = true;
  11. _cclegacy._RF.push({}, "7ea66r0AApIFbjuTFjXJIWh", "Utils", undefined);
  12. _export("Utils", Utils = class Utils {
  13. /**
  14. * 随机数 (包含min,不包含max)
  15. * @param min
  16. * @param max
  17. * @param isInt
  18. * @return {*}
  19. */
  20. static getRandom(min = 0, max = 1, isInt = false) {
  21. if (min == null) min = 0;
  22. if (max == null) max = 1;
  23. if (isInt == null) isInt = false;
  24. if (min === max) return min;
  25. let value = min + Math.random() * (max - min);
  26. if (isInt) {
  27. value = Math.floor(value);
  28. }
  29. return value;
  30. }
  31. /**
  32. * 随机整数-包含最大值,最小值
  33. * @param min
  34. * @param max
  35. * @return {*}
  36. */
  37. static getRandomIntInclusive(min, max) {
  38. min = Math.ceil(min);
  39. max = Math.floor(max);
  40. if (min == max) return min;
  41. return Math.floor(Math.random() * (max - min + 1)) + min; //The maximum is inclusive and the minimum is inclusive
  42. }
  43. /**
  44. * 随机整数-不包含最大值,最小值
  45. * @param min
  46. * @param max
  47. * @return {*}
  48. */
  49. static getRandomInt(min, max) {
  50. min = Math.ceil(min);
  51. max = Math.ceil(max);
  52. return Math.floor(Math.random() * (max - min)) + min;
  53. }
  54. /** */
  55. static getRandomNumberInRange(min, max) {
  56. return Math.random() * (max - min) + min;
  57. }
  58. /**生成随机整数 -1 或 1*/
  59. static getRandomDir() {
  60. return Math.floor(Math.random() * 2) === 0 ? -1 : 1; //
  61. }
  62. /**
  63. * 在指定数组中随机取出N个不重复的数据
  64. * @param resArr
  65. * @param ranNum
  66. * @returns {Array}
  67. */
  68. static getRandomDiffValueFromArr(resArr, ranNum) {
  69. let arr = new Array();
  70. let result = new Array();
  71. if (!resArr || resArr.length <= 0 || ranNum <= 0) {
  72. return result;
  73. }
  74. for (let i = 0; i < resArr.length; i++) {
  75. arr.push(resArr[i]);
  76. }
  77. if (ranNum >= arr.length) {
  78. return arr;
  79. }
  80. ranNum = Math.min(ranNum, arr.length - 1);
  81. for (let i = 0; i < ranNum; i++) {
  82. let ran = Utils.getRandomIntInclusive(0, arr.length - 1); // Math.floor(Math.random() * arr.length);
  83. result.push(arr.splice(ran, 1)[0]);
  84. }
  85. return result;
  86. }
  87. /**
  88. * 随机一个字符串
  89. * @param length
  90. * @returns
  91. */
  92. static randomStr(length) {
  93. let result = '';
  94. const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  95. const charactersLength = characters.length;
  96. for (let i = 0; i < length; i++) {
  97. result += characters.charAt(Math.floor(Math.random() * charactersLength));
  98. }
  99. return result;
  100. }
  101. /**
  102. * 时间转换 毫秒=》2019-03-28 19:55:34
  103. * @param milliseconds
  104. * @return {string}
  105. */
  106. static time2str(milliseconds) {
  107. let time = new Date();
  108. time.setTime(milliseconds);
  109. let year = time.getFullYear();
  110. let hours = time.getHours();
  111. let min = time.getMinutes();
  112. let sec = time.getSeconds();
  113. let month = time.getMonth() + 1;
  114. let date = time.getDate();
  115. return year + "-" + month + "-" + date + " " + (hours > 9 ? hours : "0" + hours) + ":" + (min > 9 ? min : "0" + min) + ":" + (sec > 9 ? sec : "0" + sec);
  116. }
  117. /**
  118. * 将毫秒转换为时间格式 00:00:00
  119. * @param delay
  120. * @param detail
  121. * @return
  122. */
  123. static second2time(delay, detail = 2) {
  124. // if (detail == undefined) detail = 2;
  125. //天
  126. let d = Math.floor(delay / 86400000);
  127. delay = delay % 86400000; // 小时
  128. let h = Math.floor(delay / 3600000);
  129. delay = delay % 3600000; // 分钟
  130. let m = Math.floor(delay / 60000);
  131. delay = delay % 60000; // 秒
  132. let s = Math.floor(delay / 1000);
  133. delay = delay % 1000; // 毫秒
  134. let ml = delay;
  135. let result = "";
  136. if (d > 0 && detail > 0) {
  137. result += (d > 9 ? d : "0" + d) + ":"; // "天";
  138. detail--;
  139. }
  140. if (h > 0 && detail > 0) {
  141. result += (h > 9 ? h : "0" + h) + ":"; // "小时";
  142. detail--;
  143. }
  144. if (m > 0 && detail > 0) {
  145. result += (m > 9 ? m : "0" + m) + ":"; // "分钟";
  146. detail--;
  147. }
  148. if (detail > 0) {
  149. result += s > 9 ? s : "0" + s; // "秒";
  150. detail--;
  151. }
  152. if (ml > 0 && detail > 0) {
  153. detail--;
  154. }
  155. return result;
  156. }
  157. static formatMilliseconds(milliseconds) {
  158. const seconds = Math.floor(milliseconds / 1000 % 60);
  159. const minutes = Math.floor(milliseconds / (1000 * 60) % 60);
  160. const hours = Math.floor(milliseconds / (1000 * 60 * 60) % 24); //const formattedSeconds = seconds < 10 ? "0" + seconds : seconds;
  161. //const formattedMinutes = minutes < 10 ? "0" + minutes : minutes;
  162. //const formattedHours = hours < 10 ? "0" + hours : hours;
  163. //`${formattedHours}:${formattedMinutes}:${formattedSeconds}`;
  164. return `${hours.toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")}:${seconds.toString().padStart(2, "0")}`;
  165. }
  166. /**
  167. * 取小数位
  168. * @param decimal 小数
  169. * @param places 位数
  170. * @return {number}
  171. */
  172. static getDecimal(decimal, places) {
  173. let round = Math.pow(10, places);
  174. return Math.round(decimal * round) / round;
  175. }
  176. /**
  177. * 本年第几周
  178. * @param timestamp 毫秒
  179. * @returns
  180. */
  181. static getYearWeek(timestamp) {
  182. // let nowDate: Date = new Date(date);
  183. // let firstDay: Date = new Date(date);
  184. // firstDay.setMonth(0);//设置1月
  185. // firstDay.setDate(1);//设置1号
  186. // let diffDays = Math.ceil((nowDate.valueOf() - firstDay.valueOf())/(24*60*60*1000));
  187. // return Math.ceil((diffDays + (firstDay.getDay() + 1 - 1)) / 7);
  188. let currentDate = new Date(timestamp);
  189. let year = new Date(currentDate.getFullYear(), 0, 1);
  190. let days = Math.floor((currentDate.valueOf() - year.valueOf()) / (24 * 60 * 60 * 1000));
  191. let week = Math.ceil((currentDate.getDay() + 1 + days) / 7);
  192. return week;
  193. }
  194. /**
  195. * 是否是同一天
  196. * @param timeStampA
  197. * @param timeStampB
  198. * @return {boolean}
  199. */
  200. static isSameDay(timeStampA, timeStampB) {
  201. let dateA = new Date(timeStampA);
  202. dateA.setHours(0, 0, 0, 0);
  203. let dateB = new Date(timeStampB);
  204. dateB.setHours(0, 0, 0, 0);
  205. return dateA.getTime() == dateB.getTime() ? true : false;
  206. } //
  207. static checkAndAdd(map, k, v) {
  208. if (map.has(k)) {
  209. map.set(k, map.get(k) + v);
  210. } else {
  211. map.set(k, v);
  212. }
  213. } //
  214. /**
  215. * 随机权重下标
  216. * @param weights 权重数组
  217. */
  218. static randomWeights(weights) {
  219. // 总权重值
  220. const totalWeight = weights.reduce((a, b) => a + b, 0); // 随机的权重值
  221. const randomWeight = this.getRandom(0, totalWeight);
  222. for (let index = 0, weight = 0; index < weights.length; index++) {
  223. weight += weights[index];
  224. if (weight >= randomWeight) {
  225. return index;
  226. }
  227. }
  228. return -1;
  229. }
  230. /**
  231. * 随机数组下标
  232. * @param array 数组
  233. */
  234. /**
  235. * 根据权重随机数组下标
  236. * @param array 数组
  237. * @param weights 权重数组
  238. */
  239. static randomArray(array, weights) {
  240. if (!weights) {
  241. const index = this.getRandom(0, array.length, true);
  242. return index;
  243. }
  244. return this.randomWeights(weights.slice(0, array.length));
  245. }
  246. });
  247. _cclegacy._RF.pop();
  248. _crd = false;
  249. }
  250. };
  251. });
  252. //# sourceMappingURL=d8ea009be5644c7164c4b5ae51cd349e5525e465.js.map