code review 腾讯短信的实现

This commit is contained in:
YunaiV 2022-04-06 01:05:52 +08:00
parent 6fd52dfbb6
commit 9726560318
2 changed files with 11 additions and 6 deletions

View File

@ -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));
} }

View File

@ -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);
// 实例化一个认证对象入参需要传入腾讯云账户密钥对secretIdsecretKey // 实例化一个认证对象入参需要传入腾讯云账户密钥对secretIdsecretKey
@ -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 @FinallySays0L 最好说明下哈
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 @FinallySayscomb=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 {
} }
} }