http.ts 11 KB

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