WinBoxAPI.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import userDataStorage from './userDataStorage';
  2. import Consts from './Consts';
  3. class WinBoxAPI {
  4. constructor() {}
  5. static whenAjaxResponse(httpRequest, callback) {
  6. if (httpRequest.readyState != 4) {
  7. return;
  8. }
  9. try {
  10. let res = JSON.parse(httpRequest.responseText);
  11. callback(httpRequest.status == 200, res);
  12. }
  13. catch(err) {
  14. callback(false, {});
  15. }
  16. }
  17. static baseRequest(method, path, params, callback) {
  18. let xmlHttpReq = new XMLHttpRequest();
  19. let sendBody = null;
  20. let url = `${Consts.NODE_SERVER}${path}?`;
  21. if (method === 'GET') {
  22. for (let key in params) {
  23. url += `${key}=${params[key]}&`;
  24. }
  25. }
  26. if (params && (method == 'POST' || method == 'PUT')) {
  27. sendBody = JSON.stringify({...params});
  28. }
  29. xmlHttpReq.open(method, url, true);
  30. xmlHttpReq.onreadystatechange = function () {
  31. WinBoxAPI.whenAjaxResponse(xmlHttpReq, callback);
  32. };
  33. xmlHttpReq.send(sendBody);
  34. }
  35. static get(path, params, callback) {
  36. WinBoxAPI.baseRequest('GET', path, params, callback);
  37. }
  38. static post(path, params) {
  39. WinBoxAPI.baseRequest('POST', path, params, callback);
  40. }
  41. static postDownloadRequest(lessonId, courseId, callback) {
  42. const { token = '', uid = '', eid = '' } = userDataStorage.getData() || {};
  43. let path = '/lesson/downloadFile';
  44. let params = { lessonId, courseId, uid, eid, token };
  45. WinBoxAPI.get(path, params, callback);
  46. }
  47. //获取mac地址
  48. static getMacUrl(callback) {
  49. let path = '/lesson/getMac';
  50. WinBoxAPI.get(path, {}, callback);
  51. }
  52. static deleteDownloadFile(lessonId, courseId, callback) {
  53. const { token = '', uid = '', eid = '' } = userDataStorage.getData() || {};
  54. let path = '/lesson/delFile';
  55. let params = { lessonId, courseId, uid, eid, token };
  56. WinBoxAPI.get(path, params, callback);
  57. }
  58. }
  59. module.exports = WinBoxAPI;