1
0

Api.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. const ResponseStateCode = require('../utils/GameEnum').ResponseStateCode;
  2. class Api {
  3. static createImageFromUrl(avatarUrl, success, fail) {
  4. var index = avatarUrl.indexOf('https:');
  5. var httpIndex = avatarUrl.indexOf('http:');
  6. if (index === 0 || httpIndex === 0) {
  7. //以'https:'开头
  8. } else {
  9. avatarUrl = 'https:' + avatarUrl;
  10. }
  11. if (window.wx != undefined) {
  12. try {
  13. let image = wx.createImage();
  14. image.onload = () => {
  15. try {
  16. let texture = new cc.Texture2D();
  17. texture.initWithElement(image);
  18. texture.handleLoadedTexture();
  19. success(new cc.SpriteFrame(texture))
  20. } catch (e) {
  21. cc.log(e);
  22. fail();
  23. }
  24. };
  25. image.src = avatarUrl;
  26. } catch (e) {
  27. cc.log(e);
  28. fail();
  29. }
  30. } else {
  31. cc.loader.load({
  32. url: avatarUrl, type: 'jpg'
  33. }, (err, texture) => {
  34. if (err === undefined || err === null) {
  35. success(new cc.SpriteFrame(texture));
  36. } else {
  37. fail();
  38. }
  39. });
  40. }
  41. }
  42. /**
  43. * 测试网络用的方法,可以注释掉
  44. */
  45. testGetBuildingInfo() {
  46. Api.httpGet({
  47. url: '/building/getUserBuildings.do',
  48. data: {
  49. targetUid: 1,
  50. // cityId: 1
  51. },
  52. success: (response) => {
  53. // console.log('success: ' + JSON.stringify(response));
  54. },
  55. fail: (response) => {
  56. console.log('success: ' + JSON.stringify(response));
  57. },
  58. complete: () => {
  59. }
  60. });
  61. }
  62. /**
  63. * 测试网络用的方法,可以注释掉
  64. */
  65. testUpgradeBuildingInfo() {
  66. Api.httpPost({
  67. url: '/building/upGrade.do',
  68. data: {
  69. formInfo: '1'
  70. },
  71. success: (response) => {
  72. console.log('success: ' + JSON.stringify(response));
  73. },
  74. fail: (response) => {
  75. console.log('success: ' + JSON.stringify(response));
  76. },
  77. complete: () => {
  78. }
  79. });
  80. }
  81. /**
  82. * get请求对外暴露的方法
  83. * @param {*} requestBody
  84. */
  85. static httpGet(requestBody) {
  86. requestBody.data = Api.baseParam(requestBody.data);
  87. requestBody.url = Api.requestUrl(requestBody.url);
  88. if (window.wx != undefined) {
  89. Api.wxGet(requestBody);
  90. } else {
  91. Api.jsGet(requestBody);
  92. }
  93. }
  94. /**
  95. * post请求对外暴露的方法
  96. * @param {*} requestBody
  97. */
  98. static httpPost(requestBody) {
  99. requestBody.data = Api.baseParam(requestBody.data);
  100. requestBody.url = Api.requestUrl(requestBody.url);
  101. if (window.wx != undefined) {
  102. Api.wxPost(requestBody);
  103. } else {
  104. Api.jsPost(requestBody);
  105. }
  106. }
  107. static wxGet(requestBody) {
  108. let onSuccess = requestBody.success;
  109. let onFail = requestBody.fail;
  110. let onComplete = requestBody.complete;
  111. wx.request({
  112. url: requestBody.url,
  113. data: requestBody.data,
  114. method: requestBody.method === undefined ? 'GET' : 'POST',
  115. header: requestBody.header === undefined ? {} : requestBody.header,
  116. success: (res) => {
  117. //直接处理登录失效的逻辑
  118. if (res.data.code === ResponseStateCode.LOGIN_INVALIDATE) {
  119. cc.director.loadScene('login');
  120. return;
  121. }
  122. if (res.data.code === ResponseStateCode.OK) {
  123. if (onSuccess != undefined) {
  124. onSuccess(res.data.data, res.data.msg);
  125. }
  126. } else {
  127. if (onFail != undefined) {
  128. onFail(res.data.code, res.data.msg);
  129. }
  130. }
  131. },
  132. fail: (res) => {
  133. if (onFail != undefined) {
  134. onFail(-1, res.errMsg);
  135. }
  136. },
  137. complete: onComplete,
  138. });
  139. }
  140. static wxPost(requestBody) {
  141. requestBody.header = {
  142. "Content-Type": "application/x-www-form-urlencoded"
  143. };
  144. requestBody.method = 'POST';
  145. Api.wxGet(requestBody);
  146. }
  147. static jsGet(requestBody) {
  148. let url = requestBody.url;
  149. let data = requestBody.data;
  150. url = url + "?";
  151. for (var key in data) {
  152. url = url + key + '=' + data[key] + '&';
  153. }
  154. url = url.substr(0, url.length - 1)
  155. var xhr = Api.jsBaseRequest(requestBody.success, requestBody.fail, requestBody.complete);
  156. xhr.open("GET", url, true);
  157. xhr.send();
  158. }
  159. static jsPost(requestBody) {
  160. let data = requestBody.data;
  161. var xhr = Api.jsBaseRequest(requestBody.success, requestBody.fail, requestBody.complete);
  162. xhr.open("POST", requestBody.url, true);
  163. xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  164. let formData = new String();
  165. for (var key in data) {
  166. formData = formData + key + '=' + data[key] + "&";
  167. }
  168. xhr.send(formData.substr(0, formData.length - 1));
  169. }
  170. static jsBaseRequest(onSuccess, onFail, onComplete) {
  171. var xhr = new XMLHttpRequest();
  172. xhr.onreadystatechange = () => {
  173. if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
  174. // console.log('response:' + xhr.responseText);
  175. var response = JSON.parse(xhr.responseText);
  176. if (response.code == ResponseStateCode.OK) {
  177. if (onSuccess != undefined) {
  178. onSuccess(response.data, response.msg);
  179. }
  180. } else {
  181. if (onFail != undefined) {
  182. onFail(response.code, response.msg)
  183. }
  184. }
  185. if (onComplete != undefined) {
  186. onComplete();
  187. }
  188. }
  189. };
  190. return xhr;
  191. }
  192. /**
  193. * 将每个接口都需要的公用参数都放到这里
  194. * @param target [Object] 请求参数
  195. */
  196. static baseParam(target) {
  197. target.thirdType = 1;
  198. target.channel = Global.channel;
  199. target.ver = Global.ver;
  200. target.os = Global.os;
  201. target.uid = Global.user ? Global.user.uid : '';
  202. target.token = Global.user ? Global.user.token : '';
  203. return target;
  204. }
  205. /**
  206. * 自动拼接请求接口
  207. * @param targetUrl [string] 请求接口
  208. */
  209. static requestUrl(targetUrl) {
  210. let host = Global.debug ? 'https://test-api-allstar.duowan.com' : 'https://api-allstar.duowan.com';
  211. // let host = Global.debug ? 'http://172.16.15.170:8080' : 'https://api-allstar.duowan.com';
  212. return host + targetUrl;
  213. }
  214. }
  215. module.exports = Api;