http.ts 7.1 KB

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