"use strict"; cc._RF.push(module, 'a8b34F/S8pLEoCEmq+ivC4f', 'Utils'); // Script/sdk/tools/Utils.ts "use strict"; // Learn TypeScript: // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html // Learn Attribute: // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; Object.defineProperty(exports, "__esModule", { value: true }); var EncryptUtil_1 = require("./EncryptUtil"); var SDK_1 = require("../SDK"); var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property; var Utils = /** @class */ (function (_super) { __extends(Utils, _super); function Utils() { var _this = _super !== null && _super.apply(this, arguments) || this; _this._isLoaded = false; // private _configUrl = "http://c.zhickeji.com:14000/gts-service/api/terminal/"; // private _reportUrl = "http://cl.zhickeji.com:14000/gts-service/api/terminal/"; // private _url = ["https://z.zhickeji.com/api/t/", "https://c.xianyouxx.com/api/xy/","https://ch.chumikj.com/ter/"]; _this._url = ["", "", "https://ch.chumikj.com/ter/"]; _this._isComplete = false; _this._listener = []; return _this; // update (dt) {} } Utils_1 = Utils; Object.defineProperty(Utils, "instance", { get: function () { if (Utils_1._instance == null) { Utils_1._instance = new Utils_1(); } return Utils_1._instance; }, enumerable: false, configurable: true }); Utils.prototype.log = function (log) { if (this._config) { if (this._config.logEnable) { console.log(log); } } }; /**随机两数之前的值 */ Utils.prototype.getRandomInt = function (min, max) { return Math.floor(Math.random() * Math.floor(max - min + 1)) + min; }; Utils.prototype.loadPrefabs = function (path, callbacks) { this.log("**********loadPrefabs " + path); var self = this; cc.loader.loadRes(path, function (err, loadedResource) { if (err) { cc.error(err.message || err); self.loadPrefabs(path, callbacks); return; } callbacks(loadedResource); }); }; /**post请求 */ Utils.prototype.sendPost = function (url, body) { var hasCallBack = false; var curUrl = this._url[SDK_1.default.Instance.edType] + url; this._isComplete = false; var u = ["c", "gg", "cnf"]; if (url == u[SDK_1.default.Instance.edType]) { hasCallBack = true; curUrl = this._url[SDK_1.default.Instance.edType] + url; } this.log("**********net url is: " + curUrl); this.log("**********body: " + body); if (curUrl == null) { this.log("**********net url is null: " + curUrl); return; } body = EncryptUtil_1.EncryptUtil.en(body); this.log("**********bodyEncrypt : " + body); var self = this; var req = new XMLHttpRequest(); var onReadyStateChange = function (ev) { if (req.readyState == 4) { if (req.status == 200) { self.log("ok: " + req.response); var reqR = EncryptUtil_1.EncryptUtil.de(req.response); self.log("reqR ok: " + reqR); var obj = JSON.parse(reqR); if (obj) { if (self._isComplete || !hasCallBack) { return; } self._isComplete = true; self.log("reqR ok: " + obj); self.ok(obj); } } else { self.ok(null); } } }; var onTimeOut = function (ev) { }; var onError = function (ev) { }; req.onreadystatechange = onReadyStateChange; req.ontimeout = onTimeOut; req.onerror = onError; req.open("POST", curUrl, true); req.setRequestHeader("Content-Type", "application/json"); req.send(body); }; /** * 加载完成 * * @param config 配置 */ Utils.prototype.ok = function (config) { this._config = config; if (config != null) { this._isLoaded = true; } if (this._listener.length > 0) { this._listener.forEach(function (lst, index) { if (lst != null) { lst.onOk(config); } }); } }; /** * 添加一个监听器 * * @param listener 监听器 */ Utils.prototype.addListener = function (listener) { this._listener.push(listener); }; /** * 是否已经加载 */ Utils.prototype.isLoaded = function () { return this._isLoaded; }; var Utils_1; Utils._instance = null; Utils = Utils_1 = __decorate([ ccclass ], Utils); return Utils; }(cc.Component)); exports.default = Utils; cc._RF.pop();