Api.js 11 KB

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