WeiXinEventConsumer.java
5.17 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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);
}
}
}