cloud.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. //指定用户创建云端事件的行为
  26. router.route('/event_create').post((req, res, next) => {
  27. var _arr = [req.body.userid, req.body.username, "cloudevent", req.body.eventname, req.body.eventcontent];
  28. postmysql(req, res, "cloud_event_create", _arr);
  29. }).get((req, res, next) => {
  30. var _stime = req.query.starttime || "",
  31. _etime = req.query.endtime || "";
  32. getmysql(req, res, "cloud_event_create_get", req.query.username, "cloud_event_create", _stime, _etime);
  33. });
  34. //指定用户删除云端事件的行为
  35. router.route('/event_del').post((req, res, next) => {
  36. var _arr = [req.body.userid, req.body.username, "cloudevent", req.body.eventname, req.body.eventcontent];
  37. postmysql(req, res, "cloud_event_del", _arr);
  38. }).get((req, res, next) => {
  39. var _stime = req.query.starttime || "",
  40. _etime = req.query.endtime || "";
  41. getmysql(req, res, "cloud_event_del_get", req.query.username, "cloud_event_del", _stime, _etime);
  42. });
  43. //指定用户创建云端可视化面板的行为
  44. router.route('/panel_create').post((req, res, next) => {
  45. var _arr = [req.body.userid, req.body.username, "cloudevent", req.body.eventname, req.body.eventcontent];
  46. postmysql(req, res, "cloud_panel_create", _arr);
  47. }).get((req, res, next) => {
  48. var _stime = req.query.starttime || "",
  49. _etime = req.query.endtime || "";
  50. getmysql(req, res, "cloud_panel_create_get", req.query.username, "cloud_panel_create", _stime, _etime);
  51. });
  52. //指定用户删除云端可视化面板的行为
  53. router.route('/panel_del').post((req, res, next) => {
  54. var _arr = [req.body.userid, req.body.username, "cloudevent", req.body.eventname, req.body.eventcontent];
  55. postmysql(req, res, "cloud_panel_del", _arr);
  56. }).get((req, res, next) => {
  57. var _stime = req.query.starttime || "",
  58. _etime = req.query.endtime || "";
  59. getmysql(req, res, "cloud_panel_del_get", req.query.username, "cloud_panel_del", _stime, _etime);
  60. });
  61. queryString = function (url) {
  62. var a = url.split("?");
  63. var json = querystring.parse(a[1])
  64. return json;
  65. }
  66. //post存儲過程處理
  67. postmysql = function (req, res, functionname, arr) {
  68. //mode的處理
  69. arr.unshift(_mysqlLabor[0], _mysqlLabor[1], functionname);
  70. //執行存儲過程
  71. mysql.usselect(arr, function (ret) { res.end(JSON.stringify(ret)); });
  72. }
  73. //get獲取數據庫信息
  74. getmysql = function (req, res, functionname, ...p) {
  75. p = p || [];
  76. p.unshift(_mysqlLabor[0], _mysqlLabor[1], functionname);
  77. //執行存儲過程
  78. mysql.usselect(p, function (ret) {
  79. res.end(JSON.stringify(ret));
  80. });
  81. }
  82. module.exports = router;