SingleRun.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. const API = require("../../lib/API.js");
  2. const db = require("../../plugin/DataBase/db.js");
  3. const { BaseStdResponse } = require("../../BaseStdResponse.js");
  4. const AccessControl = require("../../lib/AccessControl.js");
  5. const lepao = require("../../lib/Lepao/Lepao.js").lepao
  6. // 单次乐跑
  7. class SingleRun extends API {
  8. constructor() {
  9. super();
  10. this.setPath('/Lepao/SingleRun')
  11. this.setMethod('GET')
  12. }
  13. async onRequest(req, res) {
  14. let { uuid, session, student_num } = req.query
  15. if ([uuid, session, student_num].some(value => value === '' || value === null || value === undefined))
  16. return res.json({
  17. ...BaseStdResponse.MISSING_PARAMETER,
  18. endpoint: 1513126
  19. })
  20. if (!await AccessControl.checkSession(uuid, session))
  21. return res.status(401).json({
  22. ...BaseStdResponse.ACCESS_DENIED
  23. })
  24. let selectSql = 'SELECT create_user FROM lepao_account WHERE student_num = ?'
  25. let selectRows = await db.query(selectSql, [student_num])
  26. if (!selectRows || selectRows.length === 0)
  27. return res.json({
  28. ...BaseStdResponse.ERR,
  29. msg: '发起乐跑失败!未找到账户信息'
  30. })
  31. if (selectRows[0].create_user !== uuid) {
  32. let permission = await AccessControl.getPermission(uuid)
  33. if (!permission.includes("admin") && !permission.includes("service"))
  34. return res.json({
  35. ...BaseStdResponse.ERR,
  36. msg: '发起乐跑失败!未找到账户信息'
  37. })
  38. }
  39. let sql = 'SELECT token, uid, school_id, state FROM lepao_account WHERE student_num = ?'
  40. let rows = await db.query(sql, [student_num])
  41. if (!rows || rows.length === 0)
  42. return res.json({
  43. ...BaseStdResponse.ERR,
  44. msg: '发起乐跑失败!未找到对应的账号信息'
  45. })
  46. res.json({
  47. ...BaseStdResponse.OK
  48. })
  49. lepao.beginLepao(selectRows[0].create_user, student_num, rows[0].token, rows[0].uid, rows[0].school_id, rows[0].state)
  50. } catch(err) {
  51. this.logger.error(`手动乐跑失败!${err.stack}`);
  52. res.json({
  53. ...BaseStdResponse.ERR,
  54. msg: "乐跑失败!数据库异常"
  55. })
  56. }
  57. }
  58. module.exports.SingleRun = SingleRun;