新增短信

This commit is contained in:
zhangchenbiao
2024-04-10 16:05:10 +08:00
parent 0a1bf9773d
commit e59da5aa86
8 changed files with 82 additions and 27 deletions

View File

@@ -113,6 +113,10 @@
<groupId>com.github.wechatpay-apiv3</groupId>
<artifactId>wechatpay-java</artifactId>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibabacloud-dysmsapi20170525</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -1,22 +1,19 @@
package com.cool.store.service.impl;
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.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.models.*;
import com.aliyun.sdk.service.dysmsapi20170525.*;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.cool.store.dao.EnterpriseUserDAO;
import com.cool.store.dto.message.SendMessageDTO;
import com.cool.store.enums.*;
import com.cool.store.exception.ServiceException;
import com.cool.store.mq.producer.SimpleMessageService;
import com.cool.store.response.ResponseResult;
import com.cool.store.utils.UUIDUtils;
import com.cool.store.utils.poi.constant.Constants;
import com.google.gson.Gson;
import darabonba.core.client.ClientOverrideConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
@@ -32,6 +29,9 @@ import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
/**
@@ -56,6 +56,12 @@ public class CommonService {
private String enterpriseId;
@Value("${enterprise.dingCorpId}")
private String dingCorpId;
@Value("${aliyun.sms.accessKeyId}")
private String smsAccessKeyId;
@Value("${aliyun.sms.accessKeySecret}")
private String smsAccessKeySecret;
@Value("${aliyun.sms.signName}")
private String signName;
public LineFlowService getLineFlowService(Integer workflowSubStage){
WorkflowSubStageEnum workflowSubStageEnum = WorkflowSubStageEnum.getWorkflowSubStageEnum(workflowSubStage);
@@ -95,4 +101,43 @@ public class CommonService {
}
}
public void sendSms(String poneNumber, SMSMsgEnum templateCode, JSONObject templateParam){
sendSms(Arrays.asList(poneNumber), templateCode, templateParam);
}
public void sendSms(List<String> poneNumbers, SMSMsgEnum templateCode, JSONObject templateParam){
AsyncClient client = null;
try {
StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
.accessKeyId(smsAccessKeyId)
.accessKeySecret(smsAccessKeySecret)
.build());
client = AsyncClient.builder()
.region("cn-hangzhou")
.credentialsProvider(provider)
.overrideConfiguration(
ClientOverrideConfiguration.create()
.setEndpointOverride("dysmsapi.aliyuncs.com")
)
.build();
SendBatchSmsRequest sendBatchSmsRequest = SendBatchSmsRequest.builder()
.phoneNumberJson(JSONObject.toJSONString(poneNumbers))
.signNameJson(JSONObject.toJSONString(Arrays.asList(signName)))
.templateCode(templateCode.getTemplateCode())
.templateParamJson(templateParam.toJSONString())
.build();
CompletableFuture<SendBatchSmsResponse> response = client.sendBatchSms(sendBatchSmsRequest);
SendBatchSmsResponse resp = response.get();
log.info("短信发送response:{}", JSONObject.toJSONString(resp));
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} finally {
if(Objects.isNull(client)){
client.close();
}
}
}
}