Api.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. const ResponseStateCode = require('../utils/GameEnum').ResponseStateCode;
  2. class Api {
  3. static createImageFromUrl(avatarUrl, success, fail) {
  4. if (avatarUrl) {
  5. var index = avatarUrl.indexOf('https:');
  6. var httpIndex = avatarUrl.indexOf('http:');
  7. if (index === 0 || httpIndex === 0) {
  8. //以'https:'开头
  9. } else {
  10. avatarUrl = 'https:' + avatarUrl;
  11. }
  12. if (window.wx != undefined) {
  13. try {
  14. let image = wx.createImage();
  15. image.onload = () => {
  16. try {
  17. let texture = new cc.Texture2D();
  18. texture.initWithElement(image);
  19. texture.handleLoadedTexture();
  20. success(new cc.SpriteFrame(texture))
  21. } catch (e) {
  22. cc.log(e);
  23. fail();
  24. }
  25. };
  26. image.src = avatarUrl;
  27. } catch (e) {
  28. cc.log(e);
  29. fail();
  30. }
  31. } else {
  32. cc.loader.load({
  33. url: avatarUrl, type: 'jpg'
  34. }, (err, texture) => {
  35. if (err === undefined || err === null) {
  36. success(new cc.SpriteFrame(texture));
  37. } else {
  38. fail();
  39. }
  40. });
  41. }
  42. } else {
  43. cc.loader.loadRes('./textures/default_avatar', cc.SpriteFrame, (err, spriteFrame) => {
  44. success(spriteFrame);
  45. })
  46. }
  47. }
  48. /**
  49. * get请求对外暴露的方法
  50. * @param {object} requestBody 请求body
  51. * @param {string} requestBody.url 请求地址
  52. * @param {object} requestBody.data 请求发送的data
  53. * @param {function} requestBody.success 请求成功的回调
  54. * @param {function} requestBody.fail 请求失败的回调
  55. */
  56. static httpGet(requestBody) {
  57. requestBody.data = Api.baseParam(requestBody.data);
  58. requestBody.url = Api.requestUrl(requestBody.url);
  59. if (window.wx != undefined) {
  60. Api.wxGet(requestBody);
  61. } else {
  62. Api.jsGet(requestBody);
  63. }
  64. }
  65. /**
  66. * post请求对外暴露的方法
  67. * @param {*} requestBody
  68. */
  69. static httpPost(requestBody) {
  70. requestBody.data = Api.baseParam(requestBody.data);
  71. requestBody.url = Api.requestUrl(requestBody.url);
  72. if (window.wx != undefined) {
  73. Api.wxPost(requestBody);
  74. } else {
  75. Api.jsPost(requestBody);
  76. }
  77. }
  78. static wxGet(requestBody) {
  79. let onSuccess = requestBody.success;
  80. let onFail = requestBody.fail;
  81. let onComplete = requestBody.complete;
  82. wx.request({
  83. url: requestBody.url,
  84. data: requestBody.data,
  85. method: requestBody.method === undefined ? 'GET' : 'POST',
  86. header: requestBody.header === undefined ? {} : requestBody.header,
  87. success: (res) => {
  88. //直接处理登录失效的逻辑
  89. if (res.data.code === ResponseStateCode.LOGIN_INVALIDATE) {
  90. Global.needLogin = true;
  91. cc.director.loadScene('login');
  92. return;
  93. }
  94. if (res.data.code === ResponseStateCode.OK) {
  95. if (onSuccess != undefined) {
  96. onSuccess(res.data.data, res.data.msg);
  97. }
  98. } else {
  99. if (onFail != undefined) {
  100. onFail(res.data.code, res.data.msg);
  101. }
  102. }
  103. },
  104. fail: (res) => {
  105. console.log('wxGet fail: ' + res);
  106. if (onFail != undefined) {
  107. onFail(-1, res.errMsg);
  108. }
  109. },
  110. complete: onComplete,
  111. });
  112. }
  113. static wxPost(requestBody) {
  114. requestBody.header = {
  115. "Content-Type": "application/x-www-form-urlencoded"
  116. };
  117. requestBody.method = 'POST';
  118. Api.wxGet(requestBody);
  119. }
  120. static jsGet(requestBody) {
  121. let url = requestBody.url;
  122. let data = requestBody.data;
  123. url = url + "?";
  124. for (var key in data) {
  125. url = url + key + '=' + data[key] + '&';
  126. }
  127. url = url.substr(0, url.length - 1)
  128. var xhr = Api.jsBaseRequest(requestBody.success, requestBody.fail, requestBody.complete);
  129. xhr.open("GET", url, true);
  130. xhr.send();
  131. }
  132. static jsPost(requestBody) {
  133. let data = requestBody.data;
  134. var xhr = Api.jsBaseRequest(requestBody.success, requestBody.fail, requestBody.complete);
  135. xhr.open("POST", requestBody.url, true);
  136. xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  137. let formData = new String();
  138. for (var key in data) {
  139. formData = formData + key + '=' + data[key] + "&";
  140. }
  141. xhr.send(formData.substr(0, formData.length - 1));
  142. }
  143. static jsBaseRequest(onSuccess, onFail, onComplete) {
  144. var xhr = new XMLHttpRequest();
  145. xhr.onreadystatechange = () => {
  146. if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
  147. // console.log('response:' + xhr.responseText);
  148. var response = JSON.parse(xhr.responseText);
  149. if (response.code == ResponseStateCode.OK) {
  150. if (onSuccess != undefined) {
  151. onSuccess(response.data, response.msg);
  152. }
  153. } else {
  154. if (onFail != undefined) {
  155. onFail(response.code, response.msg)
  156. }
  157. }
  158. if (onComplete != undefined) {
  159. onComplete();
  160. }
  161. }
  162. };
  163. return xhr;
  164. }
  165. /**
  166. * get请求对外暴露的方法
  167. * @param {*} requestBody
  168. */
  169. static httpGetPromise(url, data) {
  170. url = Api.requestUrl(url);
  171. data = Api.baseParam(data);
  172. if (window.wx != undefined) {
  173. return Api.wxGetP(url, data);
  174. } else {
  175. return Api.jsGetP(url, data);
  176. }
  177. }
  178. /**
  179. * post请求对外暴露的方法
  180. * @param {*} requestBody
  181. */
  182. static httpPostPromise(url, data) {
  183. url = Api.requestUrl(url);
  184. data = Api.baseParam(data);
  185. if (window.wx != undefined) {
  186. return Api.wxPostP(url, data);
  187. } else {
  188. return Api.jsPostP(url, data);
  189. }
  190. }
  191. static wxGetP(url, data) {
  192. return Api.wxRequestPromise(url, data, 'GET');
  193. }
  194. static wxPostP(url, data) {
  195. let header = {
  196. "Content-Type": "application/x-www-form-urlencoded"
  197. };
  198. return Api.wxRequestPromise(url, data, 'POST', header);
  199. }
  200. static wxRequestPromise(url, data, method, header) {
  201. return new Promise((resolve, reject) => {
  202. wx.request({
  203. url: url,
  204. data: data,
  205. method: method,
  206. header: header === undefined ? {} : header,
  207. success: (res) => {
  208. //直接处理登录失效的逻辑
  209. if (res.data.code === ResponseStateCode.LOGIN_INVALIDATE) {
  210. Global.needLogin = true;
  211. cc.director.loadScene('login');
  212. reject(res.data);
  213. }
  214. if (res.data.code === ResponseStateCode.OK) {
  215. resolve(res.data);
  216. } else {
  217. reject(res.data);
  218. }
  219. },
  220. fail: (res) => {
  221. reject({
  222. code: -1,
  223. msg: '微信接口调用失败'
  224. });
  225. },
  226. complete: {},
  227. });
  228. });
  229. }
  230. static jsGetP(url, data) {
  231. return new Promise((resolve, reject) => {
  232. url = url + "?";
  233. for (var key in data) {
  234. url = url + key + '=' + data[key] + '&';
  235. }
  236. url = url.substr(0, url.length - 1);
  237. var xhr = Api.jsBaseRequestP(resolve, reject);
  238. xhr.open("GET", url, true);
  239. xhr.send();
  240. });
  241. }
  242. static jsPostP(url, data) {
  243. return new Promise((resolve, reject) => {
  244. var xhr = Api.jsBaseRequestP(resolve, reject);
  245. xhr.open("POST", url, true);
  246. xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  247. let formData = new String();
  248. for (var key in data) {
  249. formData = formData + key + '=' + data[key] + "&";
  250. }
  251. xhr.send(formData.substr(0, formData.length - 1));
  252. });
  253. }
  254. static jsBaseRequestP(resolve, reject) {
  255. var xhr = new XMLHttpRequest();
  256. xhr.onreadystatechange = () => {
  257. if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
  258. // console.log('response:' + xhr.responseText);
  259. var response = JSON.parse(xhr.responseText);
  260. if (response.code == ResponseStateCode.OK) {
  261. resolve(response);
  262. } else {
  263. reject(response);
  264. }
  265. }
  266. };
  267. return xhr;
  268. }
  269. /**
  270. * 将每个接口都需要的公用参数都放到这里
  271. * @param target [Object] 请求参数
  272. */
  273. static baseParam(target) {
  274. if (target === undefined) {
  275. target = {};
  276. }
  277. target.thirdType = 1;
  278. target.channel = Global.channel;
  279. target.ver = Global.ver;
  280. target.os = Global.os;
  281. target.uid = Global.user ? Global.user.uid : '';
  282. target.token = Global.user ? Global.user.token : '';
  283. return target;
  284. }
  285. /**
  286. * 自动拼接请求接口
  287. * @param targetUrl [string] 请求接口
  288. */
  289. static requestUrl(targetUrl) {
  290. let host = Global.debug ? 'https://test-api-allstar.duowan.com' : 'https://api-allstar.duowan.com';
  291. // let host = Global.debug ? 'http://172.16.15.170:8080' : 'https://api-allstar.duowan.com';
  292. return host + targetUrl;
  293. }
  294. }
  295. module.exports = Api;