"use strict"; cc._RF.push(module, '2ff3fz408tHv4n7G8zhfSLl', 'ConfigLoader'); // Scripts/erolabs/ConfigLoader.ts "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConfigLoader = void 0; var Result_1 = require("./Result"); var DomainJson = /** @class */ (function () { function DomainJson() { } return DomainJson; }()); var ConfigLoader = /** @class */ (function () { function ConfigLoader() { } ConfigLoader.prototype.initialize = function (callback) { this.onConfigLoaderInitializeCallback = callback; this.loadLocalConfig(); }; ConfigLoader.prototype.loadLocalConfig = function () { var _this = this; cc.loader.loadRes(ConfigLoader.configFilename, function (err, res) { console.log("加载成功返回" + JSON.stringify(res)); _this.parseDomainJson(res); _this.updateConfig(); }); }; ConfigLoader.prototype.parseDomainJson = function (jsonText) { this.domainJson = JSON.parse(jsonText); }; ConfigLoader.prototype.updateConfig = function () { if (this.domainJson.config.length == 0) { this.onConfigLoaderInitializeCallback(false, new Result_1.Config()); return; } var url = this.domainJson.config.shift(); // 取出第一個元素 this.httpGet(url, this.onConfigDownloaded.bind(this)); }; ConfigLoader.prototype.onConfigDownloaded = function (isError, text) { if (isError) { this.updateConfig(); return; } // 下載到新的domain_config,保留起來 cc.sys.localStorage.setItem(ConfigLoader.configFilename, text); this.parseDomainJson(text); this.findLoginDomain(); }; ConfigLoader.prototype.findLoginDomain = function () { if (this.domainJson.login.length == 0) { if (this.onConfigLoaderInitializeCallback) this.onConfigLoaderInitializeCallback(false, new Result_1.Config()); return; } var url = this.domainJson.login.shift(); // 取出第一個元素 this.config = new Result_1.Config(); this.config.Domain = url; this.config.ApiDomain = url + "/api"; console.log("当前的配置相关" + JSON.stringify(this.domainJson)); this.config.PaymentDomain = this.domainJson.payment; // this.config.Contack = this.domainJson.contack; // this.config.TGroup = this.domainJson.group; // this.config.FreeEcoin = this.domainJson.ecoin; this.httpGet(url + "/api/checkLoginUrl", this.onLoginDomainResponse.bind(this)); }; ConfigLoader.prototype.onLoginDomainResponse = function (isError, text) { if (isError) { this.findLoginDomain(); return; } // 確認login domain 可以正常使用 if (this.onConfigLoaderInitializeCallback) this.onConfigLoaderInitializeCallback(true, this.config); }; ConfigLoader.prototype.httpGet = function (url, callback) { //console.log("[httpGet]" + url) var xhr = cc.loader.getXMLHttpRequest(); xhr.onreadystatechange = function () { if (xhr.readyState !== 4) return; if (xhr.status >= 200 && xhr.status < 300) { callback(false, xhr.responseText); } else { callback(true, ""); } }; xhr.onerror = function () { callback(true, ""); }; xhr.open("GET", url, true); xhr.send(); }; ConfigLoader.configFilename = "coresdk_domain_v1.txt"; return ConfigLoader; }()); exports.ConfigLoader = ConfigLoader; cc._RF.pop();