Files
BYW-VoiceHub/server/api/admin/schedule/bulk-publish.post.ts
T
Henry_Du 3eff710d5d feat(微信订阅): 接入歌曲排期模板并加固部署流程
订阅消息(小程序侧唯一能主动触达用户的通道):
- 新增 server/utils/wechatSubscribeFields.ts:按微信字段类型裁剪值(thing≤20、
  phrase 必须 5 个汉字、time/date/character_string 各自上限),空白压成一行,
  北京时间 time 由 playTime 的开始时间生成,解析不出退化为当天 00:00。
- wechatSubscribeService 内置 SONG_SELECTED 模板(站长新给的那个模板 ID),
  并转发上述纯函数;notificationService.createSongSelectedNotification 现在会带上
  playDate/playTime/sequence,三个排期接口(schedule / publish / bulk-publish)补传 sequence。
- 新增 test/wechatSubscribeFields.test.mjs(10 例,node --test 全绿)。
- test-subscribe 管理接口支持 vars 覆盖并返回真实发送值,方便站长核对字段名。

部署流程(Gitea Actions 容器里没有 buildx 插件,旧流程在此硬失败):
- 每个 job 各自安装/探测 docker-buildx(v0.19.3,走 gh-proxy 再退直连),
  不把 `buildx version` 当唯一依据;装不上时降级为普通 `docker build` 继续发布,
  不再因为缺插件整条流水线失败。

注意:SONG_SELECTED 的字段名(thing2/date4/time1/character_string3/phrase5)是按常见
命名推断的,需在公众平台后台核对;不一致时用环境变量覆盖,模板 ID 变更后老用户需重新授权。
2026-10-11 00:04:14 +08:00

184 lines
5.9 KiB
TypeScript

import { db } from '~/drizzle/db'
import { playTimes, schedules, songs, songReplayRequests } from '~/drizzle/schema'
import { and, eq, gte, lte, inArray } from 'drizzle-orm'
import { createSongSelectedNotification } from '~~/server/services/notificationService'
import { cacheService } from '~~/server/services/cacheService'
import { getClientIP } from '~~/server/utils/ip-utils'
export default defineEventHandler(async (event) => {
// 检查用户认证和权限
const user = event.context.user
if (!user) {
throw createError({
statusCode: 401,
message: '未授权访问'
})
}
if (!['SONG_ADMIN', 'ADMIN', 'SUPER_ADMIN'].includes(user.role)) {
throw createError({
statusCode: 403,
message: '只有歌曲管理员及以上权限才能发布排期'
})
}
const body = await readBody(event)
// 验证必要参数
if (!body.playDate || !body.songs || !Array.isArray(body.songs)) {
throw createError({
statusCode: 400,
message: '参数无效:需要 playDate 和 songs 数组'
})
}
const clientIP = getClientIP(event)
const startTime = Date.now()
try {
// 解析日期
const inputDateStr =
typeof body.playDate === 'string' ? body.playDate : body.playDate.toISOString()
// 确保使用当天的开始时间(UTC)
const playDate = new Date(inputDateStr + 'T00:00:00.000Z')
const startOfDay = new Date(inputDateStr + 'T00:00:00.000Z')
const endOfDay = new Date(inputDateStr + 'T23:59:59.999Z')
const playTimeId = body.playTimeId ? parseInt(body.playTimeId) : null
// 获取所有涉及的歌曲详情(用于通知)
const songIds = body.songs.map((s: any) => s.songId)
if (songIds.length === 0) {
// 如果列表为空,说明是清空排期,直接执行删除逻辑即可
}
const songDetails =
songIds.length > 0 ? await db.select().from(songs).where(inArray(songs.id, songIds)) : []
const songMap = new Map(songDetails.map((s: any) => [s.id, s]))
// 需要发送通知的列表
const notificationsToSend: Array<{
requesterId: number
songId: number
// sequence 要给微信「歌曲排期」订阅消息的排期序号字段用
songInfo: { title: string; artist: string; playDate: Date; sequence?: number | null }
}> = []
// 开始事务
await db.transaction(async (tx) => {
// 1. 构建查询条件:指定日期 + (可选)指定时段
const whereConditions = [
gte(schedules.playDate, startOfDay),
lte(schedules.playDate, endOfDay)
]
if (playTimeId) {
whereConditions.push(eq(schedules.playTimeId, playTimeId))
}
// 2. 查找该时间段内已发布的排期(用于避免重复发送通知)
const existingPublished = await tx
.select({
songId: schedules.songId
})
.from(schedules)
.where(and(...whereConditions, eq(schedules.isDraft, false)))
const existingPublishedSongIds = new Set(existingPublished.map((s) => s.songId))
// 3. 删除该时间段内的所有排期(包括草稿和已发布)
await tx.delete(schedules).where(and(...whereConditions))
// 4. 插入新的排期并处理通知
const publishedAt = new Date()
for (const item of body.songs) {
const song = songMap.get(item.songId)
if (!song) {
console.warn(`找不到歌曲 ID: ${item.songId},跳过排期`)
continue
}
// 插入排期
await tx.insert(schedules).values({
songId: item.songId,
playDate: playDate,
sequence: item.sequence,
playTimeId: playTimeId,
isDraft: false, // 直接发布
publishedAt: publishedAt,
updatedAt: publishedAt
})
// 如果该歌曲之前未在此时间段发布过,则发送通知并更新重播状态
if (!existingPublishedSongIds.has(item.songId)) {
// 添加到通知列表,稍后发送
notificationsToSend.push({
requesterId: song.requesterId,
songId: song.id,
songInfo: {
title: song.title,
artist: song.artist,
playDate: playDate,
sequence: item.sequence
}
})
// 更新重播申请状态
await tx
.update(songReplayRequests)
.set({ status: 'FULFILLED' })
.where(
and(eq(songReplayRequests.songId, song.id), eq(songReplayRequests.status, 'PENDING'))
)
}
}
})
// 事务成功提交后,清除缓存
try {
await cacheService.clearSchedulesCache()
await cacheService.clearSongsCache()
console.log(`[Cache] 排期缓存和歌曲列表缓存已清除(批量发布排期)`)
} catch (cacheError) {
console.error('[Cache] 清除缓存失败:', cacheError)
}
// 异步发送通知(不阻塞响应)
Promise.allSettled(
notificationsToSend.map((n) =>
createSongSelectedNotification(n.requesterId, n.songId, n.songInfo, clientIP)
)
)
.then((results) => {
const successCount = results.filter((r) => r.status === 'fulfilled').length
console.log(
`[Notification] 批量发布通知发送完成: ${successCount}/${notificationsToSend.length} 成功`
)
})
.catch((err) => {
console.error('[Notification] 批量发送通知时发生未捕获错误:', err)
})
console.log(`[Performance] 批量发布排期耗时: ${Date.now() - startTime}ms`)
return {
success: true,
message: '排期发布成功'
}
} catch (error: any) {
console.error('批量发布排期失败:', {
error: error.message,
stack: error.stack,
userId: user.id,
ip: clientIP
})
throw createError({
statusCode: 500,
message: error.message || '发布排期失败'
})
}
})