From 82f1f96450ec172e7586ba3150598d89d372373c Mon Sep 17 00:00:00 2001 From: smallchill Date: Sat, 22 Feb 2020 17:00:13 +0800 Subject: [PATCH] =?UTF-8?q?:tada:=20=E5=A2=9E=E5=8A=A0=E7=9F=AD=E4=BF=A1?= =?UTF-8?q?=E8=BF=9C=E7=A8=8B=E8=B0=83=E7=94=A8=EF=BC=8C=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E7=9F=AD=E4=BF=A1=E5=8F=91=E9=80=81=E5=B7=A5=E5=85=B7=E7=B1=BB?= =?UTF-8?q?SmsUtil?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../flow/core/feign/IFlowClient.java | 10 +- blade-ops-api/blade-resource-api/pom.xml | 28 +++++ .../org/springblade/resource/entity/Oss.java | 0 .../org/springblade/resource/entity/Sms.java | 0 .../resource/enums/SmsCodeEnum.java | 62 ++++++++++ .../resource/feign/ISmsClient.java | 72 ++++++++++++ .../resource/feign/ISmsClientFallback.java | 43 +++++++ .../springblade/resource/utils/SmsUtil.java | 107 ++++++++++++++++++ .../org/springblade/resource/vo/OssVO.java | 0 .../org/springblade/resource/vo/SmsVO.java | 4 +- blade-ops-api/pom.xml | 1 + blade-ops/blade-resource/pom.xml | 5 + .../resource/builder/oss/OssBuilder.java | 24 +++- .../resource/builder/sms/SmsBuilder.java | 24 +++- .../resource/endpoint/OssEndpoint.java | 3 + .../resource/endpoint/SmsEndpoint.java | 16 +-- .../springblade/resource/feign/SmsClient.java | 67 +++++++++++ 17 files changed, 441 insertions(+), 25 deletions(-) create mode 100644 blade-ops-api/blade-resource-api/pom.xml rename {blade-ops/blade-resource => blade-ops-api/blade-resource-api}/src/main/java/org/springblade/resource/entity/Oss.java (100%) rename {blade-ops/blade-resource => blade-ops-api/blade-resource-api}/src/main/java/org/springblade/resource/entity/Sms.java (100%) create mode 100644 blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/enums/SmsCodeEnum.java create mode 100644 blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/feign/ISmsClient.java create mode 100644 blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/feign/ISmsClientFallback.java create mode 100644 blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/utils/SmsUtil.java rename {blade-ops/blade-resource => blade-ops-api/blade-resource-api}/src/main/java/org/springblade/resource/vo/OssVO.java (100%) rename {blade-ops/blade-resource => blade-ops-api/blade-resource-api}/src/main/java/org/springblade/resource/vo/SmsVO.java (100%) create mode 100644 blade-ops/blade-resource/src/main/java/org/springblade/resource/feign/SmsClient.java diff --git a/blade-ops-api/blade-flow-api/src/main/java/org/springblade/flow/core/feign/IFlowClient.java b/blade-ops-api/blade-flow-api/src/main/java/org/springblade/flow/core/feign/IFlowClient.java index abbf0d8d..03d86bf0 100644 --- a/blade-ops-api/blade-flow-api/src/main/java/org/springblade/flow/core/feign/IFlowClient.java +++ b/blade-ops-api/blade-flow-api/src/main/java/org/springblade/flow/core/feign/IFlowClient.java @@ -39,11 +39,11 @@ import java.util.Map; public interface IFlowClient { String API_PREFIX = "/client"; - String START_PROCESS_INSTANCE_BY_ID = API_PREFIX + "start-process-instance-by-id"; - String START_PROCESS_INSTANCE_BY_KEY = API_PREFIX + "start-process-instance-by-key"; - String COMPLETE_TASK = API_PREFIX + "complete-task"; - String TASK_VARIABLE = API_PREFIX + "task-variable"; - String TASK_VARIABLES = API_PREFIX + "task-variables"; + String START_PROCESS_INSTANCE_BY_ID = API_PREFIX + "/start-process-instance-by-id"; + String START_PROCESS_INSTANCE_BY_KEY = API_PREFIX + "/start-process-instance-by-key"; + String COMPLETE_TASK = API_PREFIX + "/complete-task"; + String TASK_VARIABLE = API_PREFIX + "/task-variable"; + String TASK_VARIABLES = API_PREFIX + "/task-variables"; /** * 开启流程 diff --git a/blade-ops-api/blade-resource-api/pom.xml b/blade-ops-api/blade-resource-api/pom.xml new file mode 100644 index 00000000..0c5f541c --- /dev/null +++ b/blade-ops-api/blade-resource-api/pom.xml @@ -0,0 +1,28 @@ + + + + blade-ops-api + org.springblade + 2.3.1.RELEASE + + 4.0.0 + + blade-resource-api + ${project.artifactId} + ${bladex.project.version} + jar + + + + org.springblade + blade-core-sms + + + org.springblade + blade-starter-tenant + + + + diff --git a/blade-ops/blade-resource/src/main/java/org/springblade/resource/entity/Oss.java b/blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/entity/Oss.java similarity index 100% rename from blade-ops/blade-resource/src/main/java/org/springblade/resource/entity/Oss.java rename to blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/entity/Oss.java diff --git a/blade-ops/blade-resource/src/main/java/org/springblade/resource/entity/Sms.java b/blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/entity/Sms.java similarity index 100% rename from blade-ops/blade-resource/src/main/java/org/springblade/resource/entity/Sms.java rename to blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/entity/Sms.java diff --git a/blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/enums/SmsCodeEnum.java b/blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/enums/SmsCodeEnum.java new file mode 100644 index 00000000..949681f6 --- /dev/null +++ b/blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/enums/SmsCodeEnum.java @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the dreamlu.net developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: Chill 庄骞 (smallchill@163.com) + */ +package org.springblade.resource.enums; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import org.springblade.core.tool.utils.StringPool; + +/** + * Sms资源编码枚举类 + * + * @author Chill + * @apiNote 该枚举类对应短信配置模块的资源编码,可根据业务需求自行拓展 + */ +@Getter +@AllArgsConstructor +public enum SmsCodeEnum { + + /** + * 默认编号 + */ + DEFAULT(StringPool.EMPTY, 1), + + /** + * 验证码编号 + */ + VALIDATE("validate", 2), + + /** + * 通知公告编号 + */ + NOTICE("notice", 3), + + /** + * 下单通知编号 + */ + ORDER("order", 4), + + /** + * 会议通知编号 + */ + MEETING("meeting", 5), + ; + + final String name; + final int category; + +} diff --git a/blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/feign/ISmsClient.java b/blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/feign/ISmsClient.java new file mode 100644 index 00000000..c75bb911 --- /dev/null +++ b/blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/feign/ISmsClient.java @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the dreamlu.net developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: Chill 庄骞 (smallchill@163.com) + */ +package org.springblade.resource.feign; + +import org.springblade.core.launch.constant.AppConstant; +import org.springblade.core.tool.api.R; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; + +/** + * ISmsClient + * + * @author Chill + */ +@FeignClient( + value = AppConstant.APPLICATION_RESOURCE_NAME, + fallback = ISmsClientFallback.class +) +public interface ISmsClient { + String API_PREFIX = "/client"; + String SEND_MESSAGE = API_PREFIX + "/send-message"; + String SEND_VALIDATE = API_PREFIX + "/send-validate"; + String VALIDATE_MESSAGE = API_PREFIX + "/validate-message"; + + /** + * 通用短信发送 + * + * @param code 资源编号 + * @param params 模板参数 + * @param phones 手机号集合 + * @return R + */ + @PostMapping(SEND_MESSAGE) + R sendMessage(@RequestParam("code") String code, @RequestParam("params") String params, @RequestParam("phones") String phones); + + /** + * 短信验证码发送 + * + * @param code 资源编号 + * @param phone 手机号 + * @return R + */ + @PostMapping(SEND_VALIDATE) + R sendValidate(@RequestParam("code") String code, @RequestParam("phone") String phone); + + /** + * 校验短信 + * + * @param code 资源编号 + * @param id 校验id + * @param value 校验值 + * @return R + */ + @PostMapping(VALIDATE_MESSAGE) + R validateMessage(@RequestParam("code") String code, @RequestParam("id") String id, @RequestParam("value") String value); + +} diff --git a/blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/feign/ISmsClientFallback.java b/blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/feign/ISmsClientFallback.java new file mode 100644 index 00000000..8051953b --- /dev/null +++ b/blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/feign/ISmsClientFallback.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the dreamlu.net developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: Chill 庄骞 (smallchill@163.com) + */ +package org.springblade.resource.feign; + +import org.springblade.core.tool.api.R; +import org.springframework.stereotype.Component; + +/** + * 流程远程调用失败处理类 + * + * @author Chill + */ +@Component +public class ISmsClientFallback implements ISmsClient { + @Override + public R sendMessage(String code, String params, String phones) { + return R.fail("远程调用失败"); + } + + @Override + public R sendValidate(String code, String phone) { + return R.fail("远程调用失败"); + } + + @Override + public R validateMessage(String code, String id, String value) { + return R.fail("远程调用失败"); + } +} diff --git a/blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/utils/SmsUtil.java b/blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/utils/SmsUtil.java new file mode 100644 index 00000000..c7ca483a --- /dev/null +++ b/blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/utils/SmsUtil.java @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the dreamlu.net developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: Chill 庄骞 (smallchill@163.com) + */ +package org.springblade.resource.utils; + +import org.springblade.core.sms.model.SmsCode; +import org.springblade.core.tool.api.R; +import org.springblade.core.tool.jackson.JsonUtil; +import org.springblade.core.tool.utils.RandomType; +import org.springblade.core.tool.utils.SpringUtil; +import org.springblade.core.tool.utils.StringUtil; +import org.springblade.resource.feign.ISmsClient; + +import java.util.HashMap; +import java.util.Map; + +/** + * 短信服务工具类 + * + * @author Chill + */ +public class SmsUtil { + + public static final String PARAM_KEY = "code"; + public static final String SEND_SUCCESS = "短信发送成功"; + public static final String SEND_FAIL = "短信发送失败"; + public static final String VALIDATE_SUCCESS = "短信校验成功"; + public static final String VALIDATE_FAIL = "短信校验失败"; + + private static ISmsClient smsClient; + + public static ISmsClient getSmsClient() { + if (smsClient == null) { + smsClient = SpringUtil.getBean(ISmsClient.class); + } + return smsClient; + } + + /** + * 获取短信验证码参数 + * + * @return 验证码参数 + */ + public static Map getValidateParams() { + Map params = new HashMap<>(1); + params.put(PARAM_KEY, StringUtil.random(6, RandomType.INT)); + return params; + } + + /** + * 发送短信 + * + * @param code 资源编号 + * @param params 模板参数 + * @param phones 手机号集合 + * @return 发送结果 + */ + public static boolean sendMessage(String code, Map params, String phones) { + R result = getSmsClient().sendMessage(code, JsonUtil.toJson(params), phones); + return result.isSuccess(); + } + + /** + * 发送验证码 + * + * @param code 资源编号 + * @param phone 手机号 + * @return 发送结果 + */ + public static SmsCode sendValidate(String code, String phone) { + SmsCode smsCode = new SmsCode(); + R result = getSmsClient().sendValidate(code, phone); + if (result.isSuccess()) { + smsCode = JsonUtil.parse(JsonUtil.toJson(result.getData()), SmsCode.class); + } else { + smsCode.setSuccess(Boolean.FALSE); + } + return smsCode; + } + + /** + * 校验短信 + * + * @param code 资源编号 + * @param id 校验id + * @param value 校验值 + * @return 发送结果 + */ + public static boolean validateMessage(String code, String id, String value) { + R result = getSmsClient().validateMessage(code, id, value); + return result.isSuccess(); + } + +} diff --git a/blade-ops/blade-resource/src/main/java/org/springblade/resource/vo/OssVO.java b/blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/vo/OssVO.java similarity index 100% rename from blade-ops/blade-resource/src/main/java/org/springblade/resource/vo/OssVO.java rename to blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/vo/OssVO.java diff --git a/blade-ops/blade-resource/src/main/java/org/springblade/resource/vo/SmsVO.java b/blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/vo/SmsVO.java similarity index 100% rename from blade-ops/blade-resource/src/main/java/org/springblade/resource/vo/SmsVO.java rename to blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/vo/SmsVO.java index d141b943..adf860ad 100644 --- a/blade-ops/blade-resource/src/main/java/org/springblade/resource/vo/SmsVO.java +++ b/blade-ops-api/blade-resource-api/src/main/java/org/springblade/resource/vo/SmsVO.java @@ -16,10 +16,10 @@ */ package org.springblade.resource.vo; -import org.springblade.resource.entity.Sms; +import io.swagger.annotations.ApiModel; import lombok.Data; import lombok.EqualsAndHashCode; -import io.swagger.annotations.ApiModel; +import org.springblade.resource.entity.Sms; /** * 短信配置表视图实体类 diff --git a/blade-ops-api/pom.xml b/blade-ops-api/pom.xml index 53399a71..bd835fce 100644 --- a/blade-ops-api/pom.xml +++ b/blade-ops-api/pom.xml @@ -17,6 +17,7 @@ blade-flow-api + blade-resource-api diff --git a/blade-ops/blade-resource/pom.xml b/blade-ops/blade-resource/pom.xml index 0b939408..387e233a 100644 --- a/blade-ops/blade-resource/pom.xml +++ b/blade-ops/blade-resource/pom.xml @@ -35,6 +35,11 @@ org.springblade blade-starter-sms-all + + org.springblade + blade-resource-api + ${bladex.project.version} + org.springblade blade-dict-api diff --git a/blade-ops/blade-resource/src/main/java/org/springblade/resource/builder/oss/OssBuilder.java b/blade-ops/blade-resource/src/main/java/org/springblade/resource/builder/oss/OssBuilder.java index ea48a3ae..94895efa 100644 --- a/blade-ops/blade-resource/src/main/java/org/springblade/resource/builder/oss/OssBuilder.java +++ b/blade-ops/blade-resource/src/main/java/org/springblade/resource/builder/oss/OssBuilder.java @@ -19,6 +19,7 @@ package org.springblade.resource.builder.oss; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import org.springblade.core.cache.utils.CacheUtil; +import org.springblade.core.log.exception.ServiceException; import org.springblade.core.oss.OssTemplate; import org.springblade.core.oss.enums.OssEnum; import org.springblade.core.oss.enums.OssStatusEnum; @@ -72,8 +73,18 @@ public class OssBuilder { * @return OssTemplate */ public OssTemplate template() { + return template(StringPool.EMPTY); + } + + /** + * 获取template + * + * @param code 资源编号 + * @return OssTemplate + */ + public OssTemplate template(String code) { String tenantId = SecureUtil.getTenantId(); - Oss oss = getOss(tenantId); + Oss oss = getOss(tenantId, code); Oss ossCached = ossPool.get(tenantId); OssTemplate template = templatePool.get(tenantId); // 若为空或者不一致,则重新加载 @@ -111,18 +122,18 @@ public class OssBuilder { * @param tenantId 租户ID * @return Oss */ - public Oss getOss(String tenantId) { + public Oss getOss(String tenantId, String code) { String key = tenantId; LambdaQueryWrapper lqw = Wrappers.query().lambda().eq(Oss::getTenantId, tenantId); // 获取传参的资源编号并查询,若有则返回,若没有则调启用的配置 - String ossCode = WebUtil.getParameter(OSS_PARAM_KEY); + String ossCode = StringUtil.isBlank(code) ? WebUtil.getParameter(OSS_PARAM_KEY) : code; if (StringUtil.isNotBlank(ossCode)) { key = key.concat(StringPool.DASH).concat(ossCode); lqw.eq(Oss::getOssCode, ossCode); } else { lqw.eq(Oss::getStatus, OssStatusEnum.ENABLE.getNum()); } - return CacheUtil.get(RESOURCE_CACHE, OSS_CODE, key, () -> { + Oss oss = CacheUtil.get(RESOURCE_CACHE, OSS_CODE, key, () -> { Oss o = ossMapper.selectOne(lqw); // 若为空则调用默认配置 if ((Func.isEmpty(o))) { @@ -137,6 +148,11 @@ public class OssBuilder { return o; } }); + if (oss == null || oss.getId() == null) { + throw new ServiceException("未获取到对应的对象存储配置"); + } else { + return oss; + } } diff --git a/blade-ops/blade-resource/src/main/java/org/springblade/resource/builder/sms/SmsBuilder.java b/blade-ops/blade-resource/src/main/java/org/springblade/resource/builder/sms/SmsBuilder.java index 1a35388f..d6d14a8d 100644 --- a/blade-ops/blade-resource/src/main/java/org/springblade/resource/builder/sms/SmsBuilder.java +++ b/blade-ops/blade-resource/src/main/java/org/springblade/resource/builder/sms/SmsBuilder.java @@ -19,6 +19,7 @@ package org.springblade.resource.builder.sms; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import org.springblade.core.cache.utils.CacheUtil; +import org.springblade.core.log.exception.ServiceException; import org.springblade.core.redis.cache.BladeRedisCache; import org.springblade.core.secure.utils.SecureUtil; import org.springblade.core.sms.SmsTemplate; @@ -75,8 +76,18 @@ public class SmsBuilder { * @return SmsTemplate */ public SmsTemplate template() { + return template(StringPool.EMPTY); + } + + /** + * 获取template + * + * @param code 资源编号 + * @return SmsTemplate + */ + public SmsTemplate template(String code) { String tenantId = SecureUtil.getTenantId(); - Sms sms = getSms(tenantId); + Sms sms = getSms(tenantId, code); Sms smsCached = smsPool.get(tenantId); SmsTemplate template = templatePool.get(tenantId); // 若为空或者不一致,则重新加载 @@ -108,18 +119,18 @@ public class SmsBuilder { * @param tenantId 租户ID * @return Sms */ - public Sms getSms(String tenantId) { + public Sms getSms(String tenantId, String code) { String key = tenantId; LambdaQueryWrapper lqw = Wrappers.query().lambda().eq(Sms::getTenantId, tenantId); // 获取传参的资源编号并查询,若有则返回,若没有则调启用的配置 - String smsCode = WebUtil.getParameter(SMS_PARAM_KEY); + String smsCode = StringUtil.isBlank(code) ? WebUtil.getParameter(SMS_PARAM_KEY) : code; if (StringUtil.isNotBlank(smsCode)) { key = key.concat(StringPool.DASH).concat(smsCode); lqw.eq(Sms::getSmsCode, smsCode); } else { lqw.eq(Sms::getStatus, SmsStatusEnum.ENABLE.getNum()); } - return CacheUtil.get(RESOURCE_CACHE, SMS_CODE, key, () -> { + Sms sms = CacheUtil.get(RESOURCE_CACHE, SMS_CODE, key, () -> { Sms s = smsMapper.selectOne(lqw); // 若为空则调用默认配置 if ((Func.isEmpty(s))) { @@ -133,6 +144,11 @@ public class SmsBuilder { return s; } }); + if (sms == null || sms.getId() == null) { + throw new ServiceException("未获取到对应的短信配置"); + } else { + return sms; + } } } diff --git a/blade-ops/blade-resource/src/main/java/org/springblade/resource/endpoint/OssEndpoint.java b/blade-ops/blade-resource/src/main/java/org/springblade/resource/endpoint/OssEndpoint.java index 322365c8..91566fd5 100644 --- a/blade-ops/blade-resource/src/main/java/org/springblade/resource/endpoint/OssEndpoint.java +++ b/blade-ops/blade-resource/src/main/java/org/springblade/resource/endpoint/OssEndpoint.java @@ -40,6 +40,9 @@ import org.springframework.web.multipart.MultipartFile; @Api(value = "对象存储端点", tags = "对象存储端点") public class OssEndpoint { + /** + * 对象存储构建类 + */ private OssBuilder ossBuilder; /** diff --git a/blade-ops/blade-resource/src/main/java/org/springblade/resource/endpoint/SmsEndpoint.java b/blade-ops/blade-resource/src/main/java/org/springblade/resource/endpoint/SmsEndpoint.java index 4d8ed74c..5263a073 100644 --- a/blade-ops/blade-resource/src/main/java/org/springblade/resource/endpoint/SmsEndpoint.java +++ b/blade-ops/blade-resource/src/main/java/org/springblade/resource/endpoint/SmsEndpoint.java @@ -24,8 +24,6 @@ import org.springblade.core.sms.model.SmsData; import org.springblade.core.tool.api.R; import org.springblade.core.tool.jackson.JsonUtil; import org.springblade.core.tool.utils.Func; -import org.springblade.core.tool.utils.RandomType; -import org.springblade.core.tool.utils.StringUtil; import org.springblade.resource.builder.sms.SmsBuilder; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -35,6 +33,8 @@ import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.Map; +import static org.springblade.resource.utils.SmsUtil.*; + /** * 短信服务端点 * @@ -46,12 +46,9 @@ import java.util.Map; @Api(value = "短信服务端点", tags = "短信服务端点") public class SmsEndpoint { - private static final String PARAM_KEY = "code"; - private static final String SEND_SUCCESS = "短信发送成功"; - private static final String SEND_FAIL = "短信发送失败"; - private static final String VALIDATE_SUCCESS = "短信校验成功"; - private static final String VALIDATE_FAIL = "短信校验失败"; - + /** + * 短信服务构建类 + */ private SmsBuilder smsBuilder; //================================= 短信服务校验 ================================= @@ -64,8 +61,7 @@ public class SmsEndpoint { @SneakyThrows @PostMapping("/send-validate") public R sendValidate(@RequestParam String phone) { - Map params = new HashMap<>(1); - params.put(PARAM_KEY, StringUtil.random(6, RandomType.INT)); + Map params = getValidateParams(); SmsCode smsCode = smsBuilder.template().sendValidate(new SmsData(params).setKey(PARAM_KEY), phone); return smsCode.isSuccess() ? R.data(smsCode, SEND_SUCCESS) : R.fail(SEND_FAIL); } diff --git a/blade-ops/blade-resource/src/main/java/org/springblade/resource/feign/SmsClient.java b/blade-ops/blade-resource/src/main/java/org/springblade/resource/feign/SmsClient.java new file mode 100644 index 00000000..39e8f53d --- /dev/null +++ b/blade-ops/blade-resource/src/main/java/org/springblade/resource/feign/SmsClient.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the dreamlu.net developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: Chill 庄骞 (smallchill@163.com) + */ +package org.springblade.resource.feign; + +import lombok.AllArgsConstructor; +import org.springblade.core.sms.model.SmsCode; +import org.springblade.core.sms.model.SmsData; +import org.springblade.core.tool.api.R; +import org.springblade.core.tool.jackson.JsonUtil; +import org.springblade.core.tool.utils.Func; +import org.springblade.resource.builder.sms.SmsBuilder; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Map; + +import static org.springblade.resource.utils.SmsUtil.*; + +/** + * 短信远程调用服务 + * + * @author Chill + */ +@RestController +@AllArgsConstructor +public class SmsClient implements ISmsClient { + + private SmsBuilder smsBuilder; + + @Override + @PostMapping(SEND_MESSAGE) + public R sendMessage(String code, String params, String phones) { + SmsData smsData = new SmsData(JsonUtil.readMap(params, String.class, String.class)); + boolean temp = smsBuilder.template(code).sendMulti(smsData, Func.toStrList(phones)); + return temp ? R.success(SEND_SUCCESS) : R.fail(SEND_FAIL); + } + + @Override + @PostMapping(SEND_VALIDATE) + public R sendValidate(String code, String phone) { + Map params = getValidateParams(); + SmsCode smsCode = smsBuilder.template(code).sendValidate(new SmsData(params).setKey(PARAM_KEY), phone); + return smsCode.isSuccess() ? R.data(smsCode, SEND_SUCCESS) : R.fail(SEND_FAIL); + } + + @Override + @PostMapping(VALIDATE_MESSAGE) + public R validateMessage(String code, String id, String value) { + SmsCode smsCode = new SmsCode().setId(id).setValue(value); + boolean validate = smsBuilder.template(code).validateMessage(smsCode); + return validate ? R.success(VALIDATE_SUCCESS) : R.fail(VALIDATE_FAIL); + } +}