Commit 97265603183fbeefad33f595d3b2c0d18b8b7d1d
1 parent
6fd52dfb
code review 腾讯短信的实现
Showing
2 changed files
with
11 additions
and
6 deletions
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/collection/CollectionUtils.java
| @@ -2,12 +2,9 @@ package cn.iocoder.yudao.framework.common.util.collection; | @@ -2,12 +2,9 @@ package cn.iocoder.yudao.framework.common.util.collection; | ||
| 2 | 2 | ||
| 3 | import cn.hutool.core.collection.CollUtil; | 3 | import cn.hutool.core.collection.CollUtil; |
| 4 | import cn.hutool.core.collection.CollectionUtil; | 4 | import cn.hutool.core.collection.CollectionUtil; |
| 5 | -import com.fasterxml.jackson.core.type.TypeReference; | ||
| 6 | import com.google.common.collect.ImmutableMap; | 5 | import com.google.common.collect.ImmutableMap; |
| 7 | 6 | ||
| 8 | import java.lang.reflect.Array; | 7 | import java.lang.reflect.Array; |
| 9 | -import java.lang.reflect.GenericArrayType; | ||
| 10 | -import java.lang.reflect.Type; | ||
| 11 | import java.util.*; | 8 | import java.util.*; |
| 12 | import java.util.function.BinaryOperator; | 9 | import java.util.function.BinaryOperator; |
| 13 | import java.util.function.Function; | 10 | import java.util.function.Function; |
| @@ -174,6 +171,7 @@ public class CollectionUtils { | @@ -174,6 +171,7 @@ public class CollectionUtils { | ||
| 174 | return deptId == null ? Collections.emptyList() : Collections.singleton(deptId); | 171 | return deptId == null ? Collections.emptyList() : Collections.singleton(deptId); |
| 175 | } | 172 | } |
| 176 | 173 | ||
| 174 | + // TODO @FinallySays:建议放在 ArrayUtils 里,和 hutool 对齐 | ||
| 177 | public static <T, V> V[] toArray(List<T> from, Function<T, V> mapper) { | 175 | public static <T, V> V[] toArray(List<T> from, Function<T, V> mapper) { |
| 178 | return toArray(convertList(from, mapper)); | 176 | return toArray(convertList(from, mapper)); |
| 179 | } | 177 | } |
yudao-framework/yudao-spring-boot-starter-biz-sms/src/main/java/cn/iocoder/yudao/framework/sms/core/client/impl/tencent/TencentSmsClient.java
| @@ -39,11 +39,13 @@ import static cn.iocoder.yudao.framework.common.util.date.DateUtils.TIME_ZONE_DE | @@ -39,11 +39,13 @@ import static cn.iocoder.yudao.framework.common.util.date.DateUtils.TIME_ZONE_DE | ||
| 39 | * | 39 | * |
| 40 | * @author : shiwp | 40 | * @author : shiwp |
| 41 | */ | 41 | */ |
| 42 | +// TODO @author 后面,空格即可 | ||
| 42 | public class TencentSmsClient extends AbstractSmsClient { | 43 | public class TencentSmsClient extends AbstractSmsClient { |
| 43 | 44 | ||
| 44 | private SmsClient client; | 45 | private SmsClient client; |
| 45 | 46 | ||
| 46 | public TencentSmsClient(SmsChannelProperties properties) { | 47 | public TencentSmsClient(SmsChannelProperties properties) { |
| 48 | + // TODO @FinallySays:注释的时候,中英文之间要空格哈 | ||
| 47 | // 腾讯云发放短信的时候需要额外的参数sdkAppId, 所以和secretId组合在一起放到apiKey字段中,格式为[secretId sdkAppId], | 49 | // 腾讯云发放短信的时候需要额外的参数sdkAppId, 所以和secretId组合在一起放到apiKey字段中,格式为[secretId sdkAppId], |
| 48 | // 这边需要做拆分重新封装到properties内 | 50 | // 这边需要做拆分重新封装到properties内 |
| 49 | super(TencentSmsChannelProperties.build(properties), new TencentSmsCodeMapping()); | 51 | super(TencentSmsChannelProperties.build(properties), new TencentSmsCodeMapping()); |
| @@ -53,6 +55,7 @@ public class TencentSmsClient extends AbstractSmsClient { | @@ -53,6 +55,7 @@ public class TencentSmsClient extends AbstractSmsClient { | ||
| 53 | @Override | 55 | @Override |
| 54 | protected void doInit() { | 56 | protected void doInit() { |
| 55 | // init或者refresh时需要重新封装properties | 57 | // init或者refresh时需要重新封装properties |
| 58 | + // TODO @FinallySays:是不是不用这个变量呀?p | ||
| 56 | final SmsChannelProperties p = properties; | 59 | final SmsChannelProperties p = properties; |
| 57 | properties = TencentSmsChannelProperties.build(p); | 60 | properties = TencentSmsChannelProperties.build(p); |
| 58 | // 实例化一个认证对象,入参需要传入腾讯云账户密钥对secretId,secretKey。 | 61 | // 实例化一个认证对象,入参需要传入腾讯云账户密钥对secretId,secretKey。 |
| @@ -66,7 +69,7 @@ public class TencentSmsClient extends AbstractSmsClient { | @@ -66,7 +69,7 @@ public class TencentSmsClient extends AbstractSmsClient { | ||
| 66 | String apiTemplateId, | 69 | String apiTemplateId, |
| 67 | List<KeyValue<String, Object>> templateParams) throws Throwable { | 70 | List<KeyValue<String, Object>> templateParams) throws Throwable { |
| 68 | 71 | ||
| 69 | - return invoke(() -> buildSendSmsRequest(sendLogId, mobile, apiTemplateId, templateParams), | 72 | + return invoke(() -> buildSendSmsRequest(sendLogId, mobile, apiTemplateId, templateParams), // TODO @FinallySays:上面不用空行 |
| 70 | this::doSendSms0, | 73 | this::doSendSms0, |
| 71 | response -> { | 74 | response -> { |
| 72 | SendStatus sendStatus = response.getSendStatusSet()[0]; | 75 | SendStatus sendStatus = response.getSendStatusSet()[0]; |
| @@ -117,6 +120,7 @@ public class TencentSmsClient extends AbstractSmsClient { | @@ -117,6 +120,7 @@ public class TencentSmsClient extends AbstractSmsClient { | ||
| 117 | data.setErrorCode(status.getErrCode()).setErrorMsg(status.getDescription()); | 120 | data.setErrorCode(status.getErrCode()).setErrorMsg(status.getDescription()); |
| 118 | data.setReceiveTime(status.getReceiveTime()).setSuccess("SUCCESS".equalsIgnoreCase(status.getStatus())); | 121 | data.setReceiveTime(status.getReceiveTime()).setSuccess("SUCCESS".equalsIgnoreCase(status.getStatus())); |
| 119 | data.setMobile(status.getMobile()).setSerialNo(status.getSerialNo()); | 122 | data.setMobile(status.getMobile()).setSerialNo(status.getSerialNo()); |
| 123 | + // TODO @FinallySays:建议直接判断是否为空,酱紫更易读一些 | ||
| 120 | Optional.ofNullable(status.getSessionContext()).map(SessionContext::getLogId) | 124 | Optional.ofNullable(status.getSessionContext()).map(SessionContext::getLogId) |
| 121 | .ifPresentOrElse(data::setLogId, () -> { | 125 | .ifPresentOrElse(data::setLogId, () -> { |
| 122 | throw new IllegalStateException(StrUtil.format("未回传logId,需联系腾讯云解决。")); | 126 | throw new IllegalStateException(StrUtil.format("未回传logId,需联系腾讯云解决。")); |
| @@ -170,9 +174,10 @@ public class TencentSmsClient extends AbstractSmsClient { | @@ -170,9 +174,10 @@ public class TencentSmsClient extends AbstractSmsClient { | ||
| 170 | */ | 174 | */ |
| 171 | private DescribeSmsTemplateListRequest buildSmsTemplateStatusRequest(String apiTemplateId) { | 175 | private DescribeSmsTemplateListRequest buildSmsTemplateStatusRequest(String apiTemplateId) { |
| 172 | DescribeSmsTemplateListRequest request = new DescribeSmsTemplateListRequest(); | 176 | DescribeSmsTemplateListRequest request = new DescribeSmsTemplateListRequest(); |
| 177 | + // TODO TODO @FinallySays: new Long[]{Long.parseLong(apiTemplateId)} 就 ok 啦 | ||
| 173 | request.setTemplateIdSet(CollectionUtils.toArray(Collections.singletonList(apiTemplateId), Long::parseLong)); | 178 | request.setTemplateIdSet(CollectionUtils.toArray(Collections.singletonList(apiTemplateId), Long::parseLong)); |
| 174 | // 地区 | 179 | // 地区 |
| 175 | - request.setInternational(0L); | 180 | + request.setInternational(0L); // TODO @FinallySays:0L 最好说明下哈; |
| 176 | return request; | 181 | return request; |
| 177 | } | 182 | } |
| 178 | 183 | ||
| @@ -280,9 +285,11 @@ public class TencentSmsClient extends AbstractSmsClient { | @@ -280,9 +285,11 @@ public class TencentSmsClient extends AbstractSmsClient { | ||
| 280 | return (TencentSmsChannelProperties) properties; | 285 | return (TencentSmsChannelProperties) properties; |
| 281 | } | 286 | } |
| 282 | TencentSmsChannelProperties result = BeanUtil.toBean(properties, TencentSmsChannelProperties.class); | 287 | TencentSmsChannelProperties result = BeanUtil.toBean(properties, TencentSmsChannelProperties.class); |
| 288 | + // TODO @FinallySays:comb=》combine,不缩写好点哈 | ||
| 283 | String combKey = properties.getApiKey(); | 289 | String combKey = properties.getApiKey(); |
| 284 | Assert.notEmpty(combKey, "apiKey 不能为空"); | 290 | Assert.notEmpty(combKey, "apiKey 不能为空"); |
| 285 | String[] keys = combKey.trim().split(" "); | 291 | String[] keys = combKey.trim().split(" "); |
| 292 | + // TODO @FinallySays:建议写多个断言好点,嘿嘿。然后 Assert 支持占位符 | ||
| 286 | Assert.isTrue(keys.length == 2 && StrUtil.isNotBlank(keys[0]) && StrUtil.isNotBlank(keys[1]), | 293 | Assert.isTrue(keys.length == 2 && StrUtil.isNotBlank(keys[0]) && StrUtil.isNotBlank(keys[1]), |
| 287 | "腾讯云短信api配置格式错误,请配置为[secretId sdkAppId]"); | 294 | "腾讯云短信api配置格式错误,请配置为[secretId sdkAppId]"); |
| 288 | result.setSdkAppId(keys[1]).setApiKey(keys[0]); | 295 | result.setSdkAppId(keys[1]).setApiKey(keys[0]); |
| @@ -291,4 +298,4 @@ public class TencentSmsClient extends AbstractSmsClient { | @@ -291,4 +298,4 @@ public class TencentSmsClient extends AbstractSmsClient { | ||
| 291 | } | 298 | } |
| 292 | 299 | ||
| 293 | 300 | ||
| 294 | -} | ||
| 295 | \ No newline at end of file | 301 | \ No newline at end of file |
| 302 | +} |