http.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import { _decorator, Component, Node, sys, url } from 'cc';
  2. import { config } from './config';
  3. import { tools } from './tools';
  4. import { userDataManager } from './manager/userDataManager';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('http')
  7. export class http {
  8. public static domain = "https://zcapi.xwrun.com"
  9. public static static_domain = "https://zcapi.xwrun.com"
  10. public static statistics_domain = "https://zcapi.xwrun.com"
  11. public static post(url,opt, call_back,method:string='POST') {
  12. var xml = new XMLHttpRequest()
  13. let request_url = http.domain+url
  14. xml.open(method, request_url)
  15. xml.setRequestHeader('Content-Type', 'application/json');
  16. if(userDataManager.user_data!=null){
  17. xml.setRequestHeader('token',userDataManager.user_data.token);
  18. }
  19. if(opt==null){
  20. xml.send();
  21. }else{
  22. xml.send(JSON.stringify(opt));
  23. }
  24. // xml.send(opt);
  25. var array: String[] = ['loadstart', 'abort', 'error', 'load', 'loadend', 'timeout'];
  26. array.forEach(function (eventName) {
  27. xml[('on' + eventName) as 'onloadstart' | 'onabort' | 'onerror' | 'onload' | 'onloadend' | 'ontimeout'] = function () {
  28. // var str = '\nEvent : ' + eventName;
  29. var lstr = ""
  30. if (eventName === 'timeout') {
  31. lstr += '(timeout)';
  32. }
  33. else if (eventName === 'loadend') {
  34. lstr += '...loadend!';
  35. } else if (eventName === 'onerror') {
  36. }
  37. };
  38. });
  39. // Special event
  40. xml.onreadystatechange = function () {
  41. if (xml.readyState === 4 && xml.status >= 200) {
  42. call_back(null, xml.responseText);
  43. } else if (xml.status === 404) {
  44. call_back('404 page not found!', null);
  45. }
  46. };
  47. }
  48. public static get(url, call_back) {
  49. var xml = new XMLHttpRequest()
  50. let request_url = http.static_domain+url
  51. xml.open('GET', request_url)
  52. xml.setRequestHeader('Content-Type', 'application/json');
  53. xml.send();
  54. var array: String[] = ['loadstart', 'abort', 'error', 'load', 'loadend', 'timeout'];
  55. array.forEach(function (eventName) {
  56. xml[('on' + eventName) as 'onloadstart' | 'onabort' | 'onerror' | 'onload' | 'onloadend' | 'ontimeout'] = function () {
  57. // var str = '\nEvent : ' + eventName;
  58. var lstr = ""
  59. if (eventName === 'timeout') {
  60. lstr += '(timeout)';
  61. }
  62. else if (eventName === 'loadend') {
  63. lstr += '...loadend!';
  64. } else if (eventName === 'onerror') {
  65. }
  66. };
  67. });
  68. // Special event
  69. xml.onreadystatechange = function () {
  70. if (xml.readyState === 4 && xml.status >= 200) {
  71. call_back(null, xml.responseText);
  72. } else if (xml.status === 404) {
  73. call_back('404 page not found!', null);
  74. }
  75. };
  76. }
  77. public static uploadFile(url, filePath, progress_call_back, call_back) {
  78. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  79. let task = tt.uploadFile({
  80. url: http.domain + url,
  81. filePath: filePath,
  82. name: "file",
  83. success(res) {
  84. if (res.statusCode === 200) {
  85. // console.log(`uploadFile调用成功 ${res.data}`);
  86. call_back(null,res.data)
  87. } else {
  88. call_back(res.statusCode,null)
  89. }
  90. },
  91. fail(err) {
  92. console.log(`uploadFile调用失败`,err);
  93. call_back(err,null)
  94. },
  95. });
  96. task.onProgressUpdate((res) => {
  97. // console.log('res.progress=',res.progress)
  98. if(progress_call_back) {
  99. progress_call_back(res.progress)
  100. }
  101. });
  102. return
  103. }
  104. // var xml = new XMLHttpRequest()
  105. // let request_url = http.domain+url
  106. // console.log('request_url=',request_url)
  107. // xml.open('POST', request_url, true)
  108. // xml.setRequestHeader('Content-Type', 'multipart/form-data');
  109. // if(userDataManager.user_data!=null){
  110. // xml.setRequestHeader('token',userDataManager.user_data.token);
  111. // }
  112. // //创建一个FormData对象来处理文件上传
  113. // let formData = new FormData()
  114. // formData.append("file", filePath)
  115. // //发送请求
  116. // xml.send(formData)
  117. // // 监听上传进度事件
  118. // xml.upload.onprogress = function(event) {
  119. // if (event.lengthComputable) {
  120. // var percentComplete = (event.loaded / event.total) * 100;
  121. // console.log(Math.round(percentComplete) + "% uploaded");
  122. // }
  123. // };
  124. // // 监听请求完成事件
  125. // xml.onload = function() {
  126. // if (xml.status === 200) {
  127. // console.log("File uploaded successfully");
  128. // } else {
  129. // console.log("Upload failed with status: " + xml.status);
  130. // }
  131. // }
  132. // // 监听错误事件
  133. // xml.onerror = function() {
  134. // console.log("Network error occurred");
  135. // };
  136. // // Special event
  137. // xml.onreadystatechange = function () {
  138. // console.log('xml.readyState=',xml.readyState,' xml.status=',xml.status)
  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. }