SingleRun.js 4.4 KB

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