StartLepao.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. } catch (error) {
  50. this.logger.error(error)
  51. }
  52. }
  53. }
  54. module.exports.StartLepao = StartLepao