http.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { _decorator, Component, Node } from 'cc';
  2. import { config } from './config';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('http')
  5. export class http {
  6. public static domain = "https://zcapi.xwrun.com";
  7. static post(url,data,call_back){
  8. var xml = new XMLHttpRequest()
  9. xml.open('POST',http.domain+url)
  10. xml.setRequestHeader('Content-Type','application/json');
  11. xml.send(JSON.stringify(data));
  12. var array:String[] = ['loadstart','abort', 'error', 'load', 'loadend', 'timeout'];
  13. array.forEach(function (eventName) {
  14. xml[('on' + eventName) as 'onloadstart' | 'onabort' | 'onerror' | 'onload' | 'onloadend' | 'ontimeout'] = function () {
  15. var str = '\nEvent : ' + eventName;
  16. var lstr = ""
  17. if (eventName === 'timeout') {
  18. lstr += '(timeout)';
  19. }
  20. else if (eventName === 'loadend') {
  21. lstr += '...loadend!';
  22. }else if (eventName === 'onerror') {
  23. }
  24. console.log("str==",str)
  25. console.log("lstr==",lstr)
  26. };
  27. });
  28. // Special event
  29. xml.onreadystatechange = function () {
  30. if (xml.readyState === 4 && xml.status >= 200) {
  31. //label.string = handler(xml.responseText);
  32. // console.log("xml.responseText==",xml.responseText)
  33. call_back(null,xml.responseText);
  34. } else if (xml.status === 404) {
  35. call_back('404 page not found!',null);
  36. console.log("status ==",'404 page not found!')
  37. } else if (xml.readyState === 3) {
  38. call_back('Request dealing!',null);
  39. console.log("status ==",'Request dealing!')
  40. } else if (xml.readyState === 2) {
  41. call_back('Request received!',null);
  42. console.log("status ==", 'Request received!')
  43. } else if (xml.readyState === 1) {
  44. call_back('Server connection established! Request hasn\'t been received',null);
  45. console.log("status ==", 'Server connection established! Request hasn\'t been received')
  46. } else if (xml.readyState === 0) {
  47. call_back( 'Request hasn\'t been initiated!',null);
  48. console.log("status ==", 'Request hasn\'t been initiated!')
  49. }
  50. };
  51. }
  52. public static getGameList(page:number,limit:number=6):string{
  53. return `/smistatic/levels/${limit}_${page}.json`
  54. }
  55. public static get_dyopen_id():string{
  56. return `/note/user/get_dyopen_id`
  57. }
  58. public static get_login():string{
  59. return `/note/user/login`
  60. }
  61. public static get_sys_config():string{
  62. return `/smistatic/sys_config.json`
  63. }
  64. public static get_level_info(id:number):string{
  65. return `/smistatic/level_info/${id}.json`
  66. }
  67. //场景 scene 0=共用 1 = 场景1 2 = 场景2,
  68. public static get_level_resource(id:number,scene:number):string{
  69. return `/smistatic/resource/${id}_${scene}.json`
  70. }
  71. public static sync_data(){
  72. return `/note/user/sync_data`
  73. }
  74. public static run_get(url,call_back){
  75. var xml = new XMLHttpRequest()
  76. xml.open('GET',http.domain+url)
  77. xml.setRequestHeader('Content-Type','application/json');
  78. xml.send();
  79. var array:String[] = ['loadstart','abort', 'error', 'load', 'loadend', 'timeout'];
  80. array.forEach(function (eventName) {
  81. xml[('on' + eventName) as 'onloadstart' | 'onabort' | 'onerror' | 'onload' | 'onloadend' | 'ontimeout'] = function () {
  82. var str = '\nEvent : ' + eventName;
  83. var lstr = ""
  84. if (eventName === 'timeout') {
  85. lstr += '(timeout)';
  86. }
  87. else if (eventName === 'loadend') {
  88. lstr += '...loadend!';
  89. }else if (eventName === 'onerror') {
  90. }
  91. console.log("str==",str)
  92. console.log("lstr==",lstr)
  93. };
  94. });
  95. // Special event
  96. xml.onreadystatechange = function () {
  97. if (xml.readyState === 4 && xml.status >= 200) {
  98. call_back(null,xml.responseText);
  99. } else if (xml.status === 404) {
  100. call_back('404 page not found!',null);
  101. }
  102. };
  103. }
  104. public static run_post(url,data,call_back){
  105. var xml = new XMLHttpRequest()
  106. xml.open('POST',http.domain+url)
  107. xml.setRequestHeader('Content-Type','application/json');
  108. xml.setRequestHeader('token',config.TOKEN);
  109. xml.send(JSON.stringify(data));
  110. var array:String[] = ['loadstart','abort', 'error', 'load', 'loadend', 'timeout'];
  111. array.forEach(function (eventName) {
  112. xml[('on' + eventName) as 'onloadstart' | 'onabort' | 'onerror' | 'onload' | 'onloadend' | 'ontimeout'] = function () {
  113. var str = '\nEvent : ' + eventName;
  114. var lstr = ""
  115. if (eventName === 'timeout') {
  116. lstr += '(timeout)';
  117. }
  118. else if (eventName === 'loadend') {
  119. lstr += '...loadend!';
  120. }else if (eventName === 'onerror') {
  121. }
  122. console.log("str==",str)
  123. console.log("lstr==",lstr)
  124. };
  125. });
  126. // Special event
  127. xml.onreadystatechange = function () {
  128. if (xml.readyState === 4 && xml.status >= 200) {
  129. call_back(null,xml.responseText);
  130. } else if (xml.status === 404) {
  131. call_back('404 page not found!',null);
  132. }
  133. };
  134. }
  135. }