|
|
@@ -1,17 +1,25 @@
|
|
|
package cn.tr.plugin.sms.config.ali;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
+import cn.hutool.core.exceptions.ExceptionUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.tr.core.exception.TRExcCode;
|
|
|
+import cn.tr.core.pojo.KeyValue;
|
|
|
import cn.tr.core.utils.JsonUtils;
|
|
|
import cn.tr.plugin.sms.bo.SmsCommonResult;
|
|
|
+import cn.tr.plugin.sms.bo.SmsReceiveRespBO;
|
|
|
import cn.tr.plugin.sms.bo.SmsSendRespBO;
|
|
|
+import cn.tr.plugin.sms.bo.SmsTemplateRespBO;
|
|
|
import cn.tr.plugin.sms.config.AbstractSmsClient;
|
|
|
-import com.aliyuncs.DefaultAcsClient;
|
|
|
-import com.aliyuncs.IAcsClient;
|
|
|
-import com.aliyuncs.exceptions.ClientException;
|
|
|
-import com.aliyuncs.profile.DefaultProfile;
|
|
|
-import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
|
|
|
-import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
|
|
|
+import cn.tr.plugin.sms.properties.SmsChannelProperties;
|
|
|
+import com.aliyun.dysmsapi20170525.Client;
|
|
|
+import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
|
|
|
+import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
|
|
|
+import com.fasterxml.jackson.annotation.JsonProperty;
|
|
|
+import lombok.Data;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @ClassName : AliSmsClient
|
|
|
@@ -20,44 +28,136 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
* @Date: 2023年03月15日
|
|
|
*/
|
|
|
@Slf4j
|
|
|
-public class AliSmsClient extends AbstractSmsClient<AliSmsClientConfig,AliSmsClientSendConfig> {
|
|
|
- private IAcsClient client;
|
|
|
- private final AliyunSmsCodeMapping smsCodeMapping;
|
|
|
- public AliSmsClient(String id, AliSmsClientConfig clientConfig) {
|
|
|
+public class AliSmsClient extends AbstractSmsClient<AliSmsTempConfig> {
|
|
|
+ private Client client;
|
|
|
+
|
|
|
+ public AliSmsClient(String id, SmsChannelProperties clientConfig) {
|
|
|
super(id, clientConfig);
|
|
|
- this.smsCodeMapping=new AliyunSmsCodeMapping();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void doInit(AliSmsClientConfig config) {
|
|
|
- DefaultProfile profile = DefaultProfile.getProfile(config.getRegionId(), config.getAccessKeyId(), config.getAccessKeySecret());
|
|
|
- client = new DefaultAcsClient(profile);
|
|
|
+ public void doInit(SmsChannelProperties config) throws Exception {
|
|
|
+ client = createClient(config.getApiKey(), config.getApiSecret());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public SmsCommonResult<SmsSendRespBO> sendSms(AliSmsClientSendConfig sendConfig) {
|
|
|
- SendSmsRequest sendSmsRequest =new SendSmsRequest();
|
|
|
- sendSmsRequest.setPhoneNumbers(sendConfig.getPhoneNumber());
|
|
|
- sendSmsRequest.setSignName(sendConfig.getSignName());
|
|
|
- sendSmsRequest.setTemplateCode(sendConfig.getTemplateCode());
|
|
|
- sendSmsRequest.setTemplateParam(JsonUtils.toJsonString(sendConfig.getTemplateParam()));
|
|
|
- sendSmsRequest.setSmsUpExtendCode(sendConfig.getSmsUpExtendCode());
|
|
|
- sendSmsRequest.setOutId(sendConfig.getOutId());
|
|
|
- SendSmsResponse response=null;
|
|
|
+ protected SmsCommonResult<SmsSendRespBO> doSendSms(String sendLogId, String mobile, String apiTemplateId, List<KeyValue<String, Object>> templateParams) throws Throwable {
|
|
|
+ HashMap<String, Object> templateParamMap= new HashMap<>();
|
|
|
+ if (CollectionUtil.isNotEmpty(templateParams)) {
|
|
|
+ for (KeyValue<String, Object> templateParam : templateParams) {
|
|
|
+ templateParamMap.put(templateParam.getKey(),templateParam.getValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ SendSmsRequest request = new SendSmsRequest();
|
|
|
+ request.setSignName(getConfig().getSignature());
|
|
|
+ request.setTemplateParam(JsonUtils.toJsonString(templateParamMap));
|
|
|
+ request.setTemplateCode(apiTemplateId);
|
|
|
+ request.setPhoneNumbers(mobile);
|
|
|
+ request.setOutId(sendLogId);
|
|
|
+ SendSmsResponse response;
|
|
|
try {
|
|
|
- response = client.getAcsResponse(sendSmsRequest);
|
|
|
- } catch (ClientException e) {
|
|
|
- return SmsCommonResult.build(e.getErrCode(), formatResultMsg(e), e.getRequestId(), null, smsCodeMapping);
|
|
|
+ response = client.sendSms(request);
|
|
|
+ if(log.isDebugEnabled()){
|
|
|
+ log.debug("[AliSmsClient] logId:{} ,mobile:{},responseCode:{},body:{}",sendLogId,mobile,response.statusCode,StrUtil.toString(response.body));
|
|
|
+ }
|
|
|
+ SmsSendRespBO sendResp = new SmsSendRespBO().setSerialNo(response.getBody().bizId);
|
|
|
+ if(StrUtil.equals(response.body.code,"OK")){
|
|
|
+ return SmsCommonResult.build(TRExcCode.SUCCESS.getErrCode(),response.getBody().code, response.getBody().getMessage(), response.getBody().getRequestId(), sendResp);
|
|
|
+ }
|
|
|
+ return SmsCommonResult.build(TRExcCode.SERVICE_ERROR_C0001.getErrCode(),response.getBody().code, response.getBody().getMessage(), response.getBody().getRequestId(), sendResp);
|
|
|
+ }catch (Exception ex){
|
|
|
+ return SmsCommonResult.build(TRExcCode.SERVICE_ERROR_C0001.getErrCode(),null, ExceptionUtil.getMessage(ex), null, null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected List<SmsReceiveRespBO> doParseSmsReceiveStatus(String text) throws Throwable {
|
|
|
+ List<SmsReceiveStatus> statuses = JsonUtils.parseArray(text, SmsReceiveStatus.class);
|
|
|
+ if(CollectionUtil.isEmpty(statuses)){
|
|
|
+ return Collections.emptyList();
|
|
|
}
|
|
|
+ return statuses.stream().map(status -> {
|
|
|
+ SmsReceiveRespBO resp = new SmsReceiveRespBO();
|
|
|
+ resp.setSuccess(status.getSuccess());
|
|
|
+ resp.setErrorCode(status.getErrCode()).setErrorMsg(status.getErrMsg());
|
|
|
+ resp.setMobile(status.getPhoneNumber()).setReceiveTime(status.getReportTime());
|
|
|
+ resp.setSerialNo(status.getBizId()).setLogId(status.getOutId());
|
|
|
+ return resp;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ }
|
|
|
|
|
|
- return SmsCommonResult.build(response.getCode(), response.getMessage(), response.getRequestId(), new SmsSendRespBO().setSerialNo(response.getBizId()), smsCodeMapping);
|
|
|
+ @Override
|
|
|
+ protected SmsCommonResult<SmsTemplateRespBO> doGetSmsTemplate(String apiTemplateId) throws Throwable {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
|
|
|
+ private Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
|
|
|
+ com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
|
|
|
+ .setAccessKeyId(accessKeyId)
|
|
|
+ .setAccessKeySecret(accessKeySecret);
|
|
|
+ // 访问的域名
|
|
|
+ config.endpoint = "dysmsapi.aliyuncs.com";
|
|
|
+ return new Client(config);
|
|
|
}
|
|
|
|
|
|
- private static String formatResultMsg(ClientException ex) {
|
|
|
- if (StrUtil.isEmpty(ex.getErrorDescription())) {
|
|
|
- return ex.getErrMsg();
|
|
|
- }
|
|
|
- return ex.getErrMsg() + " => " + ex.getErrorDescription();
|
|
|
+ /**
|
|
|
+ * 短信接收状态
|
|
|
+ *
|
|
|
+ * 参见 https://help.aliyun.com/document_detail/101867.html 文档
|
|
|
+ *
|
|
|
+ * @author 芋道源码
|
|
|
+ */
|
|
|
+ @Data
|
|
|
+ public static class SmsReceiveStatus {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 手机号
|
|
|
+ */
|
|
|
+ @JsonProperty("phone_number")
|
|
|
+ private String phoneNumber;
|
|
|
+ /**
|
|
|
+ * 发送时间
|
|
|
+ */
|
|
|
+ @JsonProperty("send_time")
|
|
|
+ private Date sendTime;
|
|
|
+ /**
|
|
|
+ * 状态报告时间
|
|
|
+ */
|
|
|
+ @JsonProperty("report_time")
|
|
|
+ private Date reportTime;
|
|
|
+ /**
|
|
|
+ * 是否接收成功
|
|
|
+ */
|
|
|
+ private Boolean success;
|
|
|
+ /**
|
|
|
+ * 状态报告说明
|
|
|
+ */
|
|
|
+ @JsonProperty("err_msg")
|
|
|
+ private String errMsg;
|
|
|
+ /**
|
|
|
+ * 状态报告编码
|
|
|
+ */
|
|
|
+ @JsonProperty("err_code")
|
|
|
+ private String errCode;
|
|
|
+ /**
|
|
|
+ * 发送序列号
|
|
|
+ */
|
|
|
+ @JsonProperty("biz_id")
|
|
|
+ private String bizId;
|
|
|
+ /**
|
|
|
+ * 用户序列号
|
|
|
+ *
|
|
|
+ * 这里我们传递的是 SysSmsLogDO 的日志编号
|
|
|
+ */
|
|
|
+ @JsonProperty("out_id")
|
|
|
+ private String outId;
|
|
|
+ /**
|
|
|
+ * 短信长度,例如说 1、2、3
|
|
|
+ *
|
|
|
+ * 140 字节算一条短信,短信长度超过 140 字节时会拆分成多条短信发送
|
|
|
+ */
|
|
|
+ @JsonProperty("sms_size")
|
|
|
+ private Integer smsSize;
|
|
|
+
|
|
|
}
|
|
|
}
|