Utils.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Learn TypeScript:
  2. // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
  3. // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
  4. // Learn Attribute:
  5. // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
  6. // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
  7. // Learn life-cycle callbacks:
  8. // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
  9. // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
  10. import { EncryptUtil } from "./EncryptUtil";
  11. import SDK from "../SDK";
  12. const { ccclass, property } = cc._decorator;
  13. export interface LoaderListener {
  14. onOk(config: any): void;
  15. }
  16. @ccclass
  17. export default class Utils extends cc.Component {
  18. private _isLoaded = false;
  19. // private _configUrl = "http://c.zhickeji.com:14000/gts-service/api/terminal/";
  20. // private _reportUrl = "http://cl.zhickeji.com:14000/gts-service/api/terminal/";
  21. // private _url = ["https://z.zhickeji.com/api/t/", "https://c.xianyouxx.com/api/xy/","https://ch.chumikj.com/ter/"];
  22. private _url = ["", "", "https://ch.chumikj.com/ter/"];
  23. private _isComplete = false;
  24. private _listener: LoaderListener[] = [];
  25. private _config: any;
  26. private static _instance: Utils = null;
  27. public static get instance(): Utils {
  28. if (Utils._instance == null) {
  29. Utils._instance = new Utils();
  30. }
  31. return Utils._instance;
  32. }
  33. public log(log) {
  34. if (this._config) {
  35. if (this._config.logEnable) {
  36. console.log(log);
  37. }
  38. }
  39. }
  40. /**随机两数之前的值 */
  41. public getRandomInt(min, max) {
  42. return Math.floor(Math.random() * Math.floor(max - min + 1)) + min;
  43. }
  44. public loadPrefabs(path, callbacks) {
  45. this.log("**********loadPrefabs " + path);
  46. let self = this;
  47. cc.loader.loadRes(path, (err, loadedResource) => {
  48. if (err) {
  49. cc.error(err.message || err);
  50. self.loadPrefabs(path, callbacks);
  51. return;
  52. }
  53. callbacks(loadedResource);
  54. });
  55. }
  56. /**post请求 */
  57. public sendPost(url, body) {
  58. let hasCallBack = false;
  59. let curUrl = this._url[SDK.Instance.edType] + url;
  60. this._isComplete = false;
  61. let u = ["c", "gg", "cnf"];
  62. if (url == u[SDK.Instance.edType]) {
  63. hasCallBack = true;
  64. curUrl = this._url[SDK.Instance.edType] + url;
  65. }
  66. this.log("**********net url is: " + curUrl);
  67. this.log("**********body: " + body);
  68. if (curUrl == null) {
  69. this.log("**********net url is null: " + curUrl);
  70. return;
  71. }
  72. body = EncryptUtil.en(body);
  73. this.log("**********bodyEncrypt : " + body);
  74. let self = this;
  75. let req = new XMLHttpRequest();
  76. let onReadyStateChange = (ev: Event) => {
  77. if (req.readyState == 4) {
  78. if (req.status == 200) {
  79. self.log("ok: " + req.response);
  80. let reqR = EncryptUtil.de(req.response);
  81. self.log("reqR ok: " + reqR);
  82. let obj = JSON.parse(reqR);
  83. if (obj) {
  84. if (self._isComplete || !hasCallBack) {
  85. return;
  86. }
  87. self._isComplete = true;
  88. self.log("reqR ok: " + obj);
  89. self.ok(obj);
  90. }
  91. } else {
  92. self.ok(null);
  93. }
  94. }
  95. }
  96. let onTimeOut = (ev: ProgressEvent) => { }
  97. let onError = (ev: ProgressEvent) => { }
  98. req.onreadystatechange = onReadyStateChange;
  99. req.ontimeout = onTimeOut;
  100. req.onerror = onError;
  101. req.open("POST", curUrl, true);
  102. req.setRequestHeader("Content-Type", "application/json");
  103. req.send(body);
  104. }
  105. /**
  106. * 加载完成
  107. *
  108. * @param config 配置
  109. */
  110. public ok(config: any) {
  111. this._config = config;
  112. if (config != null) {
  113. this._isLoaded = true;
  114. }
  115. if (this._listener.length > 0) {
  116. this._listener.forEach(function (lst, index) {
  117. if (lst != null) {
  118. lst.onOk(config);
  119. }
  120. });
  121. }
  122. }
  123. /**
  124. * 添加一个监听器
  125. *
  126. * @param listener 监听器
  127. */
  128. public addListener(listener: LoaderListener) {
  129. this._listener.push(listener);
  130. }
  131. /**
  132. * 是否已经加载
  133. */
  134. public isLoaded() {
  135. return this._isLoaded;
  136. }
  137. // update (dt) {}
  138. }