|
|
@@ -0,0 +1,138 @@
|
|
|
+package com.aijiuyi.admin.common.util;
|
|
|
+
|
|
|
+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.stereotype.Component;
|
|
|
+
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 阿里云短信发送工具类
|
|
|
+ * 封装 dysmsapi20170525 SDK,支持传入模板码和模板参数发送短信
|
|
|
+ *
|
|
|
+ * 使用示例:
|
|
|
+ * // 发送验证码
|
|
|
+ * Map<String, String> params = new HashMap<>();
|
|
|
+ * params.put("code", "123456");
|
|
|
+ * SmsUtil.send("13800138000", "SMS_xxxxxxx", params);
|
|
|
+ *
|
|
|
+ * // 发送无参数模板
|
|
|
+ * 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 = "深圳研年健康科技";
|
|
|
+
|
|
|
+ /** 短信客户端静态实例(初始化后复用) */
|
|
|
+ private static Client smsClient;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 应用启动后初始化短信客户端
|
|
|
+ * 避免每次发送时重复创建连接
|
|
|
+ */
|
|
|
+ @PostConstruct
|
|
|
+ public void init() {
|
|
|
+ try {
|
|
|
+ Config config = new Config()
|
|
|
+ .setAccessKeyId(ACCESS_KEY_ID)
|
|
|
+ .setAccessKeySecret(ACCESS_KEY_SECRET)
|
|
|
+ .setEndpoint(ENDPOINT);
|
|
|
+ smsClient = new Client(config);
|
|
|
+ LogUtil.info(SmsUtil.class, "阿里云短信客户端初始化成功");
|
|
|
+ } catch (Exception e) {
|
|
|
+ LogUtil.error(SmsUtil.class, "阿里云短信客户端初始化失败", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送短信(使用默认签名)
|
|
|
+ *
|
|
|
+ * @param phoneNumber 接收短信的手机号(支持单号,多号用英文逗号分隔,最多1000个)
|
|
|
+ * @param templateCode 短信模板码(如:SMS_123456789)
|
|
|
+ * @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);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送短信(自定义签名)
|
|
|
+ *
|
|
|
+ * @param phoneNumber 接收短信的手机号(支持单号,多号用英文逗号分隔,最多1000个)
|
|
|
+ * @param signName 短信签名
|
|
|
+ * @param templateCode 短信模板码(如:SMS_123456789)
|
|
|
+ * @param params 模板变量参数,key 为模板变量名,value 为替换值;无变量传 null
|
|
|
+ * @return true=发送成功,false=发送失败
|
|
|
+ */
|
|
|
+ public static boolean send(String phoneNumber, String signName, String templateCode, Map<String, String> params) {
|
|
|
+ if (smsClient == null) {
|
|
|
+ LogUtil.error(SmsUtil.class, "短信客户端未初始化,发送失败: phoneNumber={}", phoneNumber);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ SendSmsRequest request = new SendSmsRequest()
|
|
|
+ .setPhoneNumbers(phoneNumber)
|
|
|
+ .setSignName(signName)
|
|
|
+ .setTemplateCode(templateCode)
|
|
|
+ .setTemplateParam(buildTemplateParam(params));
|
|
|
+
|
|
|
+ SendSmsResponse response = smsClient.sendSms(request);
|
|
|
+ String code = response.getBody().getCode();
|
|
|
+ String message = response.getBody().getMessage();
|
|
|
+ String bizId = response.getBody().getBizId();
|
|
|
+
|
|
|
+ if ("OK".equalsIgnoreCase(code)) {
|
|
|
+ LogUtil.info(SmsUtil.class, "短信发送成功: phoneNumber={}, templateCode={}, bizId={}",
|
|
|
+ phoneNumber, templateCode, bizId);
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ LogUtil.warn(SmsUtil.class, "短信发送失败: phoneNumber={}, templateCode={}, code={}, message={}",
|
|
|
+ phoneNumber, templateCode, code, message);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ LogUtil.error(SmsUtil.class, "短信发送异常: phoneNumber=" + phoneNumber + ", templateCode=" + templateCode, e);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将参数 Map 转换为阿里云短信模板参数 JSON 字符串
|
|
|
+ * 格式:{"code":"123456","name":"张三"}
|
|
|
+ *
|
|
|
+ * @param params 模板变量参数 Map
|
|
|
+ * @return JSON 格式字符串,无参数时返回 null
|
|
|
+ */
|
|
|
+ private static String buildTemplateParam(Map<String, String> params) {
|
|
|
+ if (params == null || params.isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ StringBuilder sb = new StringBuilder("{");
|
|
|
+ boolean first = true;
|
|
|
+ for (Map.Entry<String, String> entry : params.entrySet()) {
|
|
|
+ if (!first) {
|
|
|
+ sb.append(",");
|
|
|
+ }
|
|
|
+ sb.append("\"").append(entry.getKey()).append("\":")
|
|
|
+ .append("\"").append(entry.getValue()).append("\"");
|
|
|
+ first = false;
|
|
|
+ }
|
|
|
+ sb.append("}");
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+}
|