lesson.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. var express = require('express');
  2. var mac = require('getmac');//获取mac地址
  3. var os = require('os');
  4. var fs = require('fs');
  5. var url = require('url');
  6. var router = express.Router();
  7. var ljConst = require("../common/LjConst");
  8. var ljRetState = require("../common/LjRetState");
  9. var logHelper = require("../helper/LjLogHelper.js");
  10. var httpHelper = require("../helper/LjHttpHelper");
  11. var fileHelper = require("../helper/LjFileHelper");
  12. var apiHelper = require("../helper/LjApiHelper");
  13. var ljStringHelper = require("../helper/LjStringHelper");
  14. /**
  15. * 获取MAC地址
  16. */
  17. router.get("/getMac", function (req, res)
  18. {
  19. var mac = apiHelper.getApiForGetMac();
  20. ljRetState.code = 200;
  21. ljRetState.data = mac;
  22. ljRetState.success = true;
  23. res.setHeader("Access-Control-Allow-Origin", "*");
  24. res.json(ljRetState);
  25. return;
  26. });
  27. /**
  28. * 下载文件
  29. */
  30. router.get("/downloadFile", function (req, res, next)
  31. {
  32. var arg = url.parse(req.url, true).query;
  33. var courseId = arg.courseId;
  34. var eid = arg.eid;
  35. var uid = arg.uid;
  36. var token = arg.token;
  37. var lessonId = arg.lessonId;
  38. //获取参数
  39. logHelper.debug(arg);
  40. //校验参数
  41. if (ljStringHelper.isNull(courseId))
  42. {
  43. logHelper.info("null object of courseId");
  44. ljRetState.code = 2;
  45. ljRetState.msg = "null object of courseId"
  46. res.json(ljRetState);
  47. return;
  48. }
  49. if (ljStringHelper.isNull(lessonId))
  50. {
  51. logHelper.info("null object of lessonId");
  52. ljRetState.code = 2;
  53. ljRetState.msg = "null object of lessonId"
  54. res.json(ljRetState);
  55. return;
  56. }
  57. if (ljStringHelper.isNull(eid))
  58. {
  59. logHelper.info("null object of eid");
  60. ljRetState.code = 2;
  61. ljRetState.msg = "null object of eid"
  62. res.json(ljRetState);
  63. return;
  64. }
  65. if (ljStringHelper.isNull(uid))
  66. {
  67. logHelper.info("null object of uid");
  68. ljRetState.code = 2;
  69. ljRetState.msg = "null object of uid"
  70. res.json(ljRetState);
  71. return;
  72. }
  73. if (ljStringHelper.isNull(token))
  74. {
  75. logHelper.info("null object of token");
  76. ljRetState.code = 2;
  77. ljRetState.msg = "null object of token"
  78. res.json(ljRetState);
  79. return;
  80. }
  81. var opt = {};
  82. opt.eid = eid;
  83. opt.uid = uid;
  84. opt.token = token;
  85. opt.lessonId = lessonId;
  86. opt.courseId = courseId;
  87. opt.path = "/lesson/" + opt.lessonId;
  88. opt.url = apiHelper.getApiForQueryCourse(opt.lessonId, opt.courseId, opt.eid);
  89. opt.method = "get";
  90. httpHelper.request(opt, function (error, res, body)
  91. {
  92. let obj = JSON.parse(body);
  93. var lessonJsonObj = JSON.parse(body);
  94. if (lessonJsonObj.code == 200)
  95. {
  96. /*课件-Begin*/
  97. //课程集合
  98. var courseList = lessonJsonObj.data.list;
  99. //课程数
  100. var courseListSize = courseList.length;
  101. //获取总共资源数
  102. var res_total = 0;
  103. for (var i = 0; i < courseListSize; i++)
  104. {
  105. var courseObj = courseList[i];
  106. if (courseObj.type == ljConst.RES_TYPE_IMAGE || courseObj.type == ljConst.RES_TYPE_VIDEO)
  107. {
  108. //资源集合
  109. var resourceList = courseList[i].list;
  110. //资源数
  111. var resourceSize = resourceList.length;
  112. res_total += resourceSize
  113. }
  114. else if (courseObj.type == ljConst.RES_TYPE_AUDIO)
  115. {
  116. if (courseObj.list.length > 0)
  117. {
  118. var audioObj = courseObj.list[0];
  119. var image = audioObj.img;
  120. var audio = audioObj.audio;
  121. if (image != null && image.url != "")
  122. {
  123. res_total += 1;
  124. }
  125. if (audio != null && audio.url != "")
  126. {
  127. res_total += 1;
  128. }
  129. }
  130. }
  131. }
  132. logHelper.info("res_total:" + res_total);
  133. /*课件-End*/
  134. //添加下载记录
  135. opt.resTotal = res_total;
  136. opt.resSize = 0;
  137. addDownload(opt);
  138. logHelper.info("Begin to download file with lessonId【" + opt.lessonId + "】");
  139. //添加下载记录
  140. //遍历课件
  141. for (var i = 0; i < courseListSize; i++)
  142. {
  143. var courseObj = courseList[i];
  144. if (courseObj.type == ljConst.RES_TYPE_IMAGE || courseObj.type == ljConst.RES_TYPE_VIDEO)
  145. {
  146. //资源集合
  147. var resourceList = courseList[i].list;
  148. //资源数
  149. var resourceSize = resourceList.length;
  150. for (var j = 0; j < resourceSize; j++)
  151. {
  152. logHelper.debug(resourceList[j].url);
  153. var url = resourceList[j].url;
  154. fileHelper.downloadFile(url, opt);
  155. }
  156. }//处理语音
  157. else if (courseObj.type == ljConst.RES_TYPE_AUDIO)
  158. {
  159. if (courseObj.list.length > 0)
  160. {
  161. var audioObj = courseObj.list[0];
  162. var image = audioObj.img;
  163. var audio = audioObj.audio;
  164. //下载图片
  165. if (image != null && image.url != "")
  166. {
  167. fileHelper.downloadFile(image.url, opt);
  168. }
  169. //下载语音
  170. if (audio != null && audio.url != "")
  171. {
  172. fileHelper.downloadFile(audio.url, opt);
  173. }
  174. }
  175. }
  176. }
  177. /*课件-End*/
  178. return "ok";
  179. }
  180. else
  181. {
  182. logHelper.error("failed to query lesson with lessonId[" + opt.lessonId + "],caused by error:" + lessonJsonObj);
  183. return "error";
  184. }
  185. })
  186. ljRetState.code = 200;
  187. ljRetState.msg = "null object of uid"
  188. ljRetState.success = true;
  189. res.setHeader("Access-Control-Allow-Origin", "*");
  190. res.json(ljRetState);
  191. return;
  192. });
  193. /**
  194. * 读取文件
  195. */
  196. router.get("/readFile", function (req, res)
  197. {
  198. var arg = url.parse(req.url, true).query;
  199. var fileDirStr = arg.n;
  200. var platform = os.platform();
  201. var fileDirNew = "";
  202. if (platform == "win32")
  203. {
  204. fileDirNew = fileDirStr.replace(/\*/g, '\\');
  205. }
  206. else
  207. {
  208. fileDirNew = fileDirStr.replace(/\*/g, '/');
  209. }
  210. //判断文件是否存在
  211. if (!fs.existsSync(fileDirNew))
  212. {
  213. ljRetState.code = 1;
  214. ljRetState.msg = "文件没有找到"
  215. ljRetState.success = false;
  216. res.setHeader("Access-Control-Allow-Origin", "*");
  217. res.json(ljRetState);
  218. return false;
  219. }
  220. var fileDir = fileHelper.getFileDir(fileDirNew)
  221. var fileProp = fileHelper.getFileProperty(fileDir);
  222. logHelper.info(fileProp.fileType);
  223. if (fileProp.fileType == ljConst.VIDEO_TYPE_MP4)
  224. {
  225. logHelper.info(fileDir);
  226. fileHelper.readDecryptedStream(res, fileDir, "video/mp4")
  227. }
  228. else
  229. {
  230. fileHelper.readDecryptedStream(res, fileDir, "image/jpeg")
  231. }
  232. });
  233. /**
  234. * 删除下载文件
  235. */
  236. router.get("/delFile", function (req, res)
  237. {
  238. var arg = url.parse(req.url, true).query;
  239. var courseId = arg.courseId;
  240. var eid = arg.eid;
  241. var uid = arg.uid;
  242. var token = arg.token;
  243. var lessonId = arg.lessonId;
  244. //获取参数
  245. logHelper.debug(arg);
  246. //校验参数
  247. if (ljStringHelper.isNull(courseId))
  248. {
  249. logHelper.info("null object of courseId");
  250. ljRetState.code = 2;
  251. ljRetState.msg = "null object of courseId"
  252. res.json(ljRetState);
  253. return;
  254. }
  255. if (ljStringHelper.isNull(lessonId))
  256. {
  257. logHelper.info("null object of lessonId");
  258. ljRetState.code = 2;
  259. ljRetState.msg = "null object of lessonId"
  260. res.json(ljRetState);
  261. return;
  262. }
  263. if (ljStringHelper.isNull(eid))
  264. {
  265. logHelper.info("null object of eid");
  266. ljRetState.code = 2;
  267. ljRetState.msg = "null object of eid"
  268. res.json(ljRetState);
  269. return;
  270. }
  271. if (ljStringHelper.isNull(uid))
  272. {
  273. logHelper.info("null object of uid");
  274. ljRetState.code = 2;
  275. ljRetState.msg = "null object of uid"
  276. res.json(ljRetState);
  277. return;
  278. }
  279. if (ljStringHelper.isNull(token))
  280. {
  281. logHelper.info("null object of token");
  282. ljRetState.code = 2;
  283. ljRetState.msg = "null object of token"
  284. res.json(ljRetState);
  285. return;
  286. }
  287. var opt = {};
  288. opt.eid = eid;
  289. opt.uid = uid;
  290. opt.token = token;
  291. opt.lessonId = lessonId;
  292. opt.courseId = courseId;
  293. opt.path = "/lesson/" + opt.lessonId;
  294. opt.url = apiHelper.getApiForQueryCourse(opt.lessonId, opt.courseId, opt.eid);
  295. opt.method = "get";
  296. httpHelper.request(opt, function (error, res, body)
  297. {
  298. let obj = JSON.parse(body);
  299. var lessonJsonObj = JSON.parse(body);
  300. if (lessonJsonObj.code == 200)
  301. {
  302. /*课件-Begin*/
  303. //课程集合
  304. var courseList = lessonJsonObj.data.list;
  305. //课程数
  306. var courseListSize = courseList.length;
  307. //遍历课件
  308. for (var i = 0; i < courseListSize; i++)
  309. {
  310. var courseObj = courseList[i];
  311. //处理图片删除
  312. if (courseObj.type == ljConst.RES_TYPE_IMAGE)
  313. {
  314. //资源集合
  315. var resourceList = courseObj.list;
  316. //资源数
  317. var resourceSize = resourceList.length;
  318. for (var j = 0; j < resourceSize; j++)
  319. {
  320. var url = resourceList[j].url;
  321. var delFileProp = fileHelper.getFilePropertyNoMkdir(url);
  322. var delFileDir = delFileProp.folderDir + delFileProp.fileName + "_new." + delFileProp.fileType;
  323. //判断文件是否存在
  324. if (fs.existsSync(delFileDir))
  325. {
  326. //删除路径
  327. fs.unlinkSync(delFileDir);
  328. }
  329. }
  330. }//处理视频删除
  331. else if (courseObj.type == ljConst.RES_TYPE_VIDEO)
  332. {
  333. //资源集合
  334. var resourceList = courseObj.list;
  335. //资源数
  336. var resourceSize = resourceList.length;
  337. for (var j = 0; j < resourceSize; j++)
  338. {
  339. var url = resourceList[j].url;
  340. var delFileProp = fileHelper.getFilePropertyNoMkdir(url);
  341. var delFileDir = delFileProp.folderDir + delFileProp.fileName + "_new.mp4";
  342. //判断文件是否存在
  343. if (fs.existsSync(delFileDir))
  344. {
  345. //删除路径
  346. fs.unlinkSync(delFileDir);
  347. }
  348. }
  349. }
  350. //处理语音删除
  351. else if (courseObj.type == ljConst.RES_TYPE_AUDIO)
  352. {
  353. if (courseObj.list.length > 0)
  354. {
  355. var audioObj = courseObj.list[0];
  356. var image = audioObj.img;
  357. var audio = audioObj.audio;
  358. //删除图片
  359. if (image != null && image.url != "")
  360. {
  361. var delFileProp = fileHelper.getFilePropertyNoMkdir(image.url);
  362. var delFileDir = delFileProp.folderDir + delFileProp.fileName + "_new." + delFileProp.fileType;
  363. //判断文件是否存在
  364. if (fs.existsSync(delFileDir))
  365. {
  366. //删除路径
  367. fs.unlinkSync(delFileDir);
  368. }
  369. }
  370. //删除语音
  371. if (audio != null && audio.url != "")
  372. {
  373. var delFileProp = fileHelper.getFilePropertyNoMkdir(audio.url);
  374. var delFileDir = delFileProp.folderDir + delFileProp.fileName + "_new.mp3";
  375. //判断文件是否存在
  376. if (fs.existsSync(delFileDir))
  377. {
  378. //删除路径
  379. fs.unlinkSync(delFileDir);
  380. }
  381. }
  382. }
  383. }
  384. }
  385. /*课件-End*/
  386. //删除记录
  387. delDownload(opt);
  388. return "ok";
  389. }
  390. else
  391. {
  392. logHelper.error("failed to query lesson with lessonId[" + opt.lessonId + "],caused by error:" + lessonJsonObj);
  393. return "error";
  394. }
  395. });
  396. ljRetState.code = 2;
  397. ljRetState.msg = "null object of uid"
  398. ljRetState.success = true;
  399. res.setHeader("Access-Control-Allow-Origin", "*");
  400. res.json(ljRetState);
  401. return "";
  402. });
  403. var addDownload = function (opt)
  404. {
  405. var macAddressStr = "";
  406. mac.getMac(function (err, macAddress)
  407. {
  408. if (err) throw err;
  409. macAddressStr = macAddress; //获取mac地址
  410. });
  411. opt.url = apiHelper.getApiForAddDownload(opt.lessonId, opt.courseId, opt.resTotal, opt.resSize);
  412. opt.method = "post";
  413. opt.path = "/callback/download/add";
  414. httpHelper.request(opt, function (error, res, body)
  415. {
  416. var retObj = JSON.parse(body);
  417. logHelper.debug(retObj);
  418. if (retObj.code == 200)
  419. {
  420. logHelper.info("Succeed to call api[" + opt.path + "]");
  421. return true;
  422. }
  423. else
  424. {
  425. logHelper.error("Failed to call api[" + opt.path + "],caused by error[" + retObj + "]");
  426. return false;
  427. }
  428. });
  429. }
  430. var delDownload = function (opt)
  431. {
  432. opt.url = apiHelper.getApiForDelDownload(opt.lessonId, null);
  433. opt.method = "delete";
  434. opt.path = "/callback/download/delete";
  435. httpHelper.request(opt, function (error, res, body)
  436. {
  437. var retObj = JSON.parse(body);
  438. logHelper.debug(retObj);
  439. if (retObj.code == 200)
  440. {
  441. logHelper.info("Succeed to call api[" + opt.path + "]");
  442. return true;
  443. }
  444. else
  445. {
  446. logHelper.error("Failed to call api[" + opt.path + "],caused by error[" + retObj + "]");
  447. return false;
  448. }
  449. });
  450. }
  451. module.exports = router;