Files
BYW-VoiceHub/server/api/user/wechat-subscribe-templates.get.ts
Henry_Du a2ba15c3ca feat(notify): 微信订阅消息推送(驳回通知已对接)
- 新增 server/services/wechatSubscribeService.ts:access_token 缓存 + 模板配置 + 字段裁剪 + subscribe/send
  · 内置默认模板 SONG_REJECTED(模板 ID erAK**…,审核内容=thing6←歌名,驳回原因=thing4←驳回原因)
  · 环境变量 WECHAT_MINI_SUBSCRIBE_TEMPLATES 可按键覆盖默认模板
  · thing 字段按微信限制裁到 20 字(超长截 19 字 + 省略号),避免 47003
  · 发送永不抛异常;43101(用户没授权/没额度)只记 info
- 新增 server/api/user/wechat-subscribe-templates.get.ts:给小程序返回模板 ID(客户端要拿它去申请授权)
- 新增 server/api/admin/wechat/test-subscribe.post.ts:超管自测接口(token/openid/字段/微信原文一次看清)
- notificationService.ts:歌曲被选中/已播放/被驳回/系统通知 四处顺带发微信订阅消息
- .env.example + 部署流水线:WECHAT_MINI_SUBSCRIBE_TEMPLATES 可用仓库 Secret 下发
2026-09-27 13:55:55 +08:00

20 lines
735 B
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { getSubscribeTemplateIds, isWechatSubscribeEnabled } from '~~/server/services/wechatSubscribeService'
/**
* 小程序申请「订阅消息」授权时需要的模板 ID。
*
* 模板 ID 不是秘密:它本来就要出现在客户端 `wx.requestSubscribeMessage({ tmplIds })` 里。
* 没配置模板(环境变量 WECHAT_MINI_SUBSCRIBE_TEMPLATES 为空)时 enabled=false,
* 小程序那边会静默隐藏「开启微信通知」的入口。
*/
export default defineEventHandler((event) => {
const user = event.context.user
if (!user) {
throw createError({ statusCode: 401, message: '未授权访问' })
}
return {
enabled: isWechatSubscribeEnabled(),
tmplIds: getSubscribeTemplateIds()
}
})