Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jul 24, 2025

问题描述

WxOpenMessageRouter 类原本只支持将消息路由到 WxMpService(公众号),但缺少对 WxOpenMaService(小程序)的支持。这导致开发者在通过路由器处理小程序消息时无法访问小程序特定的功能。

用户原始问题:

@Component
public class WxMaAuditHandler extends AbstractWxOpenHandler {
    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, 
                                   WxMpService wxMpService, WxSessionManager sessionManager) {
        // ❌ 问题:无法获取 WxOpenMaService 来进行小程序操作
        // wxOpenService.getWxOpenComponentService().getWxMaServiceByAppid(""); // 缺少 appid 上下文
    }
}

解决方案

WxOpenMessageRouter 中新增了两个方法,将 WxOpenMaService 注入到消息上下文中:

  1. routeForMa(WxMpXmlMessage wxMessage, String appId) - 路由小程序消息并自动注入服务
  2. routeForMa(WxMpXmlMessage wxMessage, Map<String, Object> context, String appId) - 支持自定义上下文并注入服务

主要特性:

  • 向后兼容:所有现有的 route() 方法保持不变
  • 上下文注入:可通过 context.get("wxOpenMaService") 获取 WxOpenMaService
  • API一致性:使用与现有方法相同的 WxMpXmlMessage 格式
  • 改动最小:仅新增约25行代码

使用方式

开发者现在可以在处理器中访问 WxOpenMaService

@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, 
                               WxMpService wxMpService, WxSessionManager sessionManager) {
    
    // ✅ 现在可以获取 WxOpenMaService 进行小程序操作
    WxOpenMaService maService = (WxOpenMaService) context.get("wxOpenMaService");
    if (maService != null) {
        WxOpenMaQueryAuditResult result = maService.getLatestAuditStatus();
        maService.releaseAudited();
    }
    return null;
}

// 使用新方法路由小程序消息
router.routeForMa(message, appId);  // ✨ 新增 - 注入 WxOpenMaService
router.route(message, appId);       // 不变 - 用于公众号消息

这使开发者能够通过现有的路由器基础设施处理小程序审核事件、代码提交和其他小程序特定操作。

修复 #3629


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] WxOpenMessageRouter类方法中中缺少对WxOpenMaService的支持 Add WxOpenMaService support to WxOpenMessageRouter with routeForMa methods Jul 24, 2025
Copilot AI requested a review from binarywang July 24, 2025 08:02
Copilot finished work on behalf of binarywang July 24, 2025 08:02
@binarywang binarywang added this to the 4.7.9 milestone Sep 24, 2025
@binarywang binarywang modified the milestones: 4.7.9, 4.8.0 Nov 27, 2025
@binarywang
Copy link
Owner

@copilot 请用中文改下描述文字

Copy link
Contributor Author

Copilot AI commented Nov 28, 2025

@copilot 请用中文改下描述文字

已将 PR 描述改为中文。

Copilot AI changed the title Add WxOpenMaService support to WxOpenMessageRouter with routeForMa methods 为 WxOpenMessageRouter 添加 WxOpenMaService 支持,新增 routeForMa 方法 Nov 28, 2025
Copilot finished work on behalf of binarywang November 28, 2025 03:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants