UtilsHttp.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**
  2. * Created by MSIK on 2018/12/22.
  3. */
  4. window.userData = {
  5. httpData : {
  6. data: {
  7. avatarUrl: "/media/img/oGxOa5SQ1ltBWWsVRirUvXUBGRRY.png",
  8. city: "Harbin",
  9. gender: 1,
  10. nickName: "汪汪Husky",
  11. openid: "oGxOa5SQ1ltBWWsVRirUvXUBGRRY",
  12. province: "Heilongjiang"
  13. },
  14. },
  15. // oGxOa5bd61v9jI_indIdQtI2T5P8 大佬
  16. // oGxOa5bjKLaCSbkPev09yVze0WSA 晓明
  17. // oGxOa5del9BkzJ3e8fLVzI4mlvEI 小狐仙
  18. // oGxOa5eZUuWCNCoEUQIjtu2uN9BM 9527
  19. // oGxOa5SQ1ltBWWsVRirUvXUBGRRY 汪汪
  20. openId: "oGxOa5bjKLaCSbkPev09yVze0WSA"
  21. }
  22. window.httpUtils = {
  23. http : "https://www.yuyekeji.cn/spring/api/",
  24. httpGetTable: "https://www.yuyekeji.cn/spring/api/getTables",
  25. httpEarthTest : "http://127.0.0.1:8011/api/EarthTest/0",
  26. httpEarth : "http://rest.xukening.cn/speedBall/getWanderingEarthVariables",
  27. setUserData : "https://www.yuyekeji.cn/Iamrichman_app/setUserData/",
  28. // setUserData : "https://www.youaigclm.top/Iamrichman_app/setUserData/",
  29. // getUserData : "https://www.youaigclm.top/api/getJson",
  30. getUserData : "https://www.yuyekeji.cn/Iamrichman_app/getUserData/",
  31. // getUserData : "https://www.youaigclm.top/Iamrichman_app/getUserData/",
  32. PlayButton : "https://www.youaigclm.top/media/PlayButtonnew.png",
  33. Share : "https://www.youaigclm.top/media/Share.png",
  34. // extends: cc.Component,
  35. //
  36. // properties: {
  37. // // foo: {
  38. // // default: null, // The default value will be used only when the component attaching
  39. // // to a node for the first time
  40. // // url: cc.Texture2D, // optional, default is typeof default
  41. // // serializable: true, // optional, default is true
  42. // // visible: true, // optional, default is true
  43. // // displayName: 'Foo', // optional
  44. // // readonly: false, // optional, default is false
  45. // // },
  46. // // ...
  47. // },
  48. //
  49. // statics: {
  50. // instance: null
  51. // },
  52. // use this for initialization
  53. onLoad: function () {
  54. },
  55. // get: function (url, callbackSuccess,callbackFail) {
  56. // var xhr = cc.loader.getXMLHttpRequest();
  57. //
  58. // cc.log("调用了吗 请求");
  59. // xhr.onreadystatechange = function () {
  60. // if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) {
  61. // var respone = xhr.responseText;
  62. // callbackSuccess(respone);
  63. // }else{
  64. // callbackFail();
  65. // }
  66. // }.bind(this);
  67. // xhr.open("GET", url, true);
  68. // if (cc.sys.isNative) {
  69. // xhr.setRequestHeader("Accept-Encoding", "gzip,deflate");
  70. // }
  71. //
  72. // // note: In Internet Explorer, the timeout property may be set only after calling the open()
  73. // // method and before calling the send() method.
  74. // xhr.timeout = 5000;// 5 seconds for timeout
  75. //
  76. // xhr.send();
  77. // },
  78. Get: function(url,reqData,callbackSuccess,callbackFail){
  79. var self = this;
  80. if (reqData!=null) {
  81. url += "?";
  82. for(var item in reqData){
  83. url += item +"=" +reqData[item] +"&";
  84. }
  85. }
  86. // console.log(self.ip + url)
  87. var xhr = new XMLHttpRequest();
  88. xhr.onreadystatechange = function () {
  89. if (xhr.readyState == 4){
  90. if(xhr.status >= 200 && xhr.status < 400){
  91. var response = xhr.responseText;
  92. // console.log(response)
  93. if(response){
  94. var responseJson = JSON.parse(response);
  95. callbackSuccess(responseJson);
  96. }else{
  97. // console.log("返回数据不存在")
  98. callbackFail();
  99. }
  100. }else{
  101. // console.log("请求失败")
  102. callbackFail();
  103. }
  104. }
  105. };
  106. xhr.open("GET", url, true);
  107. xhr.send();
  108. },
  109. /**
  110. * var data = [];
  111. * data["img"] = httpImag;
  112. * @param url
  113. * @param reqData
  114. * @param callbackSuccess
  115. * @param callbackFail
  116. * @constructor
  117. */
  118. Post: function (url, reqData, callbackSuccess,callbackFail) {
  119. var self = this;
  120. // console.log(url)
  121. // console.log(reqData)
  122. //1.拼接请求参数
  123. var param = "";
  124. for(var item in reqData){
  125. param += item + "=" + reqData[item] + "&";
  126. }
  127. //2.发起请求
  128. var xhr = new XMLHttpRequest();
  129. xhr.onreadystatechange = function () {
  130. if (xhr.readyState == 4){
  131. if(xhr.status >= 200 && xhr.status < 400){
  132. var response = xhr.responseText;
  133. // console.log(response)
  134. if(response){
  135. var responseJson = JSON.parse(response);
  136. callbackSuccess(responseJson);
  137. }else{
  138. // console.log("返回数据不存在")
  139. callbackFail();
  140. }
  141. }else{
  142. // console.log("请求失败")
  143. callbackFail(false);
  144. }
  145. }
  146. };
  147. xhr.open("POST", url, true);
  148. xhr.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded");
  149. xhr.send(param);//reqData为字符串形式: "key=value"
  150. },
  151. //
  152. // post: function (url, params, callbackSuccess,callbackFail) {
  153. // var xhr = cc.loader.getXMLHttpRequest();
  154. // xhr.onreadystatechange = function () {
  155. // cc.log('xhr.readyState='+xhr.readyState+' xhr.status='+xhr.status);
  156. // if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) {
  157. // var respone = xhr.responseText;
  158. // callbackSuccess(respone);
  159. // }else{
  160. // callbackFail();
  161. // }
  162. // }.bind(this);
  163. // xhr.open("POST", url, true);
  164. // if (cc.sys.isNative) {
  165. // xhr.setRequestHeader("Accept-Encoding", "gzip,deflate");
  166. // }
  167. //
  168. // // note: In Internet Explorer, the timeout property may be set only after calling the open()
  169. // // method and before calling the send() method.
  170. // xhr.timeout = 5000;// 5 seconds for timeout
  171. //
  172. // xhr.send(params);
  173. // }
  174. };
  175. // httpUtils.getInstance = function () {
  176. // if (httpUtils.instance == null) {
  177. // httpUtils.instance = new httpUtils();
  178. // }
  179. // return httpUtils.instance;
  180. // };
  181. // module.exports = httpUtils;