|
|
@@ -5,11 +5,14 @@ const Logger = require('../Logger')
|
|
|
const path = require('path')
|
|
|
const EmailTemplate = require('../../plugin/Email/emailTemplate')
|
|
|
const config = require('../../config.json')
|
|
|
+const mq = require('../../plugin/mq')
|
|
|
|
|
|
class Lepao {
|
|
|
constructor() {
|
|
|
this.logger = new Logger(path.join(__dirname, '../logs/Lepao.log'), 'INFO')
|
|
|
this.runpy = config.runpy
|
|
|
+
|
|
|
+ this.messageQueue = 'runforge_message_queue'
|
|
|
}
|
|
|
|
|
|
async getPath(account, vip) {
|
|
|
@@ -167,7 +170,7 @@ class Lepao {
|
|
|
throw error
|
|
|
}
|
|
|
|
|
|
- // 临时修复3.12~3.18期间未入库记录,后续可删除
|
|
|
+ // 临时修复3.12~3.18期间未入库记录,后续可删除
|
|
|
await this.fixRecords(uuid, ossData)
|
|
|
|
|
|
// 上传 OSS
|
|
|
@@ -199,7 +202,6 @@ class Lepao {
|
|
|
throw new Error('系统繁忙,请联系客服或稍后再试')
|
|
|
}
|
|
|
|
|
|
-
|
|
|
// 扣除乐跑次数
|
|
|
this.logger.info(`${account}开始扣减乐跑次数`)
|
|
|
const useLepaoCountSql = 'UPDATE users SET lepao_count = lepao_count - 1 WHERE uuid = ?'
|
|
|
@@ -296,27 +298,90 @@ class Lepao {
|
|
|
async sendSuccessEmail(account, lepaoData, term_num, total_num) {
|
|
|
try {
|
|
|
this.logger.info(`${account}发送乐跑成功邮件`)
|
|
|
- const emailSql = 'SELECT name, email, target_count FROM lepao_account WHERE student_num = ?'
|
|
|
+ const emailSql = `
|
|
|
+ SELECT
|
|
|
+ a.name,
|
|
|
+ a.email,
|
|
|
+ a.target_count,
|
|
|
+ a.notice_type,
|
|
|
+ e.bot_umo
|
|
|
+ FROM
|
|
|
+ lepao_account a
|
|
|
+ LEFT JOIN
|
|
|
+ lepao_extra e
|
|
|
+ ON
|
|
|
+ a.student_num = e.student_num
|
|
|
+ WHERE
|
|
|
+ a.student_num = ?
|
|
|
+ `
|
|
|
+
|
|
|
const rows = await db.query(emailSql, [account])
|
|
|
if (!rows || rows.length === 0) {
|
|
|
- this.logger.error(`${account}查找用户邮箱失败`)
|
|
|
- throw new Error('查找用户邮箱失败')
|
|
|
+ this.logger.error(`${account}查找用户信息失败`)
|
|
|
+ throw new Error('查找用户信息失败')
|
|
|
}
|
|
|
|
|
|
- const data = {
|
|
|
+ let data = {
|
|
|
...lepaoData,
|
|
|
+ type: 'lepao_success',
|
|
|
+ umo: rows[0].bot_umo,
|
|
|
term_num: rows[0].target_count,
|
|
|
total_num,
|
|
|
name: rows[0].name,
|
|
|
account
|
|
|
}
|
|
|
|
|
|
- await EmailTemplate.lepaoSuccess(rows[0].email, data)
|
|
|
- this.logger.info(`${account}乐跑成功邮件发送完成`)
|
|
|
+ if (rows[0].notice_type === 'bot' && rows[0].bot_umo) {
|
|
|
+ this.logger.info(`${account}发送乐跑成功Bot通知,UMO=${rows[0].bot_umo}`)
|
|
|
+ const ch = await mq.getChannel(this.messageQueue)
|
|
|
+
|
|
|
+ await ch.assertQueue(this.messageQueue, {
|
|
|
+ durable: true
|
|
|
+ })
|
|
|
+
|
|
|
+ ch.sendToQueue(
|
|
|
+ this.messageQueue,
|
|
|
+ Buffer.from(JSON.stringify(data)),
|
|
|
+ {
|
|
|
+ persistent: true,
|
|
|
+ contentType: 'application/json'
|
|
|
+ }
|
|
|
+ )
|
|
|
+
|
|
|
+ this.logger.info(`${account}乐跑成功Bot通知发送完成`)
|
|
|
+ } else if (rows[0].notice_type === 'email' && rows[0].email) {
|
|
|
+ await EmailTemplate.lepaoSuccess(rows[0].email, data)
|
|
|
+ this.logger.info(`${account}乐跑成功邮件发送完成`)
|
|
|
+ }
|
|
|
|
|
|
if (rows[0].target_count !== 0 && total_num >= rows[0].target_count) {
|
|
|
- this.logger.info(`${account}乐跑目标完成,发送乐跑结束邮件并关闭自动乐跑`)
|
|
|
- await EmailTemplate.lepaoOver(rows[0].email, data)
|
|
|
+ this.logger.info(`${account}乐跑目标完成,发送乐跑结束通知并关闭自动乐跑`)
|
|
|
+
|
|
|
+ if (rows[0].notice_type === 'bot' && rows[0].bot_umo) {
|
|
|
+ this.logger.info(`${account}发送乐跑完成Bot通知,UMO=${rows[0].bot_umo}`)
|
|
|
+ const ch = await mq.getChannel(this.messageQueue)
|
|
|
+
|
|
|
+ await ch.assertQueue(this.messageQueue, {
|
|
|
+ durable: true
|
|
|
+ })
|
|
|
+
|
|
|
+ data.type = 'lepao_over'
|
|
|
+
|
|
|
+ ch.sendToQueue(
|
|
|
+ this.messageQueue,
|
|
|
+ Buffer.from(JSON.stringify(data)),
|
|
|
+ {
|
|
|
+ persistent: true,
|
|
|
+ contentType: 'application/json'
|
|
|
+ }
|
|
|
+ )
|
|
|
+
|
|
|
+ this.logger.info(`${account}乐跑完成Bot通知发送完成`)
|
|
|
+ } else if (rows[0].notice_type === 'email' && rows[0].email) {
|
|
|
+ await EmailTemplate.lepaoOver(rows[0].email, data)
|
|
|
+ this.logger.info(`${account}乐跑完成邮件发送完成`)
|
|
|
+ }
|
|
|
+
|
|
|
let overSql = 'UPDATE lepao_account SET auto_run = 0 WHERE student_num = ?'
|
|
|
let overRows = await db.query(overSql, [account])
|
|
|
if (!overRows || overRows.affectedRows !== 1)
|
|
|
@@ -331,8 +396,23 @@ class Lepao {
|
|
|
|
|
|
async sendFailEmail(account, reason) {
|
|
|
try {
|
|
|
- this.logger.info(`${account}发送乐跑失败邮件,原因: ${reason}`)
|
|
|
- const emailSql = 'SELECT name, email FROM lepao_account WHERE student_num = ?'
|
|
|
+ this.logger.info(`${account}发送乐跑失败通知,原因: ${reason}`)
|
|
|
+ const emailSql = `
|
|
|
+ SELECT
|
|
|
+ a.name,
|
|
|
+ a.email,
|
|
|
+ a.target_count,
|
|
|
+ a.notice_type,
|
|
|
+ e.bot_umo
|
|
|
+ FROM
|
|
|
+ lepao_account a
|
|
|
+ LEFT JOIN
|
|
|
+ lepao_extra e
|
|
|
+ ON
|
|
|
+ a.student_num = e.student_num
|
|
|
+ WHERE
|
|
|
+ a.student_num = ?
|
|
|
+ `
|
|
|
const rows = await db.query(emailSql, [account])
|
|
|
if (!rows || rows.length == 0) {
|
|
|
this.logger.error(`${account}查找用户邮箱失败`)
|
|
|
@@ -340,13 +420,35 @@ class Lepao {
|
|
|
}
|
|
|
|
|
|
const data = {
|
|
|
+ type: 'lepao_fail',
|
|
|
+ umo: rows[0].bot_umo,
|
|
|
name: rows[0].name,
|
|
|
account,
|
|
|
reason: reason === 'Request failed with status code 503' ? 'RunForge系统维护中,请稍后再试' : reason
|
|
|
}
|
|
|
|
|
|
- await EmailTemplate.lepaoFail(rows[0].email, data)
|
|
|
- this.logger.info(`${account}乐跑失败邮件发送完成`)
|
|
|
+ if (rows[0].notice_type === 'bot' && rows[0].bot_umo) {
|
|
|
+ this.logger.info(`${account}发送乐跑失败Bot通知,UMO=${rows[0].bot_umo}`)
|
|
|
+ const ch = await mq.getChannel(this.messageQueue)
|
|
|
+
|
|
|
+ await ch.assertQueue(this.messageQueue, {
|
|
|
+ durable: true
|
|
|
+ })
|
|
|
+
|
|
|
+ ch.sendToQueue(
|
|
|
+ this.messageQueue,
|
|
|
+ Buffer.from(JSON.stringify(data)),
|
|
|
+ {
|
|
|
+ persistent: true,
|
|
|
+ contentType: 'application/json'
|
|
|
+ }
|
|
|
+ )
|
|
|
+
|
|
|
+ this.logger.info(`${account}Bot通知发送完成`)
|
|
|
+ } else if (rows[0].notice_type === 'email' && rows[0].email) {
|
|
|
+ await EmailTemplate.lepaoFail(rows[0].email, data)
|
|
|
+ this.logger.info(`${account}乐跑失败邮件发送完成`)
|
|
|
+ }
|
|
|
} catch (error) {
|
|
|
this.logger.error(`发送失败邮件失败: ${error.stack || error.message}`)
|
|
|
}
|
|
|
@@ -390,7 +492,7 @@ class Lepao {
|
|
|
|
|
|
const recordRes = await axios.post(countUrl, reqData)
|
|
|
const { data } = recordRes
|
|
|
- if(!data || !data.counts) {
|
|
|
+ if (!data || !data.counts) {
|
|
|
this.logger.warn(`修复乐跑记录失败,接口返回异常`)
|
|
|
return
|
|
|
}
|