http.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { _decorator, Component, Node, sys } from 'cc';
  2. import { config } from './config';
  3. import { tools } from './tools';
  4. import { userDataManager } from './manager/userDataManager';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('http')
  7. export class http {
  8. public static static_domain = "https://zcapi.xwrun.com"
  9. public static statistics_domain = "https://zcapi.xwrun.com"
  10. public static post(url,opt, call_back,method:string='POST') {
  11. var xml = new XMLHttpRequest()
  12. let request_url = http.static_domain+url
  13. xml.open(method, request_url)
  14. xml.setRequestHeader('Content-Type', 'application/json');
  15. if(userDataManager.user_data!=null){
  16. xml.setRequestHeader('token',userDataManager.user_data.token);
  17. }
  18. if(opt==null){
  19. xml.send();
  20. }else{
  21. xml.send(JSON.stringify(opt));
  22. }
  23. // xml.send(opt);
  24. var array: String[] = ['loadstart', 'abort', 'error', 'load', 'loadend', 'timeout'];
  25. array.forEach(function (eventName) {
  26. xml[('on' + eventName) as 'onloadstart' | 'onabort' | 'onerror' | 'onload' | 'onloadend' | 'ontimeout'] = function () {
  27. // var str = '\nEvent : ' + eventName;
  28. var lstr = ""
  29. if (eventName === 'timeout') {
  30. lstr += '(timeout)';
  31. }
  32. else if (eventName === 'loadend') {
  33. lstr += '...loadend!';
  34. } else if (eventName === 'onerror') {
  35. }
  36. };
  37. });
  38. // Special event
  39. xml.onreadystatechange = function () {
  40. if (xml.readyState === 4 && xml.status >= 200) {
  41. call_back(null, xml.responseText);
  42. } else if (xml.status === 404) {
  43. call_back('404 page not found!', null);
  44. }
  45. };
  46. }
  47. public static get(url, call_back) {
  48. var xml = new XMLHttpRequest()
  49. let request_url = http.static_domain+url
  50. xml.open('GET', request_url)
  51. xml.setRequestHeader('Content-Type', 'application/json');
  52. xml.send();
  53. var array: String[] = ['loadstart', 'abort', 'error', 'load', 'loadend', 'timeout'];
  54. array.forEach(function (eventName) {
  55. xml[('on' + eventName) as 'onloadstart' | 'onabort' | 'onerror' | 'onload' | 'onloadend' | 'ontimeout'] = function () {
  56. // var str = '\nEvent : ' + eventName;
  57. var lstr = ""
  58. if (eventName === 'timeout') {
  59. lstr += '(timeout)';
  60. }
  61. else if (eventName === 'loadend') {
  62. lstr += '...loadend!';
  63. } else if (eventName === 'onerror') {
  64. }
  65. };
  66. });
  67. // Special event
  68. xml.onreadystatechange = function () {
  69. if (xml.readyState === 4 && xml.status >= 200) {
  70. call_back(null, xml.responseText);
  71. } else if (xml.status === 404) {
  72. call_back('404 page not found!', null);
  73. }
  74. };
  75. }
  76. }