|
@@ -0,0 +1,285 @@
|
|
|
+package com.kmall.admin.utils;
|
|
|
+
|
|
|
+import com.aliyun.dysmsapi20170525.Client;
|
|
|
+import com.aliyun.dysmsapi20170525.models.*;
|
|
|
+import com.aliyun.teaopenapi.models.Config;
|
|
|
+import com.kmall.admin.properties.AliSMSProperties;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.commons.logging.Log;
|
|
|
+import org.apache.commons.logging.LogFactory;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 阿里云短信工具类
|
|
|
+ *
|
|
|
+ * @author zhuhh
|
|
|
+ * @date 2021-12-4 14:44:56
|
|
|
+ */
|
|
|
+public class SMSUtils {
|
|
|
+ private static Log logger = LogFactory.getLog(SMSUtils.class);
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 初始化账号Client
|
|
|
+ *
|
|
|
+ * @param aliSMSProperties
|
|
|
+ * @return client
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static Client createClient(AliSMSProperties aliSMSProperties) throws Exception {
|
|
|
+ Config config = new Config()
|
|
|
+ .setAccessKeyId(aliSMSProperties.getAccessKeyId())
|
|
|
+ .setAccessKeySecret(aliSMSProperties.getAccessKeySecret())
|
|
|
+ .setEndpoint(aliSMSProperties.getEndpoint());
|
|
|
+
|
|
|
+ return new Client(config);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送消息
|
|
|
+ *
|
|
|
+ * @param aliSMSProperties 短信参数,配置文件里配置
|
|
|
+ * @param params 待发送参数集合
|
|
|
+ * @return Map
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static Map<String, Object> sendSms(AliSMSProperties aliSMSProperties, Map<String, Object> params) throws Exception {
|
|
|
+ Client client = createClient(aliSMSProperties);
|
|
|
+
|
|
|
+ // 1.设置发送参数
|
|
|
+ SendSmsRequest request = new SendSmsRequest();
|
|
|
+ // 接收短信的手机号码(必填)。支持对多个手机号码发送短信,手机号码之间以半角逗号(,)分隔。上限为1000个手机号码
|
|
|
+ request.setPhoneNumbers(params.get("phoneNumbers").toString());
|
|
|
+ // 短信签名名称(必填)
|
|
|
+ if (params.get("signName") != null && StringUtils.isNotBlank(params.get("signName").toString())) {
|
|
|
+ request.setSignName(params.get("signName").toString());
|
|
|
+ } else { // 短信签名名称为空则读取配置文件默认短信签名名称
|
|
|
+ request.setSignName(aliSMSProperties.getSignName());
|
|
|
+ }
|
|
|
+ // 短信模板CODE(必填)
|
|
|
+ if (params.get("templateCode") != null && StringUtils.isNotBlank(params.get("templateCode").toString())) {
|
|
|
+ request.setTemplateCode(params.get("templateCode").toString());
|
|
|
+ } else { // 读取配置文件的清关短信模板CODE
|
|
|
+ request.setTemplateCode(aliSMSProperties.getCustomTemplateCode());
|
|
|
+ }
|
|
|
+ // 短信模板变量对应的实际值(非必填),JSON字符串,支持多个参数,示例:{"name":"张三","number":"15038****76"}
|
|
|
+ if (params.get("templateParam") != null && StringUtils.isNotBlank(params.get("templateParam").toString())) {
|
|
|
+ request.setTemplateParam(params.get("templateParam").toString());
|
|
|
+ }
|
|
|
+ // 外部流水扩展字段(非必填)
|
|
|
+ if (params.get("outId") != null && StringUtils.isNotBlank(params.get("outId").toString())) {
|
|
|
+ request.setOutId(params.get("outId").toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2.发送短信
|
|
|
+ SendSmsResponse resp = null;
|
|
|
+ Map<String, Object> result = new HashMap<>(5);
|
|
|
+ try {
|
|
|
+ resp = client.sendSms(request);
|
|
|
+ result.put("code", resp.body.getCode());
|
|
|
+ result.put("message", resp.body.getMessage());
|
|
|
+ // 发送回执ID
|
|
|
+ result.put("bizId", resp.body.getBizId());
|
|
|
+ // 请求ID
|
|
|
+ result.put("requestId", resp.body.getRequestId());
|
|
|
+ } catch (RuntimeException e) {
|
|
|
+ logger.error("发送短信失败,失败手机号码:" + params.get("phoneNumbers").toString());
|
|
|
+ logger.error("发送短信失败:" + resp.body.getMessage());
|
|
|
+ throw new RuntimeException("发送短信失败:", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量发送消息,支持在一次请求中分别向多个不同的手机号码发送不同签名的短信
|
|
|
+ *
|
|
|
+ * @param aliSMSProperties 短信参数,配置文件里配置
|
|
|
+ * @param params 待发送参数集合
|
|
|
+ * @return Map
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static Map<String, Object> SendBatchSms(AliSMSProperties aliSMSProperties, Map<String, Object> params) throws Exception {
|
|
|
+ Client client = createClient(aliSMSProperties);
|
|
|
+
|
|
|
+ // 1.设置发送参数
|
|
|
+ SendBatchSmsRequest request = new SendBatchSmsRequest();
|
|
|
+ // 接收短信的手机号码(必填),JSON数组格式的字符串
|
|
|
+ request.setPhoneNumberJson(params.get("phoneNumbersJson").toString());
|
|
|
+ // 短信签名名称(必填),JSON数组格式的字符串,短信签名的个数必须与手机号码的个数相同
|
|
|
+ request.setSignNameJson(params.get("signNameJson").toString());
|
|
|
+ // 短信模板CODE(必填)
|
|
|
+ request.setTemplateCode(params.get("templateCode").toString());
|
|
|
+ // 短信模板变量对应的实际值(非必填),JSON数组格式的字符串
|
|
|
+ if (params.get("templateParamJson") != null && StringUtils.isNotBlank(params.get("templateParamJson").toString())) {
|
|
|
+ request.setTemplateParamJson(params.get("templateParamJson").toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2.发送短信
|
|
|
+ SendBatchSmsResponse resp = null;
|
|
|
+ Map<String, Object> result = new HashMap<>(5);
|
|
|
+ try {
|
|
|
+ resp = client.sendBatchSms(request);
|
|
|
+
|
|
|
+ result.put("code", resp.body.getCode());
|
|
|
+ result.put("message", resp.body.getMessage());
|
|
|
+ // 发送回执ID
|
|
|
+ result.put("bizId", resp.body.getBizId());
|
|
|
+ // 请求ID
|
|
|
+ result.put("requestId", resp.body.getRequestId());
|
|
|
+ } catch (RuntimeException e) {
|
|
|
+ logger.error("批量发送短信失败,失败手机号码:" + params.get("phoneNumbers").toString());
|
|
|
+ logger.error("批量发送短信失败:" + resp.body.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询短信发送记录和发送状态
|
|
|
+ *
|
|
|
+ * @param aliSMSProperties 短信参数,配置文件里配置
|
|
|
+ * @param params 待发送参数集合
|
|
|
+ * @return Map
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static Map<String, Object> querySendDetails(AliSMSProperties aliSMSProperties, Map<String, Object> params) throws Exception {
|
|
|
+ Client client = createClient(aliSMSProperties);
|
|
|
+
|
|
|
+ // 1.设置发送参数
|
|
|
+ QuerySendDetailsRequest request = new QuerySendDetailsRequest();
|
|
|
+ // 接收短信的手机号码(必填)
|
|
|
+ request.setPhoneNumber(params.get("phoneNumbers").toString());
|
|
|
+ // 短信发送日期,格式为yyyyMMdd(必填)
|
|
|
+ request.setSendDate(params.get("sendDate").toString());
|
|
|
+ // 发送回执ID(非必填)
|
|
|
+ if (params.get("bizId") != null && StringUtils.isNotBlank(params.get("bizId").toString())) {
|
|
|
+ request.setBizId(params.get("bizId").toString());
|
|
|
+ }
|
|
|
+ // 指定每页显示的短信记录数量(必填)
|
|
|
+ request.setPageSize(Long.parseLong(params.get("pageSize").toString()));
|
|
|
+ // 指定发送记录的当前页码(必填)
|
|
|
+ request.setCurrentPage(Long.parseLong(params.get("currentPage").toString()));
|
|
|
+
|
|
|
+ // 发送请求
|
|
|
+ QuerySendDetailsResponse resp = null;
|
|
|
+ Map<String, Object> result = new HashMap<>(5);
|
|
|
+ try {
|
|
|
+ resp = client.querySendDetails(request);
|
|
|
+
|
|
|
+ result.put("code", resp.body.getCode());
|
|
|
+ result.put("message", resp.body.getMessage());
|
|
|
+ // 短信发送总条数
|
|
|
+ result.put("totalCount", resp.body.getTotalCount());
|
|
|
+ // 请求ID
|
|
|
+ result.put("requestId", resp.body.getRequestId());
|
|
|
+ // 短信发送明细
|
|
|
+ result.put("smsSendDetailDTOs", resp.body.getSmsSendDetailDTOs());
|
|
|
+ } catch (RuntimeException e) {
|
|
|
+ logger.error("查询短信发送记录和发送状态失败,失败手机号码:" + params.get("phoneNumbers").toString());
|
|
|
+ logger.error("查询短信发送记录和发送状态失败:" + resp.body.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 申请短信签名
|
|
|
+ *
|
|
|
+ * @param aliSMSProperties 短信参数,配置文件里配置
|
|
|
+ * @param params 待发送参数集合
|
|
|
+ * @return Map
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static Map<String, Object> addSMSSign(AliSMSProperties aliSMSProperties, Map<String, Object> params) throws Exception {
|
|
|
+ Client client = createClient(aliSMSProperties);
|
|
|
+
|
|
|
+ // 1.设置发送参数
|
|
|
+ AddSmsSignRequest request = new AddSmsSignRequest();
|
|
|
+ // 签名名称
|
|
|
+ request.setSignName(params.get("signName").toString());
|
|
|
+ // 签名来源
|
|
|
+ request.setSignSource(Integer.parseInt(params.get("signSource").toString()));
|
|
|
+ // 短信签名申请说明
|
|
|
+ request.setRemark(params.get("remark").toString());
|
|
|
+ // 签名文件列表
|
|
|
+ request.setSignFileList((List<AddSmsSignRequest.AddSmsSignRequestSignFileList>)params.get("signFileList"));
|
|
|
+
|
|
|
+ // 发送请求
|
|
|
+ AddSmsSignResponse resp = null;
|
|
|
+ Map<String, Object> result = new HashMap<>(5);
|
|
|
+ try {
|
|
|
+ resp = client.addSmsSign(request);
|
|
|
+ result.put("code", resp.body.getCode());
|
|
|
+ result.put("message", resp.body.getMessage());
|
|
|
+ // 请求ID
|
|
|
+ result.put("requestId", resp.body.getRequestId());
|
|
|
+ // 签名名称
|
|
|
+ result.put("signName", resp.body.getSignName());
|
|
|
+ } catch (RuntimeException e) {
|
|
|
+ logger.error("申请短信签名失败:" + resp.body.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 申请短信模板
|
|
|
+ *
|
|
|
+ * @param aliSMSProperties 短信参数,配置文件里配置
|
|
|
+ * @param params 待发送参数集合
|
|
|
+ * @return Map
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static Map<String, Object> AddSMSTemplate(AliSMSProperties aliSMSProperties, Map<String, Object> params) throws Exception {
|
|
|
+ Client client = createClient(aliSMSProperties);
|
|
|
+
|
|
|
+ // 1.设置发送参数
|
|
|
+ AddSmsTemplateRequest request = new AddSmsTemplateRequest();
|
|
|
+ // 短信类型
|
|
|
+ request.setTemplateType(Integer.parseInt(params.get("templateType").toString()));
|
|
|
+ // 模板名称
|
|
|
+ request.setTemplateName(params.get("templateName").toString());
|
|
|
+ // 模板内容
|
|
|
+ request.setTemplateContent(params.get("templateContent").toString());
|
|
|
+ // 短信模板申请说明
|
|
|
+ request.setRemark(params.get("remark").toString());
|
|
|
+
|
|
|
+ // 发送请求
|
|
|
+ AddSmsTemplateResponse resp = null;
|
|
|
+ Map<String, Object> result = new HashMap<>(5);
|
|
|
+ try {
|
|
|
+ resp = client.addSmsTemplate(request);
|
|
|
+ result.put("code", resp.body.getCode());
|
|
|
+ result.put("message", resp.body.getMessage());
|
|
|
+ // 请求ID
|
|
|
+ result.put("requestId", resp.body.getRequestId());
|
|
|
+ // 短信发送明细
|
|
|
+ result.put("templateCode", resp.body.getTemplateCode());
|
|
|
+ } catch (RuntimeException e) {
|
|
|
+ logger.error("申请短信模板失败:" + resp.body.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
+ // 本地测试
|
|
|
+ AliSMSProperties aliSMSProperties = new AliSMSProperties();
|
|
|
+ aliSMSProperties.setCustomTemplateCode("SMS_229645099");
|
|
|
+ aliSMSProperties.setEndpoint("dysmsapi.aliyuncs.com");
|
|
|
+ aliSMSProperties.setSignName("珠海免税MALL");
|
|
|
+ aliSMSProperties.setAccessKeyId("LTAI5tJRFwGrMcqt8RcKgkuy");
|
|
|
+ aliSMSProperties.setAccessKeySecret("ffflN7qdiU8z9yJOunwaH0kYNrwkLJ");
|
|
|
+
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("phoneNumbers", "13160675953");
|
|
|
+
|
|
|
+ // Map<String, Object> result = sendSms(aliSMSProperties, map);
|
|
|
+ // System.out.println(result.get("code"));
|
|
|
+ }
|
|
|
+
|
|
|
+}
|