5ad9214371678a1d97027b6cf28752dcbab25e0f.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. System.register(["cc"], function (_export, _context) {
  2. "use strict";
  3. var _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, size, view, UITransform, screen, ImageAsset, SpriteFrame, Texture2D, sys, assetManager, ch_util, _crd;
  4. return {
  5. setters: [function (_cc) {
  6. _cclegacy = _cc.cclegacy;
  7. __checkObsolete__ = _cc.__checkObsolete__;
  8. __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
  9. size = _cc.size;
  10. view = _cc.view;
  11. UITransform = _cc.UITransform;
  12. screen = _cc.screen;
  13. ImageAsset = _cc.ImageAsset;
  14. SpriteFrame = _cc.SpriteFrame;
  15. Texture2D = _cc.Texture2D;
  16. sys = _cc.sys;
  17. assetManager = _cc.assetManager;
  18. }],
  19. execute: function () {
  20. _crd = true;
  21. _cclegacy._RF.push({}, "1badftMxAdA7p5/bxrvSF8o", "ch_util", undefined);
  22. /**工具*/
  23. __checkObsolete__(['size', 'view', 'Node', 'UITransform', 'screen', 'ImageAsset', 'SpriteFrame', 'Texture2D', 'sys', 'assetManager']);
  24. ch_util = class ch_util {
  25. constructor() {
  26. this._k = 1000;
  27. this._k_sizes_en = ['', 'K', 'M', 'G', 'T', 'P', 'E'];
  28. this._k_sizes_cn = ['', '千', '百万', '十亿', '万亿', '拍(千万亿)', '艾(十亿亿)'];
  29. this._w = 10000;
  30. this._w_sizes_en = ['', 'W', 'M', 'B', 'T'];
  31. this._w_sizes_cn = ['', '万', '亿', '万亿'];
  32. this._url_data = null;
  33. }
  34. static getInstance() {
  35. if (!this._instance) this._instance = new ch_util();
  36. return this._instance;
  37. }
  38. /**
  39. * 随机数 (包含min,不包含max)
  40. * @param min
  41. * @param max
  42. * @param isInt
  43. * @return {*}
  44. */
  45. getRandom(min = 0, max = 1) {
  46. if (min == null) min = 0;
  47. if (max == null) max = 1;
  48. if (min === max) return min;
  49. return min + Math.random() * (max - min);
  50. }
  51. /**
  52. * 随机整数-不包含最大值
  53. * @param min
  54. * @param max
  55. * @return {*}
  56. */
  57. getRandomInt(min, max) {
  58. min = Math.ceil(min);
  59. max = Math.ceil(max);
  60. return Math.floor(Math.random() * (max - min)) + min;
  61. }
  62. /** 生成随机整数 -1 或 1*/
  63. getRandomDir() {
  64. return Math.floor(Math.random() * 2) === 0 ? -1 : 1;
  65. }
  66. /**
  67. * 在指定数组中随机取出N个不重复的数据
  68. * @param resArr
  69. * @param ranNum
  70. * @returns {Array}
  71. */
  72. getRandomDiffValueFromArr(resArr, ranNum) {
  73. let arr = new Array();
  74. let result = new Array();
  75. if (!resArr || resArr.length <= 0 || ranNum <= 0) {
  76. return result;
  77. }
  78. for (let i = 0; i < resArr.length; i++) {
  79. arr.push(resArr[i]);
  80. }
  81. if (ranNum >= arr.length) return arr;
  82. ranNum = Math.min(ranNum, arr.length - 1);
  83. for (let i = 0; i < ranNum; i++) {
  84. let ran = this.getRandomInt(0, arr.length - 1);
  85. result.push(arr.splice(ran, 1)[0]);
  86. }
  87. return result;
  88. }
  89. /**
  90. * 取小数位
  91. * @param decimal 小数
  92. * @param places 位数
  93. * @return {number}
  94. */
  95. numberToDecimal(decimal, places) {
  96. let round = Math.pow(10, places);
  97. return Math.round(decimal * round) / round;
  98. }
  99. parse(text, reciver) {
  100. try {
  101. return JSON.parse(text, reciver);
  102. } catch (error) {
  103. //ch_log.error(error);
  104. return null;
  105. }
  106. }
  107. stringify(value, replacer, space) {
  108. try {
  109. return JSON.stringify(value, replacer, space);
  110. } catch (error) {
  111. return null;
  112. }
  113. }
  114. /**
  115. * 判断字符是否为双字节字符(如中文字符)
  116. * @param string 原字符串
  117. */
  118. str_isDoubleWord(string) {
  119. return /[^\x00-\xff]/.test(string);
  120. }
  121. /**
  122. * 是否为空
  123. * @param str
  124. */
  125. str_isEmpty(str) {
  126. if (str == null || str == undefined || str.length == 0) {
  127. return true;
  128. }
  129. return false;
  130. }
  131. /**
  132. * 转美式计数字符串
  133. * @param value 数字
  134. * @example
  135. * 123456789 = 123,456,789
  136. */
  137. numberTotPermil(value) {
  138. return value.toLocaleString();
  139. }
  140. /**
  141. * 通用单位转换方法
  142. */
  143. convertNumber(value, base, sizes, fixed) {
  144. if (value < base) return value.toString();
  145. const i = Math.floor(Math.log(value) / Math.log(base));
  146. const r = value / Math.pow(base, i);
  147. if (i >= sizes.length) return value.toString();
  148. return `${r.toFixed(fixed)}${sizes[i]}`;
  149. }
  150. /**
  151. * 转单位计数(默认英文)
  152. * @param value 数字
  153. * @param fixed 保留小数位数
  154. * @param isEn 是否英文
  155. * @example
  156. * 12345 = 12.35K
  157. */
  158. numberToThousand(value, fixed = 2, isEn = true) {
  159. const sizes = isEn ? this._k_sizes_en : this._k_sizes_cn;
  160. return this.convertNumber(value, this._k, sizes, fixed);
  161. }
  162. /**
  163. * 转单位计数(默认中文)
  164. * @param value 数字
  165. * @param fixed 保留小数位数
  166. * @param isEn 是否英文
  167. * @example
  168. * 12345 = 1.23万
  169. */
  170. numberToTenThousand(value, fixed = 2, isEn = false) {
  171. const sizes = isEn ? this._w_sizes_en : this._w_sizes_cn;
  172. return this.convertNumber(value, this._w, sizes, fixed);
  173. }
  174. /**获取一个唯一标识的字符串 */
  175. guid() {
  176. let guid = Math.random().toString(36).substring(2);
  177. return guid;
  178. }
  179. /**排序一个json Object*/
  180. obj_sort(obj) {
  181. let sorted_keys = Object.keys(obj).sort();
  182. let new_obj = {};
  183. for (let i = 0; i < sorted_keys.length; i++) {
  184. const key = sorted_keys[i];
  185. const value = obj[key];
  186. const t = this.obj_get_value_type(value);
  187. new_obj[key] = t == 'object' ? this.obj_sort(value) : value;
  188. }
  189. return new_obj;
  190. }
  191. /**获取一个josn值的类型*/
  192. obj_get_value_type(value) {
  193. let type;
  194. if (value === null) {
  195. type = 'null';
  196. } else {
  197. type = typeof value;
  198. } // 如果是对象或数组,还可以进一步判断
  199. if (type === 'object') {
  200. if (Array.isArray(value)) {
  201. type = 'array';
  202. } //else if (value instanceof Date) { type = 'date'; }
  203. }
  204. return type;
  205. }
  206. /**
  207. * 判断指定的值是否为对象
  208. * @param value 值
  209. */
  210. valueIsObject(value) {
  211. return Object.prototype.toString.call(value) === '[object Object]';
  212. }
  213. /**
  214. * 深拷贝
  215. * @param target 目标
  216. */
  217. /** 克隆对象 */
  218. obj_clone(target_, record_set = new Set()) {
  219. let result;
  220. switch (typeof target_) {
  221. case "object":
  222. {
  223. // 数组:遍历拷贝
  224. if (Array.isArray(target_)) {
  225. if (record_set.has(target_)) {
  226. return target_;
  227. }
  228. record_set.add(target_);
  229. result = [];
  230. for (let k_n = 0; k_n < target_.length; ++k_n) {
  231. // 递归克隆数组中的每一项
  232. result.push(this.obj_clone(target_[k_n], record_set));
  233. }
  234. } // null:直接赋值
  235. else if (target_ === null) {
  236. result = null;
  237. } // RegExp:直接赋值
  238. else if (target_.constructor === RegExp) {
  239. result = target_;
  240. } // 普通对象:循环递归赋值对象的所有值
  241. else {
  242. if (record_set.has(target_)) {
  243. return target_;
  244. }
  245. record_set.add(target_);
  246. result = {};
  247. for (const k_s in target_) {
  248. result[k_s] = this.obj_clone(target_[k_s], record_set);
  249. }
  250. }
  251. break;
  252. }
  253. case "function":
  254. {
  255. result = target_.bind({});
  256. break;
  257. }
  258. default:
  259. {
  260. result = target_;
  261. }
  262. }
  263. return result;
  264. }
  265. /**
  266. * 拷贝对象
  267. * @param target 目标
  268. */
  269. copy(target) {
  270. return this.parse(this.stringify(target));
  271. } //请求相关--------------------------------------------
  272. getReqId() {
  273. return Date.now() + "_" + Math.ceil(1e3 * Math.random());
  274. }
  275. /**获取链接中传入的某个值*/
  276. getUrlData(key) {
  277. if (!this._url_data) {
  278. this._url_data = this.parseUrl();
  279. }
  280. const value = this._url_data[key];
  281. if (value === undefined) return null;
  282. if (typeof value === 'string') {
  283. const numberValue = parseFloat(value);
  284. if (!isNaN(numberValue)) return numberValue;
  285. return value;
  286. }
  287. return null;
  288. }
  289. parseUrl() {
  290. if (!sys.isBrowser || typeof window !== "object" || !window.document) return {};
  291. const url = window.document.location.href;
  292. const queryString = url.split("?")[1];
  293. if (!queryString) return {};
  294. return queryString.split("&").reduce((acc, param) => {
  295. const [key, value] = param.split("=");
  296. if (key) acc[decodeURIComponent(key)] = value ? decodeURIComponent(value) : '';
  297. return acc;
  298. }, {});
  299. }
  300. /**获到node的坐标和范围用于平台在对应位置创建按纽*/
  301. getBtnOp(btnNode) {
  302. let btnNodeUiTransform = btnNode.getComponent(UITransform);
  303. const btnSize = size(btnNodeUiTransform.width + 0, btnNodeUiTransform.height + 0); //let frameWidth = screen.windowSize.width / screen.devicePixelRatio
  304. let frameHeight = screen.windowSize.height / screen.devicePixelRatio; //const winSize = screen.windowSize;
  305. //const designSize = view.getDesignResolutionSize();
  306. //console.log('designSize', designSize);
  307. //console.log('winSize:', winSize);
  308. //console.log('frameSize:', frameWidth,frameHeight);
  309. //适配不同机型来创建微信授权按钮
  310. let rect = btnNodeUiTransform.getBoundingBoxToWorld();
  311. let ratio = screen.devicePixelRatio;
  312. let scale = view.getScaleX();
  313. let factor = scale / ratio;
  314. let offsetX = 0;
  315. let offsetY = 0;
  316. let top = frameHeight - (rect.y + rect.height) * factor - offsetY;
  317. let left = rect.x * factor + offsetX;
  318. const width = btnSize.width * factor;
  319. const height = btnSize.height * factor;
  320. return {
  321. left: left,
  322. top: top,
  323. width: width,
  324. height: height
  325. };
  326. }
  327. /**远程加载图片*/
  328. async loadImage(url) {
  329. if (chsdk.get_pf() == chsdk.pf.web) {
  330. return await this.loadRemoteSprite(url);
  331. } else {
  332. const idata = await chsdk.loadImage(url);
  333. return this.getSpriteFrame(idata);
  334. }
  335. }
  336. /**cocos加载远程图片*/
  337. loadRemoteSprite(remoteUrl, ext = '.png') {
  338. return new Promise(resolve => {
  339. assetManager.loadRemote(remoteUrl, {
  340. ext: ext
  341. }, function (err, imageAsset) {
  342. const spriteFrame = new SpriteFrame();
  343. const texture = new Texture2D();
  344. texture.image = imageAsset;
  345. spriteFrame.texture = texture;
  346. resolve(spriteFrame);
  347. });
  348. });
  349. }
  350. /**远程加载的图片数据转成spriteFrame*/
  351. getSpriteFrame(img) {
  352. if (!img) return null;
  353. try {
  354. const spriteFrame = new SpriteFrame();
  355. const texture = new Texture2D();
  356. texture.image = img instanceof ImageAsset ? img : new ImageAsset(img);
  357. spriteFrame.texture = texture;
  358. return spriteFrame;
  359. } catch (error) {
  360. //ch_log.warn(error);
  361. return null;
  362. }
  363. }
  364. };
  365. ch_util._instance = void 0;
  366. _export("default", ch_util.getInstance());
  367. _cclegacy._RF.pop();
  368. _crd = false;
  369. }
  370. };
  371. });
  372. //# sourceMappingURL=5ad9214371678a1d97027b6cf28752dcbab25e0f.js.map