'use strict'; const { Controller } = require('egg'); /** * 微信登录网关回调(**公开接口**,靠签名鉴权) * * 网关在用户微信里确认后 `POST` 到这里(地址在网关管理台配成「服务端通知地址」, * 对应 `config.wxGateway.notifyPath`)。请求体带 `sign` / `timestamp` / `nonce`, * 用 `sha256(appSecret)` 当密钥做 HMAC-SHA256,见 `service/wx_gateway.js#verifyNotify`。 * * ⚠️ 网关只看 HTTP 状态码:**2xx 视为成功**,失败会重试 1 次。 * 真正给浏览器/设备看的结果,通过本地会话(Redis)交给正在长轮询的那个请求。 */ class GatewayNotifyController extends Controller { /** POST /api/wechat-gateway/notify */ async index() { const { ctx } = this; const svc = ctx.service.wxGateway; if (!svc.enabled) { ctx.status = 503; ctx.body = { ok: false, message: '未配置微信登录网关' }; return; } const body = ctx.request.body || {}; const checked = svc.verifyNotify(body); if (!checked.ok) { ctx.logger.warn('[wx-gateway] 通知验签失败:%s(event=%s state=%s)', checked.message, body.event, body.state); ctx.status = 401; ctx.body = { ok: false, message: checked.message }; return; } const data = await svc.handleNotify(body); ctx.logger.info('[wx-gateway] 通知已处理:event=%s state=%s → %j', body.event, body.state, data); ctx.body = { ok: true, data }; } } module.exports = GatewayNotifyController;