| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- const API = require("../../lib/API.js")
- const db = require('../../plugin/DataBase/db.js')
- const { BaseStdResponse } = require("../../BaseStdResponse.js")
- const {
- probeSetZone,
- isProbeSetZoneOk,
- getProbeFailMessage
- } = require('../../plugin/Lepao/runforgeSetZoneProbe')
- const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms))
- class UpdateStateAll extends API {
- constructor() {
- super()
- this.noEncrypt()
- this.setPath('/Corn/UpdateStateAll')
- this.setMethod('GET')
- }
- async onRequest(req, res) {
- try {
- res.json({
- ...BaseStdResponse.OK
- })
- this.logger.info('开始更新乐跑账号登录状态')
- let sql = `SELECT id, uid, token, school_id, name, student_num, userAgent FROM lepao_account WHERE token IS NOT NULL`
- let r = await db.query(sql)
- if (!r)
- return this.logger.error('更新乐跑账号登录状态失败!')
- for (const item of r) {
- const { name, student_num, token, uid, school_id, userAgent } = item
- this.logger.info(`${name}(${student_num})开始更新乐跑登录状态`)
- await sleep(2000)
- try {
- const data = await probeSetZone({
- uid,
- token,
- school_id,
- student_num,
- random_id: 1,
- userAgent
- })
- this.logger.info(`${student_num}更新乐跑登录状态返回结果: ${JSON.stringify(data)}`)
- if (!isProbeSetZoneOk(data)) {
- const msg = getProbeFailMessage(data)
- if (msg && msg.includes('重新登录')) {
- const failSql = 'UPDATE lepao_account SET state = 0 WHERE student_num = ?'
- await db.query(failSql, [student_num])
- this.logger.info(`${name}(${student_num})状态更新为待登录`)
- } else {
- this.logger.info(`${name}(${student_num})数据获取失败`)
- }
- continue
- }
- const okSql = 'UPDATE lepao_account SET state = 1 WHERE student_num = ?'
- await db.query(okSql, [student_num])
- this.logger.info(`${name}(${student_num})状态更新为正常`)
- } catch (err) {
- this.logger.error(`${name}(${student_num})更新乐跑登录状态失败:${err.message || err}`)
- }
- }
- this.logger.info('更新乐跑账号登录状态完成')
- } catch (error) {
- this.logger.error(error)
- }
- }
- }
- module.exports.UpdateStateAll = UpdateStateAll
|