116 lines
3.3 KiB
TypeScript
116 lines
3.3 KiB
TypeScript
|
|
import { Config } from './Result';
|
||
|
|
|
||
|
|
class DomainJson {
|
||
|
|
public config: [];
|
||
|
|
public login: [];
|
||
|
|
public payment: string;
|
||
|
|
public contack: string;
|
||
|
|
public ecoin: string;
|
||
|
|
public group: string;
|
||
|
|
}
|
||
|
|
|
||
|
|
export class ConfigLoader {
|
||
|
|
public static configFilename: string = "coresdk_domain_v1.txt";
|
||
|
|
|
||
|
|
onConfigLoaderInitializeCallback: (isSuccess: boolean, result: Config) => void;
|
||
|
|
domainJson: DomainJson;
|
||
|
|
config: Config;
|
||
|
|
|
||
|
|
public initialize(callback: (isSuccess: boolean, result: Config) => void): void {
|
||
|
|
this.onConfigLoaderInitializeCallback = callback;
|
||
|
|
|
||
|
|
this.loadLocalConfig();
|
||
|
|
}
|
||
|
|
|
||
|
|
loadLocalConfig(): void {
|
||
|
|
cc.loader.loadRes(ConfigLoader.configFilename, (err, res) =>
|
||
|
|
{
|
||
|
|
console.log( "加载成功返回" + JSON.stringify( res ) );
|
||
|
|
|
||
|
|
this.parseDomainJson(res);
|
||
|
|
this.updateConfig();
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
parseDomainJson(jsonText: string): void {
|
||
|
|
this.domainJson = JSON.parse(jsonText);
|
||
|
|
}
|
||
|
|
|
||
|
|
updateConfig(): void {
|
||
|
|
if (this.domainJson.config.length == 0) {
|
||
|
|
this.onConfigLoaderInitializeCallback(false, new Config());
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
var url = this.domainJson.config.shift(); // 取出第一個元素
|
||
|
|
this.httpGet(url, this.onConfigDownloaded.bind(this));
|
||
|
|
}
|
||
|
|
|
||
|
|
onConfigDownloaded(isError: boolean, text: string): void {
|
||
|
|
if (isError) {
|
||
|
|
this.updateConfig();
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 下載到新的domain_config,保留起來
|
||
|
|
cc.sys.localStorage.setItem(ConfigLoader.configFilename, text);
|
||
|
|
this.parseDomainJson(text);
|
||
|
|
this.findLoginDomain();
|
||
|
|
}
|
||
|
|
|
||
|
|
findLoginDomain(): void {
|
||
|
|
if (this.domainJson.login.length == 0) {
|
||
|
|
if (this.onConfigLoaderInitializeCallback)
|
||
|
|
this.onConfigLoaderInitializeCallback(false, new Config());
|
||
|
|
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
var url = this.domainJson.login.shift(); // 取出第一個元素
|
||
|
|
|
||
|
|
this.config = new 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));
|
||
|
|
}
|
||
|
|
|
||
|
|
onLoginDomainResponse(isError: boolean, text: string): void {
|
||
|
|
if (isError) {
|
||
|
|
this.findLoginDomain();
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 確認login domain 可以正常使用
|
||
|
|
if (this.onConfigLoaderInitializeCallback)
|
||
|
|
this.onConfigLoaderInitializeCallback(true, this.config);
|
||
|
|
}
|
||
|
|
|
||
|
|
httpGet(url: string, callback: (isError: boolean, responseText: string) => void) {
|
||
|
|
//console.log("[httpGet]" + url)
|
||
|
|
|
||
|
|
var xhr = cc.loader.getXMLHttpRequest();
|
||
|
|
xhr.onreadystatechange = () => {
|
||
|
|
if (xhr.readyState !== 4)
|
||
|
|
return;
|
||
|
|
|
||
|
|
if (xhr.status >= 200 && xhr.status < 300) {
|
||
|
|
callback(false, xhr.responseText);
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
callback(true, "");
|
||
|
|
}
|
||
|
|
};
|
||
|
|
xhr.onerror = () => {
|
||
|
|
callback(true, "");
|
||
|
|
};
|
||
|
|
xhr.open("GET", url, true);
|
||
|
|
xhr.send();
|
||
|
|
}
|
||
|
|
}
|