SingleRun.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. const API = require("../../lib/API.js")
  2. const Redis = require('../../plugin/DataBase/Redis')
  3. const db = require("../../plugin/DataBase/db.js")
  4. const { BaseStdResponse } = require("../../BaseStdResponse.js")
  5. const AccessControl = require("../../lib/AccessControl.js")
  6. const mq = require('../../plugin/mq')
  7. const {
  8. assertRunforgeTaskIngress,
  9. publishRunforgeTask
  10. } = require('../../plugin/mq/runforgeTaskMq')
  11. // 单次乐跑
  12. class SingleRun extends API {
  13. constructor() {
  14. super()
  15. this.setPath('/Lepao/SingleRun')
  16. this.setMethod('GET')
  17. }
  18. async onRequest(req, res) {
  19. let { uuid, session, student_num } = req.query
  20. if ([uuid, session, student_num].some(value => value === '' || value === null || value === undefined))
  21. return res.json({
  22. ...BaseStdResponse.MISSING_PARAMETER
  23. })
  24. if (!await AccessControl.checkSession(uuid, session))
  25. return res.status(401).json({
  26. ...BaseStdResponse.ACCESS_DENIED
  27. })
  28. let hour = new Date().getHours()
  29. if (hour < 7)
  30. return res.json({
  31. ...BaseStdResponse.ERR,
  32. msg: '当前不在有效乐跑时间范围内。请在7:00~24:00发起乐跑'
  33. })
  34. try {
  35. // 检查redis是否存在当天乐跑成功记录
  36. const isSuccess = await Redis.get(`lepaoSuccess:${student_num}`)
  37. if (isSuccess)
  38. return res.json({
  39. ...BaseStdResponse.ERR,
  40. msg: '该账号当天已乐跑成功!请勿重复乐跑'
  41. })
  42. const isProgress = await Redis.get(`lepaoProgress:${student_num}`)
  43. if (isProgress)
  44. return res.json({
  45. ...BaseStdResponse.ERR,
  46. msg: '该账号已进入乐跑任务队列,请等待乐跑完成后再进行乐跑操作'
  47. })
  48. let selectSql = 'SELECT create_user FROM lepao_account WHERE student_num = ?'
  49. let selectRows = await db.query(selectSql, [student_num])
  50. if (!selectRows || selectRows.length === 0)
  51. return res.json({
  52. ...BaseStdResponse.ERR,
  53. msg: '发起乐跑失败!未找到账户信息'
  54. })
  55. if (selectRows[0].create_user !== uuid) {
  56. let permission = await AccessControl.getPermission(uuid)
  57. if (!permission.includes("admin") && !permission.includes("service"))
  58. return res.json({
  59. ...BaseStdResponse.ERR,
  60. msg: '发起乐跑失败!未找到账户信息'
  61. })
  62. }
  63. let sql = 'SELECT token, uid, school_id, state FROM lepao_account WHERE student_num = ?'
  64. let rows = await db.query(sql, [student_num])
  65. if (!rows || rows.length === 0)
  66. return res.json({
  67. ...BaseStdResponse.ERR,
  68. msg: '发起乐跑失败!未找到对应的账号信息'
  69. })
  70. if (rows[0].state !== 1)
  71. return res.json({
  72. ...BaseStdResponse.ERR,
  73. msg: '账号状态为未登录,请使用登录器更新账号信息后乐跑'
  74. })
  75. res.json({
  76. ...BaseStdResponse.OK
  77. })
  78. try {
  79. const channel = await mq.getChannel('lepao_api')
  80. await assertRunforgeTaskIngress(channel, this.logger)
  81. const taskId = `lepao:${Date.now()}:${student_num}`
  82. const payload = {
  83. id: taskId,
  84. type: 'lepao.startRun',
  85. data: {
  86. taskId,
  87. account: student_num
  88. },
  89. retry: 0
  90. }
  91. publishRunforgeTask(channel, payload)
  92. } catch (err) {
  93. this.logger.error(`后台乐跑任务异常:${err.stack}`)
  94. }
  95. } catch (err) {
  96. this.logger.error(`手动乐跑失败!${err.stack}`)
  97. return res.json({
  98. ...BaseStdResponse.ERR,
  99. msg: "乐跑失败!数据库异常"
  100. })
  101. }
  102. }
  103. }
  104. module.exports.SingleRun = SingleRun