UpdateStateAll.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 UpdateStateAll extends API {
  8. constructor() {
  9. super()
  10. this.noEncrypt()
  11. this.setPath('/Corn/UpdateStateAll')
  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 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. if (data?.info.includes('重新登录')) {
  37. const sql = 'UPDATE lepao_account SET state = 0 WHERE student_num = ?'
  38. await db.query(sql, [student_num])
  39. this.logger.info(`${name}(${student_num})状态更新为待登录`)
  40. }
  41. else this.logger.info(`${name}(${student_num})数据获取失败`)
  42. continue
  43. }
  44. const sql = 'UPDATE lepao_account SET state = 1 WHERE student_num = ?'
  45. await db.query(sql, [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. this.logger.info('更新乐跑账号登录状态完成')
  52. } catch (error) {
  53. this.logger.error(error)
  54. }
  55. }
  56. }
  57. module.exports.UpdateStateAll = UpdateStateAll