Commit 6e67120b8e4362616e171ad293a43ce195f3f7cb
Merge branch 'develop' of https://gitee.com/zhijiantianya/ruoyi-vue-pro into master-jdk17
Showing
38 changed files
with
826 additions
and
499 deletions
yudao-dependencies/pom.xml
| @@ -69,9 +69,6 @@ | @@ -69,9 +69,6 @@ | ||
| 69 | <okhttp3.version>4.11.0</okhttp3.version> | 69 | <okhttp3.version>4.11.0</okhttp3.version> |
| 70 | <commons-io.version>2.15.1</commons-io.version> | 70 | <commons-io.version>2.15.1</commons-io.version> |
| 71 | <minio.version>8.5.7</minio.version> | 71 | <minio.version>8.5.7</minio.version> |
| 72 | - <aliyun-java-sdk-core.version>4.6.4</aliyun-java-sdk-core.version> | ||
| 73 | - <aliyun-java-sdk-dysmsapi.version>2.2.1</aliyun-java-sdk-dysmsapi.version> | ||
| 74 | - <tencentcloud-sdk-java.version>3.1.880</tencentcloud-sdk-java.version> | ||
| 75 | <justauth.version>2.0.5</justauth.version> | 72 | <justauth.version>2.0.5</justauth.version> |
| 76 | <jimureport.version>1.7.8</jimureport.version> | 73 | <jimureport.version>1.7.8</jimureport.version> |
| 77 | <xercesImpl.version>2.12.2</xercesImpl.version> | 74 | <xercesImpl.version>2.12.2</xercesImpl.version> |
| @@ -547,34 +544,6 @@ | @@ -547,34 +544,6 @@ | ||
| 547 | <version>${minio.version}</version> | 544 | <version>${minio.version}</version> |
| 548 | </dependency> | 545 | </dependency> |
| 549 | 546 | ||
| 550 | - <!-- SMS SDK begin --> | ||
| 551 | - <dependency> | ||
| 552 | - <groupId>com.aliyun</groupId> | ||
| 553 | - <artifactId>aliyun-java-sdk-core</artifactId> | ||
| 554 | - <version>${aliyun-java-sdk-core.version}</version> | ||
| 555 | - <exclusions> | ||
| 556 | - <exclusion> | ||
| 557 | - <artifactId>opentracing-api</artifactId> | ||
| 558 | - <groupId>io.opentracing</groupId> | ||
| 559 | - </exclusion> | ||
| 560 | - <exclusion> | ||
| 561 | - <artifactId>opentracing-util</artifactId> | ||
| 562 | - <groupId>io.opentracing</groupId> | ||
| 563 | - </exclusion> | ||
| 564 | - </exclusions> | ||
| 565 | - </dependency> | ||
| 566 | - <dependency> | ||
| 567 | - <groupId>com.aliyun</groupId> | ||
| 568 | - <artifactId>aliyun-java-sdk-dysmsapi</artifactId> | ||
| 569 | - <version>${aliyun-java-sdk-dysmsapi.version}</version> | ||
| 570 | - </dependency> | ||
| 571 | - <dependency> | ||
| 572 | - <groupId>com.tencentcloudapi</groupId> | ||
| 573 | - <artifactId>tencentcloud-sdk-java-sms</artifactId> | ||
| 574 | - <version>${tencentcloud-sdk-java.version}</version> | ||
| 575 | - </dependency> | ||
| 576 | - <!-- SMS SDK end --> | ||
| 577 | - | ||
| 578 | <dependency> | 547 | <dependency> |
| 579 | <groupId>com.xingyuv</groupId> | 548 | <groupId>com.xingyuv</groupId> |
| 580 | <artifactId>spring-boot-starter-justauth</artifactId> <!-- 社交登陆(例如说,个人微信、企业微信等等) --> | 549 | <artifactId>spring-boot-starter-justauth</artifactId> <!-- 社交登陆(例如说,个人微信、企业微信等等) --> |
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/http/HttpUtils.java
| @@ -5,6 +5,8 @@ import cn.hutool.core.map.TableMap; | @@ -5,6 +5,8 @@ import cn.hutool.core.map.TableMap; | ||
| 5 | import cn.hutool.core.net.url.UrlBuilder; | 5 | import cn.hutool.core.net.url.UrlBuilder; |
| 6 | import cn.hutool.core.util.ReflectUtil; | 6 | import cn.hutool.core.util.ReflectUtil; |
| 7 | import cn.hutool.core.util.StrUtil; | 7 | import cn.hutool.core.util.StrUtil; |
| 8 | +import cn.hutool.http.HttpRequest; | ||
| 9 | +import cn.hutool.http.HttpResponse; | ||
| 8 | import org.springframework.util.StringUtils; | 10 | import org.springframework.util.StringUtils; |
| 9 | import org.springframework.web.util.UriComponents; | 11 | import org.springframework.web.util.UriComponents; |
| 10 | import org.springframework.web.util.UriComponentsBuilder; | 12 | import org.springframework.web.util.UriComponentsBuilder; |
| @@ -122,5 +124,23 @@ public class HttpUtils { | @@ -122,5 +124,23 @@ public class HttpUtils { | ||
| 122 | return null; | 124 | return null; |
| 123 | } | 125 | } |
| 124 | 126 | ||
| 127 | + /** | ||
| 128 | + * HTTP post 请求,基于 {@link cn.hutool.http.HttpUtil} 实现 | ||
| 129 | + * | ||
| 130 | + * 为什么要封装该方法,因为 HttpUtil 默认封装的方法,没有允许传递 headers 参数 | ||
| 131 | + * | ||
| 132 | + * @param url URL | ||
| 133 | + * @param headers 请求头 | ||
| 134 | + * @param requestBody 请求体 | ||
| 135 | + * @return 请求结果 | ||
| 136 | + */ | ||
| 137 | + public static String post(String url, Map<String, String> headers, String requestBody) { | ||
| 138 | + try (HttpResponse response = HttpRequest.post(url) | ||
| 139 | + .addHeaders(headers) | ||
| 140 | + .body(requestBody) | ||
| 141 | + .execute()) { | ||
| 142 | + return response.body(); | ||
| 143 | + } | ||
| 144 | + } | ||
| 125 | 145 | ||
| 126 | } | 146 | } |
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/spring/SpringExpressionUtils.java
| @@ -3,11 +3,15 @@ package cn.iocoder.yudao.framework.common.util.spring; | @@ -3,11 +3,15 @@ package cn.iocoder.yudao.framework.common.util.spring; | ||
| 3 | import cn.hutool.core.collection.CollUtil; | 3 | import cn.hutool.core.collection.CollUtil; |
| 4 | import cn.hutool.core.map.MapUtil; | 4 | import cn.hutool.core.map.MapUtil; |
| 5 | import cn.hutool.core.util.ArrayUtil; | 5 | import cn.hutool.core.util.ArrayUtil; |
| 6 | +import cn.hutool.core.util.StrUtil; | ||
| 7 | +import cn.hutool.extra.spring.SpringUtil; | ||
| 6 | import org.aspectj.lang.JoinPoint; | 8 | import org.aspectj.lang.JoinPoint; |
| 7 | import org.aspectj.lang.reflect.MethodSignature; | 9 | import org.aspectj.lang.reflect.MethodSignature; |
| 10 | +import org.springframework.context.expression.BeanFactoryResolver; | ||
| 8 | import org.springframework.core.DefaultParameterNameDiscoverer; | 11 | import org.springframework.core.DefaultParameterNameDiscoverer; |
| 9 | import org.springframework.core.ParameterNameDiscoverer; | 12 | import org.springframework.core.ParameterNameDiscoverer; |
| 10 | import org.springframework.expression.EvaluationContext; | 13 | import org.springframework.expression.EvaluationContext; |
| 14 | +import org.springframework.expression.Expression; | ||
| 11 | import org.springframework.expression.ExpressionParser; | 15 | import org.springframework.expression.ExpressionParser; |
| 12 | import org.springframework.expression.spel.standard.SpelExpressionParser; | 16 | import org.springframework.expression.spel.standard.SpelExpressionParser; |
| 13 | import org.springframework.expression.spel.support.StandardEvaluationContext; | 17 | import org.springframework.expression.spel.support.StandardEvaluationContext; |
| @@ -86,4 +90,20 @@ public class SpringExpressionUtils { | @@ -86,4 +90,20 @@ public class SpringExpressionUtils { | ||
| 86 | return result; | 90 | return result; |
| 87 | } | 91 | } |
| 88 | 92 | ||
| 93 | + /** | ||
| 94 | + * 从 Bean 工厂,解析 EL 表达式的结果 | ||
| 95 | + * | ||
| 96 | + * @param expressionString EL 表达式 | ||
| 97 | + * @return 执行界面 | ||
| 98 | + */ | ||
| 99 | + public static Object parseExpression(String expressionString) { | ||
| 100 | + if (StrUtil.isBlank(expressionString)) { | ||
| 101 | + return null; | ||
| 102 | + } | ||
| 103 | + Expression expression = EXPRESSION_PARSER.parseExpression(expressionString); | ||
| 104 | + StandardEvaluationContext context = new StandardEvaluationContext(); | ||
| 105 | + context.setBeanResolver(new BeanFactoryResolver(SpringUtil.getApplicationContext())); | ||
| 106 | + return expression.getValue(context); | ||
| 107 | + } | ||
| 108 | + | ||
| 89 | } | 109 | } |
yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/mapper/BaseMapperX.java
| @@ -185,10 +185,6 @@ public interface BaseMapperX<T> extends MPJBaseMapper<T> { | @@ -185,10 +185,6 @@ public interface BaseMapperX<T> extends MPJBaseMapper<T> { | ||
| 185 | return Db.updateBatchById(entities, size); | 185 | return Db.updateBatchById(entities, size); |
| 186 | } | 186 | } |
| 187 | 187 | ||
| 188 | - default boolean insertOrUpdate(T entity) { | ||
| 189 | - return Db.saveOrUpdate(entity); | ||
| 190 | - } | ||
| 191 | - | ||
| 192 | default Boolean insertOrUpdateBatch(Collection<T> collection) { | 188 | default Boolean insertOrUpdateBatch(Collection<T> collection) { |
| 193 | return Db.saveOrUpdateBatch(collection); | 189 | return Db.saveOrUpdateBatch(collection); |
| 194 | } | 190 | } |
yudao-framework/yudao-spring-boot-starter-web/pom.xml
| @@ -32,6 +32,11 @@ | @@ -32,6 +32,11 @@ | ||
| 32 | <artifactId>spring-boot-configuration-processor</artifactId> | 32 | <artifactId>spring-boot-configuration-processor</artifactId> |
| 33 | <optional>true</optional> | 33 | <optional>true</optional> |
| 34 | </dependency> | 34 | </dependency> |
| 35 | + <dependency> | ||
| 36 | + <groupId>org.aspectj</groupId> | ||
| 37 | + <artifactId>aspectjweaver</artifactId> | ||
| 38 | + <scope>provided</scope> <!-- 解决工具类 SpringExpressionUtils 加载的时候访问不到 org.aspectj.lang.JoinPoint 问题 --> | ||
| 39 | + </dependency> | ||
| 35 | 40 | ||
| 36 | <dependency> | 41 | <dependency> |
| 37 | <groupId>com.github.xiaoymin</groupId> | 42 | <groupId>com.github.xiaoymin</groupId> |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/base/handler/DesensitizationHandler.java
| 1 | package cn.iocoder.yudao.framework.desensitize.core.base.handler; | 1 | package cn.iocoder.yudao.framework.desensitize.core.base.handler; |
| 2 | 2 | ||
| 3 | +import cn.hutool.core.util.ReflectUtil; | ||
| 4 | + | ||
| 3 | import java.lang.annotation.Annotation; | 5 | import java.lang.annotation.Annotation; |
| 4 | 6 | ||
| 5 | /** | 7 | /** |
| @@ -18,4 +20,21 @@ public interface DesensitizationHandler<T extends Annotation> { | @@ -18,4 +20,21 @@ public interface DesensitizationHandler<T extends Annotation> { | ||
| 18 | */ | 20 | */ |
| 19 | String desensitize(String origin, T annotation); | 21 | String desensitize(String origin, T annotation); |
| 20 | 22 | ||
| 23 | + /** | ||
| 24 | + * 是否禁用脱敏的 Spring EL 表达式 | ||
| 25 | + * | ||
| 26 | + * 如果返回 true 则跳过脱敏 | ||
| 27 | + * | ||
| 28 | + * @param annotation 注解信息 | ||
| 29 | + * @return 是否禁用脱敏的 Spring EL 表达式 | ||
| 30 | + */ | ||
| 31 | + default String getDisable(T annotation) { | ||
| 32 | + // 约定:默认就是 enable() 属性。如果不符合,子类重写 | ||
| 33 | + try { | ||
| 34 | + return (String) ReflectUtil.invoke(annotation, "disable"); | ||
| 35 | + } catch (Exception ex) { | ||
| 36 | + return ""; | ||
| 37 | + } | ||
| 38 | + } | ||
| 39 | + | ||
| 21 | } | 40 | } |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/EmailDesensitize.java
| @@ -33,4 +33,12 @@ public @interface EmailDesensitize { | @@ -33,4 +33,12 @@ public @interface EmailDesensitize { | ||
| 33 | * 比如:example@gmail.com 脱敏之后为 e****@gmail.com | 33 | * 比如:example@gmail.com 脱敏之后为 e****@gmail.com |
| 34 | */ | 34 | */ |
| 35 | String replacer() default "$1****$2"; | 35 | String replacer() default "$1****$2"; |
| 36 | + | ||
| 37 | + /** | ||
| 38 | + * 是否禁用脱敏 | ||
| 39 | + * | ||
| 40 | + * 支持 Spring EL 表达式,如果返回 true 则跳过脱敏 | ||
| 41 | + */ | ||
| 42 | + String disable() default ""; | ||
| 43 | + | ||
| 36 | } | 44 | } |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/RegexDesensitize.java
| @@ -35,4 +35,12 @@ public @interface RegexDesensitize { | @@ -35,4 +35,12 @@ public @interface RegexDesensitize { | ||
| 35 | * 脱敏后字符串 ******456789 | 35 | * 脱敏后字符串 ******456789 |
| 36 | */ | 36 | */ |
| 37 | String replacer() default "******"; | 37 | String replacer() default "******"; |
| 38 | + | ||
| 39 | + /** | ||
| 40 | + * 是否禁用脱敏 | ||
| 41 | + * | ||
| 42 | + * 支持 Spring EL 表达式,如果返回 true 则跳过脱敏 | ||
| 43 | + */ | ||
| 44 | + String disable() default ""; | ||
| 45 | + | ||
| 38 | } | 46 | } |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/AbstractRegexDesensitizationHandler.java
| 1 | package cn.iocoder.yudao.framework.desensitize.core.regex.handler; | 1 | package cn.iocoder.yudao.framework.desensitize.core.regex.handler; |
| 2 | 2 | ||
| 3 | +import cn.iocoder.yudao.framework.common.util.spring.SpringExpressionUtils; | ||
| 3 | import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler; | 4 | import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler; |
| 4 | 5 | ||
| 5 | import java.lang.annotation.Annotation; | 6 | import java.lang.annotation.Annotation; |
| @@ -14,6 +15,13 @@ public abstract class AbstractRegexDesensitizationHandler<T extends Annotation> | @@ -14,6 +15,13 @@ public abstract class AbstractRegexDesensitizationHandler<T extends Annotation> | ||
| 14 | 15 | ||
| 15 | @Override | 16 | @Override |
| 16 | public String desensitize(String origin, T annotation) { | 17 | public String desensitize(String origin, T annotation) { |
| 18 | + // 1. 判断是否禁用脱敏 | ||
| 19 | + Object disable = SpringExpressionUtils.parseExpression(getDisable(annotation)); | ||
| 20 | + if (Boolean.TRUE.equals(disable)) { | ||
| 21 | + return origin; | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + // 2. 执行脱敏 | ||
| 17 | String regex = getRegex(annotation); | 25 | String regex = getRegex(annotation); |
| 18 | String replacer = getReplacer(annotation); | 26 | String replacer = getReplacer(annotation); |
| 19 | return origin.replaceAll(regex, replacer); | 27 | return origin.replaceAll(regex, replacer); |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/DefaultRegexDesensitizationHandler.java
| @@ -18,4 +18,10 @@ public class DefaultRegexDesensitizationHandler extends AbstractRegexDesensitiza | @@ -18,4 +18,10 @@ public class DefaultRegexDesensitizationHandler extends AbstractRegexDesensitiza | ||
| 18 | String getReplacer(RegexDesensitize annotation) { | 18 | String getReplacer(RegexDesensitize annotation) { |
| 19 | return annotation.replacer(); | 19 | return annotation.replacer(); |
| 20 | } | 20 | } |
| 21 | + | ||
| 22 | + @Override | ||
| 23 | + public String getDisable(RegexDesensitize annotation) { | ||
| 24 | + return annotation.disable(); | ||
| 25 | + } | ||
| 26 | + | ||
| 21 | } | 27 | } |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/BankCardDesensitize.java
| @@ -37,4 +37,11 @@ public @interface BankCardDesensitize { | @@ -37,4 +37,11 @@ public @interface BankCardDesensitize { | ||
| 37 | */ | 37 | */ |
| 38 | String replacer() default "*"; | 38 | String replacer() default "*"; |
| 39 | 39 | ||
| 40 | + /** | ||
| 41 | + * 是否禁用脱敏 | ||
| 42 | + * | ||
| 43 | + * 支持 Spring EL 表达式,如果返回 true 则跳过脱敏 | ||
| 44 | + */ | ||
| 45 | + String disable() default ""; | ||
| 46 | + | ||
| 40 | } | 47 | } |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/CarLicenseDesensitize.java
| @@ -37,4 +37,11 @@ public @interface CarLicenseDesensitize { | @@ -37,4 +37,11 @@ public @interface CarLicenseDesensitize { | ||
| 37 | */ | 37 | */ |
| 38 | String replacer() default "*"; | 38 | String replacer() default "*"; |
| 39 | 39 | ||
| 40 | + /** | ||
| 41 | + * 是否禁用脱敏 | ||
| 42 | + * | ||
| 43 | + * 支持 Spring EL 表达式,如果返回 true 则跳过脱敏 | ||
| 44 | + */ | ||
| 45 | + String disable() default ""; | ||
| 46 | + | ||
| 40 | } | 47 | } |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/ChineseNameDesensitize.java
| @@ -37,4 +37,11 @@ public @interface ChineseNameDesensitize { | @@ -37,4 +37,11 @@ public @interface ChineseNameDesensitize { | ||
| 37 | */ | 37 | */ |
| 38 | String replacer() default "*"; | 38 | String replacer() default "*"; |
| 39 | 39 | ||
| 40 | + /** | ||
| 41 | + * 是否禁用脱敏 | ||
| 42 | + * | ||
| 43 | + * 支持 Spring EL 表达式,如果返回 true 则跳过脱敏 | ||
| 44 | + */ | ||
| 45 | + String disable() default ""; | ||
| 46 | + | ||
| 40 | } | 47 | } |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/FixedPhoneDesensitize.java
| @@ -37,4 +37,11 @@ public @interface FixedPhoneDesensitize { | @@ -37,4 +37,11 @@ public @interface FixedPhoneDesensitize { | ||
| 37 | */ | 37 | */ |
| 38 | String replacer() default "*"; | 38 | String replacer() default "*"; |
| 39 | 39 | ||
| 40 | + /** | ||
| 41 | + * 是否禁用脱敏 | ||
| 42 | + * | ||
| 43 | + * 支持 Spring EL 表达式,如果返回 true 则跳过脱敏 | ||
| 44 | + */ | ||
| 45 | + String disable() default ""; | ||
| 46 | + | ||
| 40 | } | 47 | } |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/IdCardDesensitize.java
| @@ -37,4 +37,11 @@ public @interface IdCardDesensitize { | @@ -37,4 +37,11 @@ public @interface IdCardDesensitize { | ||
| 37 | */ | 37 | */ |
| 38 | String replacer() default "*"; | 38 | String replacer() default "*"; |
| 39 | 39 | ||
| 40 | + /** | ||
| 41 | + * 是否禁用脱敏 | ||
| 42 | + * | ||
| 43 | + * 支持 Spring EL 表达式,如果返回 true 则跳过脱敏 | ||
| 44 | + */ | ||
| 45 | + String disable() default ""; | ||
| 46 | + | ||
| 40 | } | 47 | } |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/MobileDesensitize.java
| @@ -37,4 +37,11 @@ public @interface MobileDesensitize { | @@ -37,4 +37,11 @@ public @interface MobileDesensitize { | ||
| 37 | */ | 37 | */ |
| 38 | String replacer() default "*"; | 38 | String replacer() default "*"; |
| 39 | 39 | ||
| 40 | + /** | ||
| 41 | + * 是否禁用脱敏 | ||
| 42 | + * | ||
| 43 | + * 支持 Spring EL 表达式,如果返回 true 则跳过脱敏 | ||
| 44 | + */ | ||
| 45 | + String disable() default ""; | ||
| 46 | + | ||
| 40 | } | 47 | } |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/PasswordDesensitize.java
| @@ -39,4 +39,11 @@ public @interface PasswordDesensitize { | @@ -39,4 +39,11 @@ public @interface PasswordDesensitize { | ||
| 39 | */ | 39 | */ |
| 40 | String replacer() default "*"; | 40 | String replacer() default "*"; |
| 41 | 41 | ||
| 42 | + /** | ||
| 43 | + * 是否禁用脱敏 | ||
| 44 | + * | ||
| 45 | + * 支持 Spring EL 表达式,如果返回 true 则跳过脱敏 | ||
| 46 | + */ | ||
| 47 | + String disable() default ""; | ||
| 48 | + | ||
| 42 | } | 49 | } |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/SliderDesensitize.java
| @@ -40,4 +40,12 @@ public @interface SliderDesensitize { | @@ -40,4 +40,12 @@ public @interface SliderDesensitize { | ||
| 40 | * 前缀保留长度 | 40 | * 前缀保留长度 |
| 41 | */ | 41 | */ |
| 42 | int prefixKeep() default 0; | 42 | int prefixKeep() default 0; |
| 43 | + | ||
| 44 | + /** | ||
| 45 | + * 是否禁用脱敏 | ||
| 46 | + * | ||
| 47 | + * 支持 Spring EL 表达式,如果返回 true 则跳过脱敏 | ||
| 48 | + */ | ||
| 49 | + String disable() default ""; | ||
| 50 | + | ||
| 43 | } | 51 | } |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/AbstractSliderDesensitizationHandler.java
| 1 | package cn.iocoder.yudao.framework.desensitize.core.slider.handler; | 1 | package cn.iocoder.yudao.framework.desensitize.core.slider.handler; |
| 2 | 2 | ||
| 3 | +import cn.iocoder.yudao.framework.common.util.spring.SpringExpressionUtils; | ||
| 3 | import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler; | 4 | import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler; |
| 4 | 5 | ||
| 5 | import java.lang.annotation.Annotation; | 6 | import java.lang.annotation.Annotation; |
| @@ -14,6 +15,13 @@ public abstract class AbstractSliderDesensitizationHandler<T extends Annotation> | @@ -14,6 +15,13 @@ public abstract class AbstractSliderDesensitizationHandler<T extends Annotation> | ||
| 14 | 15 | ||
| 15 | @Override | 16 | @Override |
| 16 | public String desensitize(String origin, T annotation) { | 17 | public String desensitize(String origin, T annotation) { |
| 18 | + // 1. 判断是否禁用脱敏 | ||
| 19 | + Object disable = SpringExpressionUtils.parseExpression(getDisable(annotation)); | ||
| 20 | + if (Boolean.FALSE.equals(disable)) { | ||
| 21 | + return origin; | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + // 2. 执行脱敏 | ||
| 17 | int prefixKeep = getPrefixKeep(annotation); | 25 | int prefixKeep = getPrefixKeep(annotation); |
| 18 | int suffixKeep = getSuffixKeep(annotation); | 26 | int suffixKeep = getSuffixKeep(annotation); |
| 19 | String replacer = getReplacer(annotation); | 27 | String replacer = getReplacer(annotation); |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/BankCardDesensitization.java
| @@ -24,4 +24,9 @@ public class BankCardDesensitization extends AbstractSliderDesensitizationHandle | @@ -24,4 +24,9 @@ public class BankCardDesensitization extends AbstractSliderDesensitizationHandle | ||
| 24 | return annotation.replacer(); | 24 | return annotation.replacer(); |
| 25 | } | 25 | } |
| 26 | 26 | ||
| 27 | + @Override | ||
| 28 | + public String getDisable(BankCardDesensitize annotation) { | ||
| 29 | + return ""; | ||
| 30 | + } | ||
| 31 | + | ||
| 27 | } | 32 | } |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/CarLicenseDesensitization.java
| @@ -8,6 +8,7 @@ import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.CarLicenseD | @@ -8,6 +8,7 @@ import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.CarLicenseD | ||
| 8 | * @author gaibu | 8 | * @author gaibu |
| 9 | */ | 9 | */ |
| 10 | public class CarLicenseDesensitization extends AbstractSliderDesensitizationHandler<CarLicenseDesensitize> { | 10 | public class CarLicenseDesensitization extends AbstractSliderDesensitizationHandler<CarLicenseDesensitize> { |
| 11 | + | ||
| 11 | @Override | 12 | @Override |
| 12 | Integer getPrefixKeep(CarLicenseDesensitize annotation) { | 13 | Integer getPrefixKeep(CarLicenseDesensitize annotation) { |
| 13 | return annotation.prefixKeep(); | 14 | return annotation.prefixKeep(); |
| @@ -22,4 +23,10 @@ public class CarLicenseDesensitization extends AbstractSliderDesensitizationHand | @@ -22,4 +23,10 @@ public class CarLicenseDesensitization extends AbstractSliderDesensitizationHand | ||
| 22 | String getReplacer(CarLicenseDesensitize annotation) { | 23 | String getReplacer(CarLicenseDesensitize annotation) { |
| 23 | return annotation.replacer(); | 24 | return annotation.replacer(); |
| 24 | } | 25 | } |
| 26 | + | ||
| 27 | + @Override | ||
| 28 | + public String getDisable(CarLicenseDesensitize annotation) { | ||
| 29 | + return annotation.disable(); | ||
| 30 | + } | ||
| 31 | + | ||
| 25 | } | 32 | } |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/DefaultDesensitizationHandler.java
| @@ -8,6 +8,7 @@ import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.SliderDesen | @@ -8,6 +8,7 @@ import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.SliderDesen | ||
| 8 | * @author gaibu | 8 | * @author gaibu |
| 9 | */ | 9 | */ |
| 10 | public class DefaultDesensitizationHandler extends AbstractSliderDesensitizationHandler<SliderDesensitize> { | 10 | public class DefaultDesensitizationHandler extends AbstractSliderDesensitizationHandler<SliderDesensitize> { |
| 11 | + | ||
| 11 | @Override | 12 | @Override |
| 12 | Integer getPrefixKeep(SliderDesensitize annotation) { | 13 | Integer getPrefixKeep(SliderDesensitize annotation) { |
| 13 | return annotation.prefixKeep(); | 14 | return annotation.prefixKeep(); |
| @@ -22,4 +23,5 @@ public class DefaultDesensitizationHandler extends AbstractSliderDesensitization | @@ -22,4 +23,5 @@ public class DefaultDesensitizationHandler extends AbstractSliderDesensitization | ||
| 22 | String getReplacer(SliderDesensitize annotation) { | 23 | String getReplacer(SliderDesensitize annotation) { |
| 23 | return annotation.replacer(); | 24 | return annotation.replacer(); |
| 24 | } | 25 | } |
| 26 | + | ||
| 25 | } | 27 | } |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/FixedPhoneDesensitization.java
| @@ -8,6 +8,7 @@ import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.FixedPhoneD | @@ -8,6 +8,7 @@ import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.FixedPhoneD | ||
| 8 | * @author gaibu | 8 | * @author gaibu |
| 9 | */ | 9 | */ |
| 10 | public class FixedPhoneDesensitization extends AbstractSliderDesensitizationHandler<FixedPhoneDesensitize> { | 10 | public class FixedPhoneDesensitization extends AbstractSliderDesensitizationHandler<FixedPhoneDesensitize> { |
| 11 | + | ||
| 11 | @Override | 12 | @Override |
| 12 | Integer getPrefixKeep(FixedPhoneDesensitize annotation) { | 13 | Integer getPrefixKeep(FixedPhoneDesensitize annotation) { |
| 13 | return annotation.prefixKeep(); | 14 | return annotation.prefixKeep(); |
| @@ -22,4 +23,5 @@ public class FixedPhoneDesensitization extends AbstractSliderDesensitizationHand | @@ -22,4 +23,5 @@ public class FixedPhoneDesensitization extends AbstractSliderDesensitizationHand | ||
| 22 | String getReplacer(FixedPhoneDesensitize annotation) { | 23 | String getReplacer(FixedPhoneDesensitize annotation) { |
| 23 | return annotation.replacer(); | 24 | return annotation.replacer(); |
| 24 | } | 25 | } |
| 26 | + | ||
| 25 | } | 27 | } |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/IdCardDesensitization.java
| @@ -22,4 +22,5 @@ public class IdCardDesensitization extends AbstractSliderDesensitizationHandler< | @@ -22,4 +22,5 @@ public class IdCardDesensitization extends AbstractSliderDesensitizationHandler< | ||
| 22 | String getReplacer(IdCardDesensitize annotation) { | 22 | String getReplacer(IdCardDesensitize annotation) { |
| 23 | return annotation.replacer(); | 23 | return annotation.replacer(); |
| 24 | } | 24 | } |
| 25 | + | ||
| 25 | } | 26 | } |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/MobileDesensitization.java
| @@ -23,4 +23,5 @@ public class MobileDesensitization extends AbstractSliderDesensitizationHandler< | @@ -23,4 +23,5 @@ public class MobileDesensitization extends AbstractSliderDesensitizationHandler< | ||
| 23 | String getReplacer(MobileDesensitize annotation) { | 23 | String getReplacer(MobileDesensitize annotation) { |
| 24 | return annotation.replacer(); | 24 | return annotation.replacer(); |
| 25 | } | 25 | } |
| 26 | + | ||
| 26 | } | 27 | } |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/PasswordDesensitization.java
| @@ -22,4 +22,5 @@ public class PasswordDesensitization extends AbstractSliderDesensitizationHandle | @@ -22,4 +22,5 @@ public class PasswordDesensitization extends AbstractSliderDesensitizationHandle | ||
| 22 | String getReplacer(PasswordDesensitize annotation) { | 22 | String getReplacer(PasswordDesensitize annotation) { |
| 23 | return annotation.replacer(); | 23 | return annotation.replacer(); |
| 24 | } | 24 | } |
| 25 | + | ||
| 25 | } | 26 | } |
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/controller/app/order/vo/AppTradeOrderSettlementRespVO.java
| @@ -2,11 +2,11 @@ package cn.iocoder.yudao.module.trade.controller.app.order.vo; | @@ -2,11 +2,11 @@ package cn.iocoder.yudao.module.trade.controller.app.order.vo; | ||
| 2 | 2 | ||
| 3 | import cn.iocoder.yudao.module.trade.controller.app.base.property.AppProductPropertyValueDetailRespVO; | 3 | import cn.iocoder.yudao.module.trade.controller.app.base.property.AppProductPropertyValueDetailRespVO; |
| 4 | import io.swagger.v3.oas.annotations.media.Schema; | 4 | import io.swagger.v3.oas.annotations.media.Schema; |
| 5 | +import jakarta.validation.constraints.NotNull; | ||
| 5 | import lombok.AllArgsConstructor; | 6 | import lombok.AllArgsConstructor; |
| 6 | import lombok.Data; | 7 | import lombok.Data; |
| 7 | import lombok.NoArgsConstructor; | 8 | import lombok.NoArgsConstructor; |
| 8 | 9 | ||
| 9 | -import jakarta.validation.constraints.NotNull; | ||
| 10 | import java.util.List; | 10 | import java.util.List; |
| 11 | 11 | ||
| 12 | @Schema(description = "用户 App - 交易订单结算信息 Response VO") | 12 | @Schema(description = "用户 App - 交易订单结算信息 Response VO") |
| @@ -26,7 +26,7 @@ public class AppTradeOrderSettlementRespVO { | @@ -26,7 +26,7 @@ public class AppTradeOrderSettlementRespVO { | ||
| 26 | private Address address; | 26 | private Address address; |
| 27 | 27 | ||
| 28 | @Schema(description = "已使用的积分", requiredMode = Schema.RequiredMode.REQUIRED, example = "10") | 28 | @Schema(description = "已使用的积分", requiredMode = Schema.RequiredMode.REQUIRED, example = "10") |
| 29 | - private Integer usedPoint; | 29 | + private Integer usePoint; |
| 30 | 30 | ||
| 31 | @Schema(description = "总积分", requiredMode = Schema.RequiredMode.REQUIRED, example = "10") | 31 | @Schema(description = "总积分", requiredMode = Schema.RequiredMode.REQUIRED, example = "10") |
| 32 | private Integer totalPoint; | 32 | private Integer totalPoint; |
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/bo/TradePriceCalculateRespBO.java
| @@ -49,12 +49,16 @@ public class TradePriceCalculateRespBO { | @@ -49,12 +49,16 @@ public class TradePriceCalculateRespBO { | ||
| 49 | private Long couponId; | 49 | private Long couponId; |
| 50 | 50 | ||
| 51 | /** | 51 | /** |
| 52 | + * 会员剩余积分 | ||
| 53 | + */ | ||
| 54 | + private Integer totalPoint; | ||
| 55 | + /** | ||
| 52 | * 使用的积分 | 56 | * 使用的积分 |
| 53 | */ | 57 | */ |
| 54 | private Integer usePoint; | 58 | private Integer usePoint; |
| 55 | 59 | ||
| 56 | /** | 60 | /** |
| 57 | - * 使用的积分 | 61 | + * 赠送的积分 |
| 58 | */ | 62 | */ |
| 59 | private Integer givePoint; | 63 | private Integer givePoint; |
| 60 | 64 |
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradeDeliveryPriceCalculator.java
| @@ -55,6 +55,7 @@ public class TradeDeliveryPriceCalculator implements TradePriceCalculator { | @@ -55,6 +55,7 @@ public class TradeDeliveryPriceCalculator implements TradePriceCalculator { | ||
| 55 | if (param.getDeliveryType() == null) { | 55 | if (param.getDeliveryType() == null) { |
| 56 | return; | 56 | return; |
| 57 | } | 57 | } |
| 58 | + // TODO @puhui999:需要校验,是不是存在商品不能门店自提,或者不能快递发货的情况。就是说,配送方式不匹配哈 | ||
| 58 | if (DeliveryTypeEnum.PICK_UP.getType().equals(param.getDeliveryType())) { | 59 | if (DeliveryTypeEnum.PICK_UP.getType().equals(param.getDeliveryType())) { |
| 59 | calculateByPickUp(param); | 60 | calculateByPickUp(param); |
| 60 | } else if (DeliveryTypeEnum.EXPRESS.getType().equals(param.getDeliveryType())) { | 61 | } else if (DeliveryTypeEnum.EXPRESS.getType().equals(param.getDeliveryType())) { |
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradePointUsePriceCalculator.java
| @@ -9,11 +9,11 @@ import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO; | @@ -9,11 +9,11 @@ import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO; | ||
| 9 | import cn.iocoder.yudao.module.promotion.enums.common.PromotionTypeEnum; | 9 | import cn.iocoder.yudao.module.promotion.enums.common.PromotionTypeEnum; |
| 10 | import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateReqBO; | 10 | import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateReqBO; |
| 11 | import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateRespBO; | 11 | import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateRespBO; |
| 12 | +import jakarta.annotation.Resource; | ||
| 12 | import lombok.extern.slf4j.Slf4j; | 13 | import lombok.extern.slf4j.Slf4j; |
| 13 | import org.springframework.core.annotation.Order; | 14 | import org.springframework.core.annotation.Order; |
| 14 | import org.springframework.stereotype.Component; | 15 | import org.springframework.stereotype.Component; |
| 15 | 16 | ||
| 16 | -import jakarta.annotation.Resource; | ||
| 17 | import java.util.List; | 17 | import java.util.List; |
| 18 | 18 | ||
| 19 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | 19 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| @@ -37,11 +37,12 @@ public class TradePointUsePriceCalculator implements TradePriceCalculator { | @@ -37,11 +37,12 @@ public class TradePointUsePriceCalculator implements TradePriceCalculator { | ||
| 37 | 37 | ||
| 38 | @Override | 38 | @Override |
| 39 | public void calculate(TradePriceCalculateReqBO param, TradePriceCalculateRespBO result) { | 39 | public void calculate(TradePriceCalculateReqBO param, TradePriceCalculateRespBO result) { |
| 40 | - // 默认使用积分为 0 | ||
| 41 | - result.setUsePoint(0); | 40 | + // 0. 初始化积分 |
| 41 | + MemberUserRespDTO user = memberUserApi.getUser(param.getUserId()); | ||
| 42 | + result.setTotalPoint(user.getPoint()).setUsePoint(0); | ||
| 43 | + | ||
| 42 | // 1.1 校验是否使用积分 | 44 | // 1.1 校验是否使用积分 |
| 43 | if (!BooleanUtil.isTrue(param.getPointStatus())) { | 45 | if (!BooleanUtil.isTrue(param.getPointStatus())) { |
| 44 | - result.setUsePoint(0); | ||
| 45 | return; | 46 | return; |
| 46 | } | 47 | } |
| 47 | // 1.2 校验积分抵扣是否开启 | 48 | // 1.2 校验积分抵扣是否开启 |
| @@ -50,7 +51,6 @@ public class TradePointUsePriceCalculator implements TradePriceCalculator { | @@ -50,7 +51,6 @@ public class TradePointUsePriceCalculator implements TradePriceCalculator { | ||
| 50 | return; | 51 | return; |
| 51 | } | 52 | } |
| 52 | // 1.3 校验用户积分余额 | 53 | // 1.3 校验用户积分余额 |
| 53 | - MemberUserRespDTO user = memberUserApi.getUser(param.getUserId()); | ||
| 54 | if (user.getPoint() == null || user.getPoint() <= 0) { | 54 | if (user.getPoint() == null || user.getPoint() <= 0) { |
| 55 | return; | 55 | return; |
| 56 | } | 56 | } |
yudao-module-system/yudao-module-system-biz/pom.xml
| @@ -111,19 +111,6 @@ | @@ -111,19 +111,6 @@ | ||
| 111 | </dependency> | 111 | </dependency> |
| 112 | 112 | ||
| 113 | <dependency> | 113 | <dependency> |
| 114 | - <groupId>com.aliyun</groupId> | ||
| 115 | - <artifactId>aliyun-java-sdk-core</artifactId> <!-- 短信(阿里云) --> | ||
| 116 | - </dependency> | ||
| 117 | - <dependency> | ||
| 118 | - <groupId>com.aliyun</groupId> | ||
| 119 | - <artifactId>aliyun-java-sdk-dysmsapi</artifactId> <!-- 短信(阿里云) --> | ||
| 120 | - </dependency> | ||
| 121 | - <dependency> | ||
| 122 | - <groupId>com.tencentcloudapi</groupId> | ||
| 123 | - <artifactId>tencentcloud-sdk-java-sms</artifactId> <!-- 短信(腾讯云) --> | ||
| 124 | - </dependency> | ||
| 125 | - | ||
| 126 | - <dependency> | ||
| 127 | <groupId>com.xingyuv</groupId> | 114 | <groupId>com.xingyuv</groupId> |
| 128 | <artifactId>spring-boot-starter-captcha-plus</artifactId> <!-- 验证码,一般用于登录使用 --> | 115 | <artifactId>spring-boot-starter-captcha-plus</artifactId> <!-- 验证码,一般用于登录使用 --> |
| 129 | </dependency> | 116 | </dependency> |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/sms/core/client/SmsClient.java
| @@ -46,6 +46,8 @@ public interface SmsClient { | @@ -46,6 +46,8 @@ public interface SmsClient { | ||
| 46 | /** | 46 | /** |
| 47 | * 查询指定的短信模板 | 47 | * 查询指定的短信模板 |
| 48 | * | 48 | * |
| 49 | + * 如果查询失败,则返回 null 空 | ||
| 50 | + * | ||
| 49 | * @param apiTemplateId 短信 API 的模板编号 | 51 | * @param apiTemplateId 短信 API 的模板编号 |
| 50 | * @return 短信模板 | 52 | * @return 短信模板 |
| 51 | */ | 53 | */ |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/sms/core/client/impl/AliyunSmsClient.java
| 1 | package cn.iocoder.yudao.module.system.framework.sms.core.client.impl; | 1 | package cn.iocoder.yudao.module.system.framework.sms.core.client.impl; |
| 2 | 2 | ||
| 3 | +import cn.hutool.core.date.format.FastDateFormat; | ||
| 3 | import cn.hutool.core.lang.Assert; | 4 | import cn.hutool.core.lang.Assert; |
| 5 | +import cn.hutool.core.util.IdUtil; | ||
| 6 | +import cn.hutool.core.util.ObjectUtil; | ||
| 7 | +import cn.hutool.crypto.SecureUtil; | ||
| 8 | +import cn.hutool.crypto.digest.DigestUtil; | ||
| 9 | +import cn.hutool.json.JSONArray; | ||
| 10 | +import cn.hutool.json.JSONObject; | ||
| 11 | +import cn.hutool.json.JSONUtil; | ||
| 4 | import cn.iocoder.yudao.framework.common.core.KeyValue; | 12 | import cn.iocoder.yudao.framework.common.core.KeyValue; |
| 5 | import cn.iocoder.yudao.framework.common.util.collection.MapUtils; | 13 | import cn.iocoder.yudao.framework.common.util.collection.MapUtils; |
| 14 | +import cn.iocoder.yudao.framework.common.util.http.HttpUtils; | ||
| 6 | import cn.iocoder.yudao.framework.common.util.json.JsonUtils; | 15 | import cn.iocoder.yudao.framework.common.util.json.JsonUtils; |
| 7 | import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsReceiveRespDTO; | 16 | import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsReceiveRespDTO; |
| 8 | import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsSendRespDTO; | 17 | import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsSendRespDTO; |
| 9 | import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsTemplateRespDTO; | 18 | import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsTemplateRespDTO; |
| 10 | import cn.iocoder.yudao.module.system.framework.sms.core.enums.SmsTemplateAuditStatusEnum; | 19 | import cn.iocoder.yudao.module.system.framework.sms.core.enums.SmsTemplateAuditStatusEnum; |
| 11 | import cn.iocoder.yudao.module.system.framework.sms.core.property.SmsChannelProperties; | 20 | import cn.iocoder.yudao.module.system.framework.sms.core.property.SmsChannelProperties; |
| 12 | -import com.aliyuncs.DefaultAcsClient; | ||
| 13 | -import com.aliyuncs.IAcsClient; | ||
| 14 | -import com.aliyuncs.dysmsapi.model.v20170525.QuerySmsTemplateRequest; | ||
| 15 | -import com.aliyuncs.dysmsapi.model.v20170525.QuerySmsTemplateResponse; | ||
| 16 | -import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest; | ||
| 17 | -import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse; | ||
| 18 | -import com.aliyuncs.profile.DefaultProfile; | ||
| 19 | -import com.aliyuncs.profile.IClientProfile; | ||
| 20 | -import com.fasterxml.jackson.annotation.JsonFormat; | ||
| 21 | -import com.fasterxml.jackson.annotation.JsonProperty; | ||
| 22 | import com.google.common.annotations.VisibleForTesting; | 21 | import com.google.common.annotations.VisibleForTesting; |
| 23 | -import lombok.Data; | 22 | +import lombok.SneakyThrows; |
| 24 | import lombok.extern.slf4j.Slf4j; | 23 | import lombok.extern.slf4j.Slf4j; |
| 25 | 24 | ||
| 26 | -import java.time.LocalDateTime; | ||
| 27 | -import java.util.List; | ||
| 28 | -import java.util.Objects; | 25 | +import java.net.URLEncoder; |
| 26 | +import java.nio.charset.StandardCharsets; | ||
| 27 | +import java.util.*; | ||
| 28 | +import java.util.stream.Collectors; | ||
| 29 | 29 | ||
| 30 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; | 30 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; |
| 31 | -import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND; | ||
| 32 | -import static cn.iocoder.yudao.framework.common.util.date.DateUtils.TIME_ZONE_DEFAULT; | ||
| 33 | 31 | ||
| 34 | /** | 32 | /** |
| 35 | * 阿里短信客户端的实现类 | 33 | * 阿里短信客户端的实现类 |
| @@ -40,20 +38,11 @@ import static cn.iocoder.yudao.framework.common.util.date.DateUtils.TIME_ZONE_DE | @@ -40,20 +38,11 @@ import static cn.iocoder.yudao.framework.common.util.date.DateUtils.TIME_ZONE_DE | ||
| 40 | @Slf4j | 38 | @Slf4j |
| 41 | public class AliyunSmsClient extends AbstractSmsClient { | 39 | public class AliyunSmsClient extends AbstractSmsClient { |
| 42 | 40 | ||
| 43 | - /** | ||
| 44 | - * 调用成功 code | ||
| 45 | - */ | ||
| 46 | - public static final String API_CODE_SUCCESS = "OK"; | ||
| 47 | - | ||
| 48 | - /** | ||
| 49 | - * REGION, 使用杭州 | ||
| 50 | - */ | ||
| 51 | - private static final String ENDPOINT = "cn-hangzhou"; | 41 | + private static final String URL = "https://dysmsapi.aliyuncs.com"; |
| 42 | + private static final String HOST = "dysmsapi.aliyuncs.com"; | ||
| 43 | + private static final String VERSION = "2017-05-25"; | ||
| 52 | 44 | ||
| 53 | - /** | ||
| 54 | - * 阿里云客户端 | ||
| 55 | - */ | ||
| 56 | - private volatile IAcsClient client; | 45 | + private static final String RESPONSE_CODE_SUCCESS = "OK"; |
| 57 | 46 | ||
| 58 | public AliyunSmsClient(SmsChannelProperties properties) { | 47 | public AliyunSmsClient(SmsChannelProperties properties) { |
| 59 | super(properties); | 48 | super(properties); |
| @@ -63,47 +52,70 @@ public class AliyunSmsClient extends AbstractSmsClient { | @@ -63,47 +52,70 @@ public class AliyunSmsClient extends AbstractSmsClient { | ||
| 63 | 52 | ||
| 64 | @Override | 53 | @Override |
| 65 | protected void doInit() { | 54 | protected void doInit() { |
| 66 | - IClientProfile profile = DefaultProfile.getProfile(ENDPOINT, properties.getApiKey(), properties.getApiSecret()); | ||
| 67 | - client = new DefaultAcsClient(profile); | ||
| 68 | } | 55 | } |
| 69 | 56 | ||
| 70 | @Override | 57 | @Override |
| 71 | public SmsSendRespDTO sendSms(Long sendLogId, String mobile, String apiTemplateId, | 58 | public SmsSendRespDTO sendSms(Long sendLogId, String mobile, String apiTemplateId, |
| 72 | List<KeyValue<String, Object>> templateParams) throws Throwable { | 59 | List<KeyValue<String, Object>> templateParams) throws Throwable { |
| 73 | - // 构建请求 | ||
| 74 | - SendSmsRequest request = new SendSmsRequest(); | ||
| 75 | - request.setPhoneNumbers(mobile); | ||
| 76 | - request.setSignName(properties.getSignature()); | ||
| 77 | - request.setTemplateCode(apiTemplateId); | ||
| 78 | - request.setTemplateParam(JsonUtils.toJsonString(MapUtils.convertMap(templateParams))); | ||
| 79 | - request.setOutId(String.valueOf(sendLogId)); | ||
| 80 | - // 执行请求 | ||
| 81 | - SendSmsResponse response = client.getAcsResponse(request); | ||
| 82 | - return new SmsSendRespDTO().setSuccess(Objects.equals(response.getCode(), API_CODE_SUCCESS)).setSerialNo(response.getBizId()) | ||
| 83 | - .setApiRequestId(response.getRequestId()).setApiCode(response.getCode()).setApiMsg(response.getMessage()); | 60 | + Assert.notBlank(properties.getSignature(), "短信签名不能为空"); |
| 61 | + // 1. 执行请求 | ||
| 62 | + // 参考链接 https://api.aliyun.com/document/Dysmsapi/2017-05-25/SendSms | ||
| 63 | + TreeMap<String, Object> queryParam = new TreeMap<>(); | ||
| 64 | + queryParam.put("PhoneNumbers", mobile); | ||
| 65 | + queryParam.put("SignName", properties.getSignature()); | ||
| 66 | + queryParam.put("TemplateCode", apiTemplateId); | ||
| 67 | + queryParam.put("TemplateParam", JsonUtils.toJsonString(MapUtils.convertMap(templateParams))); | ||
| 68 | + queryParam.put("OutId", sendLogId); | ||
| 69 | + JSONObject response = request("SendSms", queryParam); | ||
| 70 | + | ||
| 71 | + // 2. 解析请求 | ||
| 72 | + return new SmsSendRespDTO() | ||
| 73 | + .setSuccess(Objects.equals(response.getStr("Code"), RESPONSE_CODE_SUCCESS)) | ||
| 74 | + .setSerialNo(response.getStr("BizId")) | ||
| 75 | + .setApiRequestId(response.getStr("RequestId")) | ||
| 76 | + .setApiCode(response.getStr("Code")) | ||
| 77 | + .setApiMsg(response.getStr("Message")); | ||
| 84 | } | 78 | } |
| 85 | 79 | ||
| 86 | @Override | 80 | @Override |
| 87 | public List<SmsReceiveRespDTO> parseSmsReceiveStatus(String text) { | 81 | public List<SmsReceiveRespDTO> parseSmsReceiveStatus(String text) { |
| 88 | - List<SmsReceiveStatus> statuses = JsonUtils.parseArray(text, SmsReceiveStatus.class); | ||
| 89 | - return convertList(statuses, status -> new SmsReceiveRespDTO().setSuccess(status.getSuccess()) | ||
| 90 | - .setErrorCode(status.getErrCode()).setErrorMsg(status.getErrMsg()) | ||
| 91 | - .setMobile(status.getPhoneNumber()).setReceiveTime(status.getReportTime()) | ||
| 92 | - .setSerialNo(status.getBizId()).setLogId(Long.valueOf(status.getOutId()))); | 82 | + JSONArray statuses = JSONUtil.parseArray(text); |
| 83 | + // 字段参考 | ||
| 84 | + return convertList(statuses, status -> { | ||
| 85 | + JSONObject statusObj = (JSONObject) status; | ||
| 86 | + return new SmsReceiveRespDTO() | ||
| 87 | + .setSuccess(statusObj.getBool("success")) // 是否接收成功 | ||
| 88 | + .setErrorCode(statusObj.getStr("err_code")) // 状态报告编码 | ||
| 89 | + .setErrorMsg(statusObj.getStr("err_msg")) // 状态报告说明 | ||
| 90 | + .setMobile(statusObj.getStr("phone_number")) // 手机号 | ||
| 91 | + .setReceiveTime(statusObj.getLocalDateTime("report_time", null)) // 状态报告时间 | ||
| 92 | + .setSerialNo(statusObj.getStr("biz_id")) // 发送序列号 | ||
| 93 | + .setLogId(statusObj.getLong("out_id")); // 用户序列号 | ||
| 94 | + }); | ||
| 93 | } | 95 | } |
| 94 | 96 | ||
| 95 | @Override | 97 | @Override |
| 96 | public SmsTemplateRespDTO getSmsTemplate(String apiTemplateId) throws Throwable { | 98 | public SmsTemplateRespDTO getSmsTemplate(String apiTemplateId) throws Throwable { |
| 97 | - // 构建请求 | ||
| 98 | - QuerySmsTemplateRequest request = new QuerySmsTemplateRequest(); | ||
| 99 | - request.setTemplateCode(apiTemplateId); | ||
| 100 | - // 执行请求 | ||
| 101 | - QuerySmsTemplateResponse response = client.getAcsResponse(request); | ||
| 102 | - if (response.getTemplateStatus() == null) { | 99 | + // 1. 执行请求 |
| 100 | + // 参考链接 https://api.aliyun.com/document/Dysmsapi/2017-05-25/QuerySmsTemplate | ||
| 101 | + TreeMap<String, Object> queryParam = new TreeMap<>(); | ||
| 102 | + queryParam.put("TemplateCode", apiTemplateId); | ||
| 103 | + JSONObject response = request("QuerySmsTemplate", queryParam); | ||
| 104 | + | ||
| 105 | + System.out.println("getSmsTemplate response is =====" + response.toString()); | ||
| 106 | + | ||
| 107 | + // 2.1 请求失败 | ||
| 108 | + String code = response.getStr("Code"); | ||
| 109 | + if (ObjectUtil.notEqual(code, RESPONSE_CODE_SUCCESS)) { | ||
| 110 | + log.error("[getSmsTemplate][模版编号({}) 响应不正确({})]", apiTemplateId, response); | ||
| 103 | return null; | 111 | return null; |
| 104 | } | 112 | } |
| 105 | - return new SmsTemplateRespDTO().setId(response.getTemplateCode()).setContent(response.getTemplateContent()) | ||
| 106 | - .setAuditStatus(convertSmsTemplateAuditStatus(response.getTemplateStatus())).setAuditReason(response.getReason()); | 113 | + // 2.2 请求成功 |
| 114 | + return new SmsTemplateRespDTO() | ||
| 115 | + .setId(response.getStr("TemplateCode")) | ||
| 116 | + .setContent(response.getStr("TemplateContent")) | ||
| 117 | + .setAuditStatus(convertSmsTemplateAuditStatus(response.getInt("TemplateStatus"))) | ||
| 118 | + .setAuditReason(response.getStr("Reason")); | ||
| 107 | } | 119 | } |
| 108 | 120 | ||
| 109 | @VisibleForTesting | 121 | @VisibleForTesting |
| @@ -117,66 +129,71 @@ public class AliyunSmsClient extends AbstractSmsClient { | @@ -117,66 +129,71 @@ public class AliyunSmsClient extends AbstractSmsClient { | ||
| 117 | } | 129 | } |
| 118 | 130 | ||
| 119 | /** | 131 | /** |
| 120 | - * 短信接收状态 | ||
| 121 | - * | ||
| 122 | - * 参见 <a href="https://help.aliyun.com/document_detail/101867.html">文档</a> | 132 | + * 请求阿里云短信 |
| 123 | * | 133 | * |
| 124 | - * @author 芋道源码 | 134 | + * @see <a href="https://help.aliyun.com/zh/sdk/product-overview/v3-request-structure-and-signature">V3 版本请求体&签名机制</> |
| 135 | + * @param apiName 请求的 API 名称 | ||
| 136 | + * @param queryParams 请求参数 | ||
| 137 | + * @return 请求结果 | ||
| 125 | */ | 138 | */ |
| 126 | - @Data | ||
| 127 | - public static class SmsReceiveStatus { | ||
| 128 | - | ||
| 129 | - /** | ||
| 130 | - * 手机号 | ||
| 131 | - */ | ||
| 132 | - @JsonProperty("phone_number") | ||
| 133 | - private String phoneNumber; | ||
| 134 | - /** | ||
| 135 | - * 发送时间 | ||
| 136 | - */ | ||
| 137 | - @JsonProperty("send_time") | ||
| 138 | - @JsonFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND, timezone = TIME_ZONE_DEFAULT) | ||
| 139 | - private LocalDateTime sendTime; | ||
| 140 | - /** | ||
| 141 | - * 状态报告时间 | ||
| 142 | - */ | ||
| 143 | - @JsonProperty("report_time") | ||
| 144 | - @JsonFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND, timezone = TIME_ZONE_DEFAULT) | ||
| 145 | - private LocalDateTime reportTime; | ||
| 146 | - /** | ||
| 147 | - * 是否接收成功 | ||
| 148 | - */ | ||
| 149 | - private Boolean success; | ||
| 150 | - /** | ||
| 151 | - * 状态报告说明 | ||
| 152 | - */ | ||
| 153 | - @JsonProperty("err_msg") | ||
| 154 | - private String errMsg; | ||
| 155 | - /** | ||
| 156 | - * 状态报告编码 | ||
| 157 | - */ | ||
| 158 | - @JsonProperty("err_code") | ||
| 159 | - private String errCode; | ||
| 160 | - /** | ||
| 161 | - * 发送序列号 | ||
| 162 | - */ | ||
| 163 | - @JsonProperty("biz_id") | ||
| 164 | - private String bizId; | ||
| 165 | - /** | ||
| 166 | - * 用户序列号 | ||
| 167 | - * | ||
| 168 | - * 这里我们传递的是 SysSmsLogDO 的日志编号 | ||
| 169 | - */ | ||
| 170 | - @JsonProperty("out_id") | ||
| 171 | - private String outId; | ||
| 172 | - /** | ||
| 173 | - * 短信长度,例如说 1、2、3 | ||
| 174 | - * | ||
| 175 | - * 140 字节算一条短信,短信长度超过 140 字节时会拆分成多条短信发送 | ||
| 176 | - */ | ||
| 177 | - @JsonProperty("sms_size") | ||
| 178 | - private Integer smsSize; | 139 | + private JSONObject request(String apiName, TreeMap<String, Object> queryParams) { |
| 140 | + // 1. 请求参数 | ||
| 141 | + String queryString = queryParams.entrySet().stream() | ||
| 142 | + .map(entry -> percentCode(entry.getKey()) + "=" + percentCode(String.valueOf(entry.getValue()))) | ||
| 143 | + .collect(Collectors.joining("&")); | ||
| 144 | + | ||
| 145 | + // 2.1 请求 Header | ||
| 146 | + TreeMap<String, String> headers = new TreeMap<>(); | ||
| 147 | + headers.put("host", HOST); | ||
| 148 | + headers.put("x-acs-version", VERSION); | ||
| 149 | + headers.put("x-acs-action", apiName); | ||
| 150 | + headers.put("x-acs-date", FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone("GMT")).format(new Date())); | ||
| 151 | + headers.put("x-acs-signature-nonce", IdUtil.randomUUID()); | ||
| 152 | + | ||
| 153 | + // 2.2 构建签名 Header | ||
| 154 | + StringBuilder canonicalHeaders = new StringBuilder(); // 构造请求头,多个规范化消息头,按照消息头名称(小写)的字符代码顺序以升序排列后拼接在一起 | ||
| 155 | + StringBuilder signedHeadersBuilder = new StringBuilder(); // 已签名消息头列表,多个请求头名称(小写)按首字母升序排列并以英文分号(;)分隔 | ||
| 156 | + headers.entrySet().stream().filter(entry -> entry.getKey().toLowerCase().startsWith("x-acs-") | ||
| 157 | + || entry.getKey().equalsIgnoreCase("host") | ||
| 158 | + || entry.getKey().equalsIgnoreCase("content-type")) | ||
| 159 | + .sorted(Map.Entry.comparingByKey()).forEach(entry -> { | ||
| 160 | + String lowerKey = entry.getKey().toLowerCase(); | ||
| 161 | + canonicalHeaders.append(lowerKey).append(":").append(String.valueOf(entry.getValue()).trim()).append("\n"); | ||
| 162 | + signedHeadersBuilder.append(lowerKey).append(";"); | ||
| 163 | + }); | ||
| 164 | + String signedHeaders = signedHeadersBuilder.substring(0, signedHeadersBuilder.length() - 1); | ||
| 165 | + | ||
| 166 | + // 3. 请求 Body | ||
| 167 | + String requestBody = ""; // 短信 API 为 RPC 接口,query parameters 在 uri 中拼接,因此 request body 如果没有特殊要求,设置为空。 | ||
| 168 | + String hashedRequestBody = DigestUtil.sha256Hex(requestBody); | ||
| 169 | + | ||
| 170 | + // 4. 构建 Authorization 签名 | ||
| 171 | + String canonicalRequest = "POST" + "\n" + "/" + "\n" + queryString + "\n" + canonicalHeaders + "\n" + signedHeaders + "\n" + hashedRequestBody; | ||
| 172 | + String hashedCanonicalRequest = DigestUtil.sha256Hex(canonicalRequest); | ||
| 173 | + | ||
| 174 | + String stringToSign = "ACS3-HMAC-SHA256" + "\n" + hashedCanonicalRequest; | ||
| 175 | + String signature = SecureUtil.hmacSha256(properties.getApiSecret()).digestHex(stringToSign); // 计算签名 | ||
| 176 | + headers.put("Authorization", "ACS3-HMAC-SHA256" + " " + "Credential=" + properties.getApiKey() | ||
| 177 | + + ", " + "SignedHeaders=" + signedHeaders + ", " + "Signature=" + signature); | ||
| 178 | + | ||
| 179 | + // 5. 发起请求 | ||
| 180 | + String responseBody = HttpUtils.post(URL + "?" + queryString, headers, requestBody); | ||
| 181 | + return JSONUtil.parseObj(responseBody); | ||
| 182 | + } | ||
| 179 | 183 | ||
| 184 | + /** | ||
| 185 | + * 对指定的字符串进行 URL 编码,并对特定的字符进行替换,以符合URL编码规范 | ||
| 186 | + * | ||
| 187 | + * @param str 需要进行 URL 编码的字符串 | ||
| 188 | + * @return 编码后的字符串 | ||
| 189 | + */ | ||
| 190 | + @SneakyThrows | ||
| 191 | + private static String percentCode(String str) { | ||
| 192 | + Assert.notNull(str, "str 不能为空"); | ||
| 193 | + return URLEncoder.encode(str, StandardCharsets.UTF_8.name()) | ||
| 194 | + .replace("+", "%20") // 加号 "+" 被替换为 "%20" | ||
| 195 | + .replace("*", "%2A") // 星号 "*" 被替换为 "%2A" | ||
| 196 | + .replace("%7E", "~"); // 波浪号 "%7E" 被替换为 "~" | ||
| 180 | } | 197 | } |
| 181 | 198 | ||
| 182 | -} | 199 | -} |
| 200 | +} | ||
| 183 | \ No newline at end of file | 201 | \ No newline at end of file |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/sms/core/client/impl/HuaweiSmsClient.java
| 1 | package cn.iocoder.yudao.module.system.framework.sms.core.client.impl; | 1 | package cn.iocoder.yudao.module.system.framework.sms.core.client.impl; |
| 2 | 2 | ||
| 3 | + | ||
| 3 | import cn.hutool.core.lang.Assert; | 4 | import cn.hutool.core.lang.Assert; |
| 4 | -import cn.hutool.core.util.HexUtil; | ||
| 5 | import cn.hutool.core.util.StrUtil; | 5 | import cn.hutool.core.util.StrUtil; |
| 6 | + | ||
| 6 | import cn.hutool.crypto.SecureUtil; | 7 | import cn.hutool.crypto.SecureUtil; |
| 7 | -import cn.hutool.crypto.digest.DigestUtil; | ||
| 8 | -import cn.hutool.json.JSONArray; | 8 | +import cn.hutool.http.HttpRequest; |
| 9 | +import cn.hutool.http.HttpResponse; | ||
| 10 | +import cn.hutool.json.JSONObject; | ||
| 11 | +import cn.hutool.json.JSONUtil; | ||
| 9 | import cn.iocoder.yudao.framework.common.core.KeyValue; | 12 | import cn.iocoder.yudao.framework.common.core.KeyValue; |
| 13 | +import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; | ||
| 10 | import cn.iocoder.yudao.framework.common.util.json.JsonUtils; | 14 | import cn.iocoder.yudao.framework.common.util.json.JsonUtils; |
| 11 | import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsReceiveRespDTO; | 15 | import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsReceiveRespDTO; |
| 12 | import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsSendRespDTO; | 16 | import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsSendRespDTO; |
| 13 | import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsTemplateRespDTO; | 17 | import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsTemplateRespDTO; |
| 14 | import cn.iocoder.yudao.module.system.framework.sms.core.enums.SmsTemplateAuditStatusEnum; | 18 | import cn.iocoder.yudao.module.system.framework.sms.core.enums.SmsTemplateAuditStatusEnum; |
| 15 | import cn.iocoder.yudao.module.system.framework.sms.core.property.SmsChannelProperties; | 19 | import cn.iocoder.yudao.module.system.framework.sms.core.property.SmsChannelProperties; |
| 20 | + | ||
| 16 | import com.fasterxml.jackson.annotation.JsonFormat; | 21 | import com.fasterxml.jackson.annotation.JsonFormat; |
| 17 | import com.fasterxml.jackson.annotation.JsonProperty; | 22 | import com.fasterxml.jackson.annotation.JsonProperty; |
| 18 | import lombok.Data; | 23 | import lombok.Data; |
| 19 | import lombok.extern.slf4j.Slf4j; | 24 | import lombok.extern.slf4j.Slf4j; |
| 20 | -import org.apache.http.HttpResponse; | ||
| 21 | -import org.apache.http.client.methods.HttpUriRequest; | ||
| 22 | -import org.apache.http.client.methods.RequestBuilder; | ||
| 23 | -import org.apache.http.entity.StringEntity; | ||
| 24 | -import org.apache.http.impl.client.CloseableHttpClient; | ||
| 25 | -import org.apache.http.impl.client.HttpClientBuilder; | ||
| 26 | 25 | ||
| 26 | +import java.io.UnsupportedEncodingException; | ||
| 27 | import java.net.URLEncoder; | 27 | import java.net.URLEncoder; |
| 28 | -import java.nio.charset.StandardCharsets; | ||
| 29 | import java.text.SimpleDateFormat; | 28 | import java.text.SimpleDateFormat; |
| 30 | -import java.time.LocalDateTime; | ||
| 31 | import java.util.*; | 29 | import java.util.*; |
| 32 | 30 | ||
| 31 | + | ||
| 32 | +import java.time.LocalDateTime; | ||
| 33 | + | ||
| 34 | + | ||
| 35 | +import static cn.hutool.crypto.digest.DigestUtil.sha256Hex; | ||
| 33 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; | 36 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; |
| 34 | import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND; | 37 | import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND; |
| 35 | import static cn.iocoder.yudao.framework.common.util.date.DateUtils.TIME_ZONE_DEFAULT; | 38 | import static cn.iocoder.yudao.framework.common.util.date.DateUtils.TIME_ZONE_DEFAULT; |
| 36 | 39 | ||
| 40 | + | ||
| 37 | /** | 41 | /** |
| 38 | * 华为短信客户端的实现类 | 42 | * 华为短信客户端的实现类 |
| 39 | * | 43 | * |
| @@ -46,7 +50,14 @@ public class HuaweiSmsClient extends AbstractSmsClient { | @@ -46,7 +50,14 @@ public class HuaweiSmsClient extends AbstractSmsClient { | ||
| 46 | /** | 50 | /** |
| 47 | * 调用成功 code | 51 | * 调用成功 code |
| 48 | */ | 52 | */ |
| 49 | - public static final String API_CODE_SUCCESS = "OK"; | 53 | + public static final String URL = "https://smsapi.cn-north-4.myhuaweicloud.com:443/sms/batchSendSms/v1";//APP接入地址+接口访问URI |
| 54 | + public static final String HOST = "smsapi.cn-north-4.myhuaweicloud.com:443"; | ||
| 55 | + public static final String SIGNEDHEADERS = "content-type;host;x-sdk-date"; | ||
| 56 | + | ||
| 57 | + @Override | ||
| 58 | + protected void doInit() { | ||
| 59 | + | ||
| 60 | + } | ||
| 50 | 61 | ||
| 51 | public HuaweiSmsClient(SmsChannelProperties properties) { | 62 | public HuaweiSmsClient(SmsChannelProperties properties) { |
| 52 | super(properties); | 63 | super(properties); |
| @@ -55,95 +66,78 @@ public class HuaweiSmsClient extends AbstractSmsClient { | @@ -55,95 +66,78 @@ public class HuaweiSmsClient extends AbstractSmsClient { | ||
| 55 | } | 66 | } |
| 56 | 67 | ||
| 57 | @Override | 68 | @Override |
| 58 | - protected void doInit() { | ||
| 59 | - } | ||
| 60 | - | ||
| 61 | - @Override | ||
| 62 | public SmsSendRespDTO sendSms(Long sendLogId, String mobile, String apiTemplateId, | 69 | public SmsSendRespDTO sendSms(Long sendLogId, String mobile, String apiTemplateId, |
| 63 | List<KeyValue<String, Object>> templateParams) throws Throwable { | 70 | List<KeyValue<String, Object>> templateParams) throws Throwable { |
| 64 | - // TODO @scholar:https://smsapi.cn-north-4.myhuaweicloud.com:443 是不是枚举成静态变量 | ||
| 65 | - String url = "https://smsapi.cn-north-4.myhuaweicloud.com:443/sms/batchSendSms/v1"; //APP接入地址+接口访问URI | ||
| 66 | // 相比较阿里短信,华为短信发送的时候需要额外的参数“通道号”,考虑到不破坏原有的的结构 | 71 | // 相比较阿里短信,华为短信发送的时候需要额外的参数“通道号”,考虑到不破坏原有的的结构 |
| 67 | // 所以将 通道号 拼接到 apiTemplateId 字段中,格式为 "apiTemplateId 通道号"。空格为分隔符。 | 72 | // 所以将 通道号 拼接到 apiTemplateId 字段中,格式为 "apiTemplateId 通道号"。空格为分隔符。 |
| 68 | - // TODO @scholar:暂时只考虑中国大陆,所以不需要 sender 哈 | ||
| 69 | String sender = StrUtil.subAfter(apiTemplateId, " ", true); //中国大陆短信签名通道号或全球短信通道号 | 73 | String sender = StrUtil.subAfter(apiTemplateId, " ", true); //中国大陆短信签名通道号或全球短信通道号 |
| 70 | String templateId = StrUtil.subBefore(apiTemplateId, " ", true); //模板ID | 74 | String templateId = StrUtil.subBefore(apiTemplateId, " ", true); //模板ID |
| 71 | 75 | ||
| 72 | - // 选填,短信状态报告接收地址,推荐使用域名,为空或者不填表示不接收状态报告 | 76 | + //选填,短信状态报告接收地址,推荐使用域名,为空或者不填表示不接收状态报告 |
| 73 | String statusCallBack = properties.getCallbackUrl(); | 77 | String statusCallBack = properties.getCallbackUrl(); |
| 74 | 78 | ||
| 75 | - // TODO @scholar:1)是不是用 LocalDateTimeUtil.format();这样 3 行变成一行 | ||
| 76 | - // TODO @scholar:singerDate 叫 sdkDate 会更合适哈,这样理解起来简单。另外,singer 应该是 signed 么? | 79 | + List<String> templateParas = CollectionUtils.convertList(templateParams, kv -> String.valueOf(kv.getValue())); |
| 80 | + | ||
| 81 | + JSONObject JsonResponse = sendSmsRequest(sender,mobile,templateId,templateParas,statusCallBack); | ||
| 82 | + SmsResponse smsResponse = getSmsSendResponse(JsonResponse); | ||
| 83 | + | ||
| 84 | + return new SmsSendRespDTO().setSuccess(smsResponse.success).setApiMsg(smsResponse.data.toString()); | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + JSONObject sendSmsRequest(String sender,String mobile,String templateId,List<String> templateParas,String statusCallBack) throws UnsupportedEncodingException { | ||
| 88 | + | ||
| 77 | SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'", Locale.ENGLISH); | 89 | SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'", Locale.ENGLISH); |
| 78 | sdf.setTimeZone(TimeZone.getTimeZone("UTC")); | 90 | sdf.setTimeZone(TimeZone.getTimeZone("UTC")); |
| 79 | - String singerDate = sdf.format(new Date()); | 91 | + String sdkDate = sdf.format(new Date()); |
| 80 | 92 | ||
| 81 | - // TODO @scholar:整个处理加密的过程,是不是应该抽成一个 private 方法哈。这样整个调用的主干更清晰。 | ||
| 82 | // ************* 步骤 1:拼接规范请求串 ************* | 93 | // ************* 步骤 1:拼接规范请求串 ************* |
| 83 | String httpRequestMethod = "POST"; | 94 | String httpRequestMethod = "POST"; |
| 84 | String canonicalUri = "/sms/batchSendSms/v1/"; | 95 | String canonicalUri = "/sms/batchSendSms/v1/"; |
| 85 | - String canonicalQueryString = ""; // 查询参数为空 | 96 | + String canonicalQueryString = "";//查询参数为空 |
| 86 | String canonicalHeaders = "content-type:application/x-www-form-urlencoded\n" | 97 | String canonicalHeaders = "content-type:application/x-www-form-urlencoded\n" |
| 87 | - + "host:smsapi.cn-north-4.myhuaweicloud.com:443\n" | ||
| 88 | - + "x-sdk-date:" + singerDate + "\n"; | ||
| 89 | - // TODO @scholar:静态枚举了 | ||
| 90 | - String signedHeaders = "content-type;host;x-sdk-date"; | ||
| 91 | - // TODO @scholar:下面的注释,可以考虑去掉 | ||
| 92 | - /* | ||
| 93 | - * 选填,使用无变量模板时请赋空值 String templateParas = ""; | ||
| 94 | - * 单变量模板示例:模板内容为"您的验证码是${NUM_6}"时,templateParas可填写为"[\"111111\"]" | ||
| 95 | - * 双变量模板示例:模板内容为"您有${NUM_2}件快递请到${TXT_20}领取"时,templateParas可填写为"[\"3\",\"人民公园正门\"]" | ||
| 96 | - */ | ||
| 97 | - // TODO @scholar:CollectionUtils.convertList 可以把 4 行变成 1 行。 | ||
| 98 | - // TODO @scholar:templateParams 拼写错误哈 | ||
| 99 | - List<String> templateParas = new ArrayList<>(); | ||
| 100 | - for (KeyValue<String, Object> kv : templateParams) { | ||
| 101 | - templateParas.add(String.valueOf(kv.getValue())); | ||
| 102 | - } | ||
| 103 | - | ||
| 104 | - // 请求Body,不携带签名名称时,signature请填null | 98 | + + "host:"+ HOST +"\n" |
| 99 | + + "x-sdk-date:" + sdkDate + "\n"; | ||
| 100 | + //请求Body,不携带签名名称时,signature请填null | ||
| 105 | String body = buildRequestBody(sender, mobile, templateId, templateParas, statusCallBack, null); | 101 | String body = buildRequestBody(sender, mobile, templateId, templateParas, statusCallBack, null); |
| 106 | - // TODO @scholar:Assert 断言,抛出异常 | ||
| 107 | if (null == body || body.isEmpty()) { | 102 | if (null == body || body.isEmpty()) { |
| 108 | return null; | 103 | return null; |
| 109 | } | 104 | } |
| 110 | - String hashedRequestBody = HexUtil.encodeHexStr(DigestUtil.sha256(body)); | 105 | + String hashedRequestBody = sha256Hex(body); |
| 111 | String canonicalRequest = httpRequestMethod + "\n" + canonicalUri + "\n" + canonicalQueryString + "\n" | 106 | String canonicalRequest = httpRequestMethod + "\n" + canonicalUri + "\n" + canonicalQueryString + "\n" |
| 112 | - + canonicalHeaders + "\n" + signedHeaders + "\n" + hashedRequestBody; | 107 | + + canonicalHeaders + "\n" + SIGNEDHEADERS + "\n" + hashedRequestBody; |
| 113 | 108 | ||
| 114 | // ************* 步骤 2:拼接待签名字符串 ************* | 109 | // ************* 步骤 2:拼接待签名字符串 ************* |
| 115 | - // TODO @scholar:sha256Hex 是不是更简洁哈 | ||
| 116 | - String hashedCanonicalRequest = HexUtil.encodeHexStr(DigestUtil.sha256(canonicalRequest)); | ||
| 117 | - String stringToSign = "SDK-HMAC-SHA256" + "\n" + singerDate + "\n" + hashedCanonicalRequest; | 110 | + String hashedCanonicalRequest = sha256Hex(canonicalRequest); |
| 111 | + String stringToSign = "SDK-HMAC-SHA256" + "\n" + sdkDate + "\n" + hashedCanonicalRequest; | ||
| 118 | 112 | ||
| 119 | // ************* 步骤 3:计算签名 ************* | 113 | // ************* 步骤 3:计算签名 ************* |
| 120 | String signature = SecureUtil.hmacSha256(properties.getApiSecret()).digestHex(stringToSign); | 114 | String signature = SecureUtil.hmacSha256(properties.getApiSecret()).digestHex(stringToSign); |
| 121 | 115 | ||
| 122 | // ************* 步骤 4:拼接 Authorization ************* | 116 | // ************* 步骤 4:拼接 Authorization ************* |
| 123 | String authorization = "SDK-HMAC-SHA256" + " " + "Access=" + properties.getApiKey() + ", " | 117 | String authorization = "SDK-HMAC-SHA256" + " " + "Access=" + properties.getApiKey() + ", " |
| 124 | - + "SignedHeaders=" + signedHeaders + ", " + "Signature=" + signature; | 118 | + + "SignedHeaders=" + SIGNEDHEADERS + ", " + "Signature=" + signature; |
| 125 | 119 | ||
| 126 | // ************* 步骤 5:构造HttpRequest 并执行request请求,获得response ************* | 120 | // ************* 步骤 5:构造HttpRequest 并执行request请求,获得response ************* |
| 127 | - // TODO @scholar:考虑了下,还是换 hutool 的 httpUtils。因为未来 httpclient 我们可能会移除掉 | ||
| 128 | - HttpUriRequest postMethod = RequestBuilder.post() | ||
| 129 | - .setUri(url) | ||
| 130 | - .setEntity(new StringEntity(body, StandardCharsets.UTF_8)) | ||
| 131 | - .setHeader("Content-Type","application/x-www-form-urlencoded") | ||
| 132 | - .setHeader("X-Sdk-Date", singerDate) | ||
| 133 | - .setHeader("Authorization", authorization) | ||
| 134 | - .build(); | ||
| 135 | - // TODO @scholar:这种不太适合一直 new 的哈 | ||
| 136 | - CloseableHttpClient client = HttpClientBuilder.create().build(); | ||
| 137 | - HttpResponse response = client.execute(postMethod); | ||
| 138 | - // TODO @scholar:失败的情况下的处理 | ||
| 139 | - // TODO @scholar:setSerialNo(Integer.toString(response.getStatusLine().getStatusCode())) 这部分,空一行。一行代码太多了,阅读性不太好哈 | ||
| 140 | - return new SmsSendRespDTO().setSuccess(Objects.equals(response.getStatusLine().getReasonPhrase(), API_CODE_SUCCESS)).setSerialNo(Integer.toString(response.getStatusLine().getStatusCode())) | ||
| 141 | - .setApiRequestId(null).setApiCode(null).setApiMsg(null); | 121 | + HttpResponse response = HttpRequest.post(URL) |
| 122 | + .header("Content-Type", "application/x-www-form-urlencoded") | ||
| 123 | + .header("X-Sdk-Date", sdkDate) | ||
| 124 | + .header("host",HOST) | ||
| 125 | + .header("Authorization", authorization) | ||
| 126 | + .body(body) | ||
| 127 | + .execute(); | ||
| 128 | + | ||
| 129 | + return JSONUtil.parseObj(response.body()); | ||
| 130 | + } | ||
| 131 | + | ||
| 132 | + private SmsResponse getSmsSendResponse(JSONObject resJson) { | ||
| 133 | + SmsResponse smsResponse = new SmsResponse(); | ||
| 134 | + smsResponse.setSuccess("000000".equals(resJson.getStr("code"))); | ||
| 135 | + smsResponse.setData(resJson); | ||
| 136 | + return smsResponse; | ||
| 142 | } | 137 | } |
| 143 | 138 | ||
| 144 | static String buildRequestBody(String sender, String receiver, String templateId, List<String> templateParas, | 139 | static String buildRequestBody(String sender, String receiver, String templateId, List<String> templateParas, |
| 145 | - String statusCallBack, @SuppressWarnings("SameParameterValue") String signature) { | ||
| 146 | - // TODO @scholar:参数不满足,是不是抛出异常更好哈;通过 hutool 的 Assert 去断言 | 140 | + String statusCallBack, String signature) throws UnsupportedEncodingException { |
| 147 | if (null == sender || null == receiver || null == templateId || sender.isEmpty() || receiver.isEmpty() | 141 | if (null == sender || null == receiver || null == templateId || sender.isEmpty() || receiver.isEmpty() |
| 148 | || templateId.isEmpty()) { | 142 | || templateId.isEmpty()) { |
| 149 | System.out.println("buildRequestBody(): sender, receiver or templateId is null."); | 143 | System.out.println("buildRequestBody(): sender, receiver or templateId is null."); |
| @@ -154,20 +148,17 @@ public class HuaweiSmsClient extends AbstractSmsClient { | @@ -154,20 +148,17 @@ public class HuaweiSmsClient extends AbstractSmsClient { | ||
| 154 | appendToBody(body, "from=", sender); | 148 | appendToBody(body, "from=", sender); |
| 155 | appendToBody(body, "&to=", receiver); | 149 | appendToBody(body, "&to=", receiver); |
| 156 | appendToBody(body, "&templateId=", templateId); | 150 | appendToBody(body, "&templateId=", templateId); |
| 157 | - // TODO @scholar:new JSONArray(templateParas).toString(),是不是 JsonUtils.toString 呀? | ||
| 158 | - appendToBody(body, "&templateParas=", new JSONArray(templateParas).toString()); | 151 | + appendToBody(body, "&templateParas=", JsonUtils.toJsonString(templateParas)); |
| 159 | appendToBody(body, "&statusCallback=", statusCallBack); | 152 | appendToBody(body, "&statusCallback=", statusCallBack); |
| 160 | appendToBody(body, "&signature=", signature); | 153 | appendToBody(body, "&signature=", signature); |
| 161 | return body.toString(); | 154 | return body.toString(); |
| 162 | } | 155 | } |
| 163 | 156 | ||
| 164 | - private static void appendToBody(StringBuilder body, String key, String val) { | ||
| 165 | - // TODO @scholar:StrUtils.isNotEmpty(val),是不是更简洁哈 | 157 | + private static void appendToBody(StringBuilder body, String key, String val) throws UnsupportedEncodingException { |
| 166 | if (null != val && !val.isEmpty()) { | 158 | if (null != val && !val.isEmpty()) { |
| 167 | - body.append(key).append(URLEncoder.encode(val, StandardCharsets.UTF_8)); | 159 | + body.append(key).append(URLEncoder.encode(val, "UTF-8")); |
| 168 | } | 160 | } |
| 169 | } | 161 | } |
| 170 | - | ||
| 171 | @Override | 162 | @Override |
| 172 | public List<SmsReceiveRespDTO> parseSmsReceiveStatus(String text) { | 163 | public List<SmsReceiveRespDTO> parseSmsReceiveStatus(String text) { |
| 173 | List<SmsReceiveStatus> statuses = JsonUtils.parseArray(text, SmsReceiveStatus.class); | 164 | List<SmsReceiveStatus> statuses = JsonUtils.parseArray(text, SmsReceiveStatus.class); |
| @@ -179,12 +170,28 @@ public class HuaweiSmsClient extends AbstractSmsClient { | @@ -179,12 +170,28 @@ public class HuaweiSmsClient extends AbstractSmsClient { | ||
| 179 | 170 | ||
| 180 | @Override | 171 | @Override |
| 181 | public SmsTemplateRespDTO getSmsTemplate(String apiTemplateId) throws Throwable { | 172 | public SmsTemplateRespDTO getSmsTemplate(String apiTemplateId) throws Throwable { |
| 182 | - // 华为短信模板查询和发送短信,是不同的两套 key 和 secret,与阿里、腾讯的区别较大,这里模板查询校验暂不实现 | ||
| 183 | - // 对应文档 https://support.huaweicloud.com/api-msgsms/sms_05_0040.html | ||
| 184 | - return new SmsTemplateRespDTO().setId(apiTemplateId).setContent(null) | 173 | + //华为短信模板查询和发送短信,是不同的两套key和secret,与阿里、腾讯的区别较大,这里模板查询校验暂不实现。 |
| 174 | + return new SmsTemplateRespDTO().setId(null).setContent(null) | ||
| 185 | .setAuditStatus(SmsTemplateAuditStatusEnum.SUCCESS.getStatus()).setAuditReason(null); | 175 | .setAuditStatus(SmsTemplateAuditStatusEnum.SUCCESS.getStatus()).setAuditReason(null); |
| 176 | + | ||
| 177 | + } | ||
| 178 | + | ||
| 179 | + @Data | ||
| 180 | + public static class SmsResponse { | ||
| 181 | + | ||
| 182 | + /** | ||
| 183 | + * 是否成功 | ||
| 184 | + */ | ||
| 185 | + private boolean success; | ||
| 186 | + | ||
| 187 | + /** | ||
| 188 | + * 厂商原返回体 | ||
| 189 | + */ | ||
| 190 | + private Object data; | ||
| 191 | + | ||
| 186 | } | 192 | } |
| 187 | 193 | ||
| 194 | + | ||
| 188 | /** | 195 | /** |
| 189 | * 短信接收状态 | 196 | * 短信接收状态 |
| 190 | * | 197 | * |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/sms/core/client/impl/TencentSmsClient.java
| @@ -2,6 +2,11 @@ package cn.iocoder.yudao.module.system.framework.sms.core.client.impl; | @@ -2,6 +2,11 @@ package cn.iocoder.yudao.module.system.framework.sms.core.client.impl; | ||
| 2 | 2 | ||
| 3 | import cn.hutool.core.lang.Assert; | 3 | import cn.hutool.core.lang.Assert; |
| 4 | import cn.hutool.core.util.StrUtil; | 4 | import cn.hutool.core.util.StrUtil; |
| 5 | +import cn.hutool.http.HttpRequest; | ||
| 6 | +import cn.hutool.http.HttpResponse; | ||
| 7 | +import cn.hutool.json.JSONArray; | ||
| 8 | +import cn.hutool.json.JSONObject; | ||
| 9 | +import cn.hutool.json.JSONUtil; | ||
| 5 | import cn.iocoder.yudao.framework.common.core.KeyValue; | 10 | import cn.iocoder.yudao.framework.common.core.KeyValue; |
| 6 | import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils; | 11 | import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils; |
| 7 | import cn.iocoder.yudao.framework.common.util.json.JsonUtils; | 12 | import cn.iocoder.yudao.framework.common.util.json.JsonUtils; |
| @@ -13,19 +18,22 @@ import cn.iocoder.yudao.module.system.framework.sms.core.property.SmsChannelProp | @@ -13,19 +18,22 @@ import cn.iocoder.yudao.module.system.framework.sms.core.property.SmsChannelProp | ||
| 13 | import com.fasterxml.jackson.annotation.JsonFormat; | 18 | import com.fasterxml.jackson.annotation.JsonFormat; |
| 14 | import com.fasterxml.jackson.annotation.JsonProperty; | 19 | import com.fasterxml.jackson.annotation.JsonProperty; |
| 15 | import com.google.common.annotations.VisibleForTesting; | 20 | import com.google.common.annotations.VisibleForTesting; |
| 16 | -import com.tencentcloudapi.common.Credential; | ||
| 17 | -import com.tencentcloudapi.sms.v20210111.SmsClient; | ||
| 18 | -import com.tencentcloudapi.sms.v20210111.models.*; | 21 | +import jakarta.xml.bind.DatatypeConverter; |
| 19 | import lombok.Data; | 22 | import lombok.Data; |
| 20 | 23 | ||
| 24 | +import javax.crypto.Mac; | ||
| 25 | +import javax.crypto.spec.SecretKeySpec; | ||
| 26 | +import java.nio.charset.StandardCharsets; | ||
| 27 | +import java.text.SimpleDateFormat; | ||
| 21 | import java.time.LocalDateTime; | 28 | import java.time.LocalDateTime; |
| 22 | -import java.util.List; | ||
| 23 | -import java.util.Objects; | 29 | +import java.util.*; |
| 24 | 30 | ||
| 31 | +import static cn.hutool.crypto.digest.DigestUtil.sha256Hex; | ||
| 25 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; | 32 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; |
| 26 | import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND; | 33 | import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND; |
| 27 | import static cn.iocoder.yudao.framework.common.util.date.DateUtils.TIME_ZONE_DEFAULT; | 34 | import static cn.iocoder.yudao.framework.common.util.date.DateUtils.TIME_ZONE_DEFAULT; |
| 28 | 35 | ||
| 36 | +// TODO @scholar 建议参考 AliyunSmsClient 优化下 | ||
| 29 | /** | 37 | /** |
| 30 | * 腾讯云短信功能实现 | 38 | * 腾讯云短信功能实现 |
| 31 | * | 39 | * |
| @@ -41,11 +49,6 @@ public class TencentSmsClient extends AbstractSmsClient { | @@ -41,11 +49,6 @@ public class TencentSmsClient extends AbstractSmsClient { | ||
| 41 | public static final String API_CODE_SUCCESS = "Ok"; | 49 | public static final String API_CODE_SUCCESS = "Ok"; |
| 42 | 50 | ||
| 43 | /** | 51 | /** |
| 44 | - * REGION,使用南京 | ||
| 45 | - */ | ||
| 46 | - private static final String ENDPOINT = "ap-nanjing"; | ||
| 47 | - | ||
| 48 | - /** | ||
| 49 | * 是否国际/港澳台短信: | 52 | * 是否国际/港澳台短信: |
| 50 | * | 53 | * |
| 51 | * 0:表示国内短信。 | 54 | * 0:表示国内短信。 |
| @@ -53,7 +56,6 @@ public class TencentSmsClient extends AbstractSmsClient { | @@ -53,7 +56,6 @@ public class TencentSmsClient extends AbstractSmsClient { | ||
| 53 | */ | 56 | */ |
| 54 | private static final long INTERNATIONAL_CHINA = 0L; | 57 | private static final long INTERNATIONAL_CHINA = 0L; |
| 55 | 58 | ||
| 56 | - private SmsClient client; | ||
| 57 | 59 | ||
| 58 | public TencentSmsClient(SmsChannelProperties properties) { | 60 | public TencentSmsClient(SmsChannelProperties properties) { |
| 59 | super(properties); | 61 | super(properties); |
| @@ -63,9 +65,7 @@ public class TencentSmsClient extends AbstractSmsClient { | @@ -63,9 +65,7 @@ public class TencentSmsClient extends AbstractSmsClient { | ||
| 63 | 65 | ||
| 64 | @Override | 66 | @Override |
| 65 | protected void doInit() { | 67 | protected void doInit() { |
| 66 | - // 实例化一个认证对象,入参需要传入腾讯云账户密钥对 secretId,secretKey | ||
| 67 | - Credential credential = new Credential(getApiKey(), properties.getApiSecret()); | ||
| 68 | - client = new SmsClient(credential, ENDPOINT); | 68 | + |
| 69 | } | 69 | } |
| 70 | 70 | ||
| 71 | /** | 71 | /** |
| @@ -96,18 +96,87 @@ public class TencentSmsClient extends AbstractSmsClient { | @@ -96,18 +96,87 @@ public class TencentSmsClient extends AbstractSmsClient { | ||
| 96 | public SmsSendRespDTO sendSms(Long sendLogId, String mobile, | 96 | public SmsSendRespDTO sendSms(Long sendLogId, String mobile, |
| 97 | String apiTemplateId, List<KeyValue<String, Object>> templateParams) throws Throwable { | 97 | String apiTemplateId, List<KeyValue<String, Object>> templateParams) throws Throwable { |
| 98 | // 构建请求 | 98 | // 构建请求 |
| 99 | - SendSmsRequest request = new SendSmsRequest(); | ||
| 100 | - request.setSmsSdkAppId(getSdkAppId()); | ||
| 101 | - request.setPhoneNumberSet(new String[]{mobile}); | ||
| 102 | - request.setSignName(properties.getSignature()); | ||
| 103 | - request.setTemplateId(apiTemplateId); | ||
| 104 | - request.setTemplateParamSet(ArrayUtils.toArray(templateParams, e -> String.valueOf(e.getValue()))); | ||
| 105 | - request.setSessionContext(JsonUtils.toJsonString(new SessionContext().setLogId(sendLogId))); | ||
| 106 | - // 执行请求 | ||
| 107 | - SendSmsResponse response = client.SendSms(request); | ||
| 108 | - SendStatus status = response.getSendStatusSet()[0]; | ||
| 109 | - return new SmsSendRespDTO().setSuccess(Objects.equals(status.getCode(), API_CODE_SUCCESS)).setSerialNo(status.getSerialNo()) | ||
| 110 | - .setApiRequestId(response.getRequestId()).setApiCode(status.getCode()).setApiMsg(status.getMessage()); | 99 | + TreeMap<String, Object> body = new TreeMap<>(); |
| 100 | + String[] phones = {mobile}; | ||
| 101 | + body.put("PhoneNumberSet",phones); | ||
| 102 | + body.put("SmsSdkAppId",getSdkAppId()); | ||
| 103 | + body.put("SignName",properties.getSignature()); | ||
| 104 | + body.put("TemplateId",apiTemplateId); | ||
| 105 | + body.put("TemplateParamSet",ArrayUtils.toArray(templateParams, e -> String.valueOf(e.getValue()))); | ||
| 106 | + | ||
| 107 | + JSONObject JsonResponse = sendSmsRequest(body,"SendSms","2021-01-11","ap-guangzhou"); | ||
| 108 | + SmsResponse smsResponse = getSmsSendResponse(JsonResponse); | ||
| 109 | + | ||
| 110 | + return new SmsSendRespDTO().setSuccess(smsResponse.success).setApiMsg(smsResponse.data.toString()); | ||
| 111 | + | ||
| 112 | + } | ||
| 113 | + | ||
| 114 | + JSONObject sendSmsRequest(TreeMap<String, Object> body,String action,String version,String region) throws Exception { | ||
| 115 | + | ||
| 116 | + String timestamp = String.valueOf(System.currentTimeMillis() / 1000); | ||
| 117 | + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); | ||
| 118 | + // 注意时区,否则容易出错 | ||
| 119 | + sdf.setTimeZone(TimeZone.getTimeZone("UTC")); | ||
| 120 | + String date = sdf.format(new Date(Long.valueOf(timestamp + "000"))); | ||
| 121 | + | ||
| 122 | + // ************* 步骤 1:拼接规范请求串 ************* | ||
| 123 | + String host = "sms.tencentcloudapi.com"; //APP接入地址+接口访问URI | ||
| 124 | + String httpMethod = "POST"; // 请求方式 | ||
| 125 | + String canonicalUri = "/"; | ||
| 126 | + String canonicalQueryString = ""; | ||
| 127 | + | ||
| 128 | + String canonicalHeaders = "content-type:application/json; charset=utf-8\n" | ||
| 129 | + + "host:" + host + "\n" + "x-tc-action:" + action.toLowerCase() + "\n"; | ||
| 130 | + String signedHeaders = "content-type;host;x-tc-action"; | ||
| 131 | + String hashedRequestBody = sha256Hex(JSONUtil.toJsonStr(body)); | ||
| 132 | + String canonicalRequest = httpMethod + "\n" + canonicalUri + "\n" + canonicalQueryString + "\n" + canonicalHeaders + "\n" + signedHeaders + "\n" + hashedRequestBody; | ||
| 133 | + | ||
| 134 | + // ************* 步骤 2:拼接待签名字符串 ************* | ||
| 135 | + String credentialScope = date + "/" + "sms" + "/" + "tc3_request"; | ||
| 136 | + String hashedCanonicalRequest = sha256Hex(canonicalRequest); | ||
| 137 | + String stringToSign = "TC3-HMAC-SHA256" + "\n" + timestamp + "\n" + credentialScope + "\n" + hashedCanonicalRequest; | ||
| 138 | + | ||
| 139 | + // ************* 步骤 3:计算签名 ************* | ||
| 140 | + byte[] secretDate = hmac256(("TC3" + properties.getApiSecret()).getBytes(StandardCharsets.UTF_8), date); | ||
| 141 | + byte[] secretService = hmac256(secretDate, "sms"); | ||
| 142 | + byte[] secretSigning = hmac256(secretService, "tc3_request"); | ||
| 143 | + String signature = DatatypeConverter.printHexBinary(hmac256(secretSigning, stringToSign)).toLowerCase(); | ||
| 144 | + | ||
| 145 | + // ************* 步骤 4:拼接 Authorization ************* | ||
| 146 | + String authorization = "TC3-HMAC-SHA256" + " " + "Credential=" + getApiKey() + "/" + credentialScope + ", " | ||
| 147 | + + "SignedHeaders=" + signedHeaders + ", " + "Signature=" + signature; | ||
| 148 | + | ||
| 149 | + // ************* 步骤 5:构造HttpRequest 并执行request请求,获得response ************* | ||
| 150 | + Map<String, String> headers = new HashMap<>(); | ||
| 151 | + headers.put("Authorization", authorization); | ||
| 152 | + headers.put("Content-Type", "application/json; charset=utf-8"); | ||
| 153 | + headers.put("Host", host); | ||
| 154 | + headers.put("X-TC-Action", action); | ||
| 155 | + headers.put("X-TC-Timestamp", timestamp); | ||
| 156 | + headers.put("X-TC-Version", version); | ||
| 157 | + headers.put("X-TC-Region", region); | ||
| 158 | + | ||
| 159 | + HttpResponse response = HttpRequest.post("https://"+host) | ||
| 160 | + .addHeaders(headers) | ||
| 161 | + .body(JSONUtil.toJsonStr(body)) | ||
| 162 | + .execute(); | ||
| 163 | + | ||
| 164 | + return JSONUtil.parseObj(response.body()); | ||
| 165 | + } | ||
| 166 | + | ||
| 167 | + public static byte[] hmac256(byte[] key, String msg) throws Exception { | ||
| 168 | + Mac mac = Mac.getInstance("HmacSHA256"); | ||
| 169 | + SecretKeySpec secretKeySpec = new SecretKeySpec(key, mac.getAlgorithm()); | ||
| 170 | + mac.init(secretKeySpec); | ||
| 171 | + return mac.doFinal(msg.getBytes(StandardCharsets.UTF_8)); | ||
| 172 | + } | ||
| 173 | + | ||
| 174 | + private SmsResponse getSmsSendResponse(JSONObject resJson) { | ||
| 175 | + SmsResponse smsResponse = new SmsResponse(); | ||
| 176 | + JSONArray statusJson =resJson.getJSONObject("Response").getJSONArray("SendStatusSet"); | ||
| 177 | + smsResponse.setSuccess("Ok".equals(statusJson.getJSONObject(0).getStr("Code"))); | ||
| 178 | + smsResponse.setData(resJson); | ||
| 179 | + return smsResponse; | ||
| 111 | } | 180 | } |
| 112 | 181 | ||
| 113 | @Override | 182 | @Override |
| @@ -122,18 +191,49 @@ public class TencentSmsClient extends AbstractSmsClient { | @@ -122,18 +191,49 @@ public class TencentSmsClient extends AbstractSmsClient { | ||
| 122 | 191 | ||
| 123 | @Override | 192 | @Override |
| 124 | public SmsTemplateRespDTO getSmsTemplate(String apiTemplateId) throws Throwable { | 193 | public SmsTemplateRespDTO getSmsTemplate(String apiTemplateId) throws Throwable { |
| 194 | + | ||
| 125 | // 构建请求 | 195 | // 构建请求 |
| 126 | - DescribeSmsTemplateListRequest request = new DescribeSmsTemplateListRequest(); | ||
| 127 | - request.setTemplateIdSet(new Long[]{Long.parseLong(apiTemplateId)}); | ||
| 128 | - request.setInternational(INTERNATIONAL_CHINA); | ||
| 129 | - // 执行请求 | ||
| 130 | - DescribeSmsTemplateListResponse response = client.DescribeSmsTemplateList(request); | ||
| 131 | - DescribeTemplateListStatus status = response.getDescribeTemplateStatusSet()[0]; | ||
| 132 | - if (status == null || status.getStatusCode() == null) { | ||
| 133 | - return null; | ||
| 134 | - } | ||
| 135 | - return new SmsTemplateRespDTO().setId(status.getTemplateId().toString()).setContent(status.getTemplateContent()) | ||
| 136 | - .setAuditStatus(convertSmsTemplateAuditStatus(status.getStatusCode().intValue())).setAuditReason(status.getReviewReply()); | 196 | + TreeMap<String, Object> body = new TreeMap<>(); |
| 197 | + body.put("International",0); | ||
| 198 | + Integer[] templateIds = {Integer.valueOf(apiTemplateId)}; | ||
| 199 | + body.put("TemplateIdSet",templateIds); | ||
| 200 | + | ||
| 201 | + JSONObject JsonResponse = sendSmsRequest(body,"DescribeSmsTemplateList","2021-01-11","ap-guangzhou"); | ||
| 202 | + QuerySmsTemplateResponse smsTemplateResponse = getSmsTemplateResponse(JsonResponse); | ||
| 203 | + String templateId = Integer.toString(smsTemplateResponse.getDescribeTemplateStatusSet().get(0).getTemplateId()); | ||
| 204 | + String content = smsTemplateResponse.getDescribeTemplateStatusSet().get(0).getTemplateContent(); | ||
| 205 | + Integer templateStatus = smsTemplateResponse.getDescribeTemplateStatusSet().get(0).getStatusCode(); | ||
| 206 | + String auditReason = smsTemplateResponse.getDescribeTemplateStatusSet().get(0).getReviewReply(); | ||
| 207 | + | ||
| 208 | + return new SmsTemplateRespDTO().setId(templateId).setContent(content) | ||
| 209 | + .setAuditStatus(convertSmsTemplateAuditStatus(templateStatus)).setAuditReason(auditReason); | ||
| 210 | + } | ||
| 211 | + | ||
| 212 | + private QuerySmsTemplateResponse getSmsTemplateResponse(JSONObject resJson) { | ||
| 213 | + | ||
| 214 | + QuerySmsTemplateResponse smsTemplateResponse = new QuerySmsTemplateResponse(); | ||
| 215 | + | ||
| 216 | + smsTemplateResponse.setRequestId(resJson.getJSONObject("Response").getStr("RequestId")); | ||
| 217 | + | ||
| 218 | + smsTemplateResponse.setDescribeTemplateStatusSet(new ArrayList<>()); | ||
| 219 | + | ||
| 220 | + QuerySmsTemplateResponse.TemplateInfo templateInfo = new QuerySmsTemplateResponse.TemplateInfo(); | ||
| 221 | + | ||
| 222 | + Object statusObject = resJson.getJSONObject("Response").getJSONArray("DescribeTemplateStatusSet").get(0); | ||
| 223 | + | ||
| 224 | + JSONObject statusJSON = new JSONObject(statusObject); | ||
| 225 | + | ||
| 226 | + templateInfo.setTemplateContent(statusJSON.get("TemplateContent").toString()); | ||
| 227 | + | ||
| 228 | + templateInfo.setStatusCode(Integer.parseInt(statusJSON.get("StatusCode").toString())); | ||
| 229 | + | ||
| 230 | + templateInfo.setReviewReply(statusJSON.get("ReviewReply").toString()); | ||
| 231 | + | ||
| 232 | + templateInfo.setTemplateId(Integer.parseInt(statusJSON.get("TemplateId").toString())); | ||
| 233 | + | ||
| 234 | + smsTemplateResponse.getDescribeTemplateStatusSet().add(templateInfo); | ||
| 235 | + | ||
| 236 | + return smsTemplateResponse; | ||
| 137 | } | 237 | } |
| 138 | 238 | ||
| 139 | @VisibleForTesting | 239 | @VisibleForTesting |
| @@ -147,6 +247,45 @@ public class TencentSmsClient extends AbstractSmsClient { | @@ -147,6 +247,45 @@ public class TencentSmsClient extends AbstractSmsClient { | ||
| 147 | } | 247 | } |
| 148 | 248 | ||
| 149 | @Data | 249 | @Data |
| 250 | + public static class SmsResponse { | ||
| 251 | + | ||
| 252 | + /** | ||
| 253 | + * 是否成功 | ||
| 254 | + */ | ||
| 255 | + private boolean success; | ||
| 256 | + | ||
| 257 | + /** | ||
| 258 | + * 厂商原返回体 | ||
| 259 | + */ | ||
| 260 | + private Object data; | ||
| 261 | + | ||
| 262 | + } | ||
| 263 | + | ||
| 264 | + | ||
| 265 | + /** | ||
| 266 | + * <p>类名: QuerySmsTemplateResponse | ||
| 267 | + * <p>说明: sms模板查询返回信息 | ||
| 268 | + * | ||
| 269 | + * @author :scholar | ||
| 270 | + * 2024/07/17 0:25 | ||
| 271 | + **/ | ||
| 272 | + @Data | ||
| 273 | + public static class QuerySmsTemplateResponse { | ||
| 274 | + private List<TemplateInfo> DescribeTemplateStatusSet; | ||
| 275 | + private String RequestId; | ||
| 276 | + @Data | ||
| 277 | + static class TemplateInfo { | ||
| 278 | + private String TemplateName; | ||
| 279 | + private Integer TemplateId; | ||
| 280 | + private Integer International; | ||
| 281 | + private String ReviewReply; | ||
| 282 | + private long CreateTime; | ||
| 283 | + private String TemplateContent; | ||
| 284 | + private Integer StatusCode; | ||
| 285 | + } | ||
| 286 | + } | ||
| 287 | + | ||
| 288 | + @Data | ||
| 150 | private static class SmsReceiveStatus { | 289 | private static class SmsReceiveStatus { |
| 151 | 290 | ||
| 152 | /** | 291 | /** |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/framework/sms/core/client/impl/AliyunSmsClientTest.java
| 1 | package cn.iocoder.yudao.module.system.framework.sms.core.client.impl; | 1 | package cn.iocoder.yudao.module.system.framework.sms.core.client.impl; |
| 2 | 2 | ||
| 3 | -import cn.hutool.core.util.ReflectUtil; | ||
| 4 | import cn.iocoder.yudao.framework.common.core.KeyValue; | 3 | import cn.iocoder.yudao.framework.common.core.KeyValue; |
| 5 | -import cn.iocoder.yudao.framework.common.util.collection.MapUtils; | 4 | +import cn.iocoder.yudao.framework.common.util.http.HttpUtils; |
| 6 | import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; | 5 | import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; |
| 7 | import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsReceiveRespDTO; | 6 | import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsReceiveRespDTO; |
| 8 | import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsSendRespDTO; | 7 | import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsSendRespDTO; |
| 9 | import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsTemplateRespDTO; | 8 | import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsTemplateRespDTO; |
| 10 | import cn.iocoder.yudao.module.system.framework.sms.core.enums.SmsTemplateAuditStatusEnum; | 9 | import cn.iocoder.yudao.module.system.framework.sms.core.enums.SmsTemplateAuditStatusEnum; |
| 11 | import cn.iocoder.yudao.module.system.framework.sms.core.property.SmsChannelProperties; | 10 | import cn.iocoder.yudao.module.system.framework.sms.core.property.SmsChannelProperties; |
| 12 | -import com.aliyuncs.IAcsClient; | ||
| 13 | -import com.aliyuncs.dysmsapi.model.v20170525.QuerySmsTemplateRequest; | ||
| 14 | -import com.aliyuncs.dysmsapi.model.v20170525.QuerySmsTemplateResponse; | ||
| 15 | -import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest; | ||
| 16 | -import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse; | ||
| 17 | import com.google.common.collect.Lists; | 11 | import com.google.common.collect.Lists; |
| 18 | import org.junit.jupiter.api.Test; | 12 | import org.junit.jupiter.api.Test; |
| 19 | -import org.mockito.ArgumentMatcher; | ||
| 20 | import org.mockito.InjectMocks; | 13 | import org.mockito.InjectMocks; |
| 21 | -import org.mockito.Mock; | 14 | +import org.mockito.MockedStatic; |
| 22 | 15 | ||
| 23 | import java.time.LocalDateTime; | 16 | import java.time.LocalDateTime; |
| 24 | import java.util.List; | 17 | import java.util.List; |
| 25 | 18 | ||
| 26 | -import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString; | ||
| 27 | -import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; | 19 | +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId; |
| 20 | +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString; | ||
| 28 | import static org.junit.jupiter.api.Assertions.*; | 21 | import static org.junit.jupiter.api.Assertions.*; |
| 29 | -import static org.mockito.ArgumentMatchers.argThat; | ||
| 30 | -import static org.mockito.Mockito.when; | 22 | +import static org.mockito.ArgumentMatchers.anyMap; |
| 23 | +import static org.mockito.ArgumentMatchers.anyString; | ||
| 24 | +import static org.mockito.Mockito.mockStatic; | ||
| 31 | 25 | ||
| 32 | /** | 26 | /** |
| 33 | - * {@link AliyunSmsClient} 的单元测试 | 27 | + * {@link cn.iocoder.yudao.module.system.framework.sms.core.client.impl.AliyunSmsClient} 的单元测试 |
| 34 | * | 28 | * |
| 35 | * @author 芋道源码 | 29 | * @author 芋道源码 |
| 36 | */ | 30 | */ |
| @@ -44,9 +38,6 @@ public class AliyunSmsClientTest extends BaseMockitoUnitTest { | @@ -44,9 +38,6 @@ public class AliyunSmsClientTest extends BaseMockitoUnitTest { | ||
| 44 | @InjectMocks | 38 | @InjectMocks |
| 45 | private final AliyunSmsClient smsClient = new AliyunSmsClient(properties); | 39 | private final AliyunSmsClient smsClient = new AliyunSmsClient(properties); |
| 46 | 40 | ||
| 47 | - @Mock | ||
| 48 | - private IAcsClient client; | ||
| 49 | - | ||
| 50 | @Test | 41 | @Test |
| 51 | public void testDoInit() { | 42 | public void testDoInit() { |
| 52 | // 准备参数 | 43 | // 准备参数 |
| @@ -54,67 +45,55 @@ public class AliyunSmsClientTest extends BaseMockitoUnitTest { | @@ -54,67 +45,55 @@ public class AliyunSmsClientTest extends BaseMockitoUnitTest { | ||
| 54 | 45 | ||
| 55 | // 调用 | 46 | // 调用 |
| 56 | smsClient.doInit(); | 47 | smsClient.doInit(); |
| 57 | - // 断言 | ||
| 58 | - assertNotSame(client, ReflectUtil.getFieldValue(smsClient, "acsClient")); | ||
| 59 | } | 48 | } |
| 60 | 49 | ||
| 61 | @Test | 50 | @Test |
| 62 | public void tesSendSms_success() throws Throwable { | 51 | public void tesSendSms_success() throws Throwable { |
| 63 | - // 准备参数 | ||
| 64 | - Long sendLogId = randomLongId(); | ||
| 65 | - String mobile = randomString(); | ||
| 66 | - String apiTemplateId = randomString(); | ||
| 67 | - List<KeyValue<String, Object>> templateParams = Lists.newArrayList( | ||
| 68 | - new KeyValue<>("code", 1234), new KeyValue<>("op", "login")); | ||
| 69 | - // mock 方法 | ||
| 70 | - SendSmsResponse response = randomPojo(SendSmsResponse.class, o -> o.setCode("OK")); | ||
| 71 | - when(client.getAcsResponse(argThat((ArgumentMatcher<SendSmsRequest>) acsRequest -> { | ||
| 72 | - assertEquals(mobile, acsRequest.getPhoneNumbers()); | ||
| 73 | - assertEquals(properties.getSignature(), acsRequest.getSignName()); | ||
| 74 | - assertEquals(apiTemplateId, acsRequest.getTemplateCode()); | ||
| 75 | - assertEquals(toJsonString(MapUtils.convertMap(templateParams)), acsRequest.getTemplateParam()); | ||
| 76 | - assertEquals(sendLogId.toString(), acsRequest.getOutId()); | ||
| 77 | - return true; | ||
| 78 | - }))).thenReturn(response); | ||
| 79 | - | ||
| 80 | - // 调用 | ||
| 81 | - SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile, | ||
| 82 | - apiTemplateId, templateParams); | ||
| 83 | - // 断言 | ||
| 84 | - assertTrue(result.getSuccess()); | ||
| 85 | - assertEquals(response.getRequestId(), result.getApiRequestId()); | ||
| 86 | - assertEquals(response.getCode(), result.getApiCode()); | ||
| 87 | - assertEquals(response.getMessage(), result.getApiMsg()); | ||
| 88 | - assertEquals(response.getBizId(), result.getSerialNo()); | 52 | + try (MockedStatic<HttpUtils> httpUtilsMockedStatic = mockStatic(HttpUtils.class)) { |
| 53 | + // 准备参数 | ||
| 54 | + Long sendLogId = randomLongId(); | ||
| 55 | + String mobile = randomString(); | ||
| 56 | + String apiTemplateId = randomString(); | ||
| 57 | + List<KeyValue<String, Object>> templateParams = Lists.newArrayList( | ||
| 58 | + new KeyValue<>("code", 1234), new KeyValue<>("op", "login")); | ||
| 59 | + // mock 方法 | ||
| 60 | + httpUtilsMockedStatic.when(() -> HttpUtils.post(anyString(), anyMap(), anyString())) | ||
| 61 | + .thenReturn("{\"Message\":\"OK\",\"RequestId\":\"30067CE9-3710-5984-8881-909B21D8DB28\",\"Code\":\"OK\",\"BizId\":\"800025323183427988\"}"); | ||
| 62 | + | ||
| 63 | + // 调用 | ||
| 64 | + SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile, | ||
| 65 | + apiTemplateId, templateParams); | ||
| 66 | + // 断言 | ||
| 67 | + assertTrue(result.getSuccess()); | ||
| 68 | + assertEquals("30067CE9-3710-5984-8881-909B21D8DB28", result.getApiRequestId()); | ||
| 69 | + assertEquals("OK", result.getApiCode()); | ||
| 70 | + assertEquals("OK", result.getApiMsg()); | ||
| 71 | + assertEquals("800025323183427988", result.getSerialNo()); | ||
| 72 | + } | ||
| 89 | } | 73 | } |
| 90 | 74 | ||
| 91 | @Test | 75 | @Test |
| 92 | public void tesSendSms_fail() throws Throwable { | 76 | public void tesSendSms_fail() throws Throwable { |
| 93 | - // 准备参数 | ||
| 94 | - Long sendLogId = randomLongId(); | ||
| 95 | - String mobile = randomString(); | ||
| 96 | - String apiTemplateId = randomString(); | ||
| 97 | - List<KeyValue<String, Object>> templateParams = Lists.newArrayList( | ||
| 98 | - new KeyValue<>("code", 1234), new KeyValue<>("op", "login")); | ||
| 99 | - // mock 方法 | ||
| 100 | - SendSmsResponse response = randomPojo(SendSmsResponse.class, o -> o.setCode("ERROR")); | ||
| 101 | - when(client.getAcsResponse(argThat((ArgumentMatcher<SendSmsRequest>) acsRequest -> { | ||
| 102 | - assertEquals(mobile, acsRequest.getPhoneNumbers()); | ||
| 103 | - assertEquals(properties.getSignature(), acsRequest.getSignName()); | ||
| 104 | - assertEquals(apiTemplateId, acsRequest.getTemplateCode()); | ||
| 105 | - assertEquals(toJsonString(MapUtils.convertMap(templateParams)), acsRequest.getTemplateParam()); | ||
| 106 | - assertEquals(sendLogId.toString(), acsRequest.getOutId()); | ||
| 107 | - return true; | ||
| 108 | - }))).thenReturn(response); | ||
| 109 | - | ||
| 110 | - // 调用 | ||
| 111 | - SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile, apiTemplateId, templateParams); | ||
| 112 | - // 断言 | ||
| 113 | - assertFalse(result.getSuccess()); | ||
| 114 | - assertEquals(response.getRequestId(), result.getApiRequestId()); | ||
| 115 | - assertEquals(response.getCode(), result.getApiCode()); | ||
| 116 | - assertEquals(response.getMessage(), result.getApiMsg()); | ||
| 117 | - assertEquals(response.getBizId(), result.getSerialNo()); | 77 | + try (MockedStatic<HttpUtils> httpUtilsMockedStatic = mockStatic(HttpUtils.class)) { |
| 78 | + // 准备参数 | ||
| 79 | + Long sendLogId = randomLongId(); | ||
| 80 | + String mobile = randomString(); | ||
| 81 | + String apiTemplateId = randomString(); | ||
| 82 | + List<KeyValue<String, Object>> templateParams = Lists.newArrayList( | ||
| 83 | + new KeyValue<>("code", 1234), new KeyValue<>("op", "login")); | ||
| 84 | + // mock 方法 | ||
| 85 | + httpUtilsMockedStatic.when(() -> HttpUtils.post(anyString(), anyMap(), anyString())) | ||
| 86 | + .thenReturn("{\"Message\":\"手机号码格式错误\",\"RequestId\":\"B7700B8E-227E-5886-9564-26036172F01F\",\"Code\":\"isv.MOBILE_NUMBER_ILLEGAL\"}"); | ||
| 87 | + | ||
| 88 | + // 调用 | ||
| 89 | + SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile, apiTemplateId, templateParams); | ||
| 90 | + // 断言 | ||
| 91 | + assertFalse(result.getSuccess()); | ||
| 92 | + assertEquals("B7700B8E-227E-5886-9564-26036172F01F", result.getApiRequestId()); | ||
| 93 | + assertEquals("isv.MOBILE_NUMBER_ILLEGAL", result.getApiCode()); | ||
| 94 | + assertEquals("手机号码格式错误", result.getApiMsg()); | ||
| 95 | + assertNull(result.getSerialNo()); | ||
| 96 | + } | ||
| 118 | } | 97 | } |
| 119 | 98 | ||
| 120 | @Test | 99 | @Test |
| @@ -151,25 +130,21 @@ public class AliyunSmsClientTest extends BaseMockitoUnitTest { | @@ -151,25 +130,21 @@ public class AliyunSmsClientTest extends BaseMockitoUnitTest { | ||
| 151 | 130 | ||
| 152 | @Test | 131 | @Test |
| 153 | public void testGetSmsTemplate() throws Throwable { | 132 | public void testGetSmsTemplate() throws Throwable { |
| 154 | - // 准备参数 | ||
| 155 | - String apiTemplateId = randomString(); | ||
| 156 | - // mock 方法 | ||
| 157 | - QuerySmsTemplateResponse response = randomPojo(QuerySmsTemplateResponse.class, o -> { | ||
| 158 | - o.setCode("OK"); | ||
| 159 | - o.setTemplateStatus(1); // 设置模板通过 | ||
| 160 | - }); | ||
| 161 | - when(client.getAcsResponse(argThat((ArgumentMatcher<QuerySmsTemplateRequest>) acsRequest -> { | ||
| 162 | - assertEquals(apiTemplateId, acsRequest.getTemplateCode()); | ||
| 163 | - return true; | ||
| 164 | - }))).thenReturn(response); | ||
| 165 | - | ||
| 166 | - // 调用 | ||
| 167 | - SmsTemplateRespDTO result = smsClient.getSmsTemplate(apiTemplateId); | ||
| 168 | - // 断言 | ||
| 169 | - assertEquals(response.getTemplateCode(), result.getId()); | ||
| 170 | - assertEquals(response.getTemplateContent(), result.getContent()); | ||
| 171 | - assertEquals(SmsTemplateAuditStatusEnum.SUCCESS.getStatus(), result.getAuditStatus()); | ||
| 172 | - assertEquals(response.getReason(), result.getAuditReason()); | 133 | + try (MockedStatic<HttpUtils> httpUtilsMockedStatic = mockStatic(HttpUtils.class)) { |
| 134 | + // 准备参数 | ||
| 135 | + String apiTemplateId = randomString(); | ||
| 136 | + // mock 方法 | ||
| 137 | + httpUtilsMockedStatic.when(() -> HttpUtils.post(anyString(), anyMap(), anyString())) | ||
| 138 | + .thenReturn("{\"TemplateCode\":\"SMS_207945135\",\"RequestId\":\"6F4CC077-29C8-5BA5-AB62-5FF95068A5AC\",\"Message\":\"OK\",\"TemplateContent\":\"您的验证码${code},该验证码5分钟内有效,请勿泄漏于他人!\",\"TemplateName\":\"公告通知\",\"TemplateType\":0,\"Code\":\"OK\",\"CreateDate\":\"2020-12-23 17:34:42\",\"Reason\":\"无审批备注\",\"TemplateStatus\":1}"); | ||
| 139 | + | ||
| 140 | + // 调用 | ||
| 141 | + SmsTemplateRespDTO result = smsClient.getSmsTemplate(apiTemplateId); | ||
| 142 | + // 断言 | ||
| 143 | + assertEquals("SMS_207945135", result.getId()); | ||
| 144 | + assertEquals("您的验证码${code},该验证码5分钟内有效,请勿泄漏于他人!", result.getContent()); | ||
| 145 | + assertEquals(SmsTemplateAuditStatusEnum.SUCCESS.getStatus(), result.getAuditStatus()); | ||
| 146 | + assertEquals("无审批备注", result.getAuditReason()); | ||
| 147 | + } | ||
| 173 | } | 148 | } |
| 174 | 149 | ||
| 175 | @Test | 150 | @Test |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/framework/sms/core/client/impl/SmsClientTests.java
| 1 | package cn.iocoder.yudao.module.system.framework.sms.core.client.impl; | 1 | package cn.iocoder.yudao.module.system.framework.sms.core.client.impl; |
| 2 | 2 | ||
| 3 | import cn.iocoder.yudao.framework.common.core.KeyValue; | 3 | import cn.iocoder.yudao.framework.common.core.KeyValue; |
| 4 | +import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsReceiveRespDTO; | ||
| 4 | import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsSendRespDTO; | 5 | import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsSendRespDTO; |
| 6 | +import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsTemplateRespDTO; | ||
| 5 | import cn.iocoder.yudao.module.system.framework.sms.core.property.SmsChannelProperties; | 7 | import cn.iocoder.yudao.module.system.framework.sms.core.property.SmsChannelProperties; |
| 6 | import org.junit.jupiter.api.Disabled; | 8 | import org.junit.jupiter.api.Disabled; |
| 7 | import org.junit.jupiter.api.Test; | 9 | import org.junit.jupiter.api.Test; |
| @@ -17,7 +19,7 @@ public class SmsClientTests { | @@ -17,7 +19,7 @@ public class SmsClientTests { | ||
| 17 | 19 | ||
| 18 | @Test | 20 | @Test |
| 19 | @Disabled | 21 | @Disabled |
| 20 | - public void testHuaweiSmsClient() throws Throwable { | 22 | + public void testHuaweiSmsClient_sendSms() throws Throwable { |
| 21 | SmsChannelProperties properties = new SmsChannelProperties() | 23 | SmsChannelProperties properties = new SmsChannelProperties() |
| 22 | .setApiKey("123") | 24 | .setApiKey("123") |
| 23 | .setApiSecret("456"); | 25 | .setApiSecret("456"); |
| @@ -33,4 +35,68 @@ public class SmsClientTests { | @@ -33,4 +35,68 @@ public class SmsClientTests { | ||
| 33 | System.out.println(smsSendRespDTO); | 35 | System.out.println(smsSendRespDTO); |
| 34 | } | 36 | } |
| 35 | 37 | ||
| 38 | + // ========== 阿里云 ========== | ||
| 39 | + | ||
| 40 | + @Test | ||
| 41 | + @Disabled | ||
| 42 | + public void testAliyunSmsClient_getSmsTemplate() throws Throwable { | ||
| 43 | + SmsChannelProperties properties = new SmsChannelProperties() | ||
| 44 | + .setApiKey("LTAI5tAicJAxaSFiZuGGeXHR") | ||
| 45 | + .setApiSecret("Fdr9vadxnDvS6GJU0W1tijQ0VmLhYz"); | ||
| 46 | + AliyunSmsClient client = new AliyunSmsClient(properties); | ||
| 47 | + // 准备参数 | ||
| 48 | + String apiTemplateId = "SMS_207945135"; | ||
| 49 | + // 调用 | ||
| 50 | + SmsTemplateRespDTO template = client.getSmsTemplate(apiTemplateId); | ||
| 51 | + // 打印结果 | ||
| 52 | + System.out.println(template); | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + @Test | ||
| 56 | + @Disabled | ||
| 57 | + public void testAliyunSmsClient_sendSms() throws Throwable { | ||
| 58 | + SmsChannelProperties properties = new SmsChannelProperties() | ||
| 59 | + .setApiKey("LTAI5tAicJAxaSFiZuGGeXHR") | ||
| 60 | + .setApiSecret("Fdr9vadxnDvS6GJU0W1tijQ0VmLhYz") | ||
| 61 | + .setSignature("Ballcat"); | ||
| 62 | + AliyunSmsClient client = new AliyunSmsClient(properties); | ||
| 63 | + // 准备参数 | ||
| 64 | + Long sendLogId = System.currentTimeMillis(); | ||
| 65 | + String mobile = "173213154791"; | ||
| 66 | + String apiTemplateId = "SMS_207945135"; | ||
| 67 | + // 调用 | ||
| 68 | + SmsSendRespDTO sendRespDTO = client.sendSms(sendLogId, mobile, apiTemplateId, List.of(new KeyValue<>("code", "1024"))); | ||
| 69 | + // 打印结果 | ||
| 70 | + System.out.println(sendRespDTO); | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + @Test | ||
| 74 | + @Disabled | ||
| 75 | + public void testAliyunSmsClient_parseSmsReceiveStatus() { | ||
| 76 | + SmsChannelProperties properties = new SmsChannelProperties() | ||
| 77 | + .setApiKey("LTAI5tAicJAxaSFiZuGGeXHR") | ||
| 78 | + .setApiSecret("Fdr9vadxnDvS6GJU0W1tijQ0VmLhYz"); | ||
| 79 | + AliyunSmsClient client = new AliyunSmsClient(properties); | ||
| 80 | + // 准备参数 | ||
| 81 | + String text = "[\n" + | ||
| 82 | + " {\n" + | ||
| 83 | + " \"phone_number\" : \"13900000001\",\n" + | ||
| 84 | + " \"send_time\" : \"2017-01-01 11:12:13\",\n" + | ||
| 85 | + " \"report_time\" : \"2017-02-02 22:23:24\",\n" + | ||
| 86 | + " \"success\" : true,\n" + | ||
| 87 | + " \"err_code\" : \"DELIVERED\",\n" + | ||
| 88 | + " \"err_msg\" : \"用户接收成功\",\n" + | ||
| 89 | + " \"sms_size\" : \"1\",\n" + | ||
| 90 | + " \"biz_id\" : \"12345\",\n" + | ||
| 91 | + " \"out_id\" : \"67890\"\n" + | ||
| 92 | + " }\n" + | ||
| 93 | + "]"; | ||
| 94 | + // mock 方法 | ||
| 95 | + | ||
| 96 | + // 调用 | ||
| 97 | + List<SmsReceiveRespDTO> statuses = client.parseSmsReceiveStatus(text); | ||
| 98 | + // 打印结果 | ||
| 99 | + System.out.println(statuses); | ||
| 100 | + } | ||
| 101 | + | ||
| 36 | } | 102 | } |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/framework/sms/core/client/impl/TencentSmsClientTest.java
| 1 | package cn.iocoder.yudao.module.system.framework.sms.core.client.impl; | 1 | package cn.iocoder.yudao.module.system.framework.sms.core.client.impl; |
| 2 | 2 | ||
| 3 | import cn.hutool.core.util.ReflectUtil; | 3 | import cn.hutool.core.util.ReflectUtil; |
| 4 | -import cn.iocoder.yudao.framework.common.core.KeyValue; | ||
| 5 | -import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils; | ||
| 6 | -import cn.iocoder.yudao.framework.common.util.collection.MapUtils; | ||
| 7 | -import cn.iocoder.yudao.framework.common.util.json.JsonUtils; | ||
| 8 | import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; | 4 | import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; |
| 5 | +import cn.iocoder.yudao.module.system.framework.sms.core.client.SmsClient; | ||
| 9 | import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsReceiveRespDTO; | 6 | import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsReceiveRespDTO; |
| 10 | -import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsSendRespDTO; | ||
| 11 | -import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsTemplateRespDTO; | ||
| 12 | import cn.iocoder.yudao.module.system.framework.sms.core.enums.SmsTemplateAuditStatusEnum; | 7 | import cn.iocoder.yudao.module.system.framework.sms.core.enums.SmsTemplateAuditStatusEnum; |
| 13 | import cn.iocoder.yudao.module.system.framework.sms.core.property.SmsChannelProperties; | 8 | import cn.iocoder.yudao.module.system.framework.sms.core.property.SmsChannelProperties; |
| 14 | -import com.google.common.collect.Lists; | ||
| 15 | -import com.tencentcloudapi.sms.v20210111.SmsClient; | ||
| 16 | -import com.tencentcloudapi.sms.v20210111.models.DescribeSmsTemplateListResponse; | ||
| 17 | -import com.tencentcloudapi.sms.v20210111.models.DescribeTemplateListStatus; | ||
| 18 | -import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse; | ||
| 19 | -import com.tencentcloudapi.sms.v20210111.models.SendStatus; | ||
| 20 | import org.junit.jupiter.api.Test; | 9 | import org.junit.jupiter.api.Test; |
| 21 | import org.mockito.InjectMocks; | 10 | import org.mockito.InjectMocks; |
| 22 | import org.mockito.Mock; | 11 | import org.mockito.Mock; |
| 23 | 12 | ||
| 24 | import java.time.LocalDateTime; | 13 | import java.time.LocalDateTime; |
| 25 | -import java.util.ArrayList; | ||
| 26 | import java.util.List; | 14 | import java.util.List; |
| 27 | 15 | ||
| 28 | -import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString; | ||
| 29 | -import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; | 16 | +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString; |
| 30 | import static org.junit.jupiter.api.Assertions.*; | 17 | import static org.junit.jupiter.api.Assertions.*; |
| 31 | -import static org.mockito.ArgumentMatchers.argThat; | ||
| 32 | -import static org.mockito.Mockito.when; | ||
| 33 | 18 | ||
| 19 | +// TODO @芋艿:补全单测 | ||
| 34 | /** | 20 | /** |
| 35 | * {@link TencentSmsClient} 的单元测试 | 21 | * {@link TencentSmsClient} 的单元测试 |
| 36 | * | 22 | * |
| @@ -73,87 +59,87 @@ public class TencentSmsClientTest extends BaseMockitoUnitTest { | @@ -73,87 +59,87 @@ public class TencentSmsClientTest extends BaseMockitoUnitTest { | ||
| 73 | assertNotSame(client, ReflectUtil.getFieldValue(smsClient, "client")); | 59 | assertNotSame(client, ReflectUtil.getFieldValue(smsClient, "client")); |
| 74 | } | 60 | } |
| 75 | 61 | ||
| 76 | - @Test | ||
| 77 | - public void testDoSendSms_success() throws Throwable { | ||
| 78 | - // 准备参数 | ||
| 79 | - Long sendLogId = randomLongId(); | ||
| 80 | - String mobile = randomString(); | ||
| 81 | - String apiTemplateId = randomString(); | ||
| 82 | - List<KeyValue<String, Object>> templateParams = Lists.newArrayList( | ||
| 83 | - new KeyValue<>("1", 1234), new KeyValue<>("2", "login")); | ||
| 84 | - String requestId = randomString(); | ||
| 85 | - String serialNo = randomString(); | ||
| 86 | - // mock 方法 | ||
| 87 | - SendSmsResponse response = randomPojo(SendSmsResponse.class, o -> { | ||
| 88 | - o.setRequestId(requestId); | ||
| 89 | - SendStatus[] sendStatuses = new SendStatus[1]; | ||
| 90 | - o.setSendStatusSet(sendStatuses); | ||
| 91 | - SendStatus sendStatus = new SendStatus(); | ||
| 92 | - sendStatuses[0] = sendStatus; | ||
| 93 | - sendStatus.setCode(TencentSmsClient.API_CODE_SUCCESS); | ||
| 94 | - sendStatus.setMessage("send success"); | ||
| 95 | - sendStatus.setSerialNo(serialNo); | ||
| 96 | - }); | ||
| 97 | - when(client.SendSms(argThat(request -> { | ||
| 98 | - assertEquals(mobile, request.getPhoneNumberSet()[0]); | ||
| 99 | - assertEquals(properties.getSignature(), request.getSignName()); | ||
| 100 | - assertEquals(apiTemplateId, request.getTemplateId()); | ||
| 101 | - assertEquals(toJsonString(ArrayUtils.toArray(new ArrayList<>(MapUtils.convertMap(templateParams).values()), String::valueOf)), | ||
| 102 | - toJsonString(request.getTemplateParamSet())); | ||
| 103 | - assertEquals(sendLogId, ReflectUtil.getFieldValue(JsonUtils.parseObject(request.getSessionContext(), TencentSmsClient.SessionContext.class), "logId")); | ||
| 104 | - return true; | ||
| 105 | - }))).thenReturn(response); | ||
| 106 | - | ||
| 107 | - // 调用 | ||
| 108 | - SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile, apiTemplateId, templateParams); | ||
| 109 | - // 断言 | ||
| 110 | - assertTrue(result.getSuccess()); | ||
| 111 | - assertEquals(response.getRequestId(), result.getApiRequestId()); | ||
| 112 | - assertEquals(response.getSendStatusSet()[0].getCode(), result.getApiCode()); | ||
| 113 | - assertEquals(response.getSendStatusSet()[0].getMessage(), result.getApiMsg()); | ||
| 114 | - assertEquals(response.getSendStatusSet()[0].getSerialNo(), result.getSerialNo()); | ||
| 115 | - } | ||
| 116 | - | ||
| 117 | - @Test | ||
| 118 | - public void testDoSendSms_fail() throws Throwable { | ||
| 119 | - // 准备参数 | ||
| 120 | - Long sendLogId = randomLongId(); | ||
| 121 | - String mobile = randomString(); | ||
| 122 | - String apiTemplateId = randomString(); | ||
| 123 | - List<KeyValue<String, Object>> templateParams = Lists.newArrayList( | ||
| 124 | - new KeyValue<>("1", 1234), new KeyValue<>("2", "login")); | ||
| 125 | - String requestId = randomString(); | ||
| 126 | - String serialNo = randomString(); | ||
| 127 | - // mock 方法 | ||
| 128 | - SendSmsResponse response = randomPojo(SendSmsResponse.class, o -> { | ||
| 129 | - o.setRequestId(requestId); | ||
| 130 | - SendStatus[] sendStatuses = new SendStatus[1]; | ||
| 131 | - o.setSendStatusSet(sendStatuses); | ||
| 132 | - SendStatus sendStatus = new SendStatus(); | ||
| 133 | - sendStatuses[0] = sendStatus; | ||
| 134 | - sendStatus.setCode("ERROR"); | ||
| 135 | - sendStatus.setMessage("send success"); | ||
| 136 | - sendStatus.setSerialNo(serialNo); | ||
| 137 | - }); | ||
| 138 | - when(client.SendSms(argThat(request -> { | ||
| 139 | - assertEquals(mobile, request.getPhoneNumberSet()[0]); | ||
| 140 | - assertEquals(properties.getSignature(), request.getSignName()); | ||
| 141 | - assertEquals(apiTemplateId, request.getTemplateId()); | ||
| 142 | - assertEquals(toJsonString(ArrayUtils.toArray(new ArrayList<>(MapUtils.convertMap(templateParams).values()), String::valueOf)), | ||
| 143 | - toJsonString(request.getTemplateParamSet())); | ||
| 144 | - assertEquals(sendLogId, ReflectUtil.getFieldValue(JsonUtils.parseObject(request.getSessionContext(), TencentSmsClient.SessionContext.class), "logId")); | ||
| 145 | - return true; | ||
| 146 | - }))).thenReturn(response); | ||
| 147 | - | ||
| 148 | - // 调用 | ||
| 149 | - SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile, apiTemplateId, templateParams); | ||
| 150 | - // 断言 | ||
| 151 | - assertFalse(result.getSuccess()); | ||
| 152 | - assertEquals(response.getRequestId(), result.getApiRequestId()); | ||
| 153 | - assertEquals(response.getSendStatusSet()[0].getCode(), result.getApiCode()); | ||
| 154 | - assertEquals(response.getSendStatusSet()[0].getMessage(), result.getApiMsg()); | ||
| 155 | - assertEquals(response.getSendStatusSet()[0].getSerialNo(), result.getSerialNo()); | ||
| 156 | - } | 62 | +// @Test |
| 63 | +// public void testDoSendSms_success() throws Throwable { | ||
| 64 | +// // 准备参数 | ||
| 65 | +// Long sendLogId = randomLongId(); | ||
| 66 | +// String mobile = randomString(); | ||
| 67 | +// String apiTemplateId = randomString(); | ||
| 68 | +// List<KeyValue<String, Object>> templateParams = Lists.newArrayList( | ||
| 69 | +// new KeyValue<>("1", 1234), new KeyValue<>("2", "login")); | ||
| 70 | +// String requestId = randomString(); | ||
| 71 | +// String serialNo = randomString(); | ||
| 72 | +// // mock 方法 | ||
| 73 | +// SendSmsResponse response = randomPojo(SendSmsResponse.class, o -> { | ||
| 74 | +// o.setRequestId(requestId); | ||
| 75 | +// SendStatus[] sendStatuses = new SendStatus[1]; | ||
| 76 | +// o.setSendStatusSet(sendStatuses); | ||
| 77 | +// SendStatus sendStatus = new SendStatus(); | ||
| 78 | +// sendStatuses[0] = sendStatus; | ||
| 79 | +// sendStatus.setCode(TencentSmsClient.API_CODE_SUCCESS); | ||
| 80 | +// sendStatus.setMessage("send success"); | ||
| 81 | +// sendStatus.setSerialNo(serialNo); | ||
| 82 | +// }); | ||
| 83 | +// when(client.SendSms(argThat(request -> { | ||
| 84 | +// assertEquals(mobile, request.getPhoneNumberSet()[0]); | ||
| 85 | +// assertEquals(properties.getSignature(), request.getSignName()); | ||
| 86 | +// assertEquals(apiTemplateId, request.getTemplateId()); | ||
| 87 | +// assertEquals(toJsonString(ArrayUtils.toArray(new ArrayList<>(MapUtils.convertMap(templateParams).values()), String::valueOf)), | ||
| 88 | +// toJsonString(request.getTemplateParamSet())); | ||
| 89 | +// assertEquals(sendLogId, ReflectUtil.getFieldValue(JsonUtils.parseObject(request.getSessionContext(), TencentSmsClient.SessionContext.class), "logId")); | ||
| 90 | +// return true; | ||
| 91 | +// }))).thenReturn(response); | ||
| 92 | +// | ||
| 93 | +// // 调用 | ||
| 94 | +// SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile, apiTemplateId, templateParams); | ||
| 95 | +// // 断言 | ||
| 96 | +// assertTrue(result.getSuccess()); | ||
| 97 | +// assertEquals(response.getRequestId(), result.getApiRequestId()); | ||
| 98 | +// assertEquals(response.getSendStatusSet()[0].getCode(), result.getApiCode()); | ||
| 99 | +// assertEquals(response.getSendStatusSet()[0].getMessage(), result.getApiMsg()); | ||
| 100 | +// assertEquals(response.getSendStatusSet()[0].getSerialNo(), result.getSerialNo()); | ||
| 101 | +// } | ||
| 102 | + | ||
| 103 | +// @Test | ||
| 104 | +// public void testDoSendSms_fail() throws Throwable { | ||
| 105 | +// // 准备参数 | ||
| 106 | +// Long sendLogId = randomLongId(); | ||
| 107 | +// String mobile = randomString(); | ||
| 108 | +// String apiTemplateId = randomString(); | ||
| 109 | +// List<KeyValue<String, Object>> templateParams = Lists.newArrayList( | ||
| 110 | +// new KeyValue<>("1", 1234), new KeyValue<>("2", "login")); | ||
| 111 | +// String requestId = randomString(); | ||
| 112 | +// String serialNo = randomString(); | ||
| 113 | +// // mock 方法 | ||
| 114 | +// SendSmsResponse response = randomPojo(SendSmsResponse.class, o -> { | ||
| 115 | +// o.setRequestId(requestId); | ||
| 116 | +// SendStatus[] sendStatuses = new SendStatus[1]; | ||
| 117 | +// o.setSendStatusSet(sendStatuses); | ||
| 118 | +// SendStatus sendStatus = new SendStatus(); | ||
| 119 | +// sendStatuses[0] = sendStatus; | ||
| 120 | +// sendStatus.setCode("ERROR"); | ||
| 121 | +// sendStatus.setMessage("send success"); | ||
| 122 | +// sendStatus.setSerialNo(serialNo); | ||
| 123 | +// }); | ||
| 124 | +// when(client.SendSms(argThat(request -> { | ||
| 125 | +// assertEquals(mobile, request.getPhoneNumberSet()[0]); | ||
| 126 | +// assertEquals(properties.getSignature(), request.getSignName()); | ||
| 127 | +// assertEquals(apiTemplateId, request.getTemplateId()); | ||
| 128 | +// assertEquals(toJsonString(ArrayUtils.toArray(new ArrayList<>(MapUtils.convertMap(templateParams).values()), String::valueOf)), | ||
| 129 | +// toJsonString(request.getTemplateParamSet())); | ||
| 130 | +// assertEquals(sendLogId, ReflectUtil.getFieldValue(JsonUtils.parseObject(request.getSessionContext(), TencentSmsClient.SessionContext.class), "logId")); | ||
| 131 | +// return true; | ||
| 132 | +// }))).thenReturn(response); | ||
| 133 | +// | ||
| 134 | +// // 调用 | ||
| 135 | +// SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile, apiTemplateId, templateParams); | ||
| 136 | +// // 断言 | ||
| 137 | +// assertFalse(result.getSuccess()); | ||
| 138 | +// assertEquals(response.getRequestId(), result.getApiRequestId()); | ||
| 139 | +// assertEquals(response.getSendStatusSet()[0].getCode(), result.getApiCode()); | ||
| 140 | +// assertEquals(response.getSendStatusSet()[0].getMessage(), result.getApiMsg()); | ||
| 141 | +// assertEquals(response.getSendStatusSet()[0].getSerialNo(), result.getSerialNo()); | ||
| 142 | +// } | ||
| 157 | 143 | ||
| 158 | @Test | 144 | @Test |
| 159 | public void testParseSmsReceiveStatus() { | 145 | public void testParseSmsReceiveStatus() { |
| @@ -185,35 +171,35 @@ public class TencentSmsClientTest extends BaseMockitoUnitTest { | @@ -185,35 +171,35 @@ public class TencentSmsClientTest extends BaseMockitoUnitTest { | ||
| 185 | assertEquals(67890L, statuses.get(0).getLogId()); | 171 | assertEquals(67890L, statuses.get(0).getLogId()); |
| 186 | } | 172 | } |
| 187 | 173 | ||
| 188 | - @Test | ||
| 189 | - public void testGetSmsTemplate() throws Throwable { | ||
| 190 | - // 准备参数 | ||
| 191 | - Long apiTemplateId = randomLongId(); | ||
| 192 | - String requestId = randomString(); | ||
| 193 | - | ||
| 194 | - // mock 方法 | ||
| 195 | - DescribeSmsTemplateListResponse response = randomPojo(DescribeSmsTemplateListResponse.class, o -> { | ||
| 196 | - DescribeTemplateListStatus[] describeTemplateListStatuses = new DescribeTemplateListStatus[1]; | ||
| 197 | - DescribeTemplateListStatus templateStatus = new DescribeTemplateListStatus(); | ||
| 198 | - templateStatus.setTemplateId(apiTemplateId); | ||
| 199 | - templateStatus.setStatusCode(0L);// 设置模板通过 | ||
| 200 | - describeTemplateListStatuses[0] = templateStatus; | ||
| 201 | - o.setDescribeTemplateStatusSet(describeTemplateListStatuses); | ||
| 202 | - o.setRequestId(requestId); | ||
| 203 | - }); | ||
| 204 | - when(client.DescribeSmsTemplateList(argThat(request -> { | ||
| 205 | - assertEquals(apiTemplateId, request.getTemplateIdSet()[0]); | ||
| 206 | - return true; | ||
| 207 | - }))).thenReturn(response); | ||
| 208 | - | ||
| 209 | - // 调用 | ||
| 210 | - SmsTemplateRespDTO result = smsClient.getSmsTemplate(apiTemplateId.toString()); | ||
| 211 | - // 断言 | ||
| 212 | - assertEquals(response.getDescribeTemplateStatusSet()[0].getTemplateId().toString(), result.getId()); | ||
| 213 | - assertEquals(response.getDescribeTemplateStatusSet()[0].getTemplateContent(), result.getContent()); | ||
| 214 | - assertEquals(SmsTemplateAuditStatusEnum.SUCCESS.getStatus(), result.getAuditStatus()); | ||
| 215 | - assertEquals(response.getDescribeTemplateStatusSet()[0].getReviewReply(), result.getAuditReason()); | ||
| 216 | - } | 174 | +// @Test |
| 175 | +// public void testGetSmsTemplate() throws Throwable { | ||
| 176 | +// // 准备参数 | ||
| 177 | +// Long apiTemplateId = randomLongId(); | ||
| 178 | +// String requestId = randomString(); | ||
| 179 | +// | ||
| 180 | +// // mock 方法 | ||
| 181 | +// DescribeSmsTemplateListResponse response = randomPojo(DescribeSmsTemplateListResponse.class, o -> { | ||
| 182 | +// DescribeTemplateListStatus[] describeTemplateListStatuses = new DescribeTemplateListStatus[1]; | ||
| 183 | +// DescribeTemplateListStatus templateStatus = new DescribeTemplateListStatus(); | ||
| 184 | +// templateStatus.setTemplateId(apiTemplateId); | ||
| 185 | +// templateStatus.setStatusCode(0L);// 设置模板通过 | ||
| 186 | +// describeTemplateListStatuses[0] = templateStatus; | ||
| 187 | +// o.setDescribeTemplateStatusSet(describeTemplateListStatuses); | ||
| 188 | +// o.setRequestId(requestId); | ||
| 189 | +// }); | ||
| 190 | +// when(client.DescribeSmsTemplateList(argThat(request -> { | ||
| 191 | +// assertEquals(apiTemplateId, request.getTemplateIdSet()[0]); | ||
| 192 | +// return true; | ||
| 193 | +// }))).thenReturn(response); | ||
| 194 | +// | ||
| 195 | +// // 调用 | ||
| 196 | +// SmsTemplateRespDTO result = smsClient.getSmsTemplate(apiTemplateId.toString()); | ||
| 197 | +// // 断言 | ||
| 198 | +// assertEquals(response.getDescribeTemplateStatusSet()[0].getTemplateId().toString(), result.getId()); | ||
| 199 | +// assertEquals(response.getDescribeTemplateStatusSet()[0].getTemplateContent(), result.getContent()); | ||
| 200 | +// assertEquals(SmsTemplateAuditStatusEnum.SUCCESS.getStatus(), result.getAuditStatus()); | ||
| 201 | +// assertEquals(response.getDescribeTemplateStatusSet()[0].getReviewReply(), result.getAuditReason()); | ||
| 202 | +// } | ||
| 217 | 203 | ||
| 218 | @Test | 204 | @Test |
| 219 | public void testConvertSmsTemplateAuditStatus() { | 205 | public void testConvertSmsTemplateAuditStatus() { |