yangys
2024-04-11 5cbb259cdcdfbfdb874b444a8e1791315eeebfdc
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
package com.qianwen.core.notify.provider.wechat.qy;
 
import com.alibaba.fastjson.JSONObject;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.qianwen.core.http.HttpRequest;
import com.qianwen.core.notify.DefaultNotifyType;
import com.qianwen.core.notify.NotifyType;
import com.qianwen.core.notify.Provider;
import com.qianwen.core.notify.notifier.AbstractNotifier;
import com.qianwen.core.notify.notifier.NotifierProperties;
import com.qianwen.core.notify.provider.wechat.qy.entity.BaseQyResult;
import com.qianwen.core.notify.provider.wechat.qy.entity.WeixinGetUidQuery;
import com.qianwen.core.notify.provider.wechat.qy.entity.WeixinGetUidResult;
import com.qianwen.core.notify.provider.wechat.qy.entity.WeixinQyTextQuery;
import com.qianwen.core.notify.provider.wechat.qy.entity.WeixinQyTokenResult;
import com.qianwen.core.notify.template.Template;
import com.qianwen.core.notify.template.TemplateManager;
import com.qianwen.core.tool.metadata.Values;
import com.qianwen.core.tool.utils.ExpressionUtils;
import com.qianwen.core.tool.utils.StringUtil;
 
/* loaded from: blade-starter-notify-9.3.0.0-SNAPSHOT.jar:org/springblade/core/notify/provider/wechat/qy/WeixinQyTextNotifier.class */
public class WeixinQyTextNotifier extends AbstractNotifier<DefaultWeixinQyTemplate> {
    private static final Logger log = LoggerFactory.getLogger(WeixinQyTextNotifier.class);
    private final String notifierId;
    private final WeixinQyProperties properties;
    protected static final String ACCESS_TOKEN_URL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
    protected static final String MESSAGE_SEND_URL = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s";
    protected static final String PHONE_TO_USER_ID_URL = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserid?access_token=%s";
    private WeixinQyTokenResult tokenResponse;
    private NotifierProperties notifierProperties;
 
 
    @Override // com.qianwen.core.notify.notifier.Notifier
    public String getNotifierId() {
        return this.notifierId;
    }
 
    public WeixinQyTextNotifier(NotifierProperties properties, TemplateManager templateManager) {
        super(templateManager);
        WeixinQyProperties weixinQyProperties = (WeixinQyProperties) new JSONObject(properties.getConfiguration()).toJavaObject(WeixinQyProperties.class);
        this.properties = weixinQyProperties;
        this.notifierId = properties.getId();
        this.notifierProperties = properties;
    }
 
    @Override // com.qianwen.core.notify.notifier.Notifier
    public NotifyType getType() {
        return DefaultNotifyType.weiXinQY;
    }
 
    @Override // com.qianwen.core.notify.notifier.Notifier
    public Provider getProvider() {
        return WeixinQyProvider.qyTextMessage;
    }
 
    @Override // com.qianwen.core.notify.notifier.Notifier
    public NotifierProperties getNotifierProperties() {
        return this.notifierProperties;
    }
 
    public void send(DefaultWeixinQyTemplate template, String traceId, Values context, List<String> notifiedParty) {
        List<String> userIds = getUserIdByPhone((String[]) notifiedParty.toArray(new String[0]));
        WeixinQyTextQuery textQuery = new WeixinQyTextQuery().setTouser(userIds).setAgentid(this.properties.getAgentid()).setContent(ExpressionUtils.analytical(template.getContent(), context.getAllValues(), "spel"));
        sendText(textQuery);
    }
 
    public List<String> getUserIdByPhone(String... phones) {
        
        String url = String.format(PHONE_TO_USER_ID_URL, getToken());
        return (List<String>) Stream.<String>of(phones).map(phone -> {
            WeixinGetUidResult result = (WeixinGetUidResult) HttpRequest.post(url).bodyJson(new WeixinGetUidQuery(phone)).execute().asValue(WeixinGetUidResult.class);
            result.errorException();
            return result.getUserid();
        }).collect(Collectors.toList());
    }
 
    private void sendText(WeixinQyTextQuery weixinQyTextQuery) {
        String url = String.format(MESSAGE_SEND_URL, getToken());
        BaseQyResult result = (BaseQyResult) HttpRequest.post(url).bodyJson(weixinQyTextQuery).execute().asValue(BaseQyResult.class);
        result.errorException();
    }
 
    private String getToken() {
        if (this.tokenResponse != null && !this.tokenResponse.isNotExpires()) {
            return this.tokenResponse.getAccess_token();
        }
        WeixinQyTokenResult result = (WeixinQyTokenResult) HttpRequest.post(ACCESS_TOKEN_URL).bodyJson(this.properties).execute().onFailed((k, v) -> {
            log.info(StringUtil.format("发送企业微信文本消息{}失败,原因{}", new Object[]{k.url(), v.getMessage()}));
        }).asValue(WeixinQyTokenResult.class);
        result.errorException();
        this.tokenResponse = result;
        return result.getAccess_token();
    }
 
    @Override // com.qianwen.core.notify.notifier.Notifier
    public void close() {
        this.tokenResponse = null;
    }
}