UtilsHttp.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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: "7OrrLHPIC7ZCggDb"
  21. }
  22. window.httpUtils = {
  23. http : "https://www.yuyekeji.cn/",
  24. // getUserData : "https://www.youaigclm.top/api/getJson",
  25. //
  26. PlayButton : "https://www.youaigclm.top/media/PlayButtonnew.png",
  27. Share : "https://www.youaigclm.top/media/Share.png",
  28. // login : "http://192.168.4.119:8011/api/login/",
  29. // setUserData : "http://192.168.4.119:8011/api/setUserData/",
  30. // getUserData : "http://192.168.4.119:8011/api/getUserData/",
  31. login : "https://www.youaigclm.top/spring/api/login/",
  32. setUserData : "https://www.youaigclm.top/spring/api/setUserData/",
  33. getUserData : "https://www.youaigclm.top/spring/api/getUserData/",
  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. url += "?";
  81. for(var item in reqData){
  82. url += item +"=" +reqData[item] +"&";
  83. }
  84. // console.log(self.ip + url)
  85. var xhr = new XMLHttpRequest();
  86. xhr.onreadystatechange = function () {
  87. if (xhr.readyState == 4){
  88. if(xhr.status >= 200 && xhr.status < 400){
  89. var response = xhr.responseText;
  90. // console.log(response)
  91. if(response){
  92. var responseJson = JSON.parse(response);
  93. callbackSuccess(responseJson);
  94. }else{
  95. // console.log("返回数据不存在")
  96. callbackFail();
  97. }
  98. }else{
  99. // console.log("请求失败")
  100. callbackFail();
  101. }
  102. }
  103. };
  104. xhr.open("GET", url, true);
  105. xhr.send();
  106. },
  107. /**
  108. * var data = [];
  109. * data["img"] = httpImag;
  110. * @param url
  111. * @param reqData
  112. * @param callbackSuccess
  113. * @param callbackFail
  114. * @constructor
  115. */
  116. Post: function (url, reqData, callbackSuccess,callbackFail) {
  117. var self = this;
  118. // console.log(url)
  119. // console.log(reqData)
  120. //1.拼接请求参数
  121. var param = "";
  122. for(var item in reqData){
  123. param += item + "=" + reqData[item] + "&";
  124. }
  125. //2.发起请求
  126. var xhr = new XMLHttpRequest();
  127. xhr.onreadystatechange = function () {
  128. if (xhr.readyState == 4){
  129. if(xhr.status >= 200 && xhr.status < 400){
  130. var response = xhr.responseText;
  131. // console.log(response)
  132. if(response){
  133. var responseJson = JSON.parse(response);
  134. callbackSuccess(responseJson);
  135. }else{
  136. // console.log("返回数据不存在")
  137. callbackFail();
  138. }
  139. }else{
  140. // console.log("请求失败")
  141. callbackFail(false);
  142. }
  143. }
  144. };
  145. xhr.open("POST", url, true);
  146. xhr.setRequestHeader("Content-Type" , "application/json");
  147. xhr.send(JSON.stringify(reqData));//reqData为字符串形式: "key=value"
  148. },
  149. //
  150. // post: function (url, params, callbackSuccess,callbackFail) {
  151. // var xhr = cc.loader.getXMLHttpRequest();
  152. // xhr.onreadystatechange = function () {
  153. // cc.log('xhr.readyState='+xhr.readyState+' xhr.status='+xhr.status);
  154. // if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) {
  155. // var respone = xhr.responseText;
  156. // callbackSuccess(respone);
  157. // }else{
  158. // callbackFail();
  159. // }
  160. // }.bind(this);
  161. // xhr.open("POST", url, true);
  162. // if (cc.sys.isNative) {
  163. // xhr.setRequestHeader("Accept-Encoding", "gzip,deflate");
  164. // }
  165. //
  166. // // note: In Internet Explorer, the timeout property may be set only after calling the open()
  167. // // method and before calling the send() method.
  168. // xhr.timeout = 5000;// 5 seconds for timeout
  169. //
  170. // xhr.send(params);
  171. // }
  172. };
  173. // httpUtils.getInstance = function () {
  174. // if (httpUtils.instance == null) {
  175. // httpUtils.instance = new httpUtils();
  176. // }
  177. // return httpUtils.instance;
  178. // };
  179. // module.exports = httpUtils;