HttpHelp.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { _decorator, Component, Node } from 'cc';
  2. import { gameManager } from './gameManager';
  3. import { uiManager } from '../framework/uiManager';
  4. const { ccclass, property } = _decorator;
  5. @ccclass('HttpHelp')
  6. export class HttpHelp {
  7. /**
  8. * 请求地址的域名配置
  9. */
  10. static HttpsUrlTest: string = "http://testapi.mstardance.com/api/";
  11. /**
  12. * 请求地址的域名配置
  13. */
  14. static HttpsUrlRelpse: string = "http://api.mstardance.com/api/"; //屏蔽掉,正式的也是测试//"http://star.mstardance.com/api/";
  15. static httpGet(url: string, callback: Function, token: string = null!, bind: any = null) {
  16. url = (gameManager.Instance.golabMemory.isReplse?HttpHelp.HttpsUrlRelpse:HttpHelp.HttpsUrlTest) + url;
  17. console.log('请求地址是:', url);
  18. let xhr = new XMLHttpRequest();
  19. xhr.onreadystatechange = function () {
  20. if (xhr.readyState === 4 && xhr.status == 200) {
  21. let respone = xhr.responseText;
  22. try {
  23. if (bind != null) {
  24. callback.call(bind, respone);
  25. } else {
  26. callback(respone);
  27. }
  28. } catch (error3) {
  29. }
  30. } else {
  31. // console.error(xhr.readyState+"httpGet 失败:"+xhr.status);
  32. }
  33. };
  34. xhr.withCredentials = true;
  35. xhr.timeout = 5000;// 5 seconds for timeout
  36. xhr.open('GET', url, true);
  37. xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
  38. xhr.setRequestHeader('Access-Control-Allow-Methods', 'GET, POST');
  39. xhr.setRequestHeader('Access-Control-Allow-Headers', 'x-requested-with,content-type,authorization');
  40. xhr.setRequestHeader("Content-Type", "application/json");
  41. if (token && token.length > 3) {
  42. xhr.setRequestHeader("accessToken", token);
  43. }
  44. xhr.send();
  45. }
  46. /**
  47. * post请求
  48. * @param {string} url
  49. * @param {object} params
  50. * @param {function} callback
  51. */
  52. static httpPost(url: string, params: any, callback: Function, token: string = null!, bind: any = null) {
  53. url = (gameManager.Instance.golabMemory.isReplse?HttpHelp.HttpsUrlRelpse:HttpHelp.HttpsUrlTest) + url;
  54. let xhr = new XMLHttpRequest();
  55. // try{
  56. xhr.onreadystatechange = function () {
  57. // console.log('xhr.readyState=' + xhr.readyState + ' xhr.status=' + xhr.status);
  58. if (xhr.readyState === 4) {
  59. let respone = xhr.responseText;
  60. if (xhr.status == 200) {
  61. try {
  62. if (bind != null) {
  63. callback.call(bind, respone);
  64. } else {
  65. callback(respone);
  66. }
  67. } catch (error2) {
  68. }
  69. } else {
  70. try {
  71. if (bind != null) {
  72. callback.call(bind, "-1");
  73. } else {
  74. callback("-1");
  75. }
  76. } catch (error1) {
  77. }
  78. }
  79. }
  80. };
  81. xhr.timeout = 5000;// 5 seconds for timeout
  82. xhr.open('POST', url, true);
  83. xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
  84. xhr.setRequestHeader('Access-Control-Allow-Methods', 'POST');
  85. // xhr.setRequestHeader('Access-Control-Allow-Headers', 'x-requested-with,content-type');
  86. xhr.setRequestHeader("Content-type", "application/json");
  87. if (token && token.length > 3) {
  88. xhr.setRequestHeader("accessToken", token);
  89. }
  90. xhr.send(params);
  91. // } catch (error1) {
  92. // uiManager.instance.showTips('网络异常,请稍微再试!data==-2');
  93. // }
  94. }
  95. }