WeiXinEventConsumer.java 3.31 KB
package com.topdraw.mq.consumer;


import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import com.topdraw.exception.BadRequestException;
import com.topdraw.mq.domain.SubscribeBean;
import com.topdraw.resttemplate.RestTemplateClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.IOException;

/**
 * 微信事件
 */
@Component
@Slf4j
public class WeiXinEventConsumer {

    @Autowired
    private RestTemplateClient restTemplateClient;

    /*@Value("#{rabbitMqErrorLogConfig.getWechatError()}")
    private Map<String, String> error;*/


    /**
     * 关注和取关事件
     * eg:
     * {
     * "appIdMap": "{\"mpId\":\"234\"}",
     * "allFieldsMap":"{\"FromUserName\":\"4343\",\"MsgType\":\"event\",\"Event\":\"unsubscribe\"}"
     * }
     * @param content
     */
    /*@RabbitHandler
    @RabbitListener(queues = "#{rabbitMqSourceConfig.getWechatQueue()}",
            containerFactory = "#{rabbitMqSourceConfig.getWechatSource()}",
            autoStartup = "#{rabbitMqSourceConfig.getWechatStartUp()}", ackMode = "AUTO")*/
    @RabbitHandler
    @RabbitListener(queues = "#{rabbitMqConfig.getWechatQueue()}", ackMode = "AUTO")
    public void subOrUnSubEvent(Channel channel, Message message, String content) throws IOException {
        log.info("消费wechat-gate公众号关注、取关消息,参数 subOrUnSubEvent# content ==>> {}", content);
        try {

            if (StringUtils.isEmpty(content)) {
                throw new BadRequestException("无参数");
            }

            JSONObject jsonObject = JSONObject.parseObject(content);
            log.info("消费uc-gate公众号关注、取关消息,解析参数结果,subOrUnSubEvent# jsonObject ==>> {} ", jsonObject);

            JSONObject map = jsonObject.getJSONObject("appIdMap");
            JSONObject wechatMsg = jsonObject.getJSONObject("allFieldsMap");
            String appid = map.getString("mpId");
            String unionid = map.getString("unionid");

            String openid = wechatMsg.getString("FromUserName");
            String msgType = wechatMsg.getString("MsgType");

            if ("event".equals(msgType)) {
                String event = wechatMsg.getString("Event");
                String eventKey = wechatMsg.getString("EventKey");

                SubscribeBean subscribeBean = new SubscribeBean();
                subscribeBean.setAppid(appid);
                subscribeBean.setOpenid(openid);
                subscribeBean.setUnionid(unionid);
                subscribeBean.setEventKey(eventKey);

                if (event.equals("subscribe")) {
                    this.restTemplateClient.subscribe(subscribeBean);
                }

                if (event.equals("unsubscribe")) {
                    this.restTemplateClient.unsubscribe(subscribeBean);
                }

//                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            }

        } catch (Exception e) {
            log.error("消费wechat-gate公众号关注、取关消息异常,subOrUnSubEvent# message ==>> {}", e.getMessage());
        }

    }

}