|
|
@@ -1,9 +1,11 @@
|
|
|
package com.aijiuyi.admin.common.util;
|
|
|
|
|
|
+import com.aijiuyi.admin.common.config.SmsProperties;
|
|
|
import com.aliyun.dysmsapi20170525.Client;
|
|
|
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
|
|
|
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
|
|
|
import com.aliyun.teaopenapi.models.Config;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
@@ -13,44 +15,41 @@ import java.util.Map;
|
|
|
* 阿里云短信发送工具类
|
|
|
* 封装 dysmsapi20170525 SDK,支持传入模板码和模板参数发送短信
|
|
|
*
|
|
|
+ * 配置项从 application.yml 中 aliyun.sms 前缀读取,不再硬编码密钥
|
|
|
+ *
|
|
|
* 使用示例:
|
|
|
- * // 发送验证码
|
|
|
- * Map<String, String> params = new HashMap<>();
|
|
|
- * params.put("code", "123456");
|
|
|
- * SmsUtil.send("13800138000", "SMS_xxxxxxx", params);
|
|
|
+ * // 发送验证码
|
|
|
+ * Map<String, String> params = new HashMap<>();
|
|
|
+ * params.put("code", "123456");
|
|
|
+ * SmsUtil.send("13800138000", "SMS_xxxxxxx", params);
|
|
|
*
|
|
|
- * // 发送无参数模板
|
|
|
- * SmsUtil.send("13800138000", "SMS_xxxxxxx", null);
|
|
|
+ * // 发送无参数模板
|
|
|
+ * SmsUtil.send("13800138000", "SMS_xxxxxxx", null);
|
|
|
*/
|
|
|
@Component
|
|
|
public class SmsUtil {
|
|
|
|
|
|
- /** 阿里云 AccessKey ID */
|
|
|
- private static final String ACCESS_KEY_ID = "LTAI5tBpAmYikD1UVTFro9pR";
|
|
|
-
|
|
|
- /** 阿里云 AccessKey Secret */
|
|
|
- private static final String ACCESS_KEY_SECRET = "HMS1VWHzayRDeBtbAt4qSJzPeZ6AkD";
|
|
|
-
|
|
|
- /** 短信服务 Endpoint */
|
|
|
- private static final String ENDPOINT = "dysmsapi.aliyuncs.com";
|
|
|
-
|
|
|
- /** 短信签名(可根据实际情况修改) */
|
|
|
- private static final String SIGN_NAME = "深圳研年健康科技";
|
|
|
+ @Autowired
|
|
|
+ private SmsProperties smsProperties;
|
|
|
|
|
|
/** 短信客户端静态实例(初始化后复用) */
|
|
|
private static Client smsClient;
|
|
|
|
|
|
+ /** 静态引用配置属性(初始化后供静态方法使用) */
|
|
|
+ private static SmsProperties staticSmsProperties;
|
|
|
+
|
|
|
/**
|
|
|
* 应用启动后初始化短信客户端
|
|
|
* 避免每次发送时重复创建连接
|
|
|
*/
|
|
|
@PostConstruct
|
|
|
public void init() {
|
|
|
+ staticSmsProperties = this.smsProperties;
|
|
|
try {
|
|
|
Config config = new Config()
|
|
|
- .setAccessKeyId(ACCESS_KEY_ID)
|
|
|
- .setAccessKeySecret(ACCESS_KEY_SECRET)
|
|
|
- .setEndpoint(ENDPOINT);
|
|
|
+ .setAccessKeyId(smsProperties.getAccessKeyId())
|
|
|
+ .setAccessKeySecret(smsProperties.getAccessKeySecret())
|
|
|
+ .setEndpoint(smsProperties.getEndpoint());
|
|
|
smsClient = new Client(config);
|
|
|
LogUtil.info(SmsUtil.class, "阿里云短信客户端初始化成功");
|
|
|
} catch (Exception e) {
|
|
|
@@ -59,24 +58,25 @@ public class SmsUtil {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 发送短信(使用默认签名)
|
|
|
+ * 发送短信(使用默认签名,从配置文件读取)
|
|
|
*
|
|
|
- * @param phoneNumber 接收短信的手机号(支持单号,多号用英文逗号分隔,最多1000个)
|
|
|
+ * @param phoneNumber 接收短信的手机号(支持单号,多号用英文逗号分隔,最多1000个)
|
|
|
* @param templateCode 短信模板码(如:SMS_123456789)
|
|
|
- * @param params 模板变量参数,key 为模板变量名,value 为替换值;无变量传 null
|
|
|
+ * @param params 模板变量参数,key 为模板变量名,value 为替换值;无变量传 null
|
|
|
* @return true=发送成功,false=发送失败
|
|
|
*/
|
|
|
public static boolean send(String phoneNumber, String templateCode, Map<String, String> params) {
|
|
|
- return send(phoneNumber, SIGN_NAME, templateCode, params);
|
|
|
+ String signName = staticSmsProperties != null ? staticSmsProperties.getSignName() : null;
|
|
|
+ return send(phoneNumber, signName, templateCode, params);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送短信(自定义签名)
|
|
|
*
|
|
|
- * @param phoneNumber 接收短信的手机号(支持单号,多号用英文逗号分隔,最多1000个)
|
|
|
- * @param signName 短信签名
|
|
|
+ * @param phoneNumber 接收短信的手机号(支持单号,多号用英文逗号分隔,最多1000个)
|
|
|
+ * @param signName 短信签名
|
|
|
* @param templateCode 短信模板码(如:SMS_123456789)
|
|
|
- * @param params 模板变量参数,key 为模板变量名,value 为替换值;无变量传 null
|
|
|
+ * @param params 模板变量参数,key 为模板变量名,value 为替换值;无变量传 null
|
|
|
* @return true=发送成功,false=发送失败
|
|
|
*/
|
|
|
public static boolean send(String phoneNumber, String signName, String templateCode, Map<String, String> params) {
|
|
|
@@ -86,10 +86,10 @@ public class SmsUtil {
|
|
|
}
|
|
|
try {
|
|
|
SendSmsRequest request = new SendSmsRequest()
|
|
|
- .setPhoneNumbers(phoneNumber)
|
|
|
- .setSignName(signName)
|
|
|
- .setTemplateCode(templateCode)
|
|
|
- .setTemplateParam(buildTemplateParam(params));
|
|
|
+ .setPhoneNumbers(phoneNumber)
|
|
|
+ .setSignName(signName)
|
|
|
+ .setTemplateCode(templateCode)
|
|
|
+ .setTemplateParam(buildTemplateParam(params));
|
|
|
|
|
|
SendSmsResponse response = smsClient.sendSms(request);
|
|
|
String code = response.getBody().getCode();
|
|
|
@@ -98,11 +98,11 @@ public class SmsUtil {
|
|
|
|
|
|
if ("OK".equalsIgnoreCase(code)) {
|
|
|
LogUtil.info(SmsUtil.class, "短信发送成功: phoneNumber={}, templateCode={}, bizId={}",
|
|
|
- phoneNumber, templateCode, bizId);
|
|
|
+ phoneNumber, templateCode, bizId);
|
|
|
return true;
|
|
|
} else {
|
|
|
LogUtil.warn(SmsUtil.class, "短信发送失败: phoneNumber={}, templateCode={}, code={}, message={}",
|
|
|
- phoneNumber, templateCode, code, message);
|
|
|
+ phoneNumber, templateCode, code, message);
|
|
|
return false;
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
@@ -129,7 +129,7 @@ public class SmsUtil {
|
|
|
sb.append(",");
|
|
|
}
|
|
|
sb.append("\"").append(entry.getKey()).append("\":")
|
|
|
- .append("\"").append(entry.getValue()).append("\"");
|
|
|
+ .append("\"").append(entry.getValue()).append("\"");
|
|
|
first = false;
|
|
|
}
|
|
|
sb.append("}");
|