提交 | 用户 | 时间
|
97edd7
|
1 |
package com.iailab.module.shasteel.framework.sms.core.client.impl; |
H |
2 |
|
|
3 |
import cn.hutool.core.lang.Assert; |
|
4 |
import cn.hutool.core.util.StrUtil; |
|
5 |
import com.fasterxml.jackson.annotation.JsonFormat; |
|
6 |
import com.fasterxml.jackson.annotation.JsonProperty; |
|
7 |
import com.google.common.annotations.VisibleForTesting; |
|
8 |
import com.iailab.framework.common.core.KeyValue; |
|
9 |
import com.iailab.framework.common.util.collection.ArrayUtils; |
|
10 |
import com.iailab.framework.common.util.json.JsonUtils; |
|
11 |
import com.iailab.module.shasteel.framework.sms.core.client.dto.SmsReceiveRespDTO; |
|
12 |
import com.iailab.module.shasteel.framework.sms.core.client.dto.SmsSendRespDTO; |
|
13 |
import com.iailab.module.shasteel.framework.sms.core.client.dto.SmsTemplateRespDTO; |
|
14 |
import com.iailab.module.shasteel.framework.sms.core.enums.SmsTemplateAuditStatusEnum; |
|
15 |
import com.iailab.module.shasteel.framework.sms.core.property.SmsChannelProperties; |
|
16 |
import com.tencentcloudapi.common.Credential; |
|
17 |
import com.tencentcloudapi.sms.v20210111.SmsClient; |
|
18 |
import com.tencentcloudapi.sms.v20210111.models.*; |
|
19 |
import lombok.Data; |
|
20 |
|
|
21 |
import java.time.LocalDateTime; |
|
22 |
import java.util.List; |
|
23 |
import java.util.Objects; |
|
24 |
|
|
25 |
import static com.iailab.framework.common.util.collection.CollectionUtils.convertList; |
|
26 |
import static com.iailab.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND; |
|
27 |
import static com.iailab.framework.common.util.date.DateUtils.TIME_ZONE_DEFAULT; |
|
28 |
|
|
29 |
/** |
|
30 |
* 腾讯云短信功能实现 |
|
31 |
* |
|
32 |
* 参见 <a href="https://cloud.tencent.com/document/product/382/52077">文档</a> |
|
33 |
* |
|
34 |
* @author shiwp |
|
35 |
*/ |
|
36 |
public class TencentSmsClient extends AbstractSmsClient { |
|
37 |
|
|
38 |
/** |
|
39 |
* 调用成功 code |
|
40 |
*/ |
|
41 |
public static final String API_CODE_SUCCESS = "Ok"; |
|
42 |
|
|
43 |
/** |
|
44 |
* REGION,使用南京 |
|
45 |
*/ |
|
46 |
private static final String ENDPOINT = "ap-nanjing"; |
|
47 |
|
|
48 |
/** |
|
49 |
* 是否国际/港澳台短信: |
|
50 |
* |
|
51 |
* 0:表示国内短信。 |
|
52 |
* 1:表示国际/港澳台短信。 |
|
53 |
*/ |
|
54 |
private static final long INTERNATIONAL_CHINA = 0L; |
|
55 |
|
|
56 |
private SmsClient client; |
|
57 |
|
|
58 |
public TencentSmsClient(SmsChannelProperties properties) { |
|
59 |
super(properties); |
|
60 |
Assert.notEmpty(properties.getApiSecret(), "apiSecret 不能为空"); |
|
61 |
validateSdkAppId(properties); |
|
62 |
} |
|
63 |
|
|
64 |
@Override |
|
65 |
protected void doInit() { |
|
66 |
// 实例化一个认证对象,入参需要传入腾讯云账户密钥对 secretId,secretKey |
|
67 |
Credential credential = new Credential(getApiKey(), properties.getApiSecret()); |
|
68 |
client = new SmsClient(credential, ENDPOINT); |
|
69 |
} |
|
70 |
|
|
71 |
/** |
|
72 |
* 参数校验腾讯云的 SDK AppId |
|
73 |
* |
|
74 |
* 原因是:腾讯云发放短信的时候,需要额外的参数 sdkAppId |
|
75 |
* |
|
76 |
* 解决方案:考虑到不破坏原有的 apiKey + apiSecret 的结构,所以将 secretId 拼接到 apiKey 字段中,格式为 "secretId sdkAppId"。 |
|
77 |
* |
|
78 |
* @param properties 配置 |
|
79 |
*/ |
|
80 |
private static void validateSdkAppId(SmsChannelProperties properties) { |
|
81 |
String combineKey = properties.getApiKey(); |
|
82 |
Assert.notEmpty(combineKey, "apiKey 不能为空"); |
|
83 |
String[] keys = combineKey.trim().split(" "); |
|
84 |
Assert.isTrue(keys.length == 2, "腾讯云短信 apiKey 配置格式错误,请配置 为[secretId sdkAppId]"); |
|
85 |
} |
|
86 |
|
|
87 |
private String getSdkAppId() { |
|
88 |
return StrUtil.subAfter(properties.getApiKey(), " ", true); |
|
89 |
} |
|
90 |
|
|
91 |
private String getApiKey() { |
|
92 |
return StrUtil.subBefore(properties.getApiKey(), " ", true); |
|
93 |
} |
|
94 |
|
|
95 |
@Override |
|
96 |
public SmsSendRespDTO sendSms(Long sendLogId, String mobile, |
|
97 |
String apiTemplateId, List<KeyValue<String, Object>> templateParams) throws Throwable { |
|
98 |
// 构建请求 |
|
99 |
SendSmsRequest request = new SendSmsRequest(); |
|
100 |
request.setSmsSdkAppId(getSdkAppId()); |
|
101 |
request.setPhoneNumberSet(new String[]{mobile}); |
|
102 |
request.setSignName(properties.getSignature()); |
|
103 |
request.setTemplateId(apiTemplateId); |
|
104 |
request.setTemplateParamSet(ArrayUtils.toArray(templateParams, e -> String.valueOf(e.getValue()))); |
|
105 |
request.setSessionContext(JsonUtils.toJsonString(new SessionContext().setLogId(sendLogId))); |
|
106 |
// 执行请求 |
|
107 |
SendSmsResponse response = client.SendSms(request); |
|
108 |
SendStatus status = response.getSendStatusSet()[0]; |
|
109 |
return new SmsSendRespDTO().setSuccess(Objects.equals(status.getCode(), API_CODE_SUCCESS)).setSerialNo(status.getSerialNo()) |
|
110 |
.setApiRequestId(response.getRequestId()).setApiCode(status.getCode()).setApiMsg(status.getMessage()); |
|
111 |
} |
|
112 |
|
|
113 |
@Override |
|
114 |
public List<SmsReceiveRespDTO> parseSmsReceiveStatus(String text) { |
|
115 |
List<SmsReceiveStatus> callback = JsonUtils.parseArray(text, SmsReceiveStatus.class); |
|
116 |
return convertList(callback, status -> new SmsReceiveRespDTO() |
|
117 |
.setSuccess(SmsReceiveStatus.SUCCESS_CODE.equalsIgnoreCase(status.getStatus())) |
|
118 |
.setErrorCode(status.getErrCode()).setErrorMsg(status.getDescription()) |
|
119 |
.setMobile(status.getMobile()).setReceiveTime(status.getReceiveTime()) |
|
120 |
.setSerialNo(status.getSerialNo()).setLogId(status.getSessionContext().getLogId())); |
|
121 |
} |
|
122 |
|
|
123 |
@Override |
|
124 |
public SmsTemplateRespDTO getSmsTemplate(String apiTemplateId) throws Throwable { |
|
125 |
// 构建请求 |
|
126 |
DescribeSmsTemplateListRequest request = new DescribeSmsTemplateListRequest(); |
|
127 |
request.setTemplateIdSet(new Long[]{Long.parseLong(apiTemplateId)}); |
|
128 |
request.setInternational(INTERNATIONAL_CHINA); |
|
129 |
// 执行请求 |
|
130 |
DescribeSmsTemplateListResponse response = client.DescribeSmsTemplateList(request); |
|
131 |
DescribeTemplateListStatus status = response.getDescribeTemplateStatusSet()[0]; |
|
132 |
if (status == null || status.getStatusCode() == null) { |
|
133 |
return null; |
|
134 |
} |
|
135 |
return new SmsTemplateRespDTO().setId(status.getTemplateId().toString()).setContent(status.getTemplateContent()) |
|
136 |
.setAuditStatus(convertSmsTemplateAuditStatus(status.getStatusCode().intValue())).setAuditReason(status.getReviewReply()); |
|
137 |
} |
|
138 |
|
|
139 |
@VisibleForTesting |
|
140 |
Integer convertSmsTemplateAuditStatus(int templateStatus) { |
|
141 |
switch (templateStatus) { |
|
142 |
case 1: return SmsTemplateAuditStatusEnum.CHECKING.getStatus(); |
|
143 |
case 0: return SmsTemplateAuditStatusEnum.SUCCESS.getStatus(); |
|
144 |
case -1: return SmsTemplateAuditStatusEnum.FAIL.getStatus(); |
|
145 |
default: throw new IllegalArgumentException(String.format("未知审核状态(%d)", templateStatus)); |
|
146 |
} |
|
147 |
} |
|
148 |
|
|
149 |
@Data |
|
150 |
private static class SmsReceiveStatus { |
|
151 |
|
|
152 |
/** |
|
153 |
* 短信接受成功 code |
|
154 |
*/ |
|
155 |
public static final String SUCCESS_CODE = "SUCCESS"; |
|
156 |
|
|
157 |
/** |
|
158 |
* 用户实际接收到短信的时间 |
|
159 |
*/ |
|
160 |
@JsonProperty("user_receive_time") |
|
161 |
@JsonFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND, timezone = TIME_ZONE_DEFAULT) |
|
162 |
private LocalDateTime receiveTime; |
|
163 |
|
|
164 |
/** |
|
165 |
* 国家(或地区)码 |
|
166 |
*/ |
|
167 |
@JsonProperty("nationcode") |
|
168 |
private String nationCode; |
|
169 |
|
|
170 |
/** |
|
171 |
* 手机号码 |
|
172 |
*/ |
|
173 |
private String mobile; |
|
174 |
|
|
175 |
/** |
|
176 |
* 实际是否收到短信接收状态,SUCCESS(成功)、FAIL(失败) |
|
177 |
*/ |
|
178 |
@JsonProperty("report_status") |
|
179 |
private String status; |
|
180 |
|
|
181 |
/** |
|
182 |
* 用户接收短信状态码错误信息 |
|
183 |
*/ |
|
184 |
@JsonProperty("errmsg") |
|
185 |
private String errCode; |
|
186 |
|
|
187 |
/** |
|
188 |
* 用户接收短信状态描述 |
|
189 |
*/ |
|
190 |
@JsonProperty("description") |
|
191 |
private String description; |
|
192 |
|
|
193 |
/** |
|
194 |
* 本次发送标识 ID(与发送接口返回的SerialNo对应) |
|
195 |
*/ |
|
196 |
@JsonProperty("sid") |
|
197 |
private String serialNo; |
|
198 |
|
|
199 |
/** |
|
200 |
* 用户的 session 内容(与发送接口的请求参数 SessionContext 一致) |
|
201 |
*/ |
|
202 |
@JsonProperty("ext") |
|
203 |
private SessionContext sessionContext; |
|
204 |
|
|
205 |
} |
|
206 |
|
|
207 |
@VisibleForTesting |
|
208 |
@Data |
|
209 |
static class SessionContext { |
|
210 |
|
|
211 |
/** |
|
212 |
* 发送短信记录id |
|
213 |
*/ |
|
214 |
private Long logId; |
|
215 |
|
|
216 |
} |
|
217 |
|
|
218 |
} |