一、背景
在集成各种短信服务时,大体实现逻辑和配置基本上是一致的,往往都是做一些重复的工作,这里使用开源的SMS4J。
官方文档:https://sms4j.com/
二、实现
2.1 引入依赖
<!-- 短信 -->
<dependency>
<groupId>org.dromara.sms4j</groupId>
<artifactId>sms4j-spring-boot-starter</artifactId>
<version>3.2.1</version>
</dependency>
2.2 基本配置
sms:
# 标注从yml读取配置
config-type: yaml
blends:
# 自定义的标识,就是configId
aliyun1:
# 厂商标识
supplier: alibaba
# accessKey
access-key-id: xxx
# accessKeySecret
access-key-secret: xxx
# 短信签名
signature: xxx
# 模板ID 非必须配置,如果使用sendMessage的快速发送需此配置
template-id: xxx
# sdkAppId
sdk-app-id:
# 自定义的标识,就是configId
aliyun2:
# 厂商标识
supplier: alibaba
# accessKey
access-key-id: xxx
# accessKeySecret
access-key-secret: xxx
# 短信签名
signature: xxx
# 模板ID 非必须配置,如果使用sendMessage的快速发送需此配置
template-id: xxx
# sdkAppId
sdk-app-id:
2.3 API使用
package com.ozo.common.sms.service;
import org.dromara.sms4j.api.callback.CallBack;
import java.util.LinkedHashMap;
import java.util.List;
/**
* 短信服务类
*
* @author qiangesoft
* @date 2024-09-06
*/
public interface SmsService {
/**
* 获取供应商标识
*
* @param configId
* @return
*/
String getSupplier(String configId);
/**
* 发送短信
*
* @param configId
* @param phone
* @param message
* @return
*/
boolean sendMessage(String configId, String phone, String message);
/**
* 发送短信
*
* @param configId
* @param phone
* @param messages
* @return
*/
boolean sendMessage(String configId, String phone, LinkedHashMap<String, String> messages);
/**
* 发送短信
*
* @param configId
* @param phone
* @param templateId
* @param messages
* @return
*/
boolean sendMessage(String configId, String phone, String templateId, LinkedHashMap<String, String> messages);
/**
* 异步发送短信
*
* @param configId
* @param phone
* @param message
* @return
*/
void sendMessageAsync(String configId, String phone, String message);
/**
* 异步发送短信
*
* @param configId
* @param phone
* @param templateId
* @param messages
* @return
*/
void sendMessageAsync(String configId, String phone, String templateId, LinkedHashMap<String, String> messages);
/**
* 异步发送短信
*
* @param configId
* @param phone
* @param message
* @param callBack
* @return
*/
void sendMessageAsync(String configId, String phone, String message, CallBack callBack);
/**
* 异步发送短信
*
* @param configId
* @param phone
* @param templateId
* @param messages
* @param callBack
* @return
*/
void sendMessageAsync(String configId, String phone, String templateId, LinkedHashMap<String, String> messages, CallBack callBack);
/**
* 批量发送短信
*
* @param configId
* @param phones
* @param message
* @return
*/
boolean sendMassMessage(String configId, List<String> phones, String message);
/**
* 批量发送短信
*
* @param configId
* @param phones
* @param templateId
* @param messages
* @return
*/
boolean sendMassMessage(String configId, List<String> phones, String templateId, LinkedHashMap<String, String> messages);
/**
* 异步批量发送短信
*
* @param configId
* @param phones
* @param message
* @return
*/
void sendMassMessageAsync(String configId, List<String> phones, String message);
/**
* 异步批量发送短信
*
* @param configId
* @param phones
* @param templateId
* @param messages
* @return
*/
void sendMassMessageAsync(String configId, List<String> phones, String templateId, LinkedHashMap<String, String> messages);
/**
* 异步批量发送短信
*
* @param configId
* @param phones
* @param message
* @param callBack
* @return
*/
void sendMassMessageAsync(String configId, List<String> phones, String message, CallBack callBack);
/**
* 异步批量发送短信
*
* @param configId
* @param phones
* @param templateId
* @param messages
* @param callBack
* @return
*/
void sendMassMessageAsync(String configId, List<String> phones, String templateId, LinkedHashMap<String, String> messages, CallBack callBack);
/**
* 发送延迟短信
*
* @param configId
* @param phone
* @param message
* @param delayedTime
* @return
*/
void sendDelayedMessage(String configId, String phone, String message, Long delayedTime);
/**
* 发送延迟短信
*
* @param configId
* @param phone
* @param templateId
* @param messages
* @param delayedTime
* @return
*/
void sendDelayedMessage(String configId, String phone, String templateId, LinkedHashMap<String, String> messages, Long delayedTime);
/**
* 批量发送延迟短信
*
* @param configId
* @param phones
* @param message
* @param delayedTime
* @return
*/
void sendMassDelayedMessage(String configId, List<String> phones, String message, Long delayedTime);
/**
* 批量发送延迟短信
*
* @param configId
* @param phones
* @param templateId
* @param messages
* @param delayedTime
* @return
*/
void sendMassDelayedMessage(String configId, List<String> phones, String templateId, LinkedHashMap<String, String> messages, Long delayedTime);
}
package com.ozo.common.sms.service;
import org.dromara.sms4j.api.SmsBlend;
import org.dromara.sms4j.api.callback.CallBack;
import org.dromara.sms4j.api.entity.SmsResponse;
import org.dromara.sms4j.core.factory.SmsFactory;
import org.springframework.stereotype.Service;
import java.util.LinkedHashMap;
import java.util.List;
/**
* 短信服务类
*
* @author qiangesoft
* @date 2024-09-06
*/
@Service
public class SmsServiceImpl implements SmsService {
@Override
public String getSupplier(String configId) {
SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);
return smsBlend.getSupplier();
}
@Override
public boolean sendMessage(String configId, String phone, String message) {
SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);
SmsResponse smsResponse = smsBlend.sendMessage(phone, message);
return smsResponse.isSuccess();
}
@Override
public boolean sendMessage(String configId, String phone, LinkedHashMap<String, String> messages) {
SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);
SmsResponse smsResponse = smsBlend.sendMessage(phone, messages);
return smsResponse.isSuccess();
}
@Override
public boolean sendMessage(String configId, String phone, String templateId, LinkedHashMap<String, String> messages) {
SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);
SmsResponse smsResponse = smsBlend.sendMessage(phone, templateId, messages);
return smsResponse.isSuccess();
}
@Override
public void sendMessageAsync(String configId, String phone, String message) {
SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);
smsBlend.sendMessageAsync(phone, message);
}
@Override
public void sendMessageAsync(String configId, String phone, String templateId, LinkedHashMap<String, String> messages) {
SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);
smsBlend.sendMessageAsync(phone, templateId, messages);
}
@Override
public void sendMessageAsync(String configId, String phone, String message, CallBack callBack) {
SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);
smsBlend.sendMessageAsync(phone, message, callBack);
}
@Override
public void sendMessageAsync(String configId, String phone, String templateId, LinkedHashMap<String, String> messages, CallBack callBack) {
SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);
smsBlend.sendMessageAsync(phone, templateId, messages, callBack);
}
@Override
public boolean sendMassMessage(String configId, List<String> phones, String message) {
SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);
SmsResponse smsResponse = smsBlend.massTexting(phones, message);
return smsResponse.isSuccess();
}
@Override
public boolean sendMassMessage(String configId, List<String> phones, String templateId, LinkedHashMap<String, String> messages) {
SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);
SmsResponse smsResponse = smsBlend.massTexting(phones, templateId, messages);
return smsResponse.isSuccess();
}
@Override
public void sendMassMessageAsync(String configId, List<String> phones, String message) {
SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);
for (String phone : phones) {
smsBlend.sendMessageAsync(phone, message);
}
}
@Override
public void sendMassMessageAsync(String configId, List<String> phones, String templateId, LinkedHashMap<String, String> messages) {
SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);
for (String phone : phones) {
smsBlend.sendMessageAsync(phone, templateId, messages);
}
}
@Override
public void sendMassMessageAsync(String configId, List<String> phones, String message, CallBack callBack) {
SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);
for (String phone : phones) {
smsBlend.sendMessageAsync(phone, message, callBack);
}
}
@Override
public void sendMassMessageAsync(String configId, List<String> phones, String templateId, LinkedHashMap<String, String> messages, CallBack callBack) {
SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);
for (String phone : phones) {
smsBlend.sendMessageAsync(phone, templateId, messages, callBack);
}
}
@Override
public void sendDelayedMessage(String configId, String phone, String message, Long delayedTime) {
SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);
smsBlend.delayedMessage(phone, message, delayedTime);
}
@Override
public void sendDelayedMessage(String configId, String phone, String templateId, LinkedHashMap<String, String> messages, Long delayedTime) {
SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);
smsBlend.delayedMessage(phone, templateId, messages, delayedTime);
}
@Override
public void sendMassDelayedMessage(String configId, List<String> phones, String message, Long delayedTime) {
SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);
smsBlend.delayMassTexting(phones, message, delayedTime);
}
@Override
public void sendMassDelayedMessage(String configId, List<String> phones, String templateId, LinkedHashMap<String, String> messages, Long delayedTime) {
SmsBlend smsBlend = SmsFactory.getSmsBlend(configId);
smsBlend.delayMassTexting(phones, templateId, messages, delayedTime);
}
}
2.4 测试
package com.ozo.common.sms.controller;
import com.ozo.common.sms.service.SmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/sms")
@RestController
public class SmsController {
@Autowired
private SmsService smsService;
@GetMapping("/send")
public Boolean send() {
// 此处的configId与配置文件的configId保持一致
return smsService.sendMessage("aliyun1", "13111111111", "hahhahah");
}
}