定时任务改为按用户分组、按优先级发送,额度耗尽即停
This commit is contained in:
+104
-47
@@ -1,8 +1,8 @@
|
||||
const cron = require('node-cron')
|
||||
const db = require('./db')
|
||||
const wx = require('./wx')
|
||||
const lunar = require('./lunar')
|
||||
const occurrence = require('./occurrence')
|
||||
const quota = require('./quota')
|
||||
|
||||
const TEMPLATE_ID = process.env.WX_TEMPLATE_ID
|
||||
const MINIPROGRAM_STATE = process.env.WX_MINIPROGRAM_STATE || 'formal'
|
||||
@@ -43,71 +43,128 @@ const insertLog = db.prepare(`
|
||||
VALUES (@anniversaryId, @personName, @typeName, @daysUntil, @sendDate, @status, @error)
|
||||
`)
|
||||
|
||||
async function runOnce() {
|
||||
console.log('[reminder] 开始扫描纪念日...')
|
||||
// 重要程度排序权重,未知值排最后
|
||||
const IMPORTANCE_RANK = { high: 0, medium: 1, low: 2 }
|
||||
function importanceRank(v) {
|
||||
return IMPORTANCE_RANK[v] === undefined ? 3 : IMPORTANCE_RANK[v]
|
||||
}
|
||||
|
||||
const list = db.prepare('SELECT * FROM anniversaries WHERE remindEnabled = 1').all()
|
||||
console.log(`[reminder] 启用提醒的纪念日 ${list.length} 条`)
|
||||
// 43101 = 用户拒收或下发次数不足,是我们与微信侧对账的唯一信号
|
||||
function isQuotaError(err) {
|
||||
return err && err.errcode === 43101
|
||||
}
|
||||
|
||||
let ok = 0
|
||||
let fail = 0
|
||||
/**
|
||||
* 处理单个用户的当日提醒
|
||||
*
|
||||
* 按「当天 > 提前」、同级「重要程度降序」排序后依次发送,余额耗尽即停,
|
||||
* 不在明知没额度时继续打无谓请求。
|
||||
*/
|
||||
async function runForUser(openid, items, today = new Date()) {
|
||||
const due = []
|
||||
for (const anniv of items) {
|
||||
const target = occurrence.getNextOccurrence(anniv, today)
|
||||
const daysUntil = occurrence.daysBetween(target, today)
|
||||
const remindDays = anniv.remindDays || 0
|
||||
|
||||
for (const anniv of list) {
|
||||
const isOnDay = daysUntil === 0
|
||||
const isAhead = remindDays > 0 && daysUntil === remindDays
|
||||
if (!isOnDay && !isAhead) continue
|
||||
if (alreadySentToday(anniv.id)) continue
|
||||
|
||||
due.push({ anniv, target, daysUntil, kind: isOnDay ? 'onDay' : 'ahead' })
|
||||
}
|
||||
|
||||
due.sort((x, y) => {
|
||||
if (x.kind !== y.kind) return x.kind === 'onDay' ? -1 : 1
|
||||
return importanceRank(x.anniv.importance) - importanceRank(y.anniv.importance)
|
||||
})
|
||||
|
||||
let balance = quota.getBalance(openid)
|
||||
let halted = false
|
||||
let ok = 0, fail = 0, skipped = 0
|
||||
|
||||
const logSkip = (item) => insertLog.run({
|
||||
anniversaryId: item.anniv.id,
|
||||
personName: item.anniv.personName,
|
||||
typeName: getTypeName(item.anniv.type, item.anniv.customTypeName),
|
||||
daysUntil: item.daysUntil,
|
||||
sendDate: Date.now(),
|
||||
status: 'skipped',
|
||||
error: 'quota_exhausted'
|
||||
})
|
||||
|
||||
for (const item of due) {
|
||||
if (halted || balance <= 0) {
|
||||
logSkip(item); skipped++
|
||||
continue
|
||||
}
|
||||
|
||||
const anniv = item.anniv
|
||||
const typeName = getTypeName(anniv.type, anniv.customTypeName)
|
||||
try {
|
||||
const target = occurrence.getNextOccurrence(anniv)
|
||||
const daysUntil = occurrence.daysBetween(target)
|
||||
|
||||
const shouldRemind = (daysUntil === 0) || (daysUntil === (anniv.remindDays || 0))
|
||||
if (!shouldRemind) continue
|
||||
|
||||
if (alreadySentToday(anniv.id)) {
|
||||
console.log(`[reminder] 今天已发过,跳过: ${anniv.personName}`)
|
||||
continue
|
||||
}
|
||||
|
||||
const typeName = getTypeName(anniv.type, anniv.customTypeName)
|
||||
|
||||
await wx.sendSubscribeMessage({
|
||||
touser: anniv.openid,
|
||||
touser: openid,
|
||||
page: 'pages/index/index',
|
||||
templateId: TEMPLATE_ID,
|
||||
miniprogramState: MINIPROGRAM_STATE,
|
||||
data: {
|
||||
name1: { value: anniv.personName },
|
||||
thing2: { value: daysUntil === 0 ? '今天' : `还有${daysUntil}天` },
|
||||
thing6: { value: formatDate(target) },
|
||||
thing2: { value: item.daysUntil === 0 ? '今天' : `还有${item.daysUntil}天` },
|
||||
thing6: { value: formatDate(item.target) },
|
||||
thing5: { value: anniv.remark || '别忘了准备一份礼物哦!' }
|
||||
}
|
||||
})
|
||||
|
||||
quota.consume(openid, 1)
|
||||
balance--
|
||||
insertLog.run({
|
||||
anniversaryId: anniv.id,
|
||||
personName: anniv.personName,
|
||||
typeName,
|
||||
daysUntil,
|
||||
sendDate: Date.now(),
|
||||
status: 'success',
|
||||
error: null
|
||||
anniversaryId: anniv.id, personName: anniv.personName, typeName,
|
||||
daysUntil: item.daysUntil, sendDate: Date.now(), status: 'success', error: null
|
||||
})
|
||||
ok++
|
||||
console.log(`[reminder] 发送成功: ${anniv.personName} (${typeName}, ${daysUntil}天)`)
|
||||
console.log(`[reminder] 发送成功: ${anniv.personName} (${typeName}, ${item.daysUntil}天)`)
|
||||
} catch (err) {
|
||||
fail++
|
||||
console.error(`[reminder] 发送失败: ${anniv.personName}`, err.message)
|
||||
insertLog.run({
|
||||
anniversaryId: anniv.id,
|
||||
personName: anniv.personName,
|
||||
typeName: null,
|
||||
daysUntil: null,
|
||||
sendDate: Date.now(),
|
||||
status: 'failed',
|
||||
error: err.message
|
||||
})
|
||||
if (isQuotaError(err)) {
|
||||
// 微信侧实际已无额度(或用户关了通知总开关),归零并停止骚扰
|
||||
quota.reset(openid)
|
||||
balance = 0
|
||||
halted = true
|
||||
logSkip(item); skipped++
|
||||
console.warn(`[reminder] ${openid} 额度已耗尽,本轮剩余全部跳过`)
|
||||
} else {
|
||||
insertLog.run({
|
||||
anniversaryId: anniv.id, personName: anniv.personName, typeName: null,
|
||||
daysUntil: null, sendDate: Date.now(), status: 'failed', error: err.message
|
||||
})
|
||||
fail++
|
||||
console.error(`[reminder] 发送失败: ${anniv.personName}`, err.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`[reminder] 完成: 成功 ${ok}, 失败 ${fail}`)
|
||||
return { total: list.length, ok, fail }
|
||||
return { ok, fail, skipped }
|
||||
}
|
||||
|
||||
async function runOnce() {
|
||||
console.log('[reminder] 开始扫描纪念日...')
|
||||
const list = db.prepare('SELECT * FROM anniversaries WHERE remindEnabled = 1').all()
|
||||
console.log(`[reminder] 启用提醒的纪念日 ${list.length} 条`)
|
||||
|
||||
// 额度是按用户算的,所以必须分组处理
|
||||
const byOpenid = new Map()
|
||||
for (const a of list) {
|
||||
if (!byOpenid.has(a.openid)) byOpenid.set(a.openid, [])
|
||||
byOpenid.get(a.openid).push(a)
|
||||
}
|
||||
|
||||
let ok = 0, fail = 0, skipped = 0
|
||||
for (const [openid, items] of byOpenid) {
|
||||
const r = await runForUser(openid, items)
|
||||
ok += r.ok; fail += r.fail; skipped += r.skipped
|
||||
}
|
||||
|
||||
console.log(`[reminder] 完成: 成功 ${ok}, 失败 ${fail}, 跳过 ${skipped}`)
|
||||
return { total: list.length, ok, fail, skipped }
|
||||
}
|
||||
|
||||
function start() {
|
||||
@@ -120,4 +177,4 @@ function start() {
|
||||
console.log(`[reminder] 定时任务已注册: ${expr} (Asia/Shanghai)`)
|
||||
}
|
||||
|
||||
module.exports = { start, runOnce }
|
||||
module.exports = { start, runOnce, runForUser }
|
||||
|
||||
+3
-1
@@ -49,7 +49,9 @@ async function sendSubscribeMessage({ touser, page, data, templateId, miniprogra
|
||||
}
|
||||
const { data: res } = await axios.post(url, body, { timeout: 8000 })
|
||||
if (res.errcode !== 0) {
|
||||
throw new Error(`发送订阅消息失败: ${JSON.stringify(res)}`)
|
||||
const err = new Error(`发送订阅消息失败: ${JSON.stringify(res)}`)
|
||||
err.errcode = res.errcode // 让调用方能精确判断 43101,而不是靠字符串匹配
|
||||
throw err
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user