UpdateState.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. const API = require("../../lib/API.js")
  2. const db = require('../../plugin/DataBase/db.js')
  3. const axios = require('axios')
  4. const config = require('../../config.json')
  5. const { BaseStdResponse } = require("../../BaseStdResponse.js")
  6. const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms))
  7. class UpdateState extends API {
  8. constructor() {
  9. super()
  10. this.noEncrypt()
  11. this.setPath('/Corn/UpdateState')
  12. this.setMethod('GET')
  13. this.runpy = config.runpy
  14. }
  15. async onRequest(req, res) {
  16. try {
  17. res.json({
  18. ...BaseStdResponse.OK
  19. })
  20. this.logger.info('开始更新乐跑账号登录状态')
  21. const zoneUrl = this.runpy + '/set_zone'
  22. let sql = `SELECT id, uid, token, school_id, name, student_num FROM lepao_account WHERE state = 0 AND auto_run = 1 AND token IS NOT NULL`
  23. let r = await db.query(sql)
  24. if (!r)
  25. return this.logger.error('更新乐跑账号登录状态失败!')
  26. for (const item of r) {
  27. const { name, student_num, token, uid, school_id } = item
  28. this.logger.info(`${name}(${student_num})开始更新乐跑登录状态`)
  29. await sleep(2000)
  30. try {
  31. const ossData = { uid, token, school_id, student_id: student_num, random_id: 1, run_end_time: 1 }
  32. const zoneRes = await axios.post(zoneUrl, ossData)
  33. const { data } = zoneRes
  34. this.logger.info(`${student_num}更新乐跑登录状态返回结果: ${JSON.stringify(data)}`)
  35. if (!data || data.status != 1 || !data.data) {
  36. this.logger.info(`${name}(${student_num})数据获取失败,不更新`)
  37. continue
  38. }
  39. const sql = 'UPDATE lepao_account SET state = 1 WHERE student_num = ?'
  40. await db.query(sql, [student_num])
  41. this.logger.info(`${name}(${student_num})数据获取成果,状态更新为正常`)
  42. } catch (err) {
  43. this.logger.error(`${name}(${student_num})更新乐跑登录状态失败:${err.message || err}`)
  44. }
  45. }
  46. this.logger.info('更新乐跑账号登录状态完成')
  47. } catch (error) {
  48. this.logger.error(error)
  49. }
  50. }
  51. }
  52. module.exports.UpdateState = UpdateState