1
0

Api.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. Global.needLogin = true;
  120. cc.director.loadScene('login');
  121. return;
  122. }
  123. if (res.data.code === ResponseStateCode.OK) {
  124. if (onSuccess != undefined) {
  125. onSuccess(res.data.data, res.data.msg);
  126. }
  127. } else {
  128. if (onFail != undefined) {
  129. onFail(res.data.code, res.data.msg);
  130. }
  131. }
  132. },
  133. fail: (res) => {
  134. if (onFail != undefined) {
  135. onFail(-1, res.errMsg);
  136. }
  137. },
  138. complete: onComplete,
  139. });
  140. }
  141. static wxPost(requestBody) {
  142. requestBody.header = {
  143. "Content-Type": "application/x-www-form-urlencoded"
  144. };
  145. requestBody.method = 'POST';
  146. Api.wxGet(requestBody);
  147. }
  148. static jsGet(requestBody) {
  149. let url = requestBody.url;
  150. let data = requestBody.data;
  151. url = url + "?";
  152. for (var key in data) {
  153. url = url + key + '=' + data[key] + '&';
  154. }
  155. url = url.substr(0, url.length - 1)
  156. var xhr = Api.jsBaseRequest(requestBody.success, requestBody.fail, requestBody.complete);
  157. xhr.open("GET", url, true);
  158. xhr.send();
  159. }
  160. static jsPost(requestBody) {
  161. let data = requestBody.data;
  162. var xhr = Api.jsBaseRequest(requestBody.success, requestBody.fail, requestBody.complete);
  163. xhr.open("POST", requestBody.url, true);
  164. xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  165. let formData = new String();
  166. for (var key in data) {
  167. formData = formData + key + '=' + data[key] + "&";
  168. }
  169. xhr.send(formData.substr(0, formData.length - 1));
  170. }
  171. static jsBaseRequest(onSuccess, onFail, onComplete) {
  172. var xhr = new XMLHttpRequest();
  173. xhr.onreadystatechange = () => {
  174. if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
  175. // console.log('response:' + xhr.responseText);
  176. var response = JSON.parse(xhr.responseText);
  177. if (response.code == ResponseStateCode.OK) {
  178. if (onSuccess != undefined) {
  179. onSuccess(response.data, response.msg);
  180. }
  181. } else {
  182. if (onFail != undefined) {
  183. onFail(response.code, response.msg)
  184. }
  185. }
  186. if (onComplete != undefined) {
  187. onComplete();
  188. }
  189. }
  190. };
  191. return xhr;
  192. }
  193. /**
  194. * get请求对外暴露的方法
  195. * @param {*} requestBody
  196. */
  197. static httpGetPromise(url, data) {
  198. url = Api.requestUrl(url);
  199. data = Api.baseParam(data);
  200. if (window.wx != undefined) {
  201. return Api.wxGetP(url, data);
  202. } else {
  203. return Api.jsGetP(url, data);
  204. }
  205. }
  206. /**
  207. * post请求对外暴露的方法
  208. * @param {*} requestBody
  209. */
  210. static httpPostPromise(url, data) {
  211. url = Api.requestUrl(url);
  212. data = Api.baseParam(data);
  213. if (window.wx != undefined) {
  214. return Api.wxPostP(url, data);
  215. } else {
  216. return Api.jsPostP(url, data);
  217. }
  218. }
  219. static wxGetP(url, data) {
  220. return Api.wxRequestPromise(url, data, 'GET');
  221. }
  222. static wxPostP(url, data) {
  223. let header = {
  224. "Content-Type": "application/x-www-form-urlencoded"
  225. };
  226. return Api.wxRequestPromise(url, data, 'POST', header);
  227. }
  228. static wxRequestPromise(url, data, method, header) {
  229. return new Promise((resolve, reject) => {
  230. wx.request({
  231. url: url,
  232. data: data,
  233. method: method,
  234. header: header === undefined ? {} : header,
  235. success: (res) => {
  236. //直接处理登录失效的逻辑
  237. if (res.data.code === ResponseStateCode.LOGIN_INVALIDATE) {
  238. Global.needLogin = true;
  239. cc.director.loadScene('login');
  240. reject(res.data);
  241. }
  242. if (res.data.code === ResponseStateCode.OK) {
  243. resolve(res.data);
  244. } else {
  245. reject(res.data);
  246. }
  247. },
  248. fail: (res) => {
  249. reject({
  250. code: -1,
  251. msg: '微信接口调用失败'
  252. });
  253. },
  254. complete: {},
  255. });
  256. });
  257. }
  258. static jsGetP(url, data) {
  259. return new Promise((resolve, reject) => {
  260. url = url + "?";
  261. for (var key in data) {
  262. url = url + key + '=' + data[key] + '&';
  263. }
  264. url = url.substr(0, url.length - 1);
  265. var xhr = Api.jsBaseRequestP(resolve, reject);
  266. xhr.open("GET", url, true);
  267. xhr.send();
  268. });
  269. }
  270. static jsPostP(url, data) {
  271. return new Promise((resolve, reject) => {
  272. var xhr = Api.jsBaseRequestP(resolve, reject);
  273. xhr.open("POST", url, true);
  274. xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  275. let formData = new String();
  276. for (var key in data) {
  277. formData = formData + key + '=' + data[key] + "&";
  278. }
  279. xhr.send(formData.substr(0, formData.length - 1));
  280. });
  281. }
  282. static jsBaseRequestP(resolve, reject) {
  283. var xhr = new XMLHttpRequest();
  284. xhr.onreadystatechange = () => {
  285. if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
  286. // console.log('response:' + xhr.responseText);
  287. var response = JSON.parse(xhr.responseText);
  288. if (response.code == ResponseStateCode.OK) {
  289. resolve(response);
  290. } else {
  291. reject(response);
  292. }
  293. }
  294. };
  295. return xhr;
  296. }
  297. /**
  298. * 将每个接口都需要的公用参数都放到这里
  299. * @param target [Object] 请求参数
  300. */
  301. static baseParam(target) {
  302. if (target === undefined) {
  303. target = {};
  304. }
  305. target.thirdType = 1;
  306. target.channel = Global.channel;
  307. target.ver = Global.ver;
  308. target.os = Global.os;
  309. target.uid = Global.user ? Global.user.uid : '';
  310. target.token = Global.user ? Global.user.token : '';
  311. return target;
  312. }
  313. /**
  314. * 自动拼接请求接口
  315. * @param targetUrl [string] 请求接口
  316. */
  317. static requestUrl(targetUrl) {
  318. let host = Global.debug ? 'https://test-api-allstar.duowan.com' : 'https://api-allstar.duowan.com';
  319. // let host = Global.debug ? 'http://172.16.15.170:8080' : 'https://api-allstar.duowan.com';
  320. return host + targetUrl;
  321. }
  322. }
  323. module.exports = Api;