添加 布丁云短信V2 Provider

This commit is contained in:
NicholasLD 2024-03-21 02:52:20 +08:00
parent a5535665f7
commit ca13119acc
5 changed files with 254 additions and 0 deletions

View File

@ -61,4 +61,9 @@ public abstract class SupplierConstant {
public static final String DINGZHONG = "dingzhong";
public static final String QINIU = "qiniu";
/**
* 布丁云V2
*/
public static final String BUDING_V2 = "buding_v2";
}

View File

@ -0,0 +1,40 @@
package org.dromara.sms4j.budingyun.config;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.dromara.sms4j.comm.constant.SupplierConstant;
import org.dromara.sms4j.provider.config.BaseConfig;
/**
* BudingV2Config
* <p> 布丁云V2短信配置
*
* @author NicholaslD
* @date 2024/03/21 12:00
* */
@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
public class BudingV2Config extends BaseConfig {
/**
* 签名密钥
* 就是发短信的时候的签名比如布丁云
*/
private String signKey;
/**
* 变量列表
* 用于替换短信模板中的变量
*/
private String[] args;
/**
* 获取供应商
*/
@Override
public String getSupplier() {
return SupplierConstant.BUDING_V2;
}
}

View File

@ -0,0 +1,33 @@
package org.dromara.sms4j.budingyun.config;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.dromara.sms4j.budingyun.service.BudingV2SmsImpl;
import org.dromara.sms4j.comm.constant.SupplierConstant;
import org.dromara.sms4j.provider.factory.AbstractProviderFactory;
/**
* BudingV2Factory
* <p> 布丁云V2短信对象建造
*
* @author NicholaslD
* @date 2024/03/21 12:00
* */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class BudingV2Factory extends AbstractProviderFactory<BudingV2SmsImpl, BudingV2Config> {
private static final BudingV2Factory INSTANCE = new BudingV2Factory();
public static BudingV2Factory instance() {
return INSTANCE;
}
@Override
public BudingV2SmsImpl createSms(BudingV2Config budingV2Config) {
return new BudingV2SmsImpl(budingV2Config);
}
@Override
public String getSupplier() {
return SupplierConstant.BUDING_V2;
}
}

View File

@ -0,0 +1,174 @@
package org.dromara.sms4j.budingyun.service;
import cn.hutool.json.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.dromara.sms4j.api.entity.SmsResponse;
import org.dromara.sms4j.budingyun.config.BudingV2Config;
import org.dromara.sms4j.comm.constant.Constant;
import org.dromara.sms4j.comm.constant.SupplierConstant;
import org.dromara.sms4j.comm.delayedTime.DelayedTime;
import org.dromara.sms4j.comm.exception.SmsBlendException;
import org.dromara.sms4j.provider.service.AbstractSmsBlend;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
/**
* BudingV2SmsImpl 布丁云V2短信实现
* @author NicholasLD
* @createTime 2024/3/21 01:28
*/
@Slf4j
public class BudingV2SmsImpl extends AbstractSmsBlend<BudingV2Config> {
/**
* 重试次数
*/
private int retry = 0;
private final String URL = "https://smsapi.idcbdy.com";
protected BudingV2SmsImpl(BudingV2Config config, Executor pool, DelayedTime delayed) {
super(config, pool, delayed);
}
public BudingV2SmsImpl(BudingV2Config config) {
super(config);
}
@Override
public String getSupplier() {
return SupplierConstant.BUDING_V2;
}
@Override
public SmsResponse sendMessage(String phone, String message) {
System.out.println("sendMessage");
Map<String, Object> body = new HashMap<>();
System.out.println(getConfig().getSignKey());
System.out.println(getConfig().getSignature());
if (getConfig().getSignKey() == null && getConfig().getSignature() == null) {
throw new SmsBlendException("签名秘钥不能为空");
}
if (getConfig().getSignKey() == null) {
body.put("sign", getConfig().getSignature());
}
body.put("key", getConfig().getAccessKeyId());
body.put("to", phone);
body.put("content", message);
Map<String, String> headers = getHeaders();
SmsResponse smsResponse;
try {
smsResponse = getResponse(http.postFrom(URL + "/Api/Sent", headers, body));
} catch (SmsBlendException e) {
smsResponse = new SmsResponse();
smsResponse.setSuccess(false);
smsResponse.setData(e.getMessage());
}
if (smsResponse.isSuccess() || retry == getConfig().getMaxRetries()) {
retry = 0;
return smsResponse;
}
return requestRetry(phone, message);
}
private SmsResponse requestRetry(String phone, String message) {
http.safeSleep(getConfig().getRetryInterval());
retry++;
log.warn("短信第 {" + retry + "} 次重新发送");
return sendMessage(phone, message);
}
private SmsResponse getResponse(JSONObject entries) {
System.out.println(entries);
SmsResponse smsResponse = new SmsResponse();
if (entries == null) {
smsResponse.setSuccess(false);
return smsResponse;
}
smsResponse.setSuccess(entries.getBool("bool"));
smsResponse.setData(entries);
smsResponse.setConfigId(getConfigId());
return smsResponse;
}
/**
* 发送多条短信
* @param phone 手机号
* @param messages 消息内容
* @return 发送结果
*/
@Override
public SmsResponse sendMessage(String phone, LinkedHashMap<String, String> messages) {
int failed = 0;
for (String message : messages.values()) {
SmsResponse smsResponse = sendMessage(phone, message);
if (!smsResponse.isSuccess()) {
failed++;
}
}
SmsResponse smsResponse = new SmsResponse();
smsResponse.setSuccess(failed == 0);
return smsResponse;
}
/**
* 发送多条短信 (布丁云V2暂不支持模板短信)
* @param phone 手机号
* @param templateId 模板ID (布丁云V2暂不支持模板短信此参数无效)
* @param messages 模板参数
* @return 发送结果
*/
@Override
public SmsResponse sendMessage(String phone, String templateId, LinkedHashMap<String, String> messages) {
return sendMessage(phone, messages);
}
/**
* 群发短信
* @param phones 手机号列表
* @param message 消息内容
* @return 发送结果
*/
@Override
public SmsResponse massTexting(List<String> phones, String message) {
int failed = 0;
for (String phone : phones) {
SmsResponse smsResponse = sendMessage(phone, message);
if (!smsResponse.isSuccess()) {
failed++;
}
}
SmsResponse smsResponse = new SmsResponse();
smsResponse.setSuccess(failed == 0);
return smsResponse;
}
/**
* 群发短信 (布丁云V2暂不支持模板短信此方法无效)
* @param phones 手机号列表
* @param templateId 模板ID (布丁云V2暂不支持模板短信此参数无效)
* @param messages 模板参数
* @return 发送结果
*/
@Override
public SmsResponse massTexting(List<String> phones, String templateId, LinkedHashMap<String, String> messages) {
throw new SmsBlendException("布丁云V2暂不支持多条短信发送");
}
private Map<String, String> getHeaders() {
Map<String, String> headers = new HashMap<>();
headers.put("Accept", Constant.APPLICATION_JSON_UTF8);
headers.put("Content-Type", Constant.FROM_URLENCODED);
return headers;
}
}

View File

@ -7,6 +7,7 @@ import lombok.extern.slf4j.Slf4j;
import org.dromara.sms4j.aliyun.config.AlibabaFactory;
import org.dromara.sms4j.api.SmsBlend;
import org.dromara.sms4j.api.universal.SupplierConfig;
import org.dromara.sms4j.budingyun.config.BudingV2Factory;
import org.dromara.sms4j.cloopen.config.CloopenFactory;
import org.dromara.sms4j.comm.constant.Constant;
import org.dromara.sms4j.comm.enumerate.ConfigType;
@ -125,6 +126,7 @@ public class SmsBlendsInitializer {
ProviderFactoryHolder.registerFactory(LianLuFactory.instance());
ProviderFactoryHolder.registerFactory(DingZhongFactory.instance());
ProviderFactoryHolder.registerFactory(QiNiuFactory.instance());
ProviderFactoryHolder.registerFactory(BudingV2Factory.instance());
if(SmsUtils.isClassExists("com.jdcloud.sdk.auth.CredentialsProvider")) {
ProviderFactoryHolder.registerFactory(JdCloudFactory.instance());
}