| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- const API = require("../../lib/API.js")
- const Redis = require('../../plugin/DataBase/Redis')
- const db = require("../../plugin/DataBase/db.js")
- const { BaseStdResponse } = require("../../BaseStdResponse.js")
- const AccessControl = require("../../lib/AccessControl.js")
- const mq = require('../../plugin/mq')
- // 单次乐跑
- class SingleRun extends API {
- constructor() {
- super()
-
- this.setPath('/Lepao/SingleRun')
- this.setMethod('GET')
- }
- async onRequest(req, res) {
- let { uuid, session, student_num } = req.query
- if ([uuid, session, student_num].some(value => value === '' || value === null || value === undefined))
- return res.json({
- ...BaseStdResponse.MISSING_PARAMETER
- })
- if (!await AccessControl.checkSession(uuid, session))
- return res.status(401).json({
- ...BaseStdResponse.ACCESS_DENIED
- })
- let hour = new Date().getHours()
- if (hour < 7)
- return res.json({
- ...BaseStdResponse.ERR,
- msg: '当前不在有效乐跑时间范围内。请在7:00~24:00发起乐跑'
- })
- try {
- // 检查redis是否存在当天乐跑成功记录
- const isSuccess = await Redis.get(`lepaoSuccess:${student_num}`)
- if (isSuccess)
- return res.json({
- ...BaseStdResponse.ERR,
- msg: '该账号当天已乐跑成功!请勿重复乐跑'
- })
- const isProgress = await Redis.get(`lepaoProgress:${student_num}`)
- if (isProgress)
- return res.json({
- ...BaseStdResponse.ERR,
- msg: '该账号已进入乐跑任务队列,请等待乐跑完成后再进行乐跑操作'
- })
- let selectSql = 'SELECT create_user FROM lepao_account WHERE student_num = ?'
- let selectRows = await db.query(selectSql, [student_num])
- if (!selectRows || selectRows.length === 0)
- return res.json({
- ...BaseStdResponse.ERR,
- msg: '发起乐跑失败!未找到账户信息'
- })
- if (selectRows[0].create_user !== uuid) {
- let permission = await AccessControl.getPermission(uuid)
- if (!permission.includes("admin") && !permission.includes("service"))
- return res.json({
- ...BaseStdResponse.ERR,
- msg: '发起乐跑失败!未找到账户信息'
- })
- }
- let sql = 'SELECT token, uid, school_id, state FROM lepao_account WHERE student_num = ?'
- let rows = await db.query(sql, [student_num])
- if (!rows || rows.length === 0)
- return res.json({
- ...BaseStdResponse.ERR,
- msg: '发起乐跑失败!未找到对应的账号信息'
- })
- if (rows[0].state !== 1)
- return res.json({
- ...BaseStdResponse.ERR,
- msg: '账号状态为未登录,请使用登录器更新账号信息后乐跑'
- })
- res.json({
- ...BaseStdResponse.OK
- })
- try {
- const channel = await mq.getChannel('lepao_api')
- await channel.assertQueue('task_queue', { durable: true })
- const taskId = `lepao:${Date.now()}:${student_num}`
- const payload = {
- id: taskId,
- type: 'lepao.startRun',
- data: {
- taskId,
- account: student_num
- },
- retry: 0
- }
- channel.sendToQueue(
- 'task_queue',
- Buffer.from(JSON.stringify(payload)),
- { persistent: true, contentType: 'application/json' }
- )
- } catch (err) {
- this.logger.error(`后台乐跑任务异常:${err.stack}`)
- }
- } catch (err) {
- this.logger.error(`手动乐跑失败!${err.stack}`)
- return res.json({
- ...BaseStdResponse.ERR,
- msg: "乐跑失败!数据库异常"
- })
- }
- }
- }
- module.exports.SingleRun = SingleRun
|