mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-23 07:41:53 +08:00
code review 腾讯短信的实现
This commit is contained in:
parent
6fd52dfbb6
commit
9726560318
@ -2,12 +2,9 @@ package cn.iocoder.yudao.framework.common.util.collection;
|
|||||||
|
|
||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.collection.CollectionUtil;
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
|
||||||
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
|
||||||
import java.lang.reflect.Array;
|
import java.lang.reflect.Array;
|
||||||
import java.lang.reflect.GenericArrayType;
|
|
||||||
import java.lang.reflect.Type;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.function.BinaryOperator;
|
import java.util.function.BinaryOperator;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
@ -174,6 +171,7 @@ public class CollectionUtils {
|
|||||||
return deptId == null ? Collections.emptyList() : Collections.singleton(deptId);
|
return deptId == null ? Collections.emptyList() : Collections.singleton(deptId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO @FinallySays:建议放在 ArrayUtils 里,和 hutool 对齐
|
||||||
public static <T, V> V[] toArray(List<T> from, Function<T, V> mapper) {
|
public static <T, V> V[] toArray(List<T> from, Function<T, V> mapper) {
|
||||||
return toArray(convertList(from, mapper));
|
return toArray(convertList(from, mapper));
|
||||||
}
|
}
|
||||||
|
@ -39,11 +39,13 @@ import static cn.iocoder.yudao.framework.common.util.date.DateUtils.TIME_ZONE_DE
|
|||||||
*
|
*
|
||||||
* @author : shiwp
|
* @author : shiwp
|
||||||
*/
|
*/
|
||||||
|
// TODO @author 后面,空格即可
|
||||||
public class TencentSmsClient extends AbstractSmsClient {
|
public class TencentSmsClient extends AbstractSmsClient {
|
||||||
|
|
||||||
private SmsClient client;
|
private SmsClient client;
|
||||||
|
|
||||||
public TencentSmsClient(SmsChannelProperties properties) {
|
public TencentSmsClient(SmsChannelProperties properties) {
|
||||||
|
// TODO @FinallySays:注释的时候,中英文之间要空格哈
|
||||||
// 腾讯云发放短信的时候需要额外的参数sdkAppId, 所以和secretId组合在一起放到apiKey字段中,格式为[secretId sdkAppId],
|
// 腾讯云发放短信的时候需要额外的参数sdkAppId, 所以和secretId组合在一起放到apiKey字段中,格式为[secretId sdkAppId],
|
||||||
// 这边需要做拆分重新封装到properties内
|
// 这边需要做拆分重新封装到properties内
|
||||||
super(TencentSmsChannelProperties.build(properties), new TencentSmsCodeMapping());
|
super(TencentSmsChannelProperties.build(properties), new TencentSmsCodeMapping());
|
||||||
@ -53,6 +55,7 @@ public class TencentSmsClient extends AbstractSmsClient {
|
|||||||
@Override
|
@Override
|
||||||
protected void doInit() {
|
protected void doInit() {
|
||||||
// init或者refresh时需要重新封装properties
|
// init或者refresh时需要重新封装properties
|
||||||
|
// TODO @FinallySays:是不是不用这个变量呀?p
|
||||||
final SmsChannelProperties p = properties;
|
final SmsChannelProperties p = properties;
|
||||||
properties = TencentSmsChannelProperties.build(p);
|
properties = TencentSmsChannelProperties.build(p);
|
||||||
// 实例化一个认证对象,入参需要传入腾讯云账户密钥对secretId,secretKey。
|
// 实例化一个认证对象,入参需要传入腾讯云账户密钥对secretId,secretKey。
|
||||||
@ -66,7 +69,7 @@ public class TencentSmsClient extends AbstractSmsClient {
|
|||||||
String apiTemplateId,
|
String apiTemplateId,
|
||||||
List<KeyValue<String, Object>> templateParams) throws Throwable {
|
List<KeyValue<String, Object>> templateParams) throws Throwable {
|
||||||
|
|
||||||
return invoke(() -> buildSendSmsRequest(sendLogId, mobile, apiTemplateId, templateParams),
|
return invoke(() -> buildSendSmsRequest(sendLogId, mobile, apiTemplateId, templateParams), // TODO @FinallySays:上面不用空行
|
||||||
this::doSendSms0,
|
this::doSendSms0,
|
||||||
response -> {
|
response -> {
|
||||||
SendStatus sendStatus = response.getSendStatusSet()[0];
|
SendStatus sendStatus = response.getSendStatusSet()[0];
|
||||||
@ -117,6 +120,7 @@ public class TencentSmsClient extends AbstractSmsClient {
|
|||||||
data.setErrorCode(status.getErrCode()).setErrorMsg(status.getDescription());
|
data.setErrorCode(status.getErrCode()).setErrorMsg(status.getDescription());
|
||||||
data.setReceiveTime(status.getReceiveTime()).setSuccess("SUCCESS".equalsIgnoreCase(status.getStatus()));
|
data.setReceiveTime(status.getReceiveTime()).setSuccess("SUCCESS".equalsIgnoreCase(status.getStatus()));
|
||||||
data.setMobile(status.getMobile()).setSerialNo(status.getSerialNo());
|
data.setMobile(status.getMobile()).setSerialNo(status.getSerialNo());
|
||||||
|
// TODO @FinallySays:建议直接判断是否为空,酱紫更易读一些
|
||||||
Optional.ofNullable(status.getSessionContext()).map(SessionContext::getLogId)
|
Optional.ofNullable(status.getSessionContext()).map(SessionContext::getLogId)
|
||||||
.ifPresentOrElse(data::setLogId, () -> {
|
.ifPresentOrElse(data::setLogId, () -> {
|
||||||
throw new IllegalStateException(StrUtil.format("未回传logId,需联系腾讯云解决。"));
|
throw new IllegalStateException(StrUtil.format("未回传logId,需联系腾讯云解决。"));
|
||||||
@ -170,9 +174,10 @@ public class TencentSmsClient extends AbstractSmsClient {
|
|||||||
*/
|
*/
|
||||||
private DescribeSmsTemplateListRequest buildSmsTemplateStatusRequest(String apiTemplateId) {
|
private DescribeSmsTemplateListRequest buildSmsTemplateStatusRequest(String apiTemplateId) {
|
||||||
DescribeSmsTemplateListRequest request = new DescribeSmsTemplateListRequest();
|
DescribeSmsTemplateListRequest request = new DescribeSmsTemplateListRequest();
|
||||||
|
// TODO TODO @FinallySays: new Long[]{Long.parseLong(apiTemplateId)} 就 ok 啦
|
||||||
request.setTemplateIdSet(CollectionUtils.toArray(Collections.singletonList(apiTemplateId), Long::parseLong));
|
request.setTemplateIdSet(CollectionUtils.toArray(Collections.singletonList(apiTemplateId), Long::parseLong));
|
||||||
// 地区
|
// 地区
|
||||||
request.setInternational(0L);
|
request.setInternational(0L); // TODO @FinallySays:0L 最好说明下哈;
|
||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -280,9 +285,11 @@ public class TencentSmsClient extends AbstractSmsClient {
|
|||||||
return (TencentSmsChannelProperties) properties;
|
return (TencentSmsChannelProperties) properties;
|
||||||
}
|
}
|
||||||
TencentSmsChannelProperties result = BeanUtil.toBean(properties, TencentSmsChannelProperties.class);
|
TencentSmsChannelProperties result = BeanUtil.toBean(properties, TencentSmsChannelProperties.class);
|
||||||
|
// TODO @FinallySays:comb=》combine,不缩写好点哈
|
||||||
String combKey = properties.getApiKey();
|
String combKey = properties.getApiKey();
|
||||||
Assert.notEmpty(combKey, "apiKey 不能为空");
|
Assert.notEmpty(combKey, "apiKey 不能为空");
|
||||||
String[] keys = combKey.trim().split(" ");
|
String[] keys = combKey.trim().split(" ");
|
||||||
|
// TODO @FinallySays:建议写多个断言好点,嘿嘿。然后 Assert 支持占位符
|
||||||
Assert.isTrue(keys.length == 2 && StrUtil.isNotBlank(keys[0]) && StrUtil.isNotBlank(keys[1]),
|
Assert.isTrue(keys.length == 2 && StrUtil.isNotBlank(keys[0]) && StrUtil.isNotBlank(keys[1]),
|
||||||
"腾讯云短信api配置格式错误,请配置为[secretId sdkAppId]");
|
"腾讯云短信api配置格式错误,请配置为[secretId sdkAppId]");
|
||||||
result.setSdkAppId(keys[1]).setApiKey(keys[0]);
|
result.setSdkAppId(keys[1]).setApiKey(keys[0]);
|
||||||
@ -291,4 +298,4 @@ public class TencentSmsClient extends AbstractSmsClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user