diff --git a/epmet-auth/src/main/java/com/epmet/controller/GovLoginController.java b/epmet-auth/src/main/java/com/epmet/controller/GovLoginController.java new file mode 100644 index 0000000000..e1e896d06c --- /dev/null +++ b/epmet-auth/src/main/java/com/epmet/controller/GovLoginController.java @@ -0,0 +1,37 @@ +package com.epmet.controller; + +import com.epmet.commons.tools.utils.Result; +import com.epmet.commons.tools.validator.ValidatorUtils; +import com.epmet.dto.form.SendSmsCodeFormDTO; +import com.epmet.service.GovLoginService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @Description 政府端登录api入口 + * @Author yinzuomei + * @Date 2020/4/18 10:32 + */ +@RestController +@RequestMapping("/gov/login") +public class GovLoginController { + + @Autowired + private GovLoginService govLoginService; + + /** + * @param formDTO 手机号 + * @return com.epmet.commons.tools.utils.Result + * @Author yinzuomei + * @Description 登录-发送验证码 + * @Date 2020/4/18 10:58 + **/ + @PostMapping(value = "sendsmscode") + public Result sendSmsCode(@RequestBody SendSmsCodeFormDTO formDTO) { + ValidatorUtils.validateEntity(formDTO); + return govLoginService.sendSmsCode(formDTO); + } +} diff --git a/epmet-auth/src/main/java/com/epmet/dto/form/LoginByPhoneFormDTO.java b/epmet-auth/src/main/java/com/epmet/dto/form/LoginByPhoneFormDTO.java new file mode 100644 index 0000000000..d4eba4f08d --- /dev/null +++ b/epmet-auth/src/main/java/com/epmet/dto/form/LoginByPhoneFormDTO.java @@ -0,0 +1,26 @@ +package com.epmet.dto.form; + +import lombok.Data; + +import javax.validation.constraints.NotBlank; +import java.io.Serializable; + +/** + * @Description 政府端手机号+验证码登录接口入参 + * @Author yinzuomei + * @Date 2020/4/18 10:38 + */ +@Data +public class LoginByPhoneFormDTO implements Serializable { + private static final long serialVersionUID = 4193133227120225342L; + + @NotBlank(message = "wxCode不能为空") + private String wxCode; + + @NotBlank(message = "手机号不能为空") + private String phone; + + @NotBlank(message="手机号不能为空") + private String smsCode; +} + diff --git a/epmet-auth/src/main/java/com/epmet/dto/form/SendSmsCodeFormDTO.java b/epmet-auth/src/main/java/com/epmet/dto/form/SendSmsCodeFormDTO.java new file mode 100644 index 0000000000..af5fa8bfa0 --- /dev/null +++ b/epmet-auth/src/main/java/com/epmet/dto/form/SendSmsCodeFormDTO.java @@ -0,0 +1,25 @@ +package com.epmet.dto.form; + +import lombok.Data; + +import javax.validation.constraints.NotBlank; +import java.io.Serializable; + +/** + * @Description 政府端手机号+验证码登录接口入参 + * @Author yinzuomei + * @Date 2020/04/18 10:26 + */ +@Data +public class SendSmsCodeFormDTO implements Serializable { + private static final long serialVersionUID = -1852541457359282018L; + + @NotBlank(message = "app不能为空(resi、gov、oper)") + private String app; + + @NotBlank(message = "client不能为空(wxmp、web)") + private String client; + + @NotBlank(message = "手机号不能为空") + private String phone; +} diff --git a/epmet-auth/src/main/java/com/epmet/service/GovLoginService.java b/epmet-auth/src/main/java/com/epmet/service/GovLoginService.java new file mode 100644 index 0000000000..9f329487bb --- /dev/null +++ b/epmet-auth/src/main/java/com/epmet/service/GovLoginService.java @@ -0,0 +1,21 @@ +package com.epmet.service; + +import com.epmet.commons.tools.utils.Result; +import com.epmet.dto.form.SendSmsCodeFormDTO; + +/** + * @Description 政府端登录 + * @Author yinzuomei + * @Date 2020/4/18 10:32 + */ +public interface GovLoginService { + + /** + * @param formDTO + * @return com.epmet.commons.tools.utils.Result + * @Author yinzuomei + * @Description 登录-发送验证码 + * @Date 2020/4/18 10:59 + **/ + Result sendSmsCode(SendSmsCodeFormDTO formDTO); +} 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 new file mode 100644 index 0000000000..3d2a8098b0 --- /dev/null +++ b/epmet-auth/src/main/java/com/epmet/service/impl/GovLoginServiceImpl.java @@ -0,0 +1,68 @@ +package com.epmet.service.impl; + +import com.epmet.commons.tools.constant.Constant; +import com.epmet.commons.tools.exception.EpmetErrorCode; +import com.epmet.commons.tools.utils.Result; +import com.epmet.commons.tools.validator.PhoneValidatorUtils; +import com.epmet.dto.form.SendSmsCodeFormDTO; +import com.epmet.feign.EpmetUserFeignClient; +import com.epmet.feign.MessageFeignClient; +import com.epmet.redis.CaptchaRedis; +import com.epmet.service.GovLoginService; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.Map; + +/** + * @Description 政府端登录 + * @Author yinzuomei + * @Date 2020/4/18 10:33 + */ +@Service +public class GovLoginServiceImpl implements GovLoginService { + private Logger logger = LogManager.getLogger(getClass()); + @Autowired + private EpmetUserFeignClient epmetUserFeignClient; + @Autowired + private MessageFeignClient messageFeignClient; + @Autowired + private CaptchaRedis captchaRedis; + + private static final String SEND_SMS_CODE_ERROR="发送短信验证码异常,手机号[%s],code[%s],msg[%s]"; + + /** + * @param formDTO + * @return com.epmet.commons.tools.utils.Result + * @Author yinzuomei + * @Description 登录-发送验证码 + * @Date 2020/4/18 10:59 + **/ + @Override + public Result sendSmsCode(SendSmsCodeFormDTO formDTO) { + //1、校验手机号是否符合规范 + if(!PhoneValidatorUtils.isMobile(formDTO.getPhone())){ + logger.error(String.format(SEND_SMS_CODE_ERROR,formDTO.getPhone(),EpmetErrorCode.ERROR_PHONE.getCode(),EpmetErrorCode.ERROR_PHONE.getMsg())); + return new Result().error(EpmetErrorCode.ERROR_PHONE.getCode()); + } + //2、根据手机号校验用户是否存在 + Result customerStaffResult=epmetUserFeignClient.checkCustomerStaff(formDTO.getPhone()); + if(!customerStaffResult.success()){ + logger.error(String.format(SEND_SMS_CODE_ERROR,formDTO.getPhone(),EpmetErrorCode.GOV_STAFF_NOT_EXISTS.getCode(),EpmetErrorCode.GOV_STAFF_NOT_EXISTS.getMsg())); + return new Result().error(customerStaffResult.getCode()); + } + //3、发送短信验证码 + Result> smsCodeResult=messageFeignClient.sendSmsCaptcha(formDTO.getPhone()); + if(!smsCodeResult.success()){ + logger.error(String.format(SEND_SMS_CODE_ERROR,formDTO.getPhone(),smsCodeResult.getCode(),smsCodeResult.getMsg())); + return new Result().error(smsCodeResult.getCode()); + } + //4、保存短信验证码(删除现有短信验证码、将新的短信验证码存入Redis) + captchaRedis.saveSmsCode(formDTO,smsCodeResult.getData().get(Constant.SMS_CODE)); + logger.info(String.format(SEND_SMS_CODE_ERROR,formDTO.getPhone())); + return new Result(); + } +} + diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/validator/PhoneValidatorUtils.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/validator/PhoneValidatorUtils.java new file mode 100644 index 0000000000..a762a6848d --- /dev/null +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/validator/PhoneValidatorUtils.java @@ -0,0 +1,27 @@ +package com.epmet.commons.tools.validator; + +import org.apache.commons.lang3.StringUtils; + +import java.util.regex.Pattern; + +/** + * @Description 手机号验证工具类 + * @Author yinzuomei + * @Date 2020/4/18 15:04 + */ +public class PhoneValidatorUtils { + private static final String REGEX_MOBILE ="((\\+86|0086)?\\s*)((134[0-8]\\d{7})|(((13([0-3]|[5-9]))|(14[5-9])|15([0-3]|[5-9])|(16(2|[5-7]))|17([0-3]|[5-8])|18[0-9]|19(1|[8-9]))\\d{8})|(14(0|1|4)0\\d{7})|(1740([0-5]|[6-9]|[10-12])\\d{7}))"; + + /** + * 判断是否是手机号 + * @param tel 手机号 + * @return boolean true:是 false:否 + */ + public static boolean isMobile(String tel) { + if (StringUtils.isEmpty(tel)){ + return false; + } + return Pattern.matches(REGEX_MOBILE, tel); + } +} +