http.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { _decorator, Component, Node, sys } from 'cc';
  2. import { config } from '../config';
  3. import { gameManager } from '../gameManager';
  4. const { ccclass, property } = _decorator;
  5. @ccclass('http')
  6. export class http extends Component {
  7. static get(url,call_back){
  8. try{
  9. if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME){
  10. // let new_data = {}
  11. // _data.forEach((value: FormDataEntryValue, key: string, parent: FormData)=>{
  12. // new_data[key] = value;
  13. // })
  14. // console.log("传送的参数",_data)
  15. const options = {
  16. url: config.domain+url, //请求地址
  17. method: "GET", //指定对应的网络请求方法,详见文档中method枚举值
  18. // dataType: "json", // 指定返回数据的类型为 json。不填默认为json
  19. enableCache: false, //是否开启 cache ,不填默认为false
  20. // responseType: "text", //期望响应的数据类型
  21. timeout: 60000, //超时时间,单位为毫秒(最大值 60000ms)
  22. }
  23. let task = tt.request({
  24. ...options,
  25. success(res) { //请求成功回调
  26. // console.log("请求成功", res.data);
  27. call_back(null,JSON.stringify(res.data));
  28. },
  29. fail(res) {//请求失败回调
  30. call_back( -1,null);
  31. console.log("请求失败", res.errMsg);
  32. },
  33. complete(res){ //请求结束回调
  34. console.log("调用接口结束",res.data)
  35. }
  36. });
  37. }else{
  38. var xml = new XMLHttpRequest()
  39. xml.open('GET',config.domain+url)
  40. xml.setRequestHeader('Content-Type','text/plain');
  41. xml.send();
  42. // Special event
  43. xml.onreadystatechange = function () {
  44. console.log("e === ",xml)
  45. if (xml.readyState === 4 && xml.status >= 200) {
  46. //label.string = handler(xml.responseText);
  47. call_back(null,xml.responseText);
  48. } else if (xml.status === 404) {
  49. call_back('404 page not found!',null);
  50. }
  51. else if (xml.readyState === 3) {
  52. call_back('Request dealing!',null);
  53. } else if (xml.readyState === 2) {
  54. call_back('Request received!',null);
  55. } else if (xml.readyState === 1) {
  56. call_back('Server connection established! Request hasn\'t been received',null);
  57. } else if (xml.readyState === 0) {
  58. call_back( 'Request hasn\'t been initiated!',null);
  59. }else if (xml.readyState === 4 &&xml.status === 0) {
  60. call_back( -1,null);
  61. }
  62. }
  63. };
  64. }catch(e){
  65. console.log("e === ",e)
  66. call_back(-1,null)
  67. }
  68. }
  69. static post(url,_data:any,call_back){
  70. console.log("post",sys.Platform.BYTEDANCE_MINI_GAME)
  71. console.log("platform",sys.platform)
  72. if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME){
  73. // let new_data = {}
  74. // _data.forEach((value: FormDataEntryValue, key: string, parent: FormData)=>{
  75. // new_data[key] = value;
  76. // })
  77. // console.log("传送的参数",_data)
  78. const options = {
  79. url: config.domain+url, //请求地址
  80. data: _data,
  81. header: {
  82. "token": gameManager.userInfo.token,
  83. },
  84. method: "POST", //指定对应的网络请求方法,详见文档中method枚举值
  85. // dataType: "json", // 指定返回数据的类型为 json。不填默认为json
  86. enableCache: false, //是否开启 cache ,不填默认为false
  87. // responseType: "text", //期望响应的数据类型
  88. timeout: 60000, //超时时间,单位为毫秒(最大值 60000ms)
  89. }
  90. let task = tt.request({
  91. ...options,
  92. success(res) { //请求成功回调
  93. // console.log("请求成功", res.data);
  94. call_back(null,JSON.stringify(res.data));
  95. },
  96. fail(res) {//请求失败回调
  97. call_back( -1,null);
  98. console.log("请求失败", res.errMsg);
  99. },
  100. complete(res){ //请求结束回调
  101. console.log("调用接口结束",res.data)
  102. }
  103. });
  104. }else{
  105. var xml = new XMLHttpRequest()
  106. xml.open('POST',config.domain+url)
  107. // xml.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  108. xml.setRequestHeader('token',gameManager.userInfo.token);
  109. xml.send(_data);
  110. // Special event
  111. xml.onreadystatechange = function () {
  112. if (xml.readyState === 4 && xml.status >= 200) {
  113. //label.string = handler(xml.responseText);
  114. call_back(null,xml.responseText);
  115. } else if (xml.status === 404) {
  116. call_back('404 page not found!',null);
  117. } else if (xml.readyState === 3) {
  118. call_back('Request dealing!',null);
  119. } else if (xml.readyState === 2) {
  120. call_back('Request received!',null);
  121. } else if (xml.readyState === 1) {
  122. call_back('Server connection established! Request hasn\'t been received',null);
  123. } else if (xml.readyState === 0) {
  124. call_back( 'Request hasn\'t been initiated!',null);
  125. }else if (xml.readyState === 4 &&xml.status === 0) {
  126. call_back( -1,null);
  127. }
  128. };
  129. }
  130. }
  131. }