diff --git a/epmet-auth/src/main/java/com/epmet/service/impl/GovLoginServiceImpl.java b/epmet-auth/src/main/java/com/epmet/service/impl/GovLoginServiceImpl.java index b1ad1b2908..45e34e282e 100644 --- a/epmet-auth/src/main/java/com/epmet/service/impl/GovLoginServiceImpl.java +++ b/epmet-auth/src/main/java/com/epmet/service/impl/GovLoginServiceImpl.java @@ -13,11 +13,13 @@ import com.epmet.commons.tools.utils.CpUserDetailRedis; import com.epmet.commons.tools.utils.DateUtils; import com.epmet.commons.tools.utils.Result; import com.epmet.commons.tools.validator.PhoneValidatorUtils; +import com.epmet.constant.SmsTemplateConstant; import com.epmet.dto.CustomerAgencyDTO; import com.epmet.dto.CustomerStaffDTO; import com.epmet.dto.GovStaffRoleDTO; import com.epmet.dto.form.*; import com.epmet.dto.result.*; +import com.epmet.feign.EpmetMessageOpenFeignClient; import com.epmet.feign.EpmetUserFeignClient; import com.epmet.feign.GovOrgFeignClient; import com.epmet.jwt.JwtTokenProperties; @@ -57,6 +59,8 @@ public class GovLoginServiceImpl implements GovLoginService { private CpUserDetailRedis cpUserDetailRedis; @Autowired private GovOrgFeignClient govOrgFeignClient; + @Autowired + private EpmetMessageOpenFeignClient epmetMessageOpenFeignClient; /** * @param formDTO @@ -70,8 +74,7 @@ public class GovLoginServiceImpl implements GovLoginService { //1、校验手机号是否符合规范 if (!PhoneValidatorUtils.isMobile(formDTO.getMobile())) { logger.error(String.format(SEND_SMS_CODE_ERROR, formDTO.getMobile(), EpmetErrorCode.ERROR_PHONE.getCode(), EpmetErrorCode.ERROR_PHONE.getMsg())); - //TODO 2020-05-21 手机号验证先去掉方便测试 - //throw new RenException(EpmetErrorCode.ERROR_PHONE.getCode()); + throw new RenException(EpmetErrorCode.ERROR_PHONE.getCode()); } //2、根据手机号校验用户是否存在 Result> customerStaffResult = epmetUserFeignClient.checkCustomerStaff(formDTO.getMobile()); @@ -79,14 +82,17 @@ public class GovLoginServiceImpl implements GovLoginService { logger.error(String.format(SEND_SMS_CODE_ERROR, formDTO.getMobile(), customerStaffResult.getCode(), customerStaffResult.getMsg())); throw new RenException(customerStaffResult.getCode()); } - //3、发送短信验证码 TODO 2020-05-21 发送验证先去掉方便测试 - /*Result> smsCodeResult = messageFeignClient.sendSmsCaptcha(formDTO.getMobile()); + //3、发送短信验证码 + SendVerificationCodeFormDTO sendVerificationCodeFormDTO=new SendVerificationCodeFormDTO(); + sendVerificationCodeFormDTO.setMobile(formDTO.getMobile()); + sendVerificationCodeFormDTO.setAliyunTemplateCode(SmsTemplateConstant.LGOIN_CONFIRM); + Result smsCodeResult=epmetMessageOpenFeignClient.sendVerificationCode(sendVerificationCodeFormDTO); if (!smsCodeResult.success()) { logger.error(String.format(SEND_SMS_CODE_ERROR, formDTO.getMobile(), smsCodeResult.getCode(), smsCodeResult.getMsg())); throw new RenException(smsCodeResult.getCode()); } //4、保存短信验证码(删除现有短信验证码、将新的短信验证码存入Redis) - captchaRedis.saveSmsCode(formDTO, smsCodeResult.getData().get("code"));*/ + captchaRedis.saveSmsCode(formDTO, smsCodeResult.getData().getCode()); logger.info(String.format("发送短信验证码成功,手机号[%s]", formDTO.getMobile())); } diff --git a/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/constant/SmsTemplateConstant.java b/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/constant/SmsTemplateConstant.java new file mode 100644 index 0000000000..3bfac0e8f7 --- /dev/null +++ b/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/constant/SmsTemplateConstant.java @@ -0,0 +1,30 @@ +package com.epmet.constant; + +/** + * 短信模板编码常亮 + * + * @author yinzuomei@elink-cn.com + * @date 2020/6/23 21:30 + */ +public interface SmsTemplateConstant { + + /** + * 登录确认验证码 + */ + String LGOIN_CONFIRM="SMS_150731396"; + + /** + * 用户注册验证码 + */ + String USER_REGISTER="SMS_150731394"; + + /** + * 信息变更验证码 + */ + String CHANGE_INFO="SMS_150731392"; + + /** + * 修改密码验证码 + */ + String CHANGE_PASSWORD = "SMS_150731393"; +} diff --git a/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/SysSmsDTO.java b/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/SysSmsDTO.java index 91eff10e8b..d698373e9f 100644 --- a/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/SysSmsDTO.java +++ b/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/SysSmsDTO.java @@ -54,5 +54,8 @@ public class SysSmsDTO implements Serializable { @ApiModelProperty(value = "参数列表") private String params; - + /** + * 短信模板编码 来源于SmsTemplateConstant + */ + private String aliyunTemplateCode; } diff --git a/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/form/SendVerificationCodeFormDTO.java b/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/form/SendVerificationCodeFormDTO.java new file mode 100644 index 0000000000..97aaa8549b --- /dev/null +++ b/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/form/SendVerificationCodeFormDTO.java @@ -0,0 +1,36 @@ +package com.epmet.dto.form; + +import com.epmet.commons.tools.validator.group.CustomerClientShowGroup; +import lombok.Data; + +import javax.validation.constraints.NotBlank; +import java.io.Serializable; + +/** + * 根据手机号、短信模板编码,发送短信 验证码 + * + * @author yinzuomei@elink-cn.com + * @date 2020/6/23 20:58 + */ +@Data +public class SendVerificationCodeFormDTO implements Serializable { + private static final long serialVersionUID = 8894395590639206399L; + + public interface AddUserInternalGroup { + } + + public interface AddUserShowGroup extends CustomerClientShowGroup { + } + + /** + * 手机号 + */ + @NotBlank(message = "手机号不能为空", groups = {AddUserInternalGroup.class, AddUserShowGroup.class}) + private String mobile; + + /** + * 场景: + */ + @NotBlank(message = "短信模板编码不能为空", groups = {AddUserInternalGroup.class}) + private String aliyunTemplateCode; +} diff --git a/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/result/SendVerificationCodeResultDTO.java b/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/result/SendVerificationCodeResultDTO.java new file mode 100644 index 0000000000..2442da7cb1 --- /dev/null +++ b/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/result/SendVerificationCodeResultDTO.java @@ -0,0 +1,19 @@ +package com.epmet.dto.result; + +import lombok.Data; + +import java.io.Serializable; + +/** + * 短信验证码 + * + * @author yinzuomei@elink-cn.com + * @date 2020/6/23 21:37 + */ +@Data +public class SendVerificationCodeResultDTO implements Serializable { + /** + * 短信验证码 + */ + private String code; +} diff --git a/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/feign/EpmetMessageOpenFeignClient.java b/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/feign/EpmetMessageOpenFeignClient.java index ba775e2546..9d0a60fbf1 100644 --- a/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/feign/EpmetMessageOpenFeignClient.java +++ b/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/feign/EpmetMessageOpenFeignClient.java @@ -1,5 +1,12 @@ package com.epmet.feign; +import com.epmet.commons.tools.constant.ServiceConstant; +import com.epmet.commons.tools.utils.Result; +import com.epmet.dto.SysSmsDTO; +import com.epmet.dto.form.SendVerificationCodeFormDTO; +import com.epmet.dto.form.UserMessageFormDTO; +import com.epmet.dto.result.SendVerificationCodeResultDTO; +import com.epmet.feign.fallback.EpmetMessageOpenFeignClientFallback; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.GetMapping; @@ -7,12 +14,6 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; -import com.epmet.commons.tools.constant.ServiceConstant; -import com.epmet.commons.tools.utils.Result; -import com.epmet.dto.SysSmsDTO; -import com.epmet.dto.form.UserMessageFormDTO; -import com.epmet.feign.fallback.EpmetMessageOpenFeignClientFallback; - /** * 本服务对外开放的API,其他服务通过引用此client调用该服务 * @@ -44,4 +45,14 @@ public interface EpmetMessageOpenFeignClient { **/ @PostMapping(value = "message/usermessage/saveusermessage", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE) Result saveUserMessage(@RequestBody UserMessageFormDTO formDto); + + /** + * @param + * @return com.epmet.commons.tools.utils.Result + * @author yinzuomei + * @description 发送短信验证码(区分场景) + * @Date 2020/6/23 20:56 + **/ + @PostMapping(value = "message/sms/sendVerificationCode", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE) + Result sendVerificationCode(@RequestBody SendVerificationCodeFormDTO formDTO); } diff --git a/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/feign/fallback/EpmetMessageOpenFeignClientFallback.java b/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/feign/fallback/EpmetMessageOpenFeignClientFallback.java index a3f4dbbbc1..6bd8c16bb8 100644 --- a/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/feign/fallback/EpmetMessageOpenFeignClientFallback.java +++ b/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/feign/fallback/EpmetMessageOpenFeignClientFallback.java @@ -1,13 +1,14 @@ package com.epmet.feign.fallback; -import org.springframework.stereotype.Component; - import com.epmet.commons.tools.constant.ServiceConstant; import com.epmet.commons.tools.utils.ModuleUtils; import com.epmet.commons.tools.utils.Result; import com.epmet.dto.SysSmsDTO; +import com.epmet.dto.form.SendVerificationCodeFormDTO; import com.epmet.dto.form.UserMessageFormDTO; +import com.epmet.dto.result.SendVerificationCodeResultDTO; import com.epmet.feign.EpmetMessageOpenFeignClient; +import org.springframework.stereotype.Component; /** * 本服务对外开放的API,其他服务通过引用此client调用该服务 @@ -32,4 +33,10 @@ public class EpmetMessageOpenFeignClientFallback implements EpmetMessageOpenFeig public Result saveUserMessage(UserMessageFormDTO formDto) { return ModuleUtils.feignConError(ServiceConstant.EPMET_MESSAGE_SERVER, "saveUserMessage", formDto); } + + @Override + public Result sendVerificationCode(SendVerificationCodeFormDTO formDTO) { + return ModuleUtils.feignConError(ServiceConstant.EPMET_MESSAGE_SERVER, "sendVerificationCode", formDTO); + } + } diff --git a/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/controller/SmsController.java b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/controller/SmsController.java index 008429520d..0a0f534a0a 100644 --- a/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/controller/SmsController.java +++ b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/controller/SmsController.java @@ -9,7 +9,6 @@ package com.epmet.controller; import com.alibaba.fastjson.JSON; -import com.epmet.commons.tools.annotation.LogOperation; import com.epmet.commons.tools.constant.Constant; import com.epmet.commons.tools.page.PageData; import com.epmet.commons.tools.utils.Result; @@ -17,6 +16,8 @@ import com.epmet.commons.tools.validator.ValidatorUtils; import com.epmet.commons.tools.validator.group.AliyunGroup; import com.epmet.commons.tools.validator.group.QcloudGroup; import com.epmet.dto.SysSmsDTO; +import com.epmet.dto.form.SendVerificationCodeFormDTO; +import com.epmet.dto.result.SendVerificationCodeResultDTO; import com.epmet.enums.PlatformEnum; import com.epmet.remote.ParamsRemoteService; import com.epmet.service.SysSmsService; @@ -31,7 +32,6 @@ import org.springframework.web.bind.annotation.*; import springfox.documentation.annotations.ApiIgnore; import java.util.Arrays; -import java.util.HashMap; import java.util.Map; /** @@ -76,7 +76,6 @@ public class SmsController { @PostMapping("saveConfig") @ApiOperation("保存配置短信") -// @LogOperation("保存配置短信") public Result saveConfig(@RequestBody SmsConfig config){ //校验类型 ValidatorUtils.validateEntity(config); @@ -96,19 +95,17 @@ public class SmsController { @PostMapping("send") @ApiOperation("发送短信") -// @LogOperation("发送短信") @ApiImplicitParams({ @ApiImplicitParam(name = "mobile", value = "手机好号", paramType = "query", required = true, dataType="String"), @ApiImplicitParam(name = "params", value = "参数", paramType = "query", required = true, dataType="String") }) public Result send(@RequestBody SysSmsDTO sysSmsDTO){ - sysSmsService.send(sysSmsDTO.getMobile(), sysSmsDTO.getParams()); + sysSmsService.send(sysSmsDTO); return new Result(); } @DeleteMapping @ApiOperation("删除") -// @LogOperation("删除") public Result delete(@RequestBody Long[] ids){ sysSmsService.deleteBatchIds(Arrays.asList(ids)); @@ -121,7 +118,6 @@ public class SmsController { */ @GetMapping("captcha/{mobile}") @ApiOperation("发送短信验证码") - //@LogOperation("发送短信验证码") @ApiImplicitParams({ @ApiImplicitParam(name = "mobile", value = "手机号", paramType = "query", required = true, dataType="String") }) @@ -129,4 +125,18 @@ public class SmsController { Result result = sysSmsService.sendSmsCaptcha(mobile); return result; } + + /** + * @return com.epmet.commons.tools.utils.Result + * @param formDTO + * @author yinzuomei + * @description 发送短信验证码(区分场景) + * @Date 2020/6/23 21:05 + **/ + @PostMapping("sendVerificationCode") + public Result sendVerificationCode(@RequestBody SendVerificationCodeFormDTO formDTO){ + ValidatorUtils.validateEntity(formDTO,SendVerificationCodeFormDTO.AddUserShowGroup.class,SendVerificationCodeFormDTO.AddUserInternalGroup.class); + SendVerificationCodeResultDTO resultDTO=sysSmsService.sendVerificationCode(formDTO); + return new Result().ok(resultDTO); + } } diff --git a/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/SysSmsService.java b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/SysSmsService.java index 79c7cddd1c..1e67125423 100644 --- a/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/SysSmsService.java +++ b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/SysSmsService.java @@ -12,6 +12,8 @@ import com.epmet.commons.mybatis.service.BaseService; import com.epmet.commons.tools.page.PageData; import com.epmet.commons.tools.utils.Result; import com.epmet.dto.SysSmsDTO; +import com.epmet.dto.form.SendVerificationCodeFormDTO; +import com.epmet.dto.result.SendVerificationCodeResultDTO; import com.epmet.entity.SysSmsEntity; import java.util.LinkedHashMap; @@ -28,10 +30,9 @@ public interface SysSmsService extends BaseService { /** * 发送短信 - * @param mobile 手机号 - * @param params 短信参数 + * @param sysSmsDTO :mobile 手机号 params短信参数 aliyunTemplateCode:短信模板 */ - void send(String mobile, String params); + void send(SysSmsDTO sysSmsDTO); /** * 保存短信发送记录 @@ -48,5 +49,14 @@ public interface SysSmsService extends BaseService { * @return */ Result sendSmsCaptcha(String mobile); + + /** + * @return java.util.HashMap + * @param formDTO + * @author yinzuomei + * @description 发送短信验证码(区分场景) + * @Date 2020/6/23 21:07 + **/ + SendVerificationCodeResultDTO sendVerificationCode(SendVerificationCodeFormDTO formDTO); } diff --git a/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/impl/SysSmsServiceImpl.java b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/impl/SysSmsServiceImpl.java index 9dc94b7a2d..2f8600641e 100644 --- a/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/impl/SysSmsServiceImpl.java +++ b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/impl/SysSmsServiceImpl.java @@ -20,6 +20,8 @@ import com.epmet.commons.tools.page.PageData; import com.epmet.commons.tools.utils.Result; import com.epmet.dao.SysSmsDao; import com.epmet.dto.SysSmsDTO; +import com.epmet.dto.form.SendVerificationCodeFormDTO; +import com.epmet.dto.result.SendVerificationCodeResultDTO; import com.epmet.entity.SysSmsEntity; import com.epmet.exception.ModuleErrorCode; import com.epmet.feign.EpmetMessageOpenFeignClient; @@ -67,10 +69,10 @@ public class SysSmsServiceImpl extends BaseServiceImpl } @Override - public void send(String mobile, String params) { + public void send(SysSmsDTO sysSmsDTO) { LinkedHashMap map; try { - map = JSON.parseObject(params, LinkedHashMap.class); + map = JSON.parseObject(sysSmsDTO.getParams(), LinkedHashMap.class); }catch (Exception e){ throw new RenException(ErrorCode.JSON_FORMAT_ERROR); } @@ -80,9 +82,12 @@ public class SysSmsServiceImpl extends BaseServiceImpl if(service == null){ throw new RenException(ModuleErrorCode.SMS_CONFIG); } - //发送短信 - service.sendSms(mobile, map); + if(StringUtils.isNotBlank(sysSmsDTO.getAliyunTemplateCode())){ + service.sendSmsByAliyunTemplateCode(sysSmsDTO.getMobile(), map,sysSmsDTO.getAliyunTemplateCode()); + }else{ + service.sendSms(sysSmsDTO.getMobile(), map); + } } @Override @@ -141,4 +146,34 @@ public class SysSmsServiceImpl extends BaseServiceImpl } return new Result().error(EpmetErrorCode.MESSAGE_SMS_SEND_ERROR.getCode()); } + + @Override + public SendVerificationCodeResultDTO sendVerificationCode(SendVerificationCodeFormDTO formDTO) { + SendVerificationCodeResultDTO data = new SendVerificationCodeResultDTO(); + Integer code = (int) ((Math.random() * 9 + 1) * 100000); + String codeStr = String.valueOf(code); + HashMap map = new HashMap<>(); + map.put("code", codeStr); + + try { + String paramJson = new ObjectMapper().writeValueAsString(map); + + SysSmsDTO sysSmsDTO = new SysSmsDTO(); + sysSmsDTO.setMobile(formDTO.getMobile()); + sysSmsDTO.setParams(paramJson); + sysSmsDTO.setAliyunTemplateCode(formDTO.getAliyunTemplateCode()); + Result result = epmetMessageOpenFeignClient.send(sysSmsDTO); + if (result != null && result.success()) { + data.setCode(codeStr); + } else { + logger.error(String.format("短信验证码发送失败,手机号:%s。code:%d,msg:%s", formDTO.getMobile(), result.getCode(), result.getMsg())); + throw new RenException(EpmetErrorCode.MESSAGE_SMS_SEND_ERROR.getCode()); + } + } catch (Exception e) { + e.printStackTrace(); + logger.error(String.format("短信验证码发送失败,手机号:%s。errorMessage:%s", formDTO.getMobile(), e.getMessage())); + throw new RenException(EpmetErrorCode.MESSAGE_SMS_SEND_ERROR.getCode()); + } + return data; + } } diff --git a/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/sms/AbstractSmsService.java b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/sms/AbstractSmsService.java index a29130457b..e1a07530fb 100644 --- a/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/sms/AbstractSmsService.java +++ b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/sms/AbstractSmsService.java @@ -37,4 +37,17 @@ public abstract class AbstractSmsService { * @param template 短信模板 */ public abstract void sendSms(String mobile, LinkedHashMap params, String signName, String template); + + /** + * @return void + * @param mobile 手机号 + * @param params 参数 + * @param aliyunTemplateCode 短息模板编码 + * @author yinzuomei + * @description + * @Date 2020/6/23 21:27 + **/ + public abstract void sendSmsByAliyunTemplateCode(String mobile, + LinkedHashMap params, + String aliyunTemplateCode); } diff --git a/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/sms/AliyunSmsService.java b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/sms/AliyunSmsService.java index 7c97eb11ad..9ec5c16cd5 100644 --- a/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/sms/AliyunSmsService.java +++ b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/sms/AliyunSmsService.java @@ -24,6 +24,7 @@ import com.epmet.commons.tools.utils.SpringContextUtils; import com.epmet.enums.PlatformEnum; import com.epmet.exception.ModuleErrorCode; import com.epmet.service.SysSmsService; +import org.apache.commons.lang3.StringUtils; import java.util.LinkedHashMap; @@ -108,4 +109,13 @@ public class AliyunSmsService extends AbstractSmsService { throw new RenException(ModuleErrorCode.SEND_SMS_ERROR, response.getMessage()); } } + + @Override + public void sendSmsByAliyunTemplateCode(String mobile, LinkedHashMap params,String aliyunTemplateCode) { + if(StringUtils.isNotBlank(aliyunTemplateCode)){ + this.sendSms(mobile, params, config.getAliyunSignName(), aliyunTemplateCode); + return; + } + this.sendSms(mobile, params, config.getAliyunSignName(), config.getAliyunTemplateCode()); + } }