123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- // ROUTES FOR OUR API
- // =============================================================================
- var express = require('express');
- var router = express.Router(); // get an instance of the express Router
- const querystring = require('querystring');
- var mysql = require('./mysql');
- const _mysqlLabor = ["127.0.0.1", "cuhkjc_query"]; //query數據庫信息
- //統壹處理區域
- router.use(async function (req, res, next) {
- //权限设置 - 登陆 验证码
- await asynnext(req, res, next);
- //// make sure we go to the next routes and don't stop here
- //res.end("", _stime, _etime);
- });
- /**
- * @des 參數解釋同上
- */
- function asynnext(req, res, next) {
- //異步處理
- return new Promise(async (resolve, reject) => {
- next();
- //resolve(true);
- });
- }
- //指定用户访问 CocoBlockly 的行为
- router.route('/history').post((req, res, next) => {
- var _arr = [req.body.userid, req.body.username, 0, "history"];
- postmysql(req, res, "blockly_history", _arr);
- }).get((req, res, next) => {
- console.log(req.query)
- var _stime = req.query.starttime || "",
- _etime = req.query.endtime || "";
- getmysql(req, res, "blockly_history_get", req.query.username, "history", _stime, _etime);
- });
- //指定用户在 CocoBlockly 停留的总时间(离开的时候请求)
- router.route('/duration').post((req, res, next) => {
- var _arr = [req.body.userid, req.body.username, req.body.time, "duration"];
- postmysql(req, res, "blockly_duration", _arr);
- }).get((req, res, next) => {
- var _stime = req.query.starttime || "",
- _etime = req.query.endtime || "";
- getmysql(req, res, "blockly_duration_get", req.query.username, "duration", _stime, _etime);
- });
- //指定用户打开实验室的行为
- router.route('/labs').post((req, res, next) => {
- postmysql(req, res, "blockly_labs");
- }).get((req, res, next) => {
- var _stime = req.query.starttime || "",
- _etime = req.query.endtime || "";
- getmysql(req, res, "blockly_labs_get", _stime, _etime);
- });
- //指定用户打开某个样例的行为
- router.route('/examples').post((req, res, next) => {
- var _arr = [req.body.userid, req.body.username, "example", req.body.eventname, req.body.eventcontent];
- postmysql(req, res, "blockly_examples", _arr);
- }).get((req, res, next) => {
- var _stime = req.query.starttime || "",
- _etime = req.query.endtime || "";
- getmysql(req, res, "blockly_examples_get", req.query.username, "example", _stime, _etime);
- });
- //指定用户切换上传模式的行为
- router.route('/mode_switch').post((req, res, next) => {
- var _arr = [req.body.userid, req.body.username, "modeswitch", req.body.eventname, req.body.eventcontent];
- postmysql(req, res, "blockly_mode_switch", _arr);
- }).get((req, res, next) => {
- var _stime = req.query.starttime || "",
- _etime = req.query.endtime || "";
- getmysql(req, res, "blockly_mode_switch_get", req.query.username, "modeswitch", _stime, _etime);
- });
- //指定用户点击串口监视窗按钮的行为
- router.route('/serial_monitor').post((req, res, next) => {
- var _arr = [req.body.userid, req.body.username, "serialmonitor", req.body.eventname, req.body.eventcontent];
- postmysql(req, res, "blockly_serial_monitor", _arr);
- }).get((req, res, next) => {
- var _stime = req.query.starttime || "",
- _etime = req.query.endtime || "";
- getmysql(req, res, "blockly_serial_monitor_get", req.query.username, "serialmonitor", _stime, _etime);
- });
- //指定用户点击复制源代码的行为
- router.route('/copy').post((req, res, next) => {
- var _arr = [req.body.userid, req.body.username, "copy", req.body.eventname, req.body.eventcontent];
- postmysql(req, res, "blockly_copy", _arr);
- }).get((req, res, next) => {
- var _stime = req.query.starttime || "",
- _etime = req.query.endtime || "";
- getmysql(req, res, "blockly_copy_get", req.query.username, "copy", _stime, _etime);
- });
- //指定用户点击运行的行为
- router.route('/upload_area/run').post((req, res, next) => {
- var _arr = [req.body.userid, req.body.username, "run", req.body.eventname, req.body.eventcontent];
- postmysql(req, res, "blockly_upload_area_run", _arr);
- }).get((req, res, next) => {
- var _stime = req.query.starttime || "",
- _etime = req.query.endtime || "";
- getmysql(req, res, "blockly_upload_area_run_get", req.query.username, "run", _stime, _etime);
- });
- //指定用户点击上传的行为
- router.route('/upload_area/upload').post((req, res, next) => {
- var _arr = [req.body.userid, req.body.username, "upload", req.body.eventname, req.body.eventcontent];
- postmysql(req, res, "blockly_upload_area_upload", _arr);
- }).get((req, res, next) => {
- var _stime = req.query.starttime || "",
- _etime = req.query.endtime || "";
- getmysql(req, res, "blockly_upload_area_upload_get", req.query.username, "upload", _stime, _etime);
- });
- //指定用户在 Blockly 中弹出错误信息的行为(并报错错误信息)
- router.route('/upload_area/errormsg').post((req, res, next) => {
- var _arr = [req.body.userid, req.body.username, "errormsg", req.body.eventname, req.body.eventcontent];
- postmysql(req, res, "blockly_upload_area_errormsg", _arr);
- }).get((req, res, next) => {
- var _stime = req.query.starttime || "",
- _etime = req.query.endtime || "";
- getmysql(req, res, "blockly_upload_area_errormsg_get", req.query.username, "errormsg", _stime, _etime);
- });
- //指定用户将积木拖入编程区域的行为
- router.route('/blocks/to_workspace').post((req, res, next) => {
- var _arr = [req.body.userid, req.body.username, "to_workspace", req.body.eventname, req.body.eventcontent];
- postmysql(req, res, "blockly_blocks_to_workspace", _arr);
- }).get((req, res, next) => {
- var _stime = req.query.starttime || "",
- _etime = req.query.endtime || "";
- getmysql(req, res, "blockly_blocks_to_workspace_get", req.query.username, "to_workspace", _stime, _etime);
- });
- //指定用户将积木拖出编程区域的行为
- router.route('/blocks/out_workspace').post((req, res, next) => {
- var _arr = [req.body.userid, req.body.username, "out_workspace", req.body.eventname, req.body.eventcontent];
- postmysql(req, res, "blockly_blocks_out_workspace", _arr);
- }).get((req, res, next) => {
- var _stime = req.query.starttime || "",
- _etime = req.query.endtime || "";
- getmysql(req, res, "blockly_blocks_out_workspace_get", req.query.username, "out_workspace", _stime, _etime);
- });
- //指定用户导出文件的行为
- router.route('/files/local_export').post((req, res, next) => {
- var _arr = [req.body.userid, req.body.username, "local_export", req.body.eventname, req.body.eventcontent];
- postmysql(req, res, "blockly_files_local_export", _arr);
- }).get((req, res, next) => {
- var _stime = req.query.starttime || "",
- _etime = req.query.endtime || "";
- getmysql(req, res, "blockly_files_local_export_get", req.query.username, "local_export", _stime, _etime);
- });
- //指定用户导入文件的行为
- router.route('/files/local_import').post((req, res, next) => {
- var _arr = [req.body.userid, req.body.username, "local_import", req.body.eventname, req.body.eventcontent];
- postmysql(req, res, "blockly_files_local_import", _arr);
- }).get((req, res, next) => {
- var _stime = req.query.starttime || "",
- _etime = req.query.endtime || "";
- getmysql(req, res, "blockly_files_local_import_get", req.query.username, "local_export", _stime, _etime);
- });
- //指定用户创建指定云端文件的行为
- router.route('/files/cloud_create').post((req, res, next) => {
- var _arr = [req.body.userid, req.body.username, "cloud", req.body.eventname, req.body.eventcontent];
- postmysql(req, res, "blockly_files_cloud_create", _arr);
- }).get((req, res, next) => {
- var _stime = req.query.starttime || "",
- _etime = req.query.endtime || "";
- getmysql(req, res, "blockly_files_cloud_create_get", req.query.username, "cloudcreate", _stime, _etime);
- });
- //指定用户更新指定云端文件的行为
- router.route('/files/cloud_update').post((req, res, next) => {
- var _arr = [req.body.userid, req.body.username, "cloud", req.body.eventname, req.body.eventcontent];
- postmysql(req, res, "blockly_files_cloud_update", _arr);
- }).get((req, res, next) => {
- var _stime = req.query.starttime || "",
- _etime = req.query.endtime || "";
- getmysql(req, res, "blockly_files_cloud_update_get", req.query.username, "cloudupdate", _stime, _etime);
- });
- //指定用户打开指定云端文件的行为
- router.route('/files/cloud_open').post((req, res, next) => {
- var _arr = [req.body.userid, req.body.username, "cloud", req.body.eventname, req.body.eventcontent];
- postmysql(req, res, "blockly_files_cloud_open", _arr);
- }).get((req, res, next) => {
- var _stime = req.query.starttime || "",
- _etime = req.query.endtime || "";
- getmysql(req, res, "blockly_files_cloud_open_get", req.query.username, "cloudopen", _stime, _etime);
- });
- //指定用户删除指定云端文件的行为
- router.route('/files/cloud_delete').post((req, res, next) => {
- var _arr = [req.body.userid, req.body.username, "cloud", req.body.eventname, req.body.eventcontent];
- postmysql(req, res, "blockly_files_cloud_delete", _arr);
- }).get((req, res, next) => {
- var _stime = req.query.starttime || "",
- _etime = req.query.endtime || "";
- getmysql(req, res, "blockly_files_cloud_delete_get", req.query.username, "clouddelete", _stime, _etime);
- });
- queryString = function (url) {
- var a = url.split("?");
- var json = querystring.parse(a[1])
- return json;
- }
- //post存儲過程處理
- postmysql = function (req, res, functionname, arr) {
- //mode的處理
- arr.unshift(_mysqlLabor[0], _mysqlLabor[1], functionname);
- //執行存儲過程
- mysql.usselect(arr, function (ret) { res.end(JSON.stringify(ret)); });
- }
- //get獲取數據庫信息
- getmysql = function (req, res, functionname, ...p) {
- p = p || [];
- p.unshift(_mysqlLabor[0], _mysqlLabor[1], functionname);
- //執行存儲過程
- mysql.usselect(p, function (ret) {
- res.end(JSON.stringify(ret));
- });
- }
- module.exports = router;
|