UtilsHttp.js 6.4 KB

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