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


import com.alibaba.fastjson.JSONObject;
import com.topdraw.config.RabbitMqConfig;
import com.topdraw.mq.domain.SubscribeBean;
import com.topdraw.resttemplate.RestTemplateClient;
import com.topdraw.utils.RedisUtils;
import com.topdraw.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

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

    @Autowired
    private RestTemplateClient restTemplateClient;

    private static final String QR_CODE_URL = "QR_CODE_URL_";

    /**
     * @description 删除用户收藏记录
     * @param content 消息内容
     */
    /*@RabbitHandler
    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue(value = RabbitMqConfig.COLLECTION_DELETE_QUEUE),
                    exchange = @Exchange(value = ExchangeTypes.DIRECT))},
            containerFactory = "managementRabbitListenerContainerFactory")*/
    public void deleteCollection(String content) {
        try {
            log.info("receive UserCollection delete message, content {}", content);
            this.restTemplateClient.deleteCollection(content);
        } catch (Exception e) {
            log.error("CollectionDeleteConsumer || UserCollection delete error || {}", e.toString(), e);
        }
    }

    /**
     * @description 删除全部收藏记录
     * @param content 消息内容
     */
    /*@RabbitHandler
    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue(value = RabbitMqConfig.COLLECTION_DELETE_ALL_QUEUE),
                    exchange = @Exchange(value = ExchangeTypes.DIRECT))},
            containerFactory = "managementRabbitListenerContainerFactory")*/
    @Transactional
    public void deleteAllCollection(String content) {
        try {
            log.info("receive UserCollection delete all message, content {}", content);
            this.restTemplateClient.deleteAllCollection(content);
        } catch (Exception e) {
            log.error("CollectionDeleteConsumer || UserCollection delete all error || {}", e.toString(), e);
        }
    }

    /**
     * 关注和取关事件
     * eg:
     * {
     * "appIdMap": "{\"mpId\":\"234\"}",
     * "allFieldsMap":"{\"FromUserName\":\"4343\",\"MsgType\":\"event\",\"Event\":\"unsubscribe\"}"
     * }
     * @param content
     */
   /* @RabbitHandler
    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue(value = RabbitMqConfig.WEIXIN_SUBORUNSUB_QUEUE),
                    exchange = @Exchange(value = ExchangeTypes.DIRECT))},
            containerFactory = "managementRabbitListenerContainerFactory")*/
    @Transactional
    public void subOrUnSubEvent(String content) {
        try {
            log.info("receive wxu subOrUnSub message, content {}", content);
            JSONObject jsonObject = JSONObject.parseObject(content);

            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");
                log.info("event ===>> [{}]",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);

            }

        } catch (Exception e) {
            log.error("WXSubscribeConsumer || subOrUnSub msg error || {} || {}", content, e.getMessage());
        }
    }

    /**
     * @description 添加收藏记录
     * @param content 消息内容
     */
    /*@RabbitHandler
    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue(value = RabbitMqConfig.COLLECTION_ADD_QUEUE),
                    exchange = @Exchange(value = ExchangeTypes.DIRECT))},
            containerFactory = "managementRabbitListenerContainerFactory")*/
    @Transactional
    public void addCollection(String content) {
        try {
            log.info("receive UserCollection add message, content {}", content);
            this.restTemplateClient.addCollection(content);
        } catch (Exception e) {
            log.error("CollectionAddConsumer || UserCollection add error || {}", e.toString(), e);
        }
    }

}