System.register(["cc"], function (_export, _context) { "use strict"; var _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, size, view, UITransform, screen, ImageAsset, SpriteFrame, Texture2D, sys, assetManager, ch_util, _crd; return { setters: [function (_cc) { _cclegacy = _cc.cclegacy; __checkObsolete__ = _cc.__checkObsolete__; __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__; size = _cc.size; view = _cc.view; UITransform = _cc.UITransform; screen = _cc.screen; ImageAsset = _cc.ImageAsset; SpriteFrame = _cc.SpriteFrame; Texture2D = _cc.Texture2D; sys = _cc.sys; assetManager = _cc.assetManager; }], execute: function () { _crd = true; _cclegacy._RF.push({}, "1badftMxAdA7p5/bxrvSF8o", "ch_util", undefined); /**工具*/ __checkObsolete__(['size', 'view', 'Node', 'UITransform', 'screen', 'ImageAsset', 'SpriteFrame', 'Texture2D', 'sys', 'assetManager']); ch_util = class ch_util { constructor() { this._k = 1000; this._k_sizes_en = ['', 'K', 'M', 'G', 'T', 'P', 'E']; this._k_sizes_cn = ['', '千', '百万', '十亿', '万亿', '拍(千万亿)', '艾(十亿亿)']; this._w = 10000; this._w_sizes_en = ['', 'W', 'M', 'B', 'T']; this._w_sizes_cn = ['', '万', '亿', '万亿']; this._url_data = null; } static getInstance() { if (!this._instance) this._instance = new ch_util(); return this._instance; } /** * 随机数 (包含min,不包含max) * @param min * @param max * @param isInt * @return {*} */ getRandom(min = 0, max = 1) { if (min == null) min = 0; if (max == null) max = 1; if (min === max) return min; return min + Math.random() * (max - min); } /** * 随机整数-不包含最大值 * @param min * @param max * @return {*} */ getRandomInt(min, max) { min = Math.ceil(min); max = Math.ceil(max); return Math.floor(Math.random() * (max - min)) + min; } /** 生成随机整数 -1 或 1*/ getRandomDir() { return Math.floor(Math.random() * 2) === 0 ? -1 : 1; } /** * 在指定数组中随机取出N个不重复的数据 * @param resArr * @param ranNum * @returns {Array} */ getRandomDiffValueFromArr(resArr, ranNum) { let arr = new Array(); let result = new Array(); if (!resArr || resArr.length <= 0 || ranNum <= 0) { return result; } for (let i = 0; i < resArr.length; i++) { arr.push(resArr[i]); } if (ranNum >= arr.length) return arr; ranNum = Math.min(ranNum, arr.length - 1); for (let i = 0; i < ranNum; i++) { let ran = this.getRandomInt(0, arr.length - 1); result.push(arr.splice(ran, 1)[0]); } return result; } /** * 取小数位 * @param decimal 小数 * @param places 位数 * @return {number} */ numberToDecimal(decimal, places) { let round = Math.pow(10, places); return Math.round(decimal * round) / round; } parse(text, reciver) { try { return JSON.parse(text, reciver); } catch (error) { //ch_log.error(error); return null; } } stringify(value, replacer, space) { try { return JSON.stringify(value, replacer, space); } catch (error) { return null; } } /** * 判断字符是否为双字节字符(如中文字符) * @param string 原字符串 */ str_isDoubleWord(string) { return /[^\x00-\xff]/.test(string); } /** * 是否为空 * @param str */ str_isEmpty(str) { if (str == null || str == undefined || str.length == 0) { return true; } return false; } /** * 转美式计数字符串 * @param value 数字 * @example * 123456789 = 123,456,789 */ numberTotPermil(value) { return value.toLocaleString(); } /** * 通用单位转换方法 */ convertNumber(value, base, sizes, fixed) { if (value < base) return value.toString(); const i = Math.floor(Math.log(value) / Math.log(base)); const r = value / Math.pow(base, i); if (i >= sizes.length) return value.toString(); return `${r.toFixed(fixed)}${sizes[i]}`; } /** * 转单位计数(默认英文) * @param value 数字 * @param fixed 保留小数位数 * @param isEn 是否英文 * @example * 12345 = 12.35K */ numberToThousand(value, fixed = 2, isEn = true) { const sizes = isEn ? this._k_sizes_en : this._k_sizes_cn; return this.convertNumber(value, this._k, sizes, fixed); } /** * 转单位计数(默认中文) * @param value 数字 * @param fixed 保留小数位数 * @param isEn 是否英文 * @example * 12345 = 1.23万 */ numberToTenThousand(value, fixed = 2, isEn = false) { const sizes = isEn ? this._w_sizes_en : this._w_sizes_cn; return this.convertNumber(value, this._w, sizes, fixed); } /**获取一个唯一标识的字符串 */ guid() { let guid = Math.random().toString(36).substring(2); return guid; } /**排序一个json Object*/ obj_sort(obj) { let sorted_keys = Object.keys(obj).sort(); let new_obj = {}; for (let i = 0; i < sorted_keys.length; i++) { const key = sorted_keys[i]; const value = obj[key]; const t = this.obj_get_value_type(value); new_obj[key] = t == 'object' ? this.obj_sort(value) : value; } return new_obj; } /**获取一个josn值的类型*/ obj_get_value_type(value) { let type; if (value === null) { type = 'null'; } else { type = typeof value; } // 如果是对象或数组,还可以进一步判断 if (type === 'object') { if (Array.isArray(value)) { type = 'array'; } //else if (value instanceof Date) { type = 'date'; } } return type; } /** * 判断指定的值是否为对象 * @param value 值 */ valueIsObject(value) { return Object.prototype.toString.call(value) === '[object Object]'; } /** * 深拷贝 * @param target 目标 */ /** 克隆对象 */ obj_clone(target_, record_set = new Set()) { let result; switch (typeof target_) { case "object": { // 数组:遍历拷贝 if (Array.isArray(target_)) { if (record_set.has(target_)) { return target_; } record_set.add(target_); result = []; for (let k_n = 0; k_n < target_.length; ++k_n) { // 递归克隆数组中的每一项 result.push(this.obj_clone(target_[k_n], record_set)); } } // null:直接赋值 else if (target_ === null) { result = null; } // RegExp:直接赋值 else if (target_.constructor === RegExp) { result = target_; } // 普通对象:循环递归赋值对象的所有值 else { if (record_set.has(target_)) { return target_; } record_set.add(target_); result = {}; for (const k_s in target_) { result[k_s] = this.obj_clone(target_[k_s], record_set); } } break; } case "function": { result = target_.bind({}); break; } default: { result = target_; } } return result; } /** * 拷贝对象 * @param target 目标 */ copy(target) { return this.parse(this.stringify(target)); } //请求相关-------------------------------------------- getReqId() { return Date.now() + "_" + Math.ceil(1e3 * Math.random()); } /**获取链接中传入的某个值*/ getUrlData(key) { if (!this._url_data) { this._url_data = this.parseUrl(); } const value = this._url_data[key]; if (value === undefined) return null; if (typeof value === 'string') { const numberValue = parseFloat(value); if (!isNaN(numberValue)) return numberValue; return value; } return null; } parseUrl() { if (!sys.isBrowser || typeof window !== "object" || !window.document) return {}; const url = window.document.location.href; const queryString = url.split("?")[1]; if (!queryString) return {}; return queryString.split("&").reduce((acc, param) => { const [key, value] = param.split("="); if (key) acc[decodeURIComponent(key)] = value ? decodeURIComponent(value) : ''; return acc; }, {}); } /**获到node的坐标和范围用于平台在对应位置创建按纽*/ getBtnOp(btnNode) { let btnNodeUiTransform = btnNode.getComponent(UITransform); const btnSize = size(btnNodeUiTransform.width + 0, btnNodeUiTransform.height + 0); //let frameWidth = screen.windowSize.width / screen.devicePixelRatio let frameHeight = screen.windowSize.height / screen.devicePixelRatio; //const winSize = screen.windowSize; //const designSize = view.getDesignResolutionSize(); //console.log('designSize', designSize); //console.log('winSize:', winSize); //console.log('frameSize:', frameWidth,frameHeight); //适配不同机型来创建微信授权按钮 let rect = btnNodeUiTransform.getBoundingBoxToWorld(); let ratio = screen.devicePixelRatio; let scale = view.getScaleX(); let factor = scale / ratio; let offsetX = 0; let offsetY = 0; let top = frameHeight - (rect.y + rect.height) * factor - offsetY; let left = rect.x * factor + offsetX; const width = btnSize.width * factor; const height = btnSize.height * factor; return { left: left, top: top, width: width, height: height }; } /**远程加载图片*/ async loadImage(url) { if (chsdk.get_pf() == chsdk.pf.web) { return await this.loadRemoteSprite(url); } else { const idata = await chsdk.loadImage(url); return this.getSpriteFrame(idata); } } /**cocos加载远程图片*/ loadRemoteSprite(remoteUrl, ext = '.png') { return new Promise(resolve => { assetManager.loadRemote(remoteUrl, { ext: ext }, function (err, imageAsset) { const spriteFrame = new SpriteFrame(); const texture = new Texture2D(); texture.image = imageAsset; spriteFrame.texture = texture; resolve(spriteFrame); }); }); } /**远程加载的图片数据转成spriteFrame*/ getSpriteFrame(img) { if (!img) return null; try { const spriteFrame = new SpriteFrame(); const texture = new Texture2D(); texture.image = img instanceof ImageAsset ? img : new ImageAsset(img); spriteFrame.texture = texture; return spriteFrame; } catch (error) { //ch_log.warn(error); return null; } } }; ch_util._instance = void 0; _export("default", ch_util.getInstance()); _cclegacy._RF.pop(); _crd = false; } }; }); //# sourceMappingURL=d6ec18d6ea05ecc84f5191004e3fcc26f59079b4.js.map