http.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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_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', http.domain + 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. // let request_url = http.static_domain + url + "?" + Date.now()
  119. let request_url = http.static_domain + url
  120. xml.open('GET', request_url)
  121. xml.setRequestHeader('Content-Type', 'application/json');
  122. xml.send();
  123. var array: String[] = ['loadstart', 'abort', 'error', 'load', 'loadend', 'timeout'];
  124. array.forEach(function (eventName) {
  125. xml[('on' + eventName) as 'onloadstart' | 'onabort' | 'onerror' | 'onload' | 'onloadend' | 'ontimeout'] = function () {
  126. // var str = '\nEvent : ' + eventName;
  127. var lstr = ""
  128. if (eventName === 'timeout') {
  129. lstr += '(timeout)';
  130. }
  131. else if (eventName === 'loadend') {
  132. lstr += '...loadend!';
  133. } else if (eventName === 'onerror') {
  134. }
  135. };
  136. });
  137. // Special event
  138. xml.onreadystatechange = function () {
  139. if (xml.readyState === 4 && xml.status >= 200) {
  140. call_back(null, xml.responseText);
  141. } else if (xml.status === 404) {
  142. call_back('404 page not found!', null);
  143. }
  144. };
  145. }
  146. public static run_post(url, data, call_back) {
  147. var xml = new XMLHttpRequest()
  148. xml.open('POST', http.domain + url)
  149. xml.setRequestHeader('Content-Type', 'application/json');
  150. xml.setRequestHeader('token', config.TOKEN);
  151. xml.send(JSON.stringify(data));
  152. var array: String[] = ['loadstart', 'abort', 'error', 'load', 'loadend', 'timeout'];
  153. array.forEach(function (eventName) {
  154. xml[('on' + eventName) as 'onloadstart' | 'onabort' | 'onerror' | 'onload' | 'onloadend' | 'ontimeout'] = function () {
  155. var str = '\nEvent : ' + eventName;
  156. var lstr = ""
  157. if (eventName === 'timeout') {
  158. lstr += '(timeout)';
  159. }
  160. else if (eventName === 'loadend') {
  161. lstr += '...loadend!';
  162. } else if (eventName === 'onerror') {
  163. }
  164. };
  165. });
  166. // Special event
  167. xml.onreadystatechange = function () {
  168. if (xml.readyState === 4 && xml.status >= 200) {
  169. call_back(null, xml.responseText);
  170. } else if (xml.status === 404) {
  171. call_back('404 page not found!', null);
  172. }
  173. };
  174. }
  175. static statistics_post(url, data, call_back) {
  176. if (sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  177. const options = {
  178. url: http.statistics_domain + url,
  179. data: data,
  180. header: {
  181. "content": "application/x-www-form-urlencoded",
  182. },
  183. method: "POST", //指定对应的网络请求方法,详见文档中method枚举值
  184. enableCache: false, //是否开启 cache ,不填默认为false
  185. timeout: 60000, //超时时间,单位为毫秒(最大值 60000ms)
  186. }
  187. // console.log('tt_options=',options)
  188. let task = tt.request({
  189. ...options,
  190. success(res) { //请求成功回调
  191. console.log("请求成功", res.data);
  192. call_back(null, JSON.stringify(res.data));
  193. },
  194. fail(res) {//请求失败回调
  195. console.log("请求失败", res);
  196. call_back(-1, null);
  197. },
  198. complete(res) { //请求结束回调
  199. console.log("调用接口结束", res.data)
  200. }
  201. });
  202. } else {
  203. var xml = new XMLHttpRequest()
  204. xml.open('POST', http.statistics_domain + url)
  205. xml.setRequestHeader('Content-Type', 'application/json');
  206. xml.setRequestHeader('token', config.TOKEN);
  207. xml.send(JSON.stringify(data));
  208. var array: String[] = ['loadstart', 'abort', 'error', 'load', 'loadend', 'timeout'];
  209. array.forEach(function (eventName) {
  210. xml[('on' + eventName) as 'onloadstart' | 'onabort' | 'onerror' | 'onload' | 'onloadend' | 'ontimeout'] = function () {
  211. var str = '\nEvent : ' + eventName;
  212. var lstr = ""
  213. if (eventName === 'timeout') {
  214. lstr += '(timeout)';
  215. }
  216. else if (eventName === 'loadend') {
  217. lstr += '...loadend!';
  218. } else if (eventName === 'onerror') {
  219. }
  220. };
  221. });
  222. // Special event
  223. xml.onreadystatechange = function () {
  224. // console.log('xml.readyState=',xml.readyState, 'xml.status=',xml.status, 'xml.responseText=',xml.responseText)
  225. if (xml.readyState === 4 && xml.status >= 200) {
  226. call_back(null, xml.responseText);
  227. } else if (xml.status === 404) {
  228. call_back('404 page not found!', null);
  229. }
  230. };
  231. }
  232. }
  233. }