package com.doumee.core.utils.aliyun;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONObject;
|
import com.aliyuncs.DefaultAcsClient;
|
import com.aliyuncs.IAcsClient;
|
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
|
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
|
import com.aliyuncs.http.MethodType;
|
import com.aliyuncs.profile.DefaultProfile;
|
import com.aliyuncs.profile.IClientProfile;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.Service;
|
|
import java.util.Map;
|
|
/**
|
* Created by IntelliJ IDEA.
|
*
|
* @Author : Rk
|
* @create 2025/7/23 11:37
|
*/
|
@Service
|
@Slf4j
|
public class AliSmsService {
|
|
private final static String ACCESS_KEY_ID = "LTAI5t835zTU4aaYpGHJCccJ";
|
private final static String ACCESS_KEY_SECRET = "98sAF2NchWVuIzu62zcLq0Ns7LIQTp";
|
private final static String SING_NAME = "南京三只鹤";
|
|
|
public static void main(String[] args) {
|
Map<String,Object> tempParam = new java.util.HashMap<>();
|
tempParam.put("code","1234");
|
AliSmsService.sendSms("15345690849","SMS_333770877", JSONObject.toJSONString(tempParam));
|
}
|
|
|
/**
|
* 发短信模板(可群发)
|
*
|
* @param phone 订单详情
|
* @param templateCode 模板code
|
* @param templateParam 模板变量json字符串
|
* @return 处理结果
|
*/
|
public static String sendSms(String phone, String templateCode, String templateParam) {
|
try {
|
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
|
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
|
final String product = "Dysmsapi";
|
final String domain = "dysmsapi.aliyuncs.com";
|
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_ID, ACCESS_KEY_SECRET);
|
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
|
IAcsClient acsClient = new DefaultAcsClient(profile);
|
SendSmsRequest request = new SendSmsRequest();
|
request.setMethod(MethodType.POST);
|
request.setPhoneNumbers(phone);
|
request.setSignName(SING_NAME);
|
request.setTemplateCode(templateCode);
|
request.setTemplateParam(templateParam);
|
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
|
if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
|
log.info("短信发送成功:phone={}, template={}", phone, templateCode);
|
return null;
|
} else {
|
String error = sendSmsResponse.getCode() + ":" + sendSmsResponse.getMessage();
|
log.error("短信发送失败:phone={}, template={}, error={}", phone, templateCode, error);
|
return error;
|
}
|
} catch (Exception e) {
|
log.error("短信发送异常:phone={}, template={}, error={}", phone, templateCode, e.getMessage());
|
return e.getMessage();
|
}
|
}
|
}
|