http.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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_wxopen_id(): string {
  61. return `/note/user/get_wxopen_id`
  62. }
  63. public static get_login(): string {
  64. return `/note/user/login`
  65. }
  66. public static get_sys_config(): string {
  67. return `/smistatic/sys_config.json`
  68. }
  69. public static get_level_info(id: number): string {
  70. return `/smistatic/level_info/${id}.json`
  71. }
  72. //场景 scene 0=共用 1 = 场景1 2 = 场景2,
  73. public static get_level_resource(id: number, scene: number): string {
  74. return `/smistatic/resource/${id}_${scene}.json`
  75. }
  76. public static get_test_user_list(): string {
  77. return `/smistatic/user_test.json`
  78. }
  79. public static sync_data() {
  80. return `/note/user/sync_data`
  81. }
  82. public static statistics_ads() {
  83. return `/smadspush`
  84. }
  85. public static user_unlock_number_status() {
  86. return `/note/user/unlock_number_status`
  87. }
  88. public static run_get(url, call_back) {
  89. var xml = new XMLHttpRequest()
  90. xml.open('GET', url)
  91. xml.setRequestHeader('Content-Type', 'application/json');
  92. xml.send();
  93. var array: String[] = ['loadstart', 'abort', 'error', 'load', 'loadend', 'timeout'];
  94. array.forEach(function (eventName) {
  95. xml[('on' + eventName) as 'onloadstart' | 'onabort' | 'onerror' | 'onload' | 'onloadend' | 'ontimeout'] = function () {
  96. // var str = '\nEvent : ' + eventName;
  97. var lstr = ""
  98. if (eventName === 'timeout') {
  99. lstr += '(timeout)';
  100. }
  101. else if (eventName === 'loadend') {
  102. lstr += '...loadend!';
  103. } else if (eventName === 'onerror') {
  104. }
  105. };
  106. });
  107. // Special event
  108. xml.onreadystatechange = function () {
  109. if (xml.readyState === 4 && xml.status >= 200) {
  110. call_back(null, xml.responseText);
  111. } else if (xml.status === 404) {
  112. call_back('404 page not found!', null);
  113. }
  114. };
  115. }
  116. public static run_get_static(url, call_back) {
  117. var xml = new XMLHttpRequest()
  118. xml.open('GET', http.static_domain + url)
  119. xml.setRequestHeader('Content-Type', 'application/json');
  120. xml.send();
  121. var array: String[] = ['loadstart', 'abort', 'error', 'load', 'loadend', 'timeout'];
  122. array.forEach(function (eventName) {
  123. xml[('on' + eventName) as 'onloadstart' | 'onabort' | 'onerror' | 'onload' | 'onloadend' | 'ontimeout'] = function () {
  124. // var str = '\nEvent : ' + eventName;
  125. var lstr = ""
  126. if (eventName === 'timeout') {
  127. lstr += '(timeout)';
  128. }
  129. else if (eventName === 'loadend') {
  130. lstr += '...loadend!';
  131. } else if (eventName === 'onerror') {
  132. }
  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. }
  142. };
  143. }
  144. public static run_post(url, data, call_back) {
  145. var xml = new XMLHttpRequest()
  146. xml.open('POST', http.domain + url)
  147. xml.setRequestHeader('Content-Type', 'application/json');
  148. xml.setRequestHeader('token', config.TOKEN);
  149. xml.send(JSON.stringify(data));
  150. var array: String[] = ['loadstart', 'abort', 'error', 'load', 'loadend', 'timeout'];
  151. array.forEach(function (eventName) {
  152. xml[('on' + eventName) as 'onloadstart' | 'onabort' | 'onerror' | 'onload' | 'onloadend' | 'ontimeout'] = function () {
  153. var str = '\nEvent : ' + eventName;
  154. var lstr = ""
  155. if (eventName === 'timeout') {
  156. lstr += '(timeout)';
  157. }
  158. else if (eventName === 'loadend') {
  159. lstr += '...loadend!';
  160. } else if (eventName === 'onerror') {
  161. }
  162. };
  163. });
  164. // Special event
  165. xml.onreadystatechange = function () {
  166. if (xml.readyState === 4 && xml.status >= 200) {
  167. call_back(null, xml.responseText);
  168. } else if (xml.status === 404) {
  169. call_back('404 page not found!', null);
  170. }
  171. };
  172. }
  173. static statistics_post(url, data, call_back) {
  174. if (sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  175. const options = {
  176. url: http.statistics_domain + url,
  177. data: data,
  178. header: {
  179. "content": "application/x-www-form-urlencoded",
  180. },
  181. method: "POST", //指定对应的网络请求方法,详见文档中method枚举值
  182. enableCache: false, //是否开启 cache ,不填默认为false
  183. timeout: 60000, //超时时间,单位为毫秒(最大值 60000ms)
  184. }
  185. // console.log('tt_options=',options)
  186. let task = tt.request({
  187. ...options,
  188. success(res) { //请求成功回调
  189. console.log("请求成功", res.data);
  190. call_back(null, JSON.stringify(res.data));
  191. },
  192. fail(res) {//请求失败回调
  193. console.log("请求失败", res);
  194. call_back(-1, null);
  195. },
  196. complete(res) { //请求结束回调
  197. console.log("调用接口结束", res.data)
  198. }
  199. });
  200. } else {
  201. var xml = new XMLHttpRequest()
  202. xml.open('POST', http.statistics_domain + url)
  203. xml.setRequestHeader('Content-Type', 'application/json');
  204. xml.setRequestHeader('token', config.TOKEN);
  205. xml.send(JSON.stringify(data));
  206. var array: String[] = ['loadstart', 'abort', 'error', 'load', 'loadend', 'timeout'];
  207. array.forEach(function (eventName) {
  208. xml[('on' + eventName) as 'onloadstart' | 'onabort' | 'onerror' | 'onload' | 'onloadend' | 'ontimeout'] = function () {
  209. var str = '\nEvent : ' + eventName;
  210. var lstr = ""
  211. if (eventName === 'timeout') {
  212. lstr += '(timeout)';
  213. }
  214. else if (eventName === 'loadend') {
  215. lstr += '...loadend!';
  216. } else if (eventName === 'onerror') {
  217. }
  218. };
  219. });
  220. // Special event
  221. xml.onreadystatechange = function () {
  222. // console.log('xml.readyState=',xml.readyState, 'xml.status=',xml.status, 'xml.responseText=',xml.responseText)
  223. if (xml.readyState === 4 && xml.status >= 200) {
  224. call_back(null, xml.responseText);
  225. } else if (xml.status === 404) {
  226. call_back('404 page not found!', null);
  227. }
  228. };
  229. }
  230. }
  231. }