接口
package cn.daenx.framework.notify.sms.service;
import cn.daenx.framework.common.vo.system.utils.SmsSendResult;
import java.util.Map;
public interface SmsService {
SmsSendResult sendSms(Map<String, String> info, String phones, String templateId, Map<String, String> param);
}
阿里云实现
package cn.daenx.framework.notify.sms.service.impl;
import cn.daenx.framework.common.vo.system.utils.SmsSendResult;
import cn.daenx.framework.notify.sms.service.SmsService;
import com.alibaba.fastjson2.JSON;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import org.springframework.stereotype.Service;
import java.util.Map;
@Service("aliyun")
public class AliyunSmsService implements SmsService {
@Override
public SmsSendResult sendSms(Map<String, String> info, String phones, String templateId, Map<String, String> param) {
try {
Config config = new Config();
config.setAccessKeyId(info.get("accessKeyId"));
config.setAccessKeySecret(info.get("accessKeySecret"));
config.setEndpoint(info.get("endpoint"));
Client client = new Client(config);
com.aliyun.dysmsapi20170525.models.SendSmsRequest req = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
.setPhoneNumbers(phones)
.setSignName(info.get("signName"))
.setTemplateCode(templateId)
.setTemplateParam(JSON.toJSONString(param));
SendSmsResponse resp = client.sendSms(req);
SmsSendResult smsSendResult = SmsSendResult.builder().isSuccess("OK".equals(resp.getBody().getCode())).msg(resp.getBody().getMessage()).aliyunRes(resp).build();
return smsSendResult;
} catch (Exception e) {
e.printStackTrace();
SmsSendResult smsSendResult = SmsSendResult.builder().isSuccess(false).msg(e.getMessage()).build();
return smsSendResult;
}
}
}
腾讯云实现
package cn.daenx.framework.notify.sms.service.impl;
import cn.daenx.framework.common.vo.system.utils.SmsSendResult;
import cn.daenx.framework.notify.sms.service.SmsService;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendStatus;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
@Service("tencent")
public class TencentSmsService implements SmsService {
@Override
public SmsSendResult sendSms(Map<String, String> info, String phones, String templateId, Map<String, String> param) {
try {
Credential credential = new Credential(info.get("accessKeyId"), info.get("accessKeySecret"));
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint(info.get("endpoint"));
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
SmsClient client = new SmsClient(credential, "", clientProfile);
com.tencentcloudapi.sms.v20190711.models.SendSmsRequest req = new SendSmsRequest();
Set<String> set = Arrays.stream(phones.split(",")).collect(Collectors.toSet());
req.setPhoneNumberSet(ArrayUtil.toArray(set, String.class));
if (CollUtil.isNotEmpty(param)) {
req.setTemplateParamSet(ArrayUtil.toArray(param.values(), String.class));
}
req.setTemplateID(templateId);
req.setSign(info.get("signName"));
req.setSmsSdkAppid(info.get("sdkAppId"));
com.tencentcloudapi.sms.v20190711.models.SendSmsResponse resp = client.SendSms(req);
SmsSendResult smsSendResult = SmsSendResult.builder().isSuccess(true).msg("ok").tencentRes(resp).build();
for (SendStatus sendStatus : resp.getSendStatusSet()) {
if (!"Ok".equals(sendStatus.getCode())) {
smsSendResult.setSuccess(false);
sendStatus.setMessage(sendStatus.getMessage());
break;
}
}
return smsSendResult;
} catch (Exception e) {
e.printStackTrace();
SmsSendResult smsSendResult = SmsSendResult.builder().isSuccess(false).msg(e.getMessage()).build();
return smsSendResult;
}
}
}
调用
String type = "aliyun";
SmsService smsService = SpringUtil.getApplicationContext().getBean(type, SmsService.class);
SmsSendResult smsSendResult = smsService.sendSms(xxx, xxx, xxx, xxx);