StartLepao.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/Lepao.js").lepao
  5. const { BaseStdResponse } = require("../../BaseStdResponse")
  6. // 出现异常情况时补充乐跑
  7. class StartLepao extends API {
  8. constructor() {
  9. super();
  10. this.noEncrypt()
  11. this.setPath('/Corn/StartLepao');
  12. this.setMethod('GET');
  13. }
  14. async onRequest(req, res) {
  15. try {
  16. let { time } = req.query
  17. if ([time].some(value => value === '' || value === null || value === undefined))
  18. return res.json({
  19. ...BaseStdResponse.MISSING_PARAMETER
  20. })
  21. res.json({
  22. ...BaseStdResponse.OK
  23. })
  24. this.logger.info('开始执行补充乐跑任务')
  25. let sql = `
  26. SELECT name, create_user, student_num, token, uid, school_id, state
  27. FROM lepao_account
  28. WHERE auto_run = 1 AND state = 1
  29. AND (auto_time = ? OR (auto_time = -1 AND today_auto_time = ?))
  30. `
  31. let r = await db.query(sql, [time, time])
  32. if (!r)
  33. return this.logger.error('获取补充乐跑账号失败!')
  34. for (const item of r) {
  35. const { name, create_user, student_num, token, uid, school_id, state } = item
  36. this.logger.info(`${name}(${student_num})开始补充乐跑`)
  37. const isSuccess = await Redis.get(`lepaoSuccess:${student_num}`)
  38. if (isSuccess) {
  39. this.logger.info(`${name}(${student_num})当天已乐跑成功,不执行补充乐跑`)
  40. continue
  41. }
  42. try {
  43. await lepao.beginLepao(create_user, student_num, token, uid, school_id, state)
  44. this.logger.info(`${name}(${student_num})补充乐跑完成`)
  45. } catch (err) {
  46. this.logger.error(`${name}(${student_num})补充乐跑失败:${err.message || err}`)
  47. }
  48. }
  49. this.logger.info('补充乐跑任务执行完毕')
  50. } catch (error) {
  51. this.logger.error(error)
  52. }
  53. }
  54. }
  55. module.exports.StartLepao = StartLepao