yangys
2024-04-05 84dea9976c29ac938fa018b8566c71461b056418
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
package com.qianwen.core.notify.provider.sms.aliyun;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.qianwen.core.log.exception.BizServiceException;
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.sms.SmsProvider;
import com.qianwen.core.notify.template.Template;
import com.qianwen.core.notify.template.TemplateManager;
import com.qianwen.core.tool.api.BizMessage;
import com.qianwen.core.tool.metadata.Values;
import com.qianwen.core.tool.utils.StringUtil;
 
/* loaded from: blade-starter-notify-9.3.0.0-SNAPSHOT.jar:org/springblade/core/notify/provider/sms/aliyun/AliyunSmsNotifier.class */
public class AliyunSmsNotifier extends AbstractNotifier<AliyunSmsTemplate> {
    private static final Logger log = LoggerFactory.getLogger(AliyunSmsNotifier.class);
    private final IAcsClient client;
    private final int connectTimeout = 1000;
    private final int readTimeout = 5000;
    private String domain;
    private String regionId;
    private NotifierProperties notifierProperties;
    private String notifierId;
 
 
    @Override // com.qianwen.core.notify.notifier.Notifier
    public NotifierProperties getNotifierProperties() {
        return this.notifierProperties;
    }
 
    @Override // com.qianwen.core.notify.notifier.Notifier
    public String getNotifierId() {
        return this.notifierId;
    }
 
    public AliyunSmsNotifier(NotifierProperties profile, TemplateManager templateManager) {
        super(templateManager);
        this.domain = "dysmsapi.aliyuncs.com";
        this.regionId = "cn-hangzhou";
        Map<String, Object> config = profile.getConfiguration();
        String str = (String) Objects.requireNonNull(config.get("regionId"), "regionId不能为空");
        this.regionId = str;
        DefaultProfile defaultProfile = DefaultProfile.getProfile(str, (String) Objects.requireNonNull(config.get("accessKeyId"), "accessKeyId不能为空"), (String) Objects.requireNonNull(config.get("secret"), "secret不能为空"));
        this.client = new DefaultAcsClient(defaultProfile);
        this.domain = (String) config.getOrDefault("domain", this.domain);
        this.notifierId = profile.getId();
        this.notifierProperties = profile;
    }
 
    public AliyunSmsNotifier(IClientProfile profile, TemplateManager templateManager) {
        this((IAcsClient) new DefaultAcsClient(profile), templateManager);
    }
 
    public AliyunSmsNotifier(IAcsClient client, TemplateManager templateManager) {
         super(templateManager);
         this.client = client;
    }
 
    @Override // com.qianwen.core.notify.notifier.Notifier
    public NotifyType getType() {
        return DefaultNotifyType.sms;
    }
 
    @Override // com.qianwen.core.notify.notifier.Notifier
    public Provider getProvider() {
        return SmsProvider.aliyunSms;
    }
 
    public void send(AliyunSmsTemplate template, String traceId, Values context, List<String> notifiedParty) {
        try {
            CommonRequest request = new CommonRequest();
            request.setSysMethod(MethodType.POST);
            request.setSysDomain(this.domain);
            request.setSysVersion("2021-04-22");
            request.setSysAction("SendSms");
            request.setSysConnectTimeout(1000);
            request.setSysReadTimeout(5000);
            request.putQueryParameter("RegionId", this.regionId);
            request.putQueryParameter("PhoneNumbers", StringUtil.join(notifiedParty));
            request.putQueryParameter("SignName", template.getSignName());
            request.putQueryParameter("TemplateCode", template.getCode());
            request.putQueryParameter("TemplateParam", template.createTtsParam(context.getAllValues()));
            CommonResponse response = this.client.getCommonResponse(request);
            log.info("发送短信通知完成 {}:{}", Integer.valueOf(response.getHttpResponse().getStatus()), response.getData());
            JSONObject json = JSON.parseObject(response.getData());
            if (!"ok".equalsIgnoreCase(json.getString("Code"))) {
                BizMessage bizMessage = new BizMessage();
                bizMessage.setMessage(json.getString("Code"));
                bizMessage.setDetail(json.getString("Message"));
                throw new BizServiceException(bizMessage);
            }
        }catch(Exception ex) {
            throw new RuntimeException(ex);
        }
    }
 
    @Override // com.qianwen.core.notify.notifier.Notifier
    public void close() {
        this.client.shutdown();
    }
}