blockly.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // ROUTES FOR OUR API
  2. // =============================================================================
  3. var express = require('express');
  4. var router = express.Router(); // get an instance of the express Router
  5. const querystring = require('querystring');
  6. var mysql = require('./mysql');
  7. const _mysqlLabor = ["127.0.0.1", "cuhkjc_query"]; //query數據庫信息
  8. //統壹處理區域
  9. router.use(async function (req, res, next) {
  10. //权限设置 - 登陆 验证码
  11. await asynnext(req, res, next);
  12. //// make sure we go to the next routes and don't stop here
  13. //res.end("", _stime, _etime);
  14. });
  15. /**
  16. * @des 參數解釋同上
  17. */
  18. function asynnext(req, res, next) {
  19. //異步處理
  20. return new Promise(async (resolve, reject) => {
  21. next();
  22. //resolve(true);
  23. });
  24. }
  25. //指定用户访问 CocoBlockly 的行为
  26. router.route('/history').post((req, res, next) => {
  27. var _arr = [req.body.userid, req.body.username, 0, "history"];
  28. postmysql(req, res, "blockly_history", _arr);
  29. }).get((req, res, next) => {
  30. console.log(req.query)
  31. var _stime = req.query.starttime || "",
  32. _etime = req.query.endtime || "";
  33. getmysql(req, res, "blockly_history_get", req.query.username, "history", _stime, _etime);
  34. });
  35. //指定用户在 CocoBlockly 停留的总时间(离开的时候请求)
  36. router.route('/duration').post((req, res, next) => {
  37. var _arr = [req.body.userid, req.body.username, req.body.time, "duration"];
  38. postmysql(req, res, "blockly_duration", _arr);
  39. }).get((req, res, next) => {
  40. var _stime = req.query.starttime || "",
  41. _etime = req.query.endtime || "";
  42. getmysql(req, res, "blockly_duration_get", req.query.username, "duration", _stime, _etime);
  43. });
  44. //指定用户打开实验室的行为
  45. router.route('/labs').post((req, res, next) => {
  46. postmysql(req, res, "blockly_labs");
  47. }).get((req, res, next) => {
  48. var _stime = req.query.starttime || "",
  49. _etime = req.query.endtime || "";
  50. getmysql(req, res, "blockly_labs_get", _stime, _etime);
  51. });
  52. //指定用户打开某个样例的行为
  53. router.route('/examples').post((req, res, next) => {
  54. var _arr = [req.body.userid, req.body.username, "example", req.body.eventname, req.body.eventcontent];
  55. postmysql(req, res, "blockly_examples", _arr);
  56. }).get((req, res, next) => {
  57. var _stime = req.query.starttime || "",
  58. _etime = req.query.endtime || "";
  59. getmysql(req, res, "blockly_examples_get", req.query.username, "example", _stime, _etime);
  60. });
  61. //指定用户切换上传模式的行为
  62. router.route('/mode_switch').post((req, res, next) => {
  63. var _arr = [req.body.userid, req.body.username, "modeswitch", req.body.eventname, req.body.eventcontent];
  64. postmysql(req, res, "blockly_mode_switch", _arr);
  65. }).get((req, res, next) => {
  66. var _stime = req.query.starttime || "",
  67. _etime = req.query.endtime || "";
  68. getmysql(req, res, "blockly_mode_switch_get", req.query.username, "modeswitch", _stime, _etime);
  69. });
  70. //指定用户点击串口监视窗按钮的行为
  71. router.route('/serial_monitor').post((req, res, next) => {
  72. var _arr = [req.body.userid, req.body.username, "serialmonitor", req.body.eventname, req.body.eventcontent];
  73. postmysql(req, res, "blockly_serial_monitor", _arr);
  74. }).get((req, res, next) => {
  75. var _stime = req.query.starttime || "",
  76. _etime = req.query.endtime || "";
  77. getmysql(req, res, "blockly_serial_monitor_get", req.query.username, "serialmonitor", _stime, _etime);
  78. });
  79. //指定用户点击复制源代码的行为
  80. router.route('/copy').post((req, res, next) => {
  81. var _arr = [req.body.userid, req.body.username, "copy", req.body.eventname, req.body.eventcontent];
  82. postmysql(req, res, "blockly_copy", _arr);
  83. }).get((req, res, next) => {
  84. var _stime = req.query.starttime || "",
  85. _etime = req.query.endtime || "";
  86. getmysql(req, res, "blockly_copy_get", req.query.username, "copy", _stime, _etime);
  87. });
  88. //指定用户点击运行的行为
  89. router.route('/upload_area/run').post((req, res, next) => {
  90. var _arr = [req.body.userid, req.body.username, "run", req.body.eventname, req.body.eventcontent];
  91. postmysql(req, res, "blockly_upload_area_run", _arr);
  92. }).get((req, res, next) => {
  93. var _stime = req.query.starttime || "",
  94. _etime = req.query.endtime || "";
  95. getmysql(req, res, "blockly_upload_area_run_get", req.query.username, "run", _stime, _etime);
  96. });
  97. //指定用户点击上传的行为
  98. router.route('/upload_area/upload').post((req, res, next) => {
  99. var _arr = [req.body.userid, req.body.username, "upload", req.body.eventname, req.body.eventcontent];
  100. postmysql(req, res, "blockly_upload_area_upload", _arr);
  101. }).get((req, res, next) => {
  102. var _stime = req.query.starttime || "",
  103. _etime = req.query.endtime || "";
  104. getmysql(req, res, "blockly_upload_area_upload_get", req.query.username, "upload", _stime, _etime);
  105. });
  106. //指定用户在 Blockly 中弹出错误信息的行为(并报错错误信息)
  107. router.route('/upload_area/errormsg').post((req, res, next) => {
  108. var _arr = [req.body.userid, req.body.username, "errormsg", req.body.eventname, req.body.eventcontent];
  109. postmysql(req, res, "blockly_upload_area_errormsg", _arr);
  110. }).get((req, res, next) => {
  111. var _stime = req.query.starttime || "",
  112. _etime = req.query.endtime || "";
  113. getmysql(req, res, "blockly_upload_area_errormsg_get", req.query.username, "errormsg", _stime, _etime);
  114. });
  115. //指定用户将积木拖入编程区域的行为
  116. router.route('/blocks/to_workspace').post((req, res, next) => {
  117. var _arr = [req.body.userid, req.body.username, "to_workspace", req.body.eventname, req.body.eventcontent];
  118. postmysql(req, res, "blockly_blocks_to_workspace", _arr);
  119. }).get((req, res, next) => {
  120. var _stime = req.query.starttime || "",
  121. _etime = req.query.endtime || "";
  122. getmysql(req, res, "blockly_blocks_to_workspace_get", req.query.username, "to_workspace", _stime, _etime);
  123. });
  124. //指定用户将积木拖出编程区域的行为
  125. router.route('/blocks/out_workspace').post((req, res, next) => {
  126. var _arr = [req.body.userid, req.body.username, "out_workspace", req.body.eventname, req.body.eventcontent];
  127. postmysql(req, res, "blockly_blocks_out_workspace", _arr);
  128. }).get((req, res, next) => {
  129. var _stime = req.query.starttime || "",
  130. _etime = req.query.endtime || "";
  131. getmysql(req, res, "blockly_blocks_out_workspace_get", req.query.username, "out_workspace", _stime, _etime);
  132. });
  133. //指定用户导出文件的行为
  134. router.route('/files/local_export').post((req, res, next) => {
  135. var _arr = [req.body.userid, req.body.username, "local_export", req.body.eventname, req.body.eventcontent];
  136. postmysql(req, res, "blockly_files_local_export", _arr);
  137. }).get((req, res, next) => {
  138. var _stime = req.query.starttime || "",
  139. _etime = req.query.endtime || "";
  140. getmysql(req, res, "blockly_files_local_export_get", req.query.username, "local_export", _stime, _etime);
  141. });
  142. //指定用户导入文件的行为
  143. router.route('/files/local_import').post((req, res, next) => {
  144. var _arr = [req.body.userid, req.body.username, "local_import", req.body.eventname, req.body.eventcontent];
  145. postmysql(req, res, "blockly_files_local_import", _arr);
  146. }).get((req, res, next) => {
  147. var _stime = req.query.starttime || "",
  148. _etime = req.query.endtime || "";
  149. getmysql(req, res, "blockly_files_local_import_get", req.query.username, "local_export", _stime, _etime);
  150. });
  151. //指定用户创建指定云端文件的行为
  152. router.route('/files/cloud_create').post((req, res, next) => {
  153. var _arr = [req.body.userid, req.body.username, "cloud", req.body.eventname, req.body.eventcontent];
  154. postmysql(req, res, "blockly_files_cloud_create", _arr);
  155. }).get((req, res, next) => {
  156. var _stime = req.query.starttime || "",
  157. _etime = req.query.endtime || "";
  158. getmysql(req, res, "blockly_files_cloud_create_get", req.query.username, "cloudcreate", _stime, _etime);
  159. });
  160. //指定用户更新指定云端文件的行为
  161. router.route('/files/cloud_update').post((req, res, next) => {
  162. var _arr = [req.body.userid, req.body.username, "cloud", req.body.eventname, req.body.eventcontent];
  163. postmysql(req, res, "blockly_files_cloud_update", _arr);
  164. }).get((req, res, next) => {
  165. var _stime = req.query.starttime || "",
  166. _etime = req.query.endtime || "";
  167. getmysql(req, res, "blockly_files_cloud_update_get", req.query.username, "cloudupdate", _stime, _etime);
  168. });
  169. //指定用户打开指定云端文件的行为
  170. router.route('/files/cloud_open').post((req, res, next) => {
  171. var _arr = [req.body.userid, req.body.username, "cloud", req.body.eventname, req.body.eventcontent];
  172. postmysql(req, res, "blockly_files_cloud_open", _arr);
  173. }).get((req, res, next) => {
  174. var _stime = req.query.starttime || "",
  175. _etime = req.query.endtime || "";
  176. getmysql(req, res, "blockly_files_cloud_open_get", req.query.username, "cloudopen", _stime, _etime);
  177. });
  178. //指定用户删除指定云端文件的行为
  179. router.route('/files/cloud_delete').post((req, res, next) => {
  180. var _arr = [req.body.userid, req.body.username, "cloud", req.body.eventname, req.body.eventcontent];
  181. postmysql(req, res, "blockly_files_cloud_delete", _arr);
  182. }).get((req, res, next) => {
  183. var _stime = req.query.starttime || "",
  184. _etime = req.query.endtime || "";
  185. getmysql(req, res, "blockly_files_cloud_delete_get", req.query.username, "clouddelete", _stime, _etime);
  186. });
  187. queryString = function (url) {
  188. var a = url.split("?");
  189. var json = querystring.parse(a[1])
  190. return json;
  191. }
  192. //post存儲過程處理
  193. postmysql = function (req, res, functionname, arr) {
  194. //mode的處理
  195. arr.unshift(_mysqlLabor[0], _mysqlLabor[1], functionname);
  196. //執行存儲過程
  197. mysql.usselect(arr, function (ret) { res.end(JSON.stringify(ret)); });
  198. }
  199. //get獲取數據庫信息
  200. getmysql = function (req, res, functionname, ...p) {
  201. p = p || [];
  202. p.unshift(_mysqlLabor[0], _mysqlLabor[1], functionname);
  203. //執行存儲過程
  204. mysql.usselect(p, function (ret) {
  205. res.end(JSON.stringify(ret));
  206. });
  207. }
  208. module.exports = router;