提交 | 用户 | 时间
|
722af2
|
1 |
package com.dl.sms.core; |
X |
2 |
|
|
3 |
import cn.hutool.core.collection.CollUtil; |
|
4 |
import cn.hutool.core.util.ArrayUtil; |
|
5 |
import com.dl.common.utils.JsonUtils; |
|
6 |
import com.dl.common.utils.StringUtils; |
|
7 |
import com.dl.sms.config.properties.SmsProperties; |
|
8 |
import com.dl.sms.entity.SmsResult; |
|
9 |
import com.dl.sms.exception.SmsException; |
|
10 |
import com.tencentcloudapi.common.Credential; |
|
11 |
import com.tencentcloudapi.common.profile.ClientProfile; |
|
12 |
import com.tencentcloudapi.common.profile.HttpProfile; |
|
13 |
import com.tencentcloudapi.sms.v20190711.SmsClient; |
|
14 |
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest; |
|
15 |
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse; |
|
16 |
import com.tencentcloudapi.sms.v20190711.models.SendStatus; |
|
17 |
import lombok.SneakyThrows; |
|
18 |
|
|
19 |
import java.util.Arrays; |
|
20 |
import java.util.Map; |
|
21 |
import java.util.Set; |
|
22 |
import java.util.stream.Collectors; |
|
23 |
|
|
24 |
/** |
|
25 |
* Tencent 短信模板 |
|
26 |
* |
|
27 |
* @author Lion Li |
|
28 |
* @version 4.2.0 |
|
29 |
*/ |
|
30 |
public class TencentSmsTemplate implements SmsTemplate { |
|
31 |
|
|
32 |
private SmsProperties properties; |
|
33 |
|
|
34 |
private SmsClient client; |
|
35 |
|
|
36 |
@SneakyThrows(Exception.class) |
|
37 |
public TencentSmsTemplate(SmsProperties smsProperties) { |
|
38 |
this.properties = smsProperties; |
|
39 |
Credential credential = new Credential(smsProperties.getAccessKeyId(), smsProperties.getAccessKeySecret()); |
|
40 |
HttpProfile httpProfile = new HttpProfile(); |
|
41 |
httpProfile.setEndpoint(smsProperties.getEndpoint()); |
|
42 |
ClientProfile clientProfile = new ClientProfile(); |
|
43 |
clientProfile.setHttpProfile(httpProfile); |
|
44 |
this.client = new SmsClient(credential, "", clientProfile); |
|
45 |
} |
|
46 |
|
|
47 |
@Override |
|
48 |
public SmsResult send(String phones, String templateId, Map<String, String> param) { |
|
49 |
if (StringUtils.isBlank(phones)) { |
|
50 |
throw new SmsException("手机号不能为空"); |
|
51 |
} |
|
52 |
if (StringUtils.isBlank(templateId)) { |
|
53 |
throw new SmsException("模板ID不能为空"); |
|
54 |
} |
|
55 |
SendSmsRequest req = new SendSmsRequest(); |
|
56 |
Set<String> set = Arrays.stream(phones.split(StringUtils.SEPARATOR)).map(p -> "+86" + p).collect(Collectors.toSet()); |
|
57 |
req.setPhoneNumberSet(ArrayUtil.toArray(set, String.class)); |
|
58 |
if (CollUtil.isNotEmpty(param)) { |
|
59 |
req.setTemplateParamSet(ArrayUtil.toArray(param.values(), String.class)); |
|
60 |
} |
|
61 |
req.setTemplateID(templateId); |
|
62 |
req.setSign(properties.getSignName()); |
|
63 |
req.setSmsSdkAppid(properties.getSdkAppId()); |
|
64 |
try { |
|
65 |
SendSmsResponse resp = client.SendSms(req); |
|
66 |
SmsResult.SmsResultBuilder builder = SmsResult.builder() |
|
67 |
.isSuccess(true) |
|
68 |
.message("send success") |
|
69 |
.response(JsonUtils.toJsonString(resp)); |
|
70 |
for (SendStatus sendStatus : resp.getSendStatusSet()) { |
|
71 |
if (!"Ok".equals(sendStatus.getCode())) { |
|
72 |
builder.isSuccess(false).message(sendStatus.getMessage()); |
|
73 |
break; |
|
74 |
} |
|
75 |
} |
|
76 |
return builder.build(); |
|
77 |
} catch (Exception e) { |
|
78 |
throw new SmsException(e.getMessage()); |
|
79 |
} |
|
80 |
} |
|
81 |
|
|
82 |
} |