http.ts 5.8 KB

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