// 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); }); } //指定用户创建云端事件的行为 router.route('/event_create').post((req, res, next) => { var _arr = [req.body.userid, req.body.username, "cloudevent", req.body.eventname, req.body.eventcontent]; postmysql(req, res, "cloud_event_create", _arr); }).get((req, res, next) => { var _stime = req.query.starttime || "", _etime = req.query.endtime || ""; getmysql(req, res, "cloud_event_create_get", req.query.username, "cloud_event_create", _stime, _etime); }); //指定用户删除云端事件的行为 router.route('/event_del').post((req, res, next) => { var _arr = [req.body.userid, req.body.username, "cloudevent", req.body.eventname, req.body.eventcontent]; postmysql(req, res, "cloud_event_del", _arr); }).get((req, res, next) => { var _stime = req.query.starttime || "", _etime = req.query.endtime || ""; getmysql(req, res, "cloud_event_del_get", req.query.username, "cloud_event_del", _stime, _etime); }); //指定用户创建云端可视化面板的行为 router.route('/panel_create').post((req, res, next) => { var _arr = [req.body.userid, req.body.username, "cloudevent", req.body.eventname, req.body.eventcontent]; postmysql(req, res, "cloud_panel_create", _arr); }).get((req, res, next) => { var _stime = req.query.starttime || "", _etime = req.query.endtime || ""; getmysql(req, res, "cloud_panel_create_get", req.query.username, "cloud_panel_create", _stime, _etime); }); //指定用户删除云端可视化面板的行为 router.route('/panel_del').post((req, res, next) => { var _arr = [req.body.userid, req.body.username, "cloudevent", req.body.eventname, req.body.eventcontent]; postmysql(req, res, "cloud_panel_del", _arr); }).get((req, res, next) => { var _stime = req.query.starttime || "", _etime = req.query.endtime || ""; getmysql(req, res, "cloud_panel_del_get", req.query.username, "cloud_panel_del", _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;