WeiXinEventConsumer.java
3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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());
}
}
}