http.ts 10 KB

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