http.ts 7.4 KB

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