http.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. static get(url,call_back){
  53. var xml = new XMLHttpRequest()
  54. xml.open('GET',http.domain+url)
  55. xml.setRequestHeader('Content-Type','application/json');
  56. xml.send()
  57. var array:String[] = ['loadstart','abort', 'error', 'load', 'loadend', 'timeout'];
  58. array.forEach(function (eventName) {
  59. xml[('on' + eventName) as 'onloadstart' | 'onabort' | 'onerror' | 'onload' | 'onloadend' | 'ontimeout'] = function () {
  60. var str = '\nEvent : ' + eventName;
  61. var lstr = ""
  62. if (eventName === 'timeout') {
  63. lstr += '(timeout)';
  64. }
  65. else if (eventName === 'loadend') {
  66. lstr += '...loadend!';
  67. }else if (eventName === 'onerror') {
  68. }
  69. // console.log("str==",str)
  70. // console.log("lstr==",lstr)
  71. };
  72. });
  73. // Special event
  74. xml.onreadystatechange = function () {
  75. if (xml.readyState === 4 && xml.status >= 200) {
  76. call_back(null,xml.responseText);
  77. } else if (xml.status === 404) {
  78. call_back('404 page not found!',null);
  79. }
  80. }
  81. }
  82. public static getGameList(page:number,limit:number=6):string{
  83. return `/smistatic/levels/${limit}_${page}.json`
  84. }
  85. public static get_dyopen_id():string{
  86. return `/note/user/get_dyopen_id`
  87. }
  88. public static get_login():string{
  89. return `/note/user/login`
  90. }
  91. public static get_sys_config():string{
  92. return `/smistatic/sys_config.json`
  93. }
  94. public static get_level_info(id:number):string{
  95. return `/smistatic/level_info/${id}.json`
  96. }
  97. //场景 scene 0=共用 1 = 场景1 2 = 场景2,
  98. public static get_level_resource(id:number,scene:number):string{
  99. return `/smistatic/resource/${id}_${scene}.json`
  100. }
  101. public static sync_data(){
  102. return `/note/user/sync_data`
  103. }
  104. public static run_get(url,call_back){
  105. var xml = new XMLHttpRequest()
  106. xml.open('GET',http.domain+url)
  107. xml.setRequestHeader('Content-Type','application/json');
  108. xml.send();
  109. var array:String[] = ['loadstart','abort', 'error', 'load', 'loadend', 'timeout'];
  110. array.forEach(function (eventName) {
  111. xml[('on' + eventName) as 'onloadstart' | 'onabort' | 'onerror' | 'onload' | 'onloadend' | 'ontimeout'] = function () {
  112. var str = '\nEvent : ' + eventName;
  113. var lstr = ""
  114. if (eventName === 'timeout') {
  115. lstr += '(timeout)';
  116. }
  117. else if (eventName === 'loadend') {
  118. lstr += '...loadend!';
  119. }else if (eventName === 'onerror') {
  120. }
  121. console.log("str==",str)
  122. console.log("lstr==",lstr)
  123. };
  124. });
  125. // Special event
  126. xml.onreadystatechange = function () {
  127. if (xml.readyState === 4 && xml.status >= 200) {
  128. call_back(null,xml.responseText);
  129. } else if (xml.status === 404) {
  130. call_back('404 page not found!',null);
  131. }
  132. };
  133. }
  134. public static run_post(url,data,call_back){
  135. var xml = new XMLHttpRequest()
  136. xml.open('POST',http.domain+url)
  137. xml.setRequestHeader('Content-Type','application/json');
  138. xml.setRequestHeader('token',config.TOKEN);
  139. xml.send(JSON.stringify(data));
  140. var array:String[] = ['loadstart','abort', 'error', 'load', 'loadend', 'timeout'];
  141. array.forEach(function (eventName) {
  142. xml[('on' + eventName) as 'onloadstart' | 'onabort' | 'onerror' | 'onload' | 'onloadend' | 'ontimeout'] = function () {
  143. var str = '\nEvent : ' + eventName;
  144. var lstr = ""
  145. if (eventName === 'timeout') {
  146. lstr += '(timeout)';
  147. }
  148. else if (eventName === 'loadend') {
  149. lstr += '...loadend!';
  150. }else if (eventName === 'onerror') {
  151. }
  152. // console.log("str==",str)
  153. // console.log("lstr==",lstr)
  154. };
  155. });
  156. // Special event
  157. xml.onreadystatechange = function () {
  158. if (xml.readyState === 4 && xml.status >= 200) {
  159. call_back(null,xml.responseText);
  160. } else if (xml.status === 404) {
  161. call_back('404 page not found!',null);
  162. }
  163. };
  164. }
  165. }