weHttp.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. // @ts-nocheck
  2. import { NetworkMgr } from "./weNetworkMgr";
  3. import tyqSdkConfig from "./tyq-sdk-config";
  4. class http {
  5. ////////////////////////////
  6. // 类成员
  7. ///////////////////////////
  8. public static readonly _instance = new http();
  9. private _timeOut = 10 * 1000;
  10. private _httpUrl = tyqSdkConfig.server;
  11. ////////////////////////////
  12. // get、set构造器
  13. ///////////////////////////
  14. public set timeOut(time: number) {
  15. this._timeOut = time;
  16. }
  17. public set httpUrl(url: string) {
  18. this._httpUrl = url;
  19. }
  20. ////////////////////////////
  21. // 构造函数
  22. ///////////////////////////
  23. public constructor() {
  24. }
  25. public init(httpUrl: string) {
  26. this._httpUrl = httpUrl;
  27. }
  28. /**
  29. * 发送请求
  30. * @param route 注册事件,通过消息机制返回服务端回调的信息
  31. * @param msg 请求数据
  32. * @param mid
  33. */
  34. public post(route: string, msg: Object, mid?: number) {
  35. //计算请求时间//////
  36. // let time = new Date().getTime();
  37. ////////////
  38. var dataStr = "";
  39. if (msg) {
  40. dataStr = this.createSign(msg);
  41. }
  42. let xhr = new XMLHttpRequest();//cc.loader.getXMLHttpRequest();
  43. // 超时时间1s,单位是毫秒
  44. xhr.timeout = this._timeOut;
  45. xhr.open('POST', this._httpUrl + route, true);
  46. // 服务端也需要设置
  47. xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
  48. //DES3加密
  49. // dataStr = this.strToDES3(dataStr);
  50. xhr.send(dataStr);
  51. xhr.onreadystatechange = function onreadystatechange() {
  52. //////////
  53. // let time2 = new Date().getTime();
  54. // const dis = time2 - time;
  55. // console.log("返回时间======");
  56. // console.log(dis);
  57. ///////////
  58. // 请求完成。在此进行处理。
  59. if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) {
  60. let respone: any = xhr.responseText;
  61. if (!respone) {
  62. // console.error('http request respone is null');
  63. NetworkMgr.onHttpError(mid, null, 'http request respone is null');
  64. return;
  65. }
  66. NetworkMgr.checkRespone(route, respone, mid);
  67. }
  68. }.bind(this);
  69. // XMLHttpRequest 超时
  70. xhr.ontimeout = function ontimeout(error) {
  71. console.error('http request timeout: ', error);
  72. NetworkMgr.onHttpError(mid, null, 'http request timeout: ');
  73. // NetworkMgr.onHttpError(mid, res, 'http request timeout: ');
  74. }.bind(this);
  75. // XMLHttpRequest 错误
  76. xhr.onerror = function onerror(error) {
  77. // console.error('http request error: ', error);
  78. NetworkMgr.onHttpError(mid, error, 'http request error: ');
  79. // alert('网络通信失败,请刷新重试。');
  80. }.bind(this);
  81. }
  82. /**
  83. * GET 请求
  84. * @param route
  85. * @param msg
  86. * @param mid
  87. */
  88. public post2(route: string, msg: Object, mid?: number) {
  89. const str = this.MsgToString(msg)
  90. let xhr = new XMLHttpRequest();
  91. // 超时时间1s,单位是毫秒
  92. xhr.timeout = this._timeOut;
  93. xhr.open('GET', this._httpUrl + route + "?" + str, true);
  94. // 服务端也需要设置
  95. xhr.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
  96. xhr.send(JSON.stringify(msg));
  97. xhr.onreadystatechange = function onreadystatechange() {
  98. // 请求完成。在此进行处理。
  99. if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) {
  100. // console.log("返回"+xhr);
  101. let respone: any = xhr.responseText;
  102. if (!respone) {
  103. // console.error('http request respone is null');
  104. NetworkMgr.onHttpError(mid, null, 'http request respone is null');
  105. return;
  106. }
  107. NetworkMgr.checkRespone(route, respone, mid);
  108. }
  109. }.bind(this);
  110. // XMLHttpRequest 超时
  111. xhr.ontimeout = function ontimeout(error) {
  112. console.error('http request timeout: ', error);
  113. // NetworkMgr.onHttpError(mid, res, 'http request timeout: ');
  114. }.bind(this);
  115. // XMLHttpRequest 错误
  116. xhr.onerror = function onerror(error) {
  117. NetworkMgr.onHttpError(mid, error, 'http request error: ');
  118. }.bind(this);
  119. }
  120. /**
  121. * Get请求,将json转换成字符串数据
  122. */
  123. MsgToString(msg: any): string {
  124. let arr = Object.keys(msg);
  125. let str = "";
  126. for (let value of arr) {
  127. str += value + "=" + '"' + msg[value] + '"' + "&"
  128. }
  129. return str;
  130. }
  131. /**
  132. * 数据加密
  133. */
  134. strToDES3(data): any {
  135. // let des3Data: string = window.DES3.encrypt(global.DES3_KEY, data);
  136. // let newData = des3Data.replace(/\+/g, "%2B");
  137. // return newData;
  138. }
  139. /**生成sign加密 */
  140. private createSign(obj: Object) {
  141. var arr = new Array();
  142. var num = 0;
  143. for (var i in obj) {
  144. arr[num] = i;
  145. num++;
  146. }
  147. var sortArr = arr.sort();//参数名ASCII字典序排序
  148. var str = ""
  149. for (var i in sortArr) {
  150. if (!obj[sortArr[i]] && typeof (obj[sortArr[i]]) != "number") {
  151. continue;
  152. }
  153. str += sortArr[i] + "=" + obj[sortArr[i]] + "&&";
  154. }
  155. var signstr = str;
  156. signstr += "api_key=" + tyqSdkConfig.api_key;
  157. str += "sign=" + this.MD5(decodeURIComponent(signstr));
  158. return str;
  159. }
  160. /**md5加密*/
  161. private md5encode(str: string) {
  162. var hexcase = 0;
  163. var b64pad = "";
  164. var chrsz = 8;
  165. let hex_md5 = (s) => {
  166. return binl2hex(core_md5(str2binl(s), s.length * chrsz));
  167. }
  168. let b64_md5 = (s) => {
  169. return binl2b64(core_md5(str2binl(s), s.length * chrsz));
  170. }
  171. let str_md5 = (s) => {
  172. return binl2str(core_md5(str2binl(s), s.length * chrsz));
  173. }
  174. let hex_hmac_md5 = (key, data) => {
  175. return binl2hex(core_hmac_md5(key, data));
  176. }
  177. let b64_hmac_md5 = (key, data) => {
  178. return binl2b64(core_hmac_md5(key, data));
  179. }
  180. let str_hmac_md5 = (key, data) => {
  181. return binl2str(core_hmac_md5(key, data));
  182. }
  183. let md5_vm_test = () => {
  184. return hex_md5("abc") == "900150983cd24fb0d6963f7d28e17f72";
  185. }
  186. let core_md5 = (x, len) => {
  187. x[len >> 5] |= 0x80 << ((len) % 32);
  188. x[(((len + 64) >>> 9) << 4) + 14] = len;
  189. var a = 1732584193;
  190. var b = -271733879;
  191. var c = -1732584194;
  192. var d = 271733878;
  193. for (var i = 0; i < x.length; i += 16) {
  194. var olda = a;
  195. var oldb = b;
  196. var oldc = c;
  197. var oldd = d;
  198. a = md5_ff(a, b, c, d, x[i + 0], 7, -680876936);
  199. d = md5_ff(d, a, b, c, x[i + 1], 12, -389564586);
  200. c = md5_ff(c, d, a, b, x[i + 2], 17, 606105819);
  201. b = md5_ff(b, c, d, a, x[i + 3], 22, -1044525330);
  202. a = md5_ff(a, b, c, d, x[i + 4], 7, -176418897);
  203. d = md5_ff(d, a, b, c, x[i + 5], 12, 1200080426);
  204. c = md5_ff(c, d, a, b, x[i + 6], 17, -1473231341);
  205. b = md5_ff(b, c, d, a, x[i + 7], 22, -45705983);
  206. a = md5_ff(a, b, c, d, x[i + 8], 7, 1770035416);
  207. d = md5_ff(d, a, b, c, x[i + 9], 12, -1958414417);
  208. c = md5_ff(c, d, a, b, x[i + 10], 17, -42063);
  209. b = md5_ff(b, c, d, a, x[i + 11], 22, -1990404162);
  210. a = md5_ff(a, b, c, d, x[i + 12], 7, 1804603682);
  211. d = md5_ff(d, a, b, c, x[i + 13], 12, -40341101);
  212. c = md5_ff(c, d, a, b, x[i + 14], 17, -1502002290);
  213. b = md5_ff(b, c, d, a, x[i + 15], 22, 1236535329);
  214. a = md5_gg(a, b, c, d, x[i + 1], 5, -165796510);
  215. d = md5_gg(d, a, b, c, x[i + 6], 9, -1069501632);
  216. c = md5_gg(c, d, a, b, x[i + 11], 14, 643717713);
  217. b = md5_gg(b, c, d, a, x[i + 0], 20, -373897302);
  218. a = md5_gg(a, b, c, d, x[i + 5], 5, -701558691);
  219. d = md5_gg(d, a, b, c, x[i + 10], 9, 38016083);
  220. c = md5_gg(c, d, a, b, x[i + 15], 14, -660478335);
  221. b = md5_gg(b, c, d, a, x[i + 4], 20, -405537848);
  222. a = md5_gg(a, b, c, d, x[i + 9], 5, 568446438);
  223. d = md5_gg(d, a, b, c, x[i + 14], 9, -1019803690);
  224. c = md5_gg(c, d, a, b, x[i + 3], 14, -187363961);
  225. b = md5_gg(b, c, d, a, x[i + 8], 20, 1163531501);
  226. a = md5_gg(a, b, c, d, x[i + 13], 5, -1444681467);
  227. d = md5_gg(d, a, b, c, x[i + 2], 9, -51403784);
  228. c = md5_gg(c, d, a, b, x[i + 7], 14, 1735328473);
  229. b = md5_gg(b, c, d, a, x[i + 12], 20, -1926607734);
  230. a = md5_hh(a, b, c, d, x[i + 5], 4, -378558);
  231. d = md5_hh(d, a, b, c, x[i + 8], 11, -2022574463);
  232. c = md5_hh(c, d, a, b, x[i + 11], 16, 1839030562);
  233. b = md5_hh(b, c, d, a, x[i + 14], 23, -35309556);
  234. a = md5_hh(a, b, c, d, x[i + 1], 4, -1530992060);
  235. d = md5_hh(d, a, b, c, x[i + 4], 11, 1272893353);
  236. c = md5_hh(c, d, a, b, x[i + 7], 16, -155497632);
  237. b = md5_hh(b, c, d, a, x[i + 10], 23, -1094730640);
  238. a = md5_hh(a, b, c, d, x[i + 13], 4, 681279174);
  239. d = md5_hh(d, a, b, c, x[i + 0], 11, -358537222);
  240. c = md5_hh(c, d, a, b, x[i + 3], 16, -722521979);
  241. b = md5_hh(b, c, d, a, x[i + 6], 23, 76029189);
  242. a = md5_hh(a, b, c, d, x[i + 9], 4, -640364487);
  243. d = md5_hh(d, a, b, c, x[i + 12], 11, -421815835);
  244. c = md5_hh(c, d, a, b, x[i + 15], 16, 530742520);
  245. b = md5_hh(b, c, d, a, x[i + 2], 23, -995338651);
  246. a = md5_ii(a, b, c, d, x[i + 0], 6, -198630844);
  247. d = md5_ii(d, a, b, c, x[i + 7], 10, 1126891415);
  248. c = md5_ii(c, d, a, b, x[i + 14], 15, -1416354905);
  249. b = md5_ii(b, c, d, a, x[i + 5], 21, -57434055);
  250. a = md5_ii(a, b, c, d, x[i + 12], 6, 1700485571);
  251. d = md5_ii(d, a, b, c, x[i + 3], 10, -1894986606);
  252. c = md5_ii(c, d, a, b, x[i + 10], 15, -1051523);
  253. b = md5_ii(b, c, d, a, x[i + 1], 21, -2054922799);
  254. a = md5_ii(a, b, c, d, x[i + 8], 6, 1873313359);
  255. d = md5_ii(d, a, b, c, x[i + 15], 10, -30611744);
  256. c = md5_ii(c, d, a, b, x[i + 6], 15, -1560198380);
  257. b = md5_ii(b, c, d, a, x[i + 13], 21, 1309151649);
  258. a = md5_ii(a, b, c, d, x[i + 4], 6, -145523070);
  259. d = md5_ii(d, a, b, c, x[i + 11], 10, -1120210379);
  260. c = md5_ii(c, d, a, b, x[i + 2], 15, 718787259);
  261. b = md5_ii(b, c, d, a, x[i + 9], 21, -343485551);
  262. a = safe_add(a, olda);
  263. b = safe_add(b, oldb);
  264. c = safe_add(c, oldc);
  265. d = safe_add(d, oldd);
  266. }
  267. return Array(a, b, c, d);
  268. }
  269. let md5_cmn = (q, a, b, x, s, t) => {
  270. return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b);
  271. }
  272. let md5_ff = (a, b, c, d, x, s, t) => {
  273. return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
  274. }
  275. let md5_gg = (a, b, c, d, x, s, t) => {
  276. return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
  277. }
  278. let md5_hh = (a, b, c, d, x, s, t) => {
  279. return md5_cmn(b ^ c ^ d, a, b, x, s, t);
  280. }
  281. let md5_ii = (a, b, c, d, x, s, t) => {
  282. return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
  283. }
  284. let core_hmac_md5 = (key, data) => {
  285. var bkey = str2binl(key);
  286. if (bkey.length > 16) bkey = core_md5(bkey, key.length * chrsz);
  287. var ipad = Array(16),
  288. opad = Array(16);
  289. for (var i = 0; i < 16; i++) {
  290. ipad[i] = bkey[i] ^ 0x36363636;
  291. opad[i] = bkey[i] ^ 0x5C5C5C5C;
  292. }
  293. var hash = core_md5(ipad.concat(str2binl(data)), 512 + data.length * chrsz);
  294. return core_md5(opad.concat(hash), 512 + 128);
  295. }
  296. let safe_add = (x, y) => {
  297. var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  298. var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  299. return (msw << 16) | (lsw & 0xFFFF);
  300. }
  301. let bit_rol = (num, cnt) => {
  302. return (num << cnt) | (num >>> (32 - cnt));
  303. }
  304. let str2binl = (str) => {
  305. var bin = Array();
  306. var mask = (1 << chrsz) - 1;
  307. for (var i = 0; i < str.length * chrsz; i += chrsz)
  308. bin[i >> 5] |= (str.charCodeAt(i / chrsz) & mask) << (i % 32);
  309. return bin;
  310. }
  311. let binl2str = (bin) => {
  312. var str = "";
  313. var mask = (1 << chrsz) - 1;
  314. for (var i = 0; i < bin.length * 32; i += chrsz)
  315. str += String.fromCharCode((bin[i >> 5] >>> (i % 32)) & mask);
  316. return str;
  317. }
  318. let binl2hex = (binarray) => {
  319. var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
  320. var str = "";
  321. for (var i = 0; i < binarray.length * 4; i++) {
  322. str += hex_tab.charAt((binarray[i >> 2] >> ((i % 4) * 8 + 4)) & 0xF) +
  323. hex_tab.charAt((binarray[i >> 2] >> ((i % 4) * 8)) & 0xF);
  324. }
  325. return str;
  326. }
  327. let binl2b64 = (binarray) => {
  328. var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  329. var str = "";
  330. for (var i = 0; i < binarray.length * 4; i += 3) {
  331. var triplet = (((binarray[i >> 2] >> 8 * (i % 4)) & 0xFF) << 16) |
  332. (((binarray[i + 1 >> 2] >> 8 * ((i + 1) % 4)) & 0xFF) << 8) |
  333. ((binarray[i + 2 >> 2] >> 8 * ((i + 2) % 4)) & 0xFF);
  334. for (var j = 0; j < 4; j++) {
  335. if (i * 8 + j * 6 > binarray.length * 32) str += b64pad;
  336. else str += tab.charAt((triplet >> 6 * (3 - j)) & 0x3F);
  337. }
  338. }
  339. return str;
  340. }
  341. return hex_md5(str);
  342. }
  343. private MD5(instring) {
  344. var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
  345. var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */
  346. /*
  347. * These are the functions you'll usually want to call
  348. * They take string arguments and return either hex or base-64 encoded strings
  349. */
  350. function hex_md5(s) { return rstr2hex(rstr_md5(str2rstr_utf8(s))); }
  351. function b64_md5(s) { return rstr2b64(rstr_md5(str2rstr_utf8(s))); }
  352. function any_md5(s, e) { return rstr2any(rstr_md5(str2rstr_utf8(s)), e); }
  353. function hex_hmac_md5(k, d) { return rstr2hex(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d))); }
  354. function b64_hmac_md5(k, d) { return rstr2b64(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d))); }
  355. function any_hmac_md5(k, d, e) { return rstr2any(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d)), e); }
  356. /*
  357. * Perform a simple self-test to see if the VM is working
  358. */
  359. function md5_vm_test() {
  360. return hex_md5("abc").toLowerCase() == "900150983cd24fb0d6963f7d28e17f72";
  361. }
  362. /*
  363. * Calculate the MD5 of a raw string
  364. */
  365. function rstr_md5(s) {
  366. return binl2rstr(binl_md5(rstr2binl(s), s.length * 8));
  367. }
  368. /*
  369. * Calculate the HMAC-MD5, of a key and some data (raw strings)
  370. */
  371. function rstr_hmac_md5(key, data) {
  372. var bkey = rstr2binl(key);
  373. if (bkey.length > 16) bkey = binl_md5(bkey, key.length * 8);
  374. var ipad = Array(16), opad = Array(16);
  375. for (var i = 0; i < 16; i++) {
  376. ipad[i] = bkey[i] ^ 0x36363636;
  377. opad[i] = bkey[i] ^ 0x5C5C5C5C;
  378. }
  379. var hash = binl_md5(ipad.concat(rstr2binl(data)), 512 + data.length * 8);
  380. return binl2rstr(binl_md5(opad.concat(hash), 512 + 128));
  381. }
  382. /*
  383. * Convert a raw string to a hex string
  384. */
  385. function rstr2hex(input) {
  386. try { hexcase } catch (e) { hexcase = 0; }
  387. var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
  388. var output = "";
  389. var x;
  390. for (var i = 0; i < input.length; i++) {
  391. x = input.charCodeAt(i);
  392. output += hex_tab.charAt((x >>> 4) & 0x0F)
  393. + hex_tab.charAt(x & 0x0F);
  394. }
  395. return output;
  396. }
  397. /*
  398. * Convert a raw string to a base-64 string
  399. */
  400. function rstr2b64(input) {
  401. try { b64pad } catch (e) { b64pad = ''; }
  402. var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  403. var output = "";
  404. var len = input.length;
  405. for (var i = 0; i < len; i += 3) {
  406. var triplet = (input.charCodeAt(i) << 16)
  407. | (i + 1 < len ? input.charCodeAt(i + 1) << 8 : 0)
  408. | (i + 2 < len ? input.charCodeAt(i + 2) : 0);
  409. for (var j = 0; j < 4; j++) {
  410. if (i * 8 + j * 6 > input.length * 8) output += b64pad;
  411. else output += tab.charAt((triplet >>> 6 * (3 - j)) & 0x3F);
  412. }
  413. }
  414. return output;
  415. }
  416. /*
  417. * Convert a raw string to an arbitrary string encoding
  418. */
  419. function rstr2any(input, encoding) {
  420. var divisor = encoding.length;
  421. var i, j, q, x, quotient;
  422. /* Convert to an array of 16-bit big-endian values, forming the dividend */
  423. var dividend = Array(Math.ceil(input.length / 2));
  424. for (i = 0; i < dividend.length; i++) {
  425. dividend[i] = (input.charCodeAt(i * 2) << 8) | input.charCodeAt(i * 2 + 1);
  426. }
  427. /*
  428. * Repeatedly perform a long division. The binary array forms the dividend,
  429. * the length of the encoding is the divisor. Once computed, the quotient
  430. * forms the dividend for the next step. All remainders are stored for later
  431. * use.
  432. */
  433. var full_length = Math.ceil(input.length * 8 /
  434. (Math.log(encoding.length) / Math.log(2)));
  435. var remainders = Array(full_length);
  436. for (j = 0; j < full_length; j++) {
  437. quotient = Array();
  438. x = 0;
  439. for (i = 0; i < dividend.length; i++) {
  440. x = (x << 16) + dividend[i];
  441. q = Math.floor(x / divisor);
  442. x -= q * divisor;
  443. if (quotient.length > 0 || q > 0)
  444. quotient[quotient.length] = q;
  445. }
  446. remainders[j] = x;
  447. dividend = quotient;
  448. }
  449. /* Convert the remainders to the output string */
  450. var output = "";
  451. for (i = remainders.length - 1; i >= 0; i--)
  452. output += encoding.charAt(remainders[i]);
  453. return output;
  454. }
  455. /*
  456. * Encode a string as utf-8.
  457. * For efficiency, this assumes the input is valid utf-16.
  458. */
  459. function str2rstr_utf8(input) {
  460. var output = "";
  461. var i = -1;
  462. var x, y;
  463. while (++i < input.length) {
  464. /* Decode utf-16 surrogate pairs */
  465. x = input.charCodeAt(i);
  466. y = i + 1 < input.length ? input.charCodeAt(i + 1) : 0;
  467. if (0xD800 <= x && x <= 0xDBFF && 0xDC00 <= y && y <= 0xDFFF) {
  468. x = 0x10000 + ((x & 0x03FF) << 10) + (y & 0x03FF);
  469. i++;
  470. }
  471. /* Encode output as utf-8 */
  472. if (x <= 0x7F)
  473. output += String.fromCharCode(x);
  474. else if (x <= 0x7FF)
  475. output += String.fromCharCode(0xC0 | ((x >>> 6) & 0x1F),
  476. 0x80 | (x & 0x3F));
  477. else if (x <= 0xFFFF)
  478. output += String.fromCharCode(0xE0 | ((x >>> 12) & 0x0F),
  479. 0x80 | ((x >>> 6) & 0x3F),
  480. 0x80 | (x & 0x3F));
  481. else if (x <= 0x1FFFFF)
  482. output += String.fromCharCode(0xF0 | ((x >>> 18) & 0x07),
  483. 0x80 | ((x >>> 12) & 0x3F),
  484. 0x80 | ((x >>> 6) & 0x3F),
  485. 0x80 | (x & 0x3F));
  486. }
  487. return output;
  488. }
  489. /*
  490. * Encode a string as utf-16
  491. */
  492. function str2rstr_utf16le(input) {
  493. var output = "";
  494. for (var i = 0; i < input.length; i++)
  495. output += String.fromCharCode(input.charCodeAt(i) & 0xFF,
  496. (input.charCodeAt(i) >>> 8) & 0xFF);
  497. return output;
  498. }
  499. function str2rstr_utf16be(input) {
  500. var output = "";
  501. for (var i = 0; i < input.length; i++)
  502. output += String.fromCharCode((input.charCodeAt(i) >>> 8) & 0xFF,
  503. input.charCodeAt(i) & 0xFF);
  504. return output;
  505. }
  506. /*
  507. * Convert a raw string to an array of little-endian words
  508. * Characters >255 have their high-byte silently ignored.
  509. */
  510. function rstr2binl(input) {
  511. var output = Array(input.length >> 2);
  512. for (var i = 0; i < output.length; i++)
  513. output[i] = 0;
  514. for (var i = 0; i < input.length * 8; i += 8)
  515. output[i >> 5] |= (input.charCodeAt(i / 8) & 0xFF) << (i % 32);
  516. return output;
  517. }
  518. /*
  519. * Convert an array of little-endian words to a string
  520. */
  521. function binl2rstr(input) {
  522. var output = "";
  523. for (var i = 0; i < input.length * 32; i += 8)
  524. output += String.fromCharCode((input[i >> 5] >>> (i % 32)) & 0xFF);
  525. return output;
  526. }
  527. /*
  528. * Calculate the MD5 of an array of little-endian words, and a bit length.
  529. */
  530. function binl_md5(x, len) {
  531. /* append padding */
  532. x[len >> 5] |= 0x80 << ((len) % 32);
  533. x[(((len + 64) >>> 9) << 4) + 14] = len;
  534. var a = 1732584193;
  535. var b = -271733879;
  536. var c = -1732584194;
  537. var d = 271733878;
  538. for (var i = 0; i < x.length; i += 16) {
  539. var olda = a;
  540. var oldb = b;
  541. var oldc = c;
  542. var oldd = d;
  543. a = md5_ff(a, b, c, d, x[i + 0], 7, -680876936);
  544. d = md5_ff(d, a, b, c, x[i + 1], 12, -389564586);
  545. c = md5_ff(c, d, a, b, x[i + 2], 17, 606105819);
  546. b = md5_ff(b, c, d, a, x[i + 3], 22, -1044525330);
  547. a = md5_ff(a, b, c, d, x[i + 4], 7, -176418897);
  548. d = md5_ff(d, a, b, c, x[i + 5], 12, 1200080426);
  549. c = md5_ff(c, d, a, b, x[i + 6], 17, -1473231341);
  550. b = md5_ff(b, c, d, a, x[i + 7], 22, -45705983);
  551. a = md5_ff(a, b, c, d, x[i + 8], 7, 1770035416);
  552. d = md5_ff(d, a, b, c, x[i + 9], 12, -1958414417);
  553. c = md5_ff(c, d, a, b, x[i + 10], 17, -42063);
  554. b = md5_ff(b, c, d, a, x[i + 11], 22, -1990404162);
  555. a = md5_ff(a, b, c, d, x[i + 12], 7, 1804603682);
  556. d = md5_ff(d, a, b, c, x[i + 13], 12, -40341101);
  557. c = md5_ff(c, d, a, b, x[i + 14], 17, -1502002290);
  558. b = md5_ff(b, c, d, a, x[i + 15], 22, 1236535329);
  559. a = md5_gg(a, b, c, d, x[i + 1], 5, -165796510);
  560. d = md5_gg(d, a, b, c, x[i + 6], 9, -1069501632);
  561. c = md5_gg(c, d, a, b, x[i + 11], 14, 643717713);
  562. b = md5_gg(b, c, d, a, x[i + 0], 20, -373897302);
  563. a = md5_gg(a, b, c, d, x[i + 5], 5, -701558691);
  564. d = md5_gg(d, a, b, c, x[i + 10], 9, 38016083);
  565. c = md5_gg(c, d, a, b, x[i + 15], 14, -660478335);
  566. b = md5_gg(b, c, d, a, x[i + 4], 20, -405537848);
  567. a = md5_gg(a, b, c, d, x[i + 9], 5, 568446438);
  568. d = md5_gg(d, a, b, c, x[i + 14], 9, -1019803690);
  569. c = md5_gg(c, d, a, b, x[i + 3], 14, -187363961);
  570. b = md5_gg(b, c, d, a, x[i + 8], 20, 1163531501);
  571. a = md5_gg(a, b, c, d, x[i + 13], 5, -1444681467);
  572. d = md5_gg(d, a, b, c, x[i + 2], 9, -51403784);
  573. c = md5_gg(c, d, a, b, x[i + 7], 14, 1735328473);
  574. b = md5_gg(b, c, d, a, x[i + 12], 20, -1926607734);
  575. a = md5_hh(a, b, c, d, x[i + 5], 4, -378558);
  576. d = md5_hh(d, a, b, c, x[i + 8], 11, -2022574463);
  577. c = md5_hh(c, d, a, b, x[i + 11], 16, 1839030562);
  578. b = md5_hh(b, c, d, a, x[i + 14], 23, -35309556);
  579. a = md5_hh(a, b, c, d, x[i + 1], 4, -1530992060);
  580. d = md5_hh(d, a, b, c, x[i + 4], 11, 1272893353);
  581. c = md5_hh(c, d, a, b, x[i + 7], 16, -155497632);
  582. b = md5_hh(b, c, d, a, x[i + 10], 23, -1094730640);
  583. a = md5_hh(a, b, c, d, x[i + 13], 4, 681279174);
  584. d = md5_hh(d, a, b, c, x[i + 0], 11, -358537222);
  585. c = md5_hh(c, d, a, b, x[i + 3], 16, -722521979);
  586. b = md5_hh(b, c, d, a, x[i + 6], 23, 76029189);
  587. a = md5_hh(a, b, c, d, x[i + 9], 4, -640364487);
  588. d = md5_hh(d, a, b, c, x[i + 12], 11, -421815835);
  589. c = md5_hh(c, d, a, b, x[i + 15], 16, 530742520);
  590. b = md5_hh(b, c, d, a, x[i + 2], 23, -995338651);
  591. a = md5_ii(a, b, c, d, x[i + 0], 6, -198630844);
  592. d = md5_ii(d, a, b, c, x[i + 7], 10, 1126891415);
  593. c = md5_ii(c, d, a, b, x[i + 14], 15, -1416354905);
  594. b = md5_ii(b, c, d, a, x[i + 5], 21, -57434055);
  595. a = md5_ii(a, b, c, d, x[i + 12], 6, 1700485571);
  596. d = md5_ii(d, a, b, c, x[i + 3], 10, -1894986606);
  597. c = md5_ii(c, d, a, b, x[i + 10], 15, -1051523);
  598. b = md5_ii(b, c, d, a, x[i + 1], 21, -2054922799);
  599. a = md5_ii(a, b, c, d, x[i + 8], 6, 1873313359);
  600. d = md5_ii(d, a, b, c, x[i + 15], 10, -30611744);
  601. c = md5_ii(c, d, a, b, x[i + 6], 15, -1560198380);
  602. b = md5_ii(b, c, d, a, x[i + 13], 21, 1309151649);
  603. a = md5_ii(a, b, c, d, x[i + 4], 6, -145523070);
  604. d = md5_ii(d, a, b, c, x[i + 11], 10, -1120210379);
  605. c = md5_ii(c, d, a, b, x[i + 2], 15, 718787259);
  606. b = md5_ii(b, c, d, a, x[i + 9], 21, -343485551);
  607. a = safe_add(a, olda);
  608. b = safe_add(b, oldb);
  609. c = safe_add(c, oldc);
  610. d = safe_add(d, oldd);
  611. }
  612. return Array(a, b, c, d);
  613. }
  614. /*
  615. * These functions implement the four basic operations the algorithm uses.
  616. */
  617. function md5_cmn(q, a, b, x, s, t) {
  618. return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b);
  619. }
  620. function md5_ff(a, b, c, d, x, s, t) {
  621. return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
  622. }
  623. function md5_gg(a, b, c, d, x, s, t) {
  624. return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
  625. }
  626. function md5_hh(a, b, c, d, x, s, t) {
  627. return md5_cmn(b ^ c ^ d, a, b, x, s, t);
  628. }
  629. function md5_ii(a, b, c, d, x, s, t) {
  630. return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
  631. }
  632. /*
  633. * Add integers, wrapping at 2^32. This uses 16-bit operations internally
  634. * to work around bugs in some JS interpreters.
  635. */
  636. function safe_add(x, y) {
  637. var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  638. var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  639. return (msw << 16) | (lsw & 0xFFFF);
  640. }
  641. /*
  642. * Bitwise rotate a 32-bit number to the left.
  643. */
  644. function bit_rol(num, cnt) {
  645. return (num << cnt) | (num >>> (32 - cnt));
  646. }
  647. return hex_md5(instring);
  648. }
  649. public get(path: string, data: any, handler: any, extraUrl?: string): XMLHttpRequest {
  650. // var xhr = cc.loader.getXMLHttpRequest();
  651. var xhr = new XMLHttpRequest();
  652. xhr.timeout = 5000;
  653. var str = "?";
  654. for (let k in data) {
  655. if (str != "?") {
  656. str += "&";
  657. }
  658. str += `${k}=${data[k]}`;
  659. }
  660. if (extraUrl == null) extraUrl = GHttpClient.url;
  661. var requestURL = extraUrl + path + str;
  662. console.log("RequestURL:" + requestURL);
  663. xhr.open('GET', requestURL, true);
  664. // if (cc.sys.isNative){
  665. // xhr.setRequestHeader("Accept-Encoding","gzip,deflate");
  666. // xhr.setRequestHeader("CONTENT-TYPE", "text/html;charset=UTF-8");
  667. // }
  668. xhr.onreadystatechange = () => {
  669. if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) {
  670. console.log("http res(" + xhr.responseText.length + "):" + xhr.responseText);
  671. try {
  672. var ret = JSON.parse(xhr.responseText);
  673. if (handler !== null) {
  674. handler(null, ret);
  675. } /* code */
  676. } catch (e) {
  677. if (handler !== null) {
  678. handler(e);
  679. }
  680. }
  681. finally {
  682. }
  683. }
  684. }
  685. xhr.send();
  686. return xhr;
  687. }
  688. }
  689. export const Http = http._instance;