http.ts 10 KB

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