StartAutoLepao.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. const API = require("../../lib/API");
  2. const db = require('../../plugin/DataBase/db')
  3. const Redis = require('../../plugin/DataBase/Redis')
  4. const lepao = require("../../lib/Lepao/cg_lepao.js").Lepao
  5. const { BaseStdResponse } = require("../../BaseStdResponse");
  6. class StartAutoLepao extends API {
  7. constructor() {
  8. super();
  9. this.noEncrypt()
  10. this.setPath('/Corn/StartAutoLepao');
  11. this.setMethod('GET');
  12. }
  13. async onRequest(req, res) {
  14. try {
  15. res.json({
  16. ...BaseStdResponse.OK
  17. })
  18. const day = new Date().getDay()
  19. const hour = new Date().getHours()
  20. this.logger.info('开始执行自动乐跑任务')
  21. let sql = `
  22. SELECT name, create_user, student_num
  23. FROM lepao_account
  24. WHERE auto_run = 1
  25. AND (auto_time = ? OR (auto_time = -1 AND today_auto_time = ?))
  26. AND JSON_CONTAINS(auto_day, CAST(? AS JSON))
  27. `
  28. let r = await db.query(sql, [hour, hour, day])
  29. if (!r)
  30. return this.logger.error('获取自动乐跑账号失败!')
  31. for (const item of r) {
  32. const { name, create_user, student_num } = item
  33. this.logger.info(`${name}(${student_num})开始乐跑`)
  34. const isProgress = await Redis.get(`cgLepaoProgress:${student_num}`)
  35. if (isProgress) {
  36. this.logger.info(`${name}(${student_num})已进入乐跑任务队列,请等待乐跑完成后再进行乐跑操作`)
  37. continue
  38. }
  39. const isSuccess = await Redis.get(`cgLepaoSuccess:${student_num}`)
  40. if (isSuccess) {
  41. this.logger.info(`${name}(${student_num})当天已乐跑成功,不执行自动乐跑`)
  42. continue
  43. }
  44. try {
  45. await lepao.beginLepao(create_user, student_num)
  46. this.logger.info(`${name}(${student_num})乐跑完成`)
  47. } catch (err) {
  48. this.logger.error(`${name}(${student_num})乐跑失败:${err.message || err}`)
  49. }
  50. }
  51. } catch (error) {
  52. this.logger.error(error)
  53. }
  54. }
  55. }
  56. module.exports.StartAutoLepao = StartAutoLepao;