Files
BYW-VoiceHub/server/utils
Henry_Du d1b643bd93 refactor(微信订阅): 重写订阅消息链路,每一步失败都可见
把「微信订阅消息」这套代码按验收标准推翻重写:用户能自己看懂为什么收不到,
任何一步失败都必须看得见。对外函数名不变,notificationService / test-subscribe
等调用点无需改动。

新增 server/utils/wechatSubscribeTemplates.ts(纯函数,零依赖):
  - 模板登记表唯一来源(内置 SONG_SELECTED / SONG_REJECTED),服务层不再自己写死
  - parseSubscribeTemplates 如实报告环境变量覆盖情况:坏 JSON 只报告不抛、
    只给 id 时沿用内置字段映射、缺 id 的项记进 unknownKeys 并忽略
  - findEmptyFields / findEmptyDataFields 发出前自查空字段(微信空值整条 47003)
  - explainWechatErrcode 把 43101/47003/40003/40037/40001/45009 翻成人话

重写 server/services/wechatSubscribeService.ts:
  - sendWechatSubscribe 返回 SendOutcome,用 stage 明确「卡在哪一步」:
    no-template / disabled-by-user / no-wechat-openid / no-access-token /
    empty-field / no-grant / rejected-no-quota / rejected-bad-field / rejected-other
  - 每一次返回都写一行日志,不再有静默跳过(这是「发不出去全靠猜」的根因)
  - inspectWechatSubscribe 只读体检:不发消息、不消耗额度,返回逐项 verdict + actions
  - grantWechatSubscribe 依靠新增的 WechatSubscribeGrant 表记账:微信没有查询
    剩余授权的接口,只能自己数(用户点允许 +1、发送成功 -1)
  - 记账表不可用时失败开放(fail-open):额度预检关闭、照常发送,避免通知整体哑掉
  - access_token 显式 responseType: json 并做字符串兜底解析

新增接口:
  - GET  /api/user/wechat-subscribe-status   小程序显示「还能收到几条 / 为什么收不到」
  - POST /api/user/wechat-subscribe-grant    小程序授权成功后上报 accept 的模板
  - GET  /api/admin/wechat/diagnose          重写:人话结论 + actions,?send=1 真发一次

其他:新增 WechatSubscribeGrant 表(schema + 手写迁移)、.env.example 说明 JSON 覆盖
写法、.gitattributes 补 *.sql eol=lf。

测试:新增 test/wechatSubscribeTemplates.test.mjs(13 个用例,可直接 import .ts,
不依赖 Nuxt 别名),npm test 里订阅相关 25/25 通过。
2026-10-11 16:02:33 +08:00
..
2026-09-06 08:19:02 +08:00
2026-09-06 08:19:02 +08:00
2026-09-06 08:19:02 +08:00
2026-09-06 08:19:02 +08:00
2026-09-06 08:19:02 +08:00
2026-09-06 08:19:02 +08:00
2026-09-06 08:19:02 +08:00
chore(auth): 临时硬关闭微信聚合登录(aggregate=false)\n\n用户反馈:先暂时不做微信登录了。\n\n双重保险:\n1) 构建期开关:nuxt.config.ts runtimeConfig.public.oauth.aggregate 由\n 原来的 !!process.env.AGGREGATE_OAUTH_APP_ID 改为硬编码 false。\n 影响范围:\n - 登录页 OAuthButtons.vue 中的 v-if 分支不再渲染绿色微信按钮\n - app/pages/account/index.vue 中 hasOAuthProviders aggregate=false → 个人页\n 第三方绑定分区即使其他 provider 都没配也会显示其它 provider,\n 但微信卡片 OAuthBindingCard.vue 因 config.public.oauth.aggregate=false\n 被 v-if 直接拿掉不渲染\n - app/utils/oauth.ts getProviderDisplayName 中 aggregate→微信 映射还保留,\n 恢复时无需再改\n2) 运行期强制关:server/utils/oauth-strategies.ts loadAggregateSettings\n 返回值 aggregateOAuthEnabled 硬编码 false(即使 process.env.AGGREGATE_OAUTH_APP_ID\n 和 system_settings 表都配齐也不启用)。并加 _hardDisabledReason 字段给后续日志\n 留解释,避免以后排查为何 ENV/DB 都有值却 500 说未配置。\n\n后续恢复步骤(两处同时改回即可):\n A. nuxt.config.ts public.oauth.aggregate 改回\n aggregate: !!process.env.AGGREGATE_OAUTH_APP_ID\n B. server/utils/oauth-strategies.ts loadAggregateSettings 返回对象\n aggregateOAuthEnabled 改回原本逻辑:\n enabled && !!(appId && appKey && endpoint)\n 并删除 _hardDisabledReason 字段即可。\n\n注意:Gitea Repository Secrets 中的 6 条 AGGREGATE_OAUTH / OAUTH_* 没有删除,\n保留在仓库里,下次恢复不用重新输入一遍。
2026-09-13 09:29:05 +08:00
2026-09-06 08:19:02 +08:00
2026-09-06 08:19:02 +08:00
2026-09-06 08:19:02 +08:00
2026-09-06 08:19:02 +08:00
2026-09-06 08:19:02 +08:00
2026-09-06 08:19:02 +08:00
2026-09-06 08:19:02 +08:00
2026-09-06 08:19:02 +08:00
2026-09-06 08:19:02 +08:00