http.ts 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import { _decorator, Component, Node, sys } from 'cc';
  2. import { config } from './config';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('http')
  5. export class http {
  6. public static domain = config.debug ? "https://zcapi.xwrun.com" : "https://zcapi.hainanmlwl.com";
  7. public static static_domain = config.debug ? "https://zcapi.xwrun.com" : "https://zaoca.oss-cn-beijing.aliyuncs.com"
  8. public static statistics_domain = config.debug ? "http://logads.xwrun.com" : "https://logads.hainanmlwl.com";
  9. static post(url, data, call_back) {
  10. var xml = new XMLHttpRequest()
  11. xml.open('POST', http.domain + url)
  12. xml.setRequestHeader('Content-Type', 'application/json');
  13. xml.send(JSON.stringify(data));
  14. var array: String[] = ['loadstart', 'abort', 'error', 'load', 'loadend', 'timeout'];
  15. array.forEach(function (eventName) {
  16. xml[('on' + eventName) as 'onloadstart' | 'onabort' | 'onerror' | 'onload' | 'onloadend' | 'ontimeout'] = function () {
  17. var str = '\nEvent : ' + eventName;
  18. var lstr = ""
  19. if (eventName === 'timeout') {
  20. lstr += '(timeout)';
  21. }
  22. else if (eventName === 'loadend') {
  23. lstr += '...loadend!';
  24. } else if (eventName === 'onerror') {
  25. }
  26. console.log("str==", str)
  27. console.log("lstr==", lstr)
  28. };
  29. });
  30. // Special event
  31. xml.onreadystatechange = function () {
  32. if (xml.readyState === 4 && xml.status >= 200) {
  33. //label.string = handler(xml.responseText);
  34. // console.log("xml.responseText==",xml.responseText)
  35. call_back(null, xml.responseText);
  36. } else if (xml.status === 404) {
  37. call_back('404 page not found!', null);
  38. console.log("status ==", '404 page not found!')
  39. } else if (xml.readyState === 3) {
  40. call_back('Request dealing!', null);
  41. console.log("status ==", 'Request dealing!')
  42. } else if (xml.readyState === 2) {
  43. call_back('Request received!', null);
  44. console.log("status ==", 'Request received!')
  45. } else if (xml.readyState === 1) {
  46. call_back('Server connection established! Request hasn\'t been received', null);
  47. console.log("status ==", 'Server connection established! Request hasn\'t been received')
  48. } else if (xml.readyState === 0) {
  49. call_back('Request hasn\'t been initiated!', null);
  50. console.log("status ==", 'Request hasn\'t been initiated!')
  51. }
  52. };
  53. }
  54. public static getGameList(page: number, limit: number = 6): string {
  55. return `/smistatic/levels/${limit}_${page}.json`
  56. }
  57. public static get_dyopen_id(): string {
  58. return `/note/user/get_dyopen_id`
  59. }
  60. public static get_login(): string {
  61. return `/note/user/login`
  62. }
  63. public static get_sys_config(): string {
  64. return `/smistatic/sys_config.json`
  65. }
  66. public static get_level_info(id: number): string {
  67. return `/smistatic/level_info/${id}.json`
  68. }
  69. //场景 scene 0=共用 1 = 场景1 2 = 场景2,
  70. public static get_level_resource(id: number, scene: number): string {
  71. return `/smistatic/resource/${id}_${scene}.json`
  72. }
  73. public static get_test_user_list(): string {
  74. return `/smistatic/user_test.json`
  75. }
  76. public static sync_data() {
  77. return `/note/user/sync_data`
  78. }
  79. public static statistics_ads() {
  80. return `/smadspush`
  81. }
  82. public static user_unlock_number_status() {
  83. return `/note/user/unlock_number_status`
  84. }
  85. public static run_get(url, call_back) {
  86. var xml = new XMLHttpRequest()
  87. xml.open('GET', http.domain + url)
  88. xml.setRequestHeader('Content-Type', 'application/json');
  89. xml.send();
  90. var array: String[] = ['loadstart', 'abort', 'error', 'load', 'loadend', 'timeout'];
  91. array.forEach(function (eventName) {
  92. xml[('on' + eventName) as 'onloadstart' | 'onabort' | 'onerror' | 'onload' | 'onloadend' | 'ontimeout'] = function () {
  93. // var str = '\nEvent : ' + eventName;
  94. var lstr = ""
  95. if (eventName === 'timeout') {
  96. lstr += '(timeout)';
  97. }
  98. else if (eventName === 'loadend') {
  99. lstr += '...loadend!';
  100. } else if (eventName === 'onerror') {
  101. }
  102. };
  103. });
  104. // Special event
  105. xml.onreadystatechange = function () {
  106. if (xml.readyState === 4 && xml.status >= 200) {
  107. call_back(null, xml.responseText);
  108. } else if (xml.status === 404) {
  109. call_back('404 page not found!', null);
  110. }
  111. };
  112. }
  113. public static run_get_static(url, call_back) {
  114. var xml = new XMLHttpRequest()
  115. xml.open('GET', http.static_domain + url)
  116. xml.setRequestHeader('Content-Type', 'application/json');
  117. xml.send();
  118. var array: String[] = ['loadstart', 'abort', 'error', 'load', 'loadend', 'timeout'];
  119. array.forEach(function (eventName) {
  120. xml[('on' + eventName) as 'onloadstart' | 'onabort' | 'onerror' | 'onload' | 'onloadend' | 'ontimeout'] = function () {
  121. // var str = '\nEvent : ' + eventName;
  122. var lstr = ""
  123. if (eventName === 'timeout') {
  124. lstr += '(timeout)';
  125. }
  126. else if (eventName === 'loadend') {
  127. lstr += '...loadend!';
  128. } else if (eventName === 'onerror') {
  129. }
  130. };
  131. });
  132. // Special event
  133. xml.onreadystatechange = function () {
  134. if (xml.readyState === 4 && xml.status >= 200) {
  135. call_back(null, xml.responseText);
  136. } else if (xml.status === 404) {
  137. call_back('404 page not found!', null);
  138. }
  139. };
  140. }
  141. public static run_post(url, data, call_back) {
  142. var xml = new XMLHttpRequest()
  143. xml.open('POST', http.domain + url)
  144. xml.setRequestHeader('Content-Type', 'application/json');
  145. xml.setRequestHeader('token', config.TOKEN);
  146. xml.send(JSON.stringify(data));
  147. var array: String[] = ['loadstart', 'abort', 'error', 'load', 'loadend', 'timeout'];
  148. array.forEach(function (eventName) {
  149. xml[('on' + eventName) as 'onloadstart' | 'onabort' | 'onerror' | 'onload' | 'onloadend' | 'ontimeout'] = function () {
  150. var str = '\nEvent : ' + eventName;
  151. var lstr = ""
  152. if (eventName === 'timeout') {
  153. lstr += '(timeout)';
  154. }
  155. else if (eventName === 'loadend') {
  156. lstr += '...loadend!';
  157. } else if (eventName === 'onerror') {
  158. }
  159. };
  160. });
  161. // Special event
  162. xml.onreadystatechange = function () {
  163. if (xml.readyState === 4 && xml.status >= 200) {
  164. call_back(null, xml.responseText);
  165. } else if (xml.status === 404) {
  166. call_back('404 page not found!', null);
  167. }
  168. };
  169. }
  170. static statistics_post(url, data, call_back) {
  171. if (sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  172. const options = {
  173. url: http.statistics_domain + url,
  174. data: data,
  175. header: {
  176. "content": "application/x-www-form-urlencoded",
  177. },
  178. method: "POST", //指定对应的网络请求方法,详见文档中method枚举值
  179. enableCache: false, //是否开启 cache ,不填默认为false
  180. timeout: 60000, //超时时间,单位为毫秒(最大值 60000ms)
  181. }
  182. // console.log('tt_options=',options)
  183. let task = tt.request({
  184. ...options,
  185. success(res) { //请求成功回调
  186. console.log("请求成功", res.data);
  187. call_back(null, JSON.stringify(res.data));
  188. },
  189. fail(res) {//请求失败回调
  190. console.log("请求失败", res);
  191. call_back(-1, null);
  192. },
  193. complete(res) { //请求结束回调
  194. console.log("调用接口结束", res.data)
  195. }
  196. });
  197. } else {
  198. var xml = new XMLHttpRequest()
  199. xml.open('POST', http.statistics_domain + url)
  200. xml.setRequestHeader('Content-Type', 'application/json');
  201. xml.setRequestHeader('token', config.TOKEN);
  202. xml.send(JSON.stringify(data));
  203. var array: String[] = ['loadstart', 'abort', 'error', 'load', 'loadend', 'timeout'];
  204. array.forEach(function (eventName) {
  205. xml[('on' + eventName) as 'onloadstart' | 'onabort' | 'onerror' | 'onload' | 'onloadend' | 'ontimeout'] = function () {
  206. var str = '\nEvent : ' + eventName;
  207. var lstr = ""
  208. if (eventName === 'timeout') {
  209. lstr += '(timeout)';
  210. }
  211. else if (eventName === 'loadend') {
  212. lstr += '...loadend!';
  213. } else if (eventName === 'onerror') {
  214. }
  215. };
  216. });
  217. // Special event
  218. xml.onreadystatechange = function () {
  219. // console.log('xml.readyState=',xml.readyState, 'xml.status=',xml.status, 'xml.responseText=',xml.responseText)
  220. if (xml.readyState === 4 && xml.status >= 200) {
  221. call_back(null, xml.responseText);
  222. } else if (xml.status === 404) {
  223. call_back('404 page not found!', null);
  224. }
  225. };
  226. }
  227. }
  228. }