From 23b18c43e09bfb03512799d8515d9fdba4cda12a Mon Sep 17 00:00:00 2001 From: lbw Date: Thu, 13 Oct 2022 10:32:42 +0800 Subject: [PATCH] =?UTF-8?q?:recycle:=20Refactoring=20code.=20=E5=8F=91?= =?UTF-8?q?=E9=80=81=E9=AA=8C=E8=AF=81=E7=A0=81=E7=BB=84=E4=BB=B6=E9=9C=80?= =?UTF-8?q?=E8=A6=81=E5=88=A4=E6=96=AD=E6=89=8B=E6=9C=BA=E5=8F=B7=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E5=AD=98=E5=9C=A8=E7=94=A8=E6=88=B7=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E4=B8=AD=20#749?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pig/admin/api/dto/AppSmsDTO.java | 27 +++++++++++++++++++ .../pig/admin/controller/AppController.java | 19 ++++++++----- .../pig/admin/service/AppService.java | 5 ++-- .../admin/service/impl/AppServiceImpl.java | 25 +++++++++++------ 4 files changed, 59 insertions(+), 17 deletions(-) create mode 100644 pig-upms/pig-upms-api/src/main/java/com/pig4cloud/pig/admin/api/dto/AppSmsDTO.java diff --git a/pig-upms/pig-upms-api/src/main/java/com/pig4cloud/pig/admin/api/dto/AppSmsDTO.java b/pig-upms/pig-upms-api/src/main/java/com/pig4cloud/pig/admin/api/dto/AppSmsDTO.java new file mode 100644 index 00000000..2069903a --- /dev/null +++ b/pig-upms/pig-upms-api/src/main/java/com/pig4cloud/pig/admin/api/dto/AppSmsDTO.java @@ -0,0 +1,27 @@ +package com.pig4cloud.pig.admin.api.dto; + +import lombok.Data; + +import javax.validation.constraints.NotBlank; + +/** + * 客户端请求验证码 + * + * @author lengleng + * @date 2022/10/13 + */ +@Data +public class AppSmsDTO { + + /** + * 手机号 + */ + @NotBlank(message = "手机号不能为空") + private String phone; + + /** + * 手机号是否存在数据库 + */ + private Boolean exist; + +} diff --git a/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/controller/AppController.java b/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/controller/AppController.java index 8fda57b8..5e4540ec 100644 --- a/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/controller/AppController.java +++ b/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/controller/AppController.java @@ -1,6 +1,7 @@ package com.pig4cloud.pig.admin.controller; import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.pig4cloud.pig.admin.api.dto.AppSmsDTO; import com.pig4cloud.pig.admin.api.dto.UserInfo; import com.pig4cloud.pig.admin.api.entity.SysUser; import com.pig4cloud.pig.admin.service.AppService; @@ -13,10 +14,9 @@ import io.swagger.v3.oas.annotations.security.SecurityRequirement; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.AllArgsConstructor; import org.springframework.http.HttpHeaders; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; + +import javax.validation.Valid; /** * @author lengleng @@ -33,10 +33,15 @@ public class AppController { private final SysUserService userService; + /** + * 发送手机验证码 + * @param sms 请求手机对象 + * @return code + */ @Inner(value = false) - @GetMapping("/{mobile}") - public R sendSmsCode(@PathVariable String mobile) { - return appService.sendSmsCode(mobile); + @PostMapping("/sms") + public R sendSmsCode(@Valid @RequestBody AppSmsDTO sms) { + return appService.sendSmsCode(sms); } /** diff --git a/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/AppService.java b/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/AppService.java index 30866429..80981864 100644 --- a/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/AppService.java +++ b/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/AppService.java @@ -17,6 +17,7 @@ package com.pig4cloud.pig.admin.service; +import com.pig4cloud.pig.admin.api.dto.AppSmsDTO; import com.pig4cloud.pig.common.core.util.R; /** @@ -27,10 +28,10 @@ public interface AppService { /** * 发送手机验证码 - * @param phone phone + * @param sms phone * @return code */ - R sendSmsCode(String phone); + R sendSmsCode(AppSmsDTO sms); /** * 校验验证码 diff --git a/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/impl/AppServiceImpl.java b/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/impl/AppServiceImpl.java index 0f3944a2..01e81a90 100644 --- a/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/impl/AppServiceImpl.java +++ b/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/impl/AppServiceImpl.java @@ -18,6 +18,9 @@ package com.pig4cloud.pig.admin.service.impl; import cn.hutool.core.util.RandomUtil; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.pig4cloud.pig.admin.api.dto.AppSmsDTO; +import com.pig4cloud.pig.admin.api.entity.SysUser; import com.pig4cloud.pig.admin.mapper.SysUserMapper; import com.pig4cloud.pig.admin.service.AppService; import com.pig4cloud.pig.common.core.constant.CacheConstants; @@ -53,25 +56,31 @@ public class AppServiceImpl implements AppService { /** * 发送手机验证码 TODO: 调用短信网关发送验证码,测试返回前端 - * @param phone 手机号 + * @param sms 手机号 * @return code */ @Override - public R sendSmsCode(String phone) { - Object codeObj = redisTemplate.opsForValue().get(CacheConstants.DEFAULT_CODE_KEY + phone); + public R sendSmsCode(AppSmsDTO sms) { + Object codeObj = redisTemplate.opsForValue().get(CacheConstants.DEFAULT_CODE_KEY + sms.getPhone()); if (codeObj != null) { - log.info("手机号验证码未过期:{},{}", phone, codeObj); + log.info("手机号验证码未过期:{},{}", sms.getPhone(), codeObj); return R.ok(Boolean.FALSE, MsgUtils.getMessage(ErrorCodes.SYS_APP_SMS_OFTEN)); } + // 校验手机号是否存在 sys_user 表 + if (sms.getExist() + && !userMapper.exists(Wrappers.lambdaQuery().eq(SysUser::getPhone, sms.getPhone()))) { + return R.ok(Boolean.FALSE, MsgUtils.getMessage(ErrorCodes.SYS_APP_PHONE_UNREGISTERED, sms.getPhone())); + } + String code = RandomUtil.randomNumbers(Integer.parseInt(SecurityConstants.CODE_SIZE)); - log.info("手机号生成验证码成功:{},{}", phone, code); - redisTemplate.opsForValue().set(CacheConstants.DEFAULT_CODE_KEY + phone, code, SecurityConstants.CODE_TIME, - TimeUnit.SECONDS); + log.info("手机号生成验证码成功:{},{}", sms.getPhone(), code); + redisTemplate.opsForValue().set(CacheConstants.DEFAULT_CODE_KEY + sms.getPhone(), code, + SecurityConstants.CODE_TIME, TimeUnit.SECONDS); // 调用短信通道发送 - this.smsClient.sendCode(code, phone); + this.smsClient.sendCode(code, sms.getPhone()); return R.ok(Boolean.TRUE, code); }