NutzCN Logo
问答 多个微信公众号事件点击异常
发布于 1923天前 作者 suyun 1643 次浏览 复制 上一个帖子 下一个帖子
标签: nutzwk

我们公司有个阿里云的服务器,部署了一套由tomcat发布的管理系统,框架用的是nutz+nutzwk,其中集成了微信模块,有多个客户的微信公众号都由这个服务器的系统代理,现在出现了一种情况,菜单上绑定的网页的跳转都没问题,唯独点击事件,发送消息事件,关键词回复有问题。
我试了很长时间,发现如果重启了tomcat之后,哪个公众号先触发了上面有问题的事件,那么这个公众号就能正常使用,但是其他代理的公众号都会出现提示:“该公众号号提供的服务出现故障,请稍后再试”,后台打印了一下接口返回的视图内容是:httpStatus:403,Forbidden.
我猜想是不是一个服务器不能代理多个公众号啊,请大神们帮忙给个思路!

11 回复

WxHandler

    public WxOutMsg eventClick(WxInMsg msg) {
        String eventKey = msg.getEventKey();
        log.debug("eventKey: " + eventKey);
        log.debug("extKey: " + msg.getExtkey());

打印下 extKey 值,是不是wx_config.id 里的值

WxHandler.class 中部分参数使用单实例的问题,下面贴代码出来

WxHandler是个接口...

原代码:

@IocBean(name = "wxHandler")
public class WxHandler extends AbstractWxHandler {
    private final static Log log = Logs.get();
    protected String token;
    protected String aeskey;
    protected WXBizMsgCrypt msgCrypt;
    protected String appid;
    protected WxApi2 api;
    @Inject
    private WxConfigService wxConfigService;
    @Inject
    private WxUserService wxUserService;
    @Inject
    private WxReplyService wxReplyService;
    @Inject
    private WxReplyNewsService wxReplyNewsService;
    @Inject
    private WxReplyTxtService wxReplyTxtService;
    @Inject
    private WxMsgService wxMsgService;

    public boolean check(String signature, String timestamp, String nonce, String key) {
        Wx_config appInfo = wxConfigService.fetch(Cnd.where("id", "=", key));
        if(appInfo!=null){
            this.token=appInfo.getToken();
            this.aeskey=appInfo.getEncodingAESKey();
            this.appid=appInfo.getAppid();
            return Wxs.check(appInfo.getToken(), signature, timestamp, nonce);
        }
        return false;
    }

    public WXBizMsgCrypt getMsgCrypt() {
        if (this.msgCrypt == null) {
            try {
                // 若抛异常Illegal key size ,需更新JDK的加密库为不限制长度
                this.msgCrypt = new WXBizMsgCrypt(this.token, this.aeskey, this.appid);
            } catch (AesException var2) {
                throw new RuntimeException(var2);
            }
        }

        return this.msgCrypt;
    }
.........
}

修改后的代码

@IocBean(name = "wxHandler")
public class WxHandler extends AbstractWxHandler {
    private final static Log log = Logs.getLog(WxHandler.class);
    protected WxApi2 api;
    private final static ThreadLocal<Wx_config> threadLocal = new ThreadLocal<>();
    @Inject
    private WxConfigService wxConfigService;
    @Inject
    private WxUserService wxUserService;
    @Inject
    private WxReplyService wxReplyService;
    @Inject
    private WxReplyNewsService wxReplyNewsService;
    @Inject
    private WxReplyTxtService wxReplyTxtService;
    @Inject
    private WxMsgService wxMsgService;

    public boolean check(String signature, String timestamp, String nonce, String key) {
        Wx_config appInfo = wxConfigService.fetch(Cnd.where("id", "=", key));
        if(appInfo!=null){
            threadLocal.set(appInfo);
            return Wxs.check(appInfo.getToken(), signature, timestamp, nonce);
        }
        return false;
    }

    public WXBizMsgCrypt getMsgCrypt() {
        Wx_config appInfo = threadLocal.get();
        if (appInfo != null) {
            try {
                // 若抛异常Illegal key size ,需更新JDK的加密库为不限制长度
                return new WXBizMsgCrypt(appInfo.getToken(), appInfo.getEncodingAESKey(), appInfo.getAppid());
            } catch (AesException var2) {
                throw new RuntimeException(var2);
            }
        }

        return null;
    }
..............
}
@IocBean(name = "wxHandler")

改为

@IocBean(name = "wxHandler",singleton = false)

即可

好的, 等下我试试,感谢

@Wizzercn 添加 singleton = false 不可以的,还是会出现问题

我用两个公众号测试了没问题啊,在singleton = true的情况下也没问题:
1、是不是你debug模式启动的,超时会报那个错;
2、你的网络不好;
3、刚修改配置,微信服务器还没有生效;

Wxs.enableDevMode();
开启了这个

添加回复
请先登陆
回到顶部