sms4j-solon-plugin-example: springboot 的测试代码

This commit is contained in:
noear 2025-01-04 18:44:27 +08:00
parent 49c5af9f70
commit 44287e62d4
3 changed files with 49 additions and 18 deletions

View File

@ -4,6 +4,7 @@ import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.dromara.sms4j.api.entity.SmsResponse; import org.dromara.sms4j.api.entity.SmsResponse;
import org.dromara.sms4j.api.utils.SmsRespUtils;
import org.dromara.sms4j.comm.delayedTime.DelayedTime; import org.dromara.sms4j.comm.delayedTime.DelayedTime;
import org.dromara.sms4j.comm.exception.SmsBlendException; import org.dromara.sms4j.comm.exception.SmsBlendException;
import org.dromara.sms4j.comm.utils.SmsUtils; import org.dromara.sms4j.comm.utils.SmsUtils;
@ -90,9 +91,7 @@ public class ZhangJunSmsImpl extends AbstractSmsBlend<ZhangJunConfig> {
try { try {
smsResponse = getResponse(http.postJson(getConfig().getUrl(), null, message)); smsResponse = getResponse(http.postJson(getConfig().getUrl(), null, message));
} catch (SmsBlendException e) { } catch (SmsBlendException e) {
smsResponse = new SmsResponse(); smsResponse = errorResp(e.message);
smsResponse.setSuccess(false);
smsResponse.setData(e.getMessage());
} }
if (smsResponse.isSuccess() || retry == getConfig().getMaxRetries()) { if (smsResponse.isSuccess() || retry == getConfig().getMaxRetries()) {
retry = 0; retry = 0;
@ -109,11 +108,7 @@ public class ZhangJunSmsImpl extends AbstractSmsBlend<ZhangJunConfig> {
} }
private SmsResponse getResponse(JSONObject resJson) { private SmsResponse getResponse(JSONObject resJson) {
SmsResponse smsResponse = new SmsResponse(); return SmsRespUtils.resp(resJson, "OK".equals(resJson.getStr("Code")), getConfigId());
smsResponse.setSuccess("OK".equals(resJson.getStr("Code")));
smsResponse.setData(resJson);
smsResponse.setConfigId(getConfigId());
return smsResponse;
} }
} }

View File

@ -79,13 +79,13 @@ sms:
access-key-secret: 你的Access Key Secret access-key-secret: 你的Access Key Secret
sdkAppId: 你的应用ID sdkAppId: 你的应用ID
#自定义广州掌骏短信添加factory全路径。config,factory,SmsImpl复制其他默认实现即可修改对应的supplier和发送核心逻辑即可 #自定义广州掌骏短信添加factory全路径。config,factory,SmsImpl复制其他默认实现即可修改对应的supplier和发送核心逻辑即可
# zhangjun: # zhangjun:
# supplier: zhangjun # supplier: zhangjun
# factory: org.dromara.sms4j.example.zhangjun.ZhangJunFactory # factory: org.dromara.sms4j.example.zhangjun.ZhangJunFactory
# templateId: d2a****777 # templateId: d2a****777
# appId: 64c52d2a****77775fe72e3 # appId: 64c52d2a****77775fe72e3
# sid: d2a****777 # sid: d2a****777
# url: https://sms.idowe.com/**/**/**/send # url: https://sms.idowe.com/**/**/**/send
qiniu: qiniu:
access-key-id: EQcDflLTCYnU1******CmqIYLhog1lkWHb2 access-key-id: EQcDflLTCYnU1******CmqIYLhog1lkWHb2
access-key-secret: NeS2ptvZQoIy*****err2DdLe7wxFfQvji1 access-key-secret: NeS2ptvZQoIy*****err2DdLe7wxFfQvji1
@ -155,21 +155,29 @@ sms:
accessKeyId: ACCOUNT SID accessKeyId: ACCOUNT SID
accessKeySecret: AUTH TOKEN accessKeySecret: AUTH TOKEN
action: 默认请求方法 distributor/sendSMS action: 默认请求方法 distributor/sendSMS
# 一信通
yixintong:
sp-code: xxxxxx #(必填)企业编号
access-key-id: xxxxxx #(必填)用户名
access-key-secret: 324gaxxxxxxxxxxxxxxxxx9sdf89 #(必填)接口密钥(正式帐户需要登陆平台,接口业务-接口申请右侧钥匙状图标查看或获取,接口密钥获取后十分钟生效)
template-id: #(可选)模板编号(若配置此参数,则会默认使用该模板,以便提高服务方性能)
sign-code: #(可选)短信前置签名编号(登陆平台-接口业务-我的签名查看)
f: 1 #(可选)默认为1提交时检测方式
sms-oa: sms-oa:
config-type: yaml config-type: yaml
oas: oas:
oaDingTalkByYaml: # configId oaDingTalkByYaml: # configId
isEnable: true # 表示该配置是否生效(默认生效,false表示不生效) isEnable: true # 表示该配置是否生效(默认生效,false表示不生效)
supplier: dingding # 厂商标识 supplier: ding_ding # 厂商标识
tokenId: 您的accessKey tokenId: 您的accessKey
sign: 您的sign sign: 您的sign
oaByteTalkByYaml: # configId oaByteTalkByYaml: # configId
supplier: feishu # 厂商标识 supplier: byte_talk # 厂商标识
tokenId: 您的accessKey tokenId: 您的accessKey
sign: 您的sign sign: 您的sign
oaWeTalkByYaml: oaWeTalkByYaml:
supplier: wetalk # 厂商标识 supplier: we_talk # 厂商标识
tokenId: 您的sign tokenId: 您的sign
core-pool-size: 20 core-pool-size: 20
queue-capacity: 20 queue-capacity: 20

View File

@ -1,5 +1,6 @@
package org.dromara.sms4j.example; package org.dromara.sms4j.example;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil; import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.UUID; import cn.hutool.core.lang.UUID;
@ -530,4 +531,31 @@ public class Sms4jTest {
SmsResponse smsResponse5 = danMiSms.voiceTemplate(PHONE, "opipedlqza", "111,222,333"); SmsResponse smsResponse5 = danMiSms.voiceTemplate(PHONE, "opipedlqza", "111,222,333");
Assert.isTrue(smsResponse5.isSuccess()); Assert.isTrue(smsResponse5.isSuccess());
} }
/**
* 联通一信通模板
*/
@Test
public void yixintongSmsTest() {
if (StrUtil.isBlank(PHONE)) {
return;
}
//短信发送模板你有一项编号为{xxxxxxxxx}的事务需要处理{x}
//其中的{xxxxxx}代表短信模板中的变量部分可变化一个x代表一个字或者字符{}为变量标识在发送时不用传实发变量字数小于等于x的个数
// 单发
String message1 = StrUtil.format("你有一项编号为{}的事务需要处理。", SmsUtils.getRandomInt(6));
SmsResponse smsResponse1 = SmsFactory.getBySupplier(SupplierConstant.YIXINTONG).sendMessage(PHONE, message1);
log.info(JSONUtil.toJsonStr(smsResponse1));
Assert.isTrue(smsResponse1.isSuccess());
// 群发
List<String> phones = CollectionUtil.toList(PHONE);
String message2 = StrUtil.format("你有一项编号为{}的事务需要处理。", SmsUtils.getRandomInt(6));
SmsResponse smsResponse2 = SmsFactory.getBySupplier(SupplierConstant.YIXINTONG).massTexting(phones, message2);
log.info(JSONUtil.toJsonStr(smsResponse2));
Assert.isTrue(smsResponse2.isSuccess());
}
} }