Commit 2e668455843b68cb3452b88ae1a8eed9f1fa9a6c
1 parent
91f758c2
code review 腾讯短信的实现,基本没啥问题了
Showing
2 changed files
with
14 additions
and
9 deletions
yudao-framework/yudao-spring-boot-starter-biz-sms/src/main/java/cn/iocoder/yudao/framework/sms/core/client/impl/tencent/TencentSmsClient.java
| @@ -43,18 +43,19 @@ public class TencentSmsClient extends AbstractSmsClient { | @@ -43,18 +43,19 @@ public class TencentSmsClient extends AbstractSmsClient { | ||
| 43 | private SmsClient client; | 43 | private SmsClient client; |
| 44 | 44 | ||
| 45 | public TencentSmsClient(SmsChannelProperties properties) { | 45 | public TencentSmsClient(SmsChannelProperties properties) { |
| 46 | - // 腾讯云发放短信的时候需要额外的参数 sdkAppId, 所以和 secretId 组合在一起放到 apiKey 字段中,格式为[secretId sdkAppId], | ||
| 47 | - // 这边需要做拆分重新封装到 properties 内 | 46 | + // 腾讯云发放短信的时候,需要额外的参数 sdkAppId。考虑到不破坏原有的 apiKey + apiSecret 的结构,所以将 secretId 拼接到 apiKey 字段中,格式为 "secretId sdkAppId"。 |
| 47 | + // 因此,这边需要使用 TencentSmsChannelProperties 做拆分,重新封装到 properties 内。 | ||
| 48 | super(TencentSmsChannelProperties.build(properties), new TencentSmsCodeMapping()); | 48 | super(TencentSmsChannelProperties.build(properties), new TencentSmsCodeMapping()); |
| 49 | Assert.notEmpty(properties.getApiSecret(), "apiSecret 不能为空"); | 49 | Assert.notEmpty(properties.getApiSecret(), "apiSecret 不能为空"); |
| 50 | } | 50 | } |
| 51 | 51 | ||
| 52 | @Override | 52 | @Override |
| 53 | protected void doInit() { | 53 | protected void doInit() { |
| 54 | - // init 或者 refresh 时需要重新封装 properties | 54 | + // init 或者 refresh 时,需要重新封装 properties |
| 55 | properties = TencentSmsChannelProperties.build(properties); | 55 | properties = TencentSmsChannelProperties.build(properties); |
| 56 | - // 实例化一个认证对象,入参需要传入腾讯云账户密钥对 secretId,secretKey。 | 56 | + // 实例化一个认证对象,入参需要传入腾讯云账户密钥对 secretId,secretKey |
| 57 | Credential credential = new Credential(properties.getApiKey(), properties.getApiSecret()); | 57 | Credential credential = new Credential(properties.getApiKey(), properties.getApiSecret()); |
| 58 | + // TODO @FinallySays:那把 ap-nanjing 枚举下到这个类的静态变量里哈。 | ||
| 58 | client = new SmsClient(credential, "ap-nanjing"); | 59 | client = new SmsClient(credential, "ap-nanjing"); |
| 59 | } | 60 | } |
| 60 | 61 | ||
| @@ -170,6 +171,7 @@ public class TencentSmsClient extends AbstractSmsClient { | @@ -170,6 +171,7 @@ public class TencentSmsClient extends AbstractSmsClient { | ||
| 170 | DescribeSmsTemplateListRequest request = new DescribeSmsTemplateListRequest(); | 171 | DescribeSmsTemplateListRequest request = new DescribeSmsTemplateListRequest(); |
| 171 | request.setTemplateIdSet(new Long[]{Long.parseLong(apiTemplateId)}); | 172 | request.setTemplateIdSet(new Long[]{Long.parseLong(apiTemplateId)}); |
| 172 | // 地区 0:表示国内短信。1:表示国际/港澳台短信。 | 173 | // 地区 0:表示国内短信。1:表示国际/港澳台短信。 |
| 174 | + // TODO @FinallySays:那把 0L 枚举下到这个类的静态变量里哈。 | ||
| 173 | request.setInternational(0L); | 175 | request.setInternational(0L); |
| 174 | return request; | 176 | return request; |
| 175 | } | 177 | } |
| @@ -268,6 +270,7 @@ public class TencentSmsClient extends AbstractSmsClient { | @@ -268,6 +270,7 @@ public class TencentSmsClient extends AbstractSmsClient { | ||
| 268 | R apply(T t) throws TencentCloudSDKException; | 270 | R apply(T t) throws TencentCloudSDKException; |
| 269 | } | 271 | } |
| 270 | 272 | ||
| 273 | + // TODO @FinallySays:要不单独一个类,不用作为内部类哈。这样,可能一看就知道,哟,腾讯短信是特殊的 | ||
| 271 | @Data | 274 | @Data |
| 272 | private static class TencentSmsChannelProperties extends SmsChannelProperties { | 275 | private static class TencentSmsChannelProperties extends SmsChannelProperties { |
| 273 | 276 | ||
| @@ -281,7 +284,7 @@ public class TencentSmsClient extends AbstractSmsClient { | @@ -281,7 +284,7 @@ public class TencentSmsClient extends AbstractSmsClient { | ||
| 281 | String combineKey = properties.getApiKey(); | 284 | String combineKey = properties.getApiKey(); |
| 282 | Assert.notEmpty(combineKey, "apiKey 不能为空"); | 285 | Assert.notEmpty(combineKey, "apiKey 不能为空"); |
| 283 | String[] keys = combineKey.trim().split(" "); | 286 | String[] keys = combineKey.trim().split(" "); |
| 284 | - Assert.isTrue(keys.length == 2, "腾讯云短信 apiKey 配置格式错误,请配置为[secretId sdkAppId]"); | 287 | + Assert.isTrue(keys.length == 2, "腾讯云短信 apiKey 配置格式错误,请配置 为[secretId sdkAppId]"); |
| 285 | Assert.notBlank(keys[0], "腾讯云短信 secretId 不能为空"); | 288 | Assert.notBlank(keys[0], "腾讯云短信 secretId 不能为空"); |
| 286 | Assert.notBlank(keys[1], "腾讯云短信 sdkAppId 不能为空"); | 289 | Assert.notBlank(keys[1], "腾讯云短信 sdkAppId 不能为空"); |
| 287 | result.setSdkAppId(keys[1]).setApiKey(keys[0]); | 290 | result.setSdkAppId(keys[1]).setApiKey(keys[0]); |
yudao-framework/yudao-spring-boot-starter-biz-sms/src/test/java/cn/iocoder/yudao/framework/sms/core/client/impl/tencent/TencentSmsClientTest.java
| @@ -38,7 +38,7 @@ import static org.mockito.Mockito.when; | @@ -38,7 +38,7 @@ import static org.mockito.Mockito.when; | ||
| 38 | /** | 38 | /** |
| 39 | * {@link TencentSmsClient} 的单元测试 | 39 | * {@link TencentSmsClient} 的单元测试 |
| 40 | * | 40 | * |
| 41 | - * @author : shiwp | 41 | + * @author shiwp |
| 42 | */ | 42 | */ |
| 43 | public class TencentSmsClientTest extends BaseMockitoUnitTest { | 43 | public class TencentSmsClientTest extends BaseMockitoUnitTest { |
| 44 | 44 | ||
| @@ -89,7 +89,8 @@ public class TencentSmsClientTest extends BaseMockitoUnitTest { | @@ -89,7 +89,8 @@ public class TencentSmsClientTest extends BaseMockitoUnitTest { | ||
| 89 | assertEquals(mobile, request.getPhoneNumberSet()[0]); | 89 | assertEquals(mobile, request.getPhoneNumberSet()[0]); |
| 90 | assertEquals(properties.getSignature(), request.getSignName()); | 90 | assertEquals(properties.getSignature(), request.getSignName()); |
| 91 | assertEquals(apiTemplateId, request.getTemplateId()); | 91 | assertEquals(apiTemplateId, request.getTemplateId()); |
| 92 | - assertEquals(toJsonString(ArrayUtils.toArray(new ArrayList<>(MapUtils.convertMap(templateParams).values()), String::valueOf)), toJsonString(request.getTemplateParamSet())); | 92 | + assertEquals(toJsonString(ArrayUtils.toArray(new ArrayList<>(MapUtils.convertMap(templateParams).values()), String::valueOf)), |
| 93 | + toJsonString(request.getTemplateParamSet())); | ||
| 93 | assertEquals(sendLogId, ReflectUtil.getFieldValue(JsonUtils.parseObject(request.getSessionContext(), TencentSmsClient.SessionContext.class), "logId")); | 94 | assertEquals(sendLogId, ReflectUtil.getFieldValue(JsonUtils.parseObject(request.getSessionContext(), TencentSmsClient.SessionContext.class), "logId")); |
| 94 | return true; | 95 | return true; |
| 95 | }))).thenReturn(response); | 96 | }))).thenReturn(response); |
| @@ -173,6 +174,7 @@ public class TencentSmsClientTest extends BaseMockitoUnitTest { | @@ -173,6 +174,7 @@ public class TencentSmsClientTest extends BaseMockitoUnitTest { | ||
| 173 | assertEquals(response.getDescribeTemplateStatusSet()[0].getReviewReply(), result.getData().getAuditReason()); | 174 | assertEquals(response.getDescribeTemplateStatusSet()[0].getReviewReply(), result.getData().getAuditReason()); |
| 174 | } | 175 | } |
| 175 | 176 | ||
| 177 | + // TODO @FinallySays:这个单测,按道理说应该是写成 4 个方法,每个对应一种情况。 | ||
| 176 | @Test | 178 | @Test |
| 177 | public void testConvertTemplateStatusDTO() { | 179 | public void testConvertTemplateStatusDTO() { |
| 178 | testTemplateStatus(SmsTemplateAuditStatusEnum.SUCCESS, 0L); | 180 | testTemplateStatus(SmsTemplateAuditStatusEnum.SUCCESS, 0L); |
| @@ -181,6 +183,7 @@ public class TencentSmsClientTest extends BaseMockitoUnitTest { | @@ -181,6 +183,7 @@ public class TencentSmsClientTest extends BaseMockitoUnitTest { | ||
| 181 | DescribeTemplateListStatus templateStatus = new DescribeTemplateListStatus(); | 183 | DescribeTemplateListStatus templateStatus = new DescribeTemplateListStatus(); |
| 182 | templateStatus.setStatusCode(3L); | 184 | templateStatus.setStatusCode(3L); |
| 183 | Long templateId = randomLongId(); | 185 | Long templateId = randomLongId(); |
| 186 | + // 调用,并断言结果 | ||
| 184 | assertThrows(IllegalStateException.class, () -> smsClient.convertTemplateStatusDTO(templateStatus), | 187 | assertThrows(IllegalStateException.class, () -> smsClient.convertTemplateStatusDTO(templateStatus), |
| 185 | StrUtil.format("不能解析短信模版审核状态[3],模版id[{}]", templateId)); | 188 | StrUtil.format("不能解析短信模版审核状态[3],模版id[{}]", templateId)); |
| 186 | } | 189 | } |
| @@ -192,5 +195,4 @@ public class TencentSmsClientTest extends BaseMockitoUnitTest { | @@ -192,5 +195,4 @@ public class TencentSmsClientTest extends BaseMockitoUnitTest { | ||
| 192 | assertEquals(expected.getStatus(), result.getAuditStatus()); | 195 | assertEquals(expected.getStatus(), result.getAuditStatus()); |
| 193 | } | 196 | } |
| 194 | 197 | ||
| 195 | - | ||
| 196 | -} | ||
| 197 | \ No newline at end of file | 198 | \ No newline at end of file |
| 199 | +} |