http.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 if (xml.readyState === 3) {
  83. // console.log("status ==",'Request dealing!')
  84. } else if (xml.readyState === 2) {
  85. // console.log("status ==", 'Request received!')
  86. } else {
  87. console.log('请求出错=',url)
  88. tools.showToast('请求出错')
  89. }
  90. }
  91. }
  92. public static getGameList(page:number,limit:number=6):string{
  93. return `/smistatic/levels/${limit}_${page}.json`
  94. }
  95. public static get_dyopen_id():string{
  96. return `/note/user/get_dyopen_id`
  97. }
  98. public static get_login():string{
  99. return `/note/user/login`
  100. }
  101. public static get_sys_config():string{
  102. return `/smistatic/sys_config.json`
  103. }
  104. public static get_level_info(id:number):string{
  105. return `/smistatic/level_info/${id}.json`
  106. }
  107. //场景 scene 0=共用 1 = 场景1 2 = 场景2,
  108. public static get_level_resource(id:number,scene:number):string{
  109. return `/smistatic/resource/${id}_${scene}.json`
  110. }
  111. public static sync_data(){
  112. return `/note/user/sync_data`
  113. }
  114. public static run_get(url,call_back){
  115. var xml = new XMLHttpRequest()
  116. xml.open('GET',config.domain+url)
  117. xml.setRequestHeader('Content-Type','application/json');
  118. xml.send();
  119. var array:String[] = ['loadstart','abort', 'error', 'load', 'loadend', 'timeout'];
  120. array.forEach(function (eventName) {
  121. xml[('on' + eventName) as 'onloadstart' | 'onabort' | 'onerror' | 'onload' | 'onloadend' | 'ontimeout'] = function () {
  122. var str = '\nEvent : ' + eventName;
  123. var lstr = ""
  124. if (eventName === 'timeout') {
  125. lstr += '(timeout)';
  126. }
  127. else if (eventName === 'loadend') {
  128. lstr += '...loadend!';
  129. }else if (eventName === 'onerror') {
  130. }
  131. // console.log("str==",str)
  132. // console.log("lstr==",lstr)
  133. };
  134. });
  135. // Special event
  136. xml.onreadystatechange = function () {
  137. if(xml.readyState === 4 && xml.status >= 200) {
  138. call_back(null,xml.responseText);
  139. } else if (xml.status === 404) {
  140. call_back('404 page not found!',null);
  141. } else {
  142. console.log('请求出错=',url)
  143. tools.showToast('请求出错')
  144. }
  145. };
  146. }
  147. public static run_post(url,data,call_back){
  148. var xml = new XMLHttpRequest()
  149. xml.open('POST',config.domain+url)
  150. xml.setRequestHeader('Content-Type','application/json');
  151. xml.setRequestHeader('token',config.TOKEN);
  152. xml.send(JSON.stringify(data));
  153. var array:String[] = ['loadstart','abort', 'error', 'load', 'loadend', 'timeout'];
  154. array.forEach(function (eventName) {
  155. xml[('on' + eventName) as 'onloadstart' | 'onabort' | 'onerror' | 'onload' | 'onloadend' | 'ontimeout'] = function () {
  156. var str = '\nEvent : ' + eventName;
  157. var lstr = ""
  158. if (eventName === 'timeout') {
  159. lstr += '(timeout)';
  160. }
  161. else if (eventName === 'loadend') {
  162. lstr += '...loadend!';
  163. }else if (eventName === 'onerror') {
  164. }
  165. // console.log("str==",str)
  166. // console.log("lstr==",lstr)
  167. };
  168. });
  169. // Special event
  170. xml.onreadystatechange = function () {
  171. if(xml.readyState === 4 && xml.status >= 200) {
  172. call_back(null,xml.responseText);
  173. } else if (xml.status === 404) {
  174. call_back('404 page not found!',null);
  175. } else {
  176. console.log('请求出错=',url)
  177. tools.showToast('请求出错')
  178. }
  179. };
  180. }
  181. }