WeCallBackEventService.java
2.5 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
package com.linkwechat.wecom.service.callback;
import cn.hutool.core.util.XmlUtil;
import com.alibaba.fastjson.JSONObject;
import com.linkwechat.wecom.domain.callback.WeBackBaseVo;
import com.linkwechat.wecom.domain.callback.WeCallBackEvent;
import com.linkwechat.wecom.factory.WeCallBackEventFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author danmo
* @description 回调通知处理
* @date 2021/11/21 19:57
**/
@Service
@Slf4j
public class WeCallBackEventService implements ApplicationListener<WeCallBackEvent> {
@Autowired
private Map<String, WeCallBackEventFactory> weCallBackEventFactoryMap = new ConcurrentHashMap<>();
/**
* 企微回调事件类型路由
*/
private final Map<String, String> eventRoute = new HashMap<String, String>() {
{
//成员事件
put("change_contact", "weEventChangeContactImpl");
//异步任务完成通知
put("batch_job_result", "weEventBatchJobResultImpl");
//外部联系人事件
put("change_external_contact", "weEventChangeExternalContactImpl");
//客户群事件
put("change_external_chat", "weEventChangeExternalChatImpl");
//客户标签事件
put("change_external_tag", "weEventChangeExternalTagImpl");
}
};
@Override
@Async
public void onApplicationEvent(WeCallBackEvent weCallBackEvent) {
String message = weCallBackEvent.getMessage();
WeBackBaseVo weBackBaseVo = XmlUtil.xmlToBean(XmlUtil.parseXml(message).getFirstChild(), WeBackBaseVo.class);
log.info("回调通知处理:weBackBaseVo:{}", JSONObject.toJSONString(weBackBaseVo));
WeCallBackEventFactory factory = factory(weBackBaseVo.getEvent());
if(factory != null){
factory.eventHandle(message);
}
}
private WeCallBackEventFactory factory(String eventType) {
if (!eventRoute.containsKey(eventType)) {
return null;
}
String resolveClass = eventRoute.get(eventType);
if (!weCallBackEventFactoryMap.containsKey(resolveClass)) {
return null;
}
return weCallBackEventFactoryMap.get(resolveClass);
}
}