WeiXinEventConsumer.java
11.1 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
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.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);
}
}
/**
* 处理带参的二维码事件
* @param content 消息内容
* @description 获取公众号带参二维码
*/
/*@RabbitHandler
@RabbitListener(bindings = {
@QueueBinding(value = @Queue(value = RabbitMqConfig.GET_QR_CODE_QUEUE),
exchange = @Exchange(value = ExchangeTypes.DIRECT))},
containerFactory = "managementRabbitListenerContainerFactory")*/
public void getQrCode(String content) {
try {
log.info("receive get qrCode message, content {}", content);
JSONObject jsonObject = JSONObject.parseObject(content);
/*String appid = jsonObject.getString("appid");
String IPTVappid = jsonObject.getString("IPTVappid");
String platformAccount = jsonObject.getString("platformAccount");
String sessionId = jsonObject.getString("sessionId");
String key = QR_CODE_URL + appid + "_" + platformAccount + "_" + sessionId;
String url = (String) redisUtils.get(key);
if (StringUtils.isBlank(url)) {
Map<String, String> wxInfo = WeixinUtil.getWeixinInfoByAppid(appid);
var appType = wxInfo.get("appType");
// 订阅号不支持带参二维码,直接返回
if (StrUtil.isNotEmpty(appType) && ObjectUtil.equals(appType, WeChatConstants.WX_SUBSCRIPTION)) {
log.error("订阅号不支持带参二维码 || {} || {}", appid, content);
return;
}
QrCode qrCode = new QrCode();
qrCode.setActionName(WeChatConstants.QR_STR_SCENE);
if (StringUtils.isNotBlank(wxInfo.get("qrCodeExpireSeconds"))) {
qrCode.setExpireSeconds(Integer.valueOf(wxInfo.get("qrCodeExpireSeconds")));
}
ActionInfo actionInfo = new ActionInfo();
Scene scene = new Scene();
scene.setSceneStr(content);
actionInfo.setScene(scene);
qrCode.setActionInfo(actionInfo);
JSONObject jsonQrCode = weixinRequestUtil.getQrCode(wxInfo, qrCode);
url = jsonQrCode.getString("url");
Integer expireSeconds = jsonQrCode.getInteger("expire_seconds");
redisUtils.set(key, url, expireSeconds, TimeUnit.SECONDS);
}
HashMap<String, Object> map = new HashMap<>();
map.put("sessionId", sessionId);
map.put("url", url);
map.put("appid", appid);
map.put("IPTVappid", IPTVappid);
map.put("platformAccount", platformAccount);
map.put("extraInfo", content);*/
restTemplateClient.sendQrCodeMessage(content);
} catch (Exception e) {
log.error("GetQrCodeConsumer || get qrCode 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");
// Map<String, String> wxInfoMap = WeixinUtil.getWeixinInfoByAppid(appid);
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);
JSONObject jsonObject = JSONObject.parseObject(content);
String platformAccount = jsonObject.getString("platformAccount");
String data = jsonObject.getString("data");
if (StringUtils.isBlank(data) || !data.startsWith("[")) {
// return;
}
/*Optional<TvUser> userOptional = tvUserRepository.findByPlatformAccount(platformAccount);
if (!userOptional.isPresent()) {
return;
}
Long tvUserId = userOptional.get().getId();
List<UserCollectionMq> userCollectionMqList = JSONObject.parseArray(data, UserCollectionMq.class);
if (userCollectionMqList == null || userCollectionMqList.isEmpty()) {
return;
}
Map<Long, List<UserCollectionMq>> collect = userCollectionMqList.stream().collect(Collectors.groupingBy(UserCollectionMq::getUserCollectionId));
for (Map.Entry<Long, List<UserCollectionMq>> entry : collect.entrySet()) {
List<UserCollectionMq> value = entry.getValue();
UserCollectionMq userCollectionMq = value.get(0);
if (StringUtils.isBlank(userCollectionMq.getName())) {
userCollectionMq.setName("DEFAULT");
}
UserCollection userCollection = userCollectionRepository
.findFirstByUserIdAndTypeAndName(tvUserId, userCollectionMq.getType(), userCollectionMq.getName()).orElseGet(UserCollection::new);
userCollection.setAppId(userCollectionMq.getAppId())
.setUserId(tvUserId)
.setName(userCollectionMq.getName())
.setType(userCollectionMq.getType())
.setCount(userCollection.getCount() == null ? value.size() : userCollection.getCount() + value.size());
UserCollection userCollectionSave = userCollectionRepository.save(userCollection);
for (UserCollectionMq collectionMq : value) {
UserCollectionDetail userCollectionDetail = collectionMq2DetailMapper.toEntity(collectionMq);
Optional<UserCollectionDetail> userCollectionDetailOptional = userCollectionDetailRepository
.findByDetailIdAndDetailTypeAndUserCollectionId(userCollectionDetail.getDetailId(), userCollectionDetail.getDetailType(), userCollectionSave.getId());
//观影记录同一天只存一条记录
if (userCollectionDetailOptional.isPresent() &&
DateUtil.isSameDay(new Date(userCollectionDetailOptional.get().getCreateTime().getTime()), new Date())) {
userCollectionDetail.setId(userCollectionDetailOptional.get().getId());
} else {
userCollectionDetail.setId(null)
.setUserCollectionId(userCollectionSave.getId());
}
userCollectionDetailRepository.save(userCollectionDetail);
}
}*/
this.restTemplateClient.addCollection(content);
} catch (Exception e) {
log.error("CollectionAddConsumer || UserCollection add error || {}", e.toString(), e);
}
}
}