import userDataStorage from './userDataStorage'; import Consts from './Consts'; class WinBoxAPI { constructor() {} static whenAjaxResponse(httpRequest, callback) { if (httpRequest.readyState != 4) { return; } try { let res = JSON.parse(httpRequest.responseText); callback(httpRequest.status == 200, res); } catch(err) { callback(false, {}); } } static baseRequest(method, path, params, callback) { let xmlHttpReq = new XMLHttpRequest(); let sendBody = null; let url = `${Consts.NODE_SERVER}${path}?`; if (method === 'GET') { for (let key in params) { url += `${key}=${params[key]}&`; } } if (params && (method == 'POST' || method == 'PUT')) { sendBody = JSON.stringify({...params}); } xmlHttpReq.open(method, url, true); xmlHttpReq.onreadystatechange = function () { WinBoxAPI.whenAjaxResponse(xmlHttpReq, callback); }; xmlHttpReq.send(sendBody); } static get(path, params, callback) { WinBoxAPI.baseRequest('GET', path, params, callback); } static post(path, params) { WinBoxAPI.baseRequest('POST', path, params, callback); } static postDownloadRequest(lessonId, courseId, callback) { const { token = '', uid = '', eid = '' } = userDataStorage.getData() || {}; let path = '/lesson/downloadFile'; let params = { lessonId, courseId, uid, eid, token }; WinBoxAPI.get(path, params, callback); } //获取mac地址 static getMacUrl(callback) { let path = '/lesson/getMac'; WinBoxAPI.get(path, {}, callback); } static deleteDownloadFile(lessonId, courseId, callback) { const { token = '', uid = '', eid = '' } = userDataStorage.getData() || {}; let path = '/lesson/delFile'; let params = { lessonId, courseId, uid, eid, token }; WinBoxAPI.get(path, params, callback); } } module.exports = WinBoxAPI;