Api.js 11 KB

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