Http.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { _decorator, Node } from "cc";
  2. import Utils from "./Utils";
  3. const { ccclass, property } = _decorator;
  4. export default class Http {
  5. private static _lastLog = "";
  6. /**
  7. * 发起一个 GET 请求
  8. * @param {string} path 请求的 URL
  9. * @param {Object} params URL 查询参数
  10. * @param {Object} headers 请求头
  11. * @returns {Promise} 请求的响应数据
  12. */
  13. static get(path, params = {}, headers = {}) {
  14. return new Promise((resolve, reject) => {
  15. // 构建 URL 查询字符串
  16. const queryString = Object.keys(params)
  17. .map(
  18. (key) =>
  19. `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`
  20. )
  21. .join("&");
  22. const xhr = new XMLHttpRequest();
  23. // 设置请求类型和 URL
  24. let fullUrl = `${path}?${queryString}`;
  25. xhr.open("GET", fullUrl, true);
  26. // 设置请求头
  27. xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
  28. for (const key in headers) {
  29. xhr.setRequestHeader(key, headers[key]);
  30. }
  31. // 设置请求超时(30秒)
  32. xhr.timeout = 8 * 1000;
  33. // 请求成功时调用
  34. xhr.onload = () => {
  35. if (xhr.status >= 200 && xhr.status < 300) {
  36. try {
  37. // 解析 JSON 响应
  38. const responseData = JSON.parse(xhr.responseText);
  39. if ("1" == Utils.getQueryString("log")) {
  40. if (Http._lastLog != fullUrl) {
  41. console.log(fullUrl);
  42. console.log(responseData);
  43. Http._lastLog = fullUrl;
  44. }
  45. }
  46. resolve(responseData);
  47. } catch (error) {
  48. console.error(error);
  49. reject(-1);
  50. }
  51. } else {
  52. reject(xhr.status);
  53. }
  54. };
  55. // 请求失败时调用
  56. xhr.onerror = () => {
  57. reject(-1);
  58. };
  59. // 超时处理
  60. xhr.ontimeout = () => {
  61. reject(-1);
  62. };
  63. // 发送请求
  64. xhr.send();
  65. });
  66. }
  67. static post(url, data, headers = {}) {
  68. return new Promise((resolve, reject) => {
  69. const xhr = new XMLHttpRequest();
  70. // 设置请求类型和 URL
  71. xhr.open("POST", `${url}`, true);
  72. // 设置请求头
  73. xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
  74. for (const key in headers) {
  75. xhr.setRequestHeader(key, headers[key]);
  76. }
  77. // 设置请求超时(30秒)
  78. xhr.timeout = 8*1000;
  79. // 请求成功时调用
  80. xhr.onload = () => {
  81. if (xhr.status >= 200 && xhr.status < 300) {
  82. try {
  83. // 解析 JSON 响应
  84. const responseData = JSON.parse(xhr.responseText);
  85. resolve(responseData);
  86. } catch (error) {
  87. reject(-1);
  88. }
  89. } else {
  90. reject(xhr.status);
  91. }
  92. };
  93. // 请求失败时调用
  94. xhr.onerror = () => {
  95. reject(-1);
  96. };
  97. // 超时处理
  98. xhr.ontimeout = () => {
  99. reject(-1);
  100. };
  101. // 发送请求
  102. xhr.send(JSON.stringify(data)); // 将数据转换为 JSON 字符串发送
  103. });
  104. }
  105. }