From 01db3ae4d7d8db75e4a65b9417ef4ff1dcb9b13a Mon Sep 17 00:00:00 2001 From: msb_89821 Date: Sun, 16 Oct 2022 17:12:13 +0800 Subject: [PATCH] =?UTF-8?q?add:=20=E5=AE=9E=E7=8E=B0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../text/consonant/PassengerConsonant.java | 11 ++++ .../text/controller/PassengerController.java | 53 +++++++++++++++---- .../mashibing/online/text/dto/UserInfo.java | 11 ++++ .../online/text/feign/SendSMSCodeService.java | 2 +- .../fallback/SendSMSCodeServiceImpl.java | 5 +- oneLineTxt/v1/onelinetextplublic/pom.xml | 10 ++++ .../controller/SendSmsCodeController.java | 4 +- 7 files changed, 81 insertions(+), 15 deletions(-) create mode 100644 oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/consonant/PassengerConsonant.java create mode 100644 oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/dto/UserInfo.java diff --git a/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/consonant/PassengerConsonant.java b/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/consonant/PassengerConsonant.java new file mode 100644 index 0000000..8a35810 --- /dev/null +++ b/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/consonant/PassengerConsonant.java @@ -0,0 +1,11 @@ +package mashibing.online.text.consonant; + +public class PassengerConsonant { + + public static final String phoneNumberCodePreKey = "smsCode_"; + + public static final String PassengerIdendity = "1"; + + public static final String DriverIdendity = "2"; + +} diff --git a/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/controller/PassengerController.java b/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/controller/PassengerController.java index ddcf070..8549a04 100644 --- a/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/controller/PassengerController.java +++ b/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/controller/PassengerController.java @@ -1,23 +1,40 @@ package mashibing.online.text.controller; import cn.hutool.core.util.ObjectUtil; +import com.alibaba.fastjson.JSON; import com.mashibing.dto.PassengerRequestDto; import com.mashibing.dto.ResponseResult; +import com.mashibing.util.JwtUtils; +import mashibing.online.text.consonant.PassengerConsonant; +import mashibing.online.text.dto.UserInfo; +import mashibing.online.text.feign.SendSMSCodeService; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; +import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; +import javax.annotation.Resource; +import java.util.concurrent.TimeUnit; + /** * @author zxx * @Description * @date 2022/10/5 */ @RestController + public class PassengerController { + @Resource + private SendSMSCodeService sendSMSCodeService; + + @Resource + private StringRedisTemplate stringRedisTemplate; + + @PostMapping("/getPhoneNumberCode") public ResponseResult getPhoneNumberCode(@RequestBody PassengerRequestDto passengerRequestDto){ // 校验参数 @@ -27,9 +44,11 @@ public class PassengerController { } // 调服务获取验证码 - - - // 存入redis 有限时间 + String smsCode = sendSMSCodeService.getSmsCode(6,passengerRequestDto.getPhoneNumber()); + System.out.println(""+ JSON.toJSONString(smsCode)); + // 存入redis 有限时间 + stringRedisTemplate.opsForValue().set(PassengerConsonant.phoneNumberCodePreKey+passengerRequestDto.getPhoneNumber(), + smsCode,2, TimeUnit.MINUTES); return ResponseResult.success("成功!"); @@ -39,15 +58,29 @@ public class PassengerController { @PostMapping("/checkPhoneNumberCode") public ResponseResult checkPhoneNumberCode(@RequestBody PassengerRequestDto passengerRequestDto){ // 校验参数 - - // 校验验证码高大上地方 - + if(ObjectUtil.isNull(passengerRequestDto)||ObjectUtil.isEmpty(passengerRequestDto.getPhoneNumber())||ObjectUtil.isEmpty(passengerRequestDto.getPhoneNumberCode())){ + throw new RuntimeException("手机号码 和短信验证码必填"); + + } + + // 校验验证码 + String phone=passengerRequestDto.getPhoneNumber(); + String redisCode = stringRedisTemplate.opsForValue().get(PassengerConsonant.phoneNumberCodePreKey + phone); + if(StringUtils.isBlank(redisCode)){ + throw new RuntimeException("短信验证码已过期!"); + } + String reqCode=passengerRequestDto.getPhoneNumberCode(); + if(!reqCode.equals(redisCode)){ + throw new RuntimeException("短信验证码错误!"); + } // 用户注册 - + System.out.println("phone 短信验证码正确"); // 返回token - - - return ResponseResult.success("成功!"); + String token = JwtUtils.genaroteToken(phone, PassengerConsonant.PassengerIdendity); + UserInfo userInfo =new UserInfo(); + userInfo.setPhoneNumber(phone); + userInfo.setToken(token); + return ResponseResult.success(userInfo); } } diff --git a/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/dto/UserInfo.java b/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/dto/UserInfo.java new file mode 100644 index 0000000..803a231 --- /dev/null +++ b/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/dto/UserInfo.java @@ -0,0 +1,11 @@ +package mashibing.online.text.dto; + +import lombok.Data; + +@Data +public class UserInfo { + + private String phoneNumber; + + private String token; +} diff --git a/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/feign/SendSMSCodeService.java b/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/feign/SendSMSCodeService.java index b0175c1..c7099c2 100644 --- a/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/feign/SendSMSCodeService.java +++ b/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/feign/SendSMSCodeService.java @@ -22,5 +22,5 @@ public interface SendSMSCodeService { @GetMapping("/send/acquireSMSCode") - public ResponseResult getSmsCode(@RequestParam("size") int size); + public String getSmsCode(@RequestParam("size") int size,@RequestParam(name = "phoneNumber") String phoneNumber); } diff --git a/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/feign/fallback/SendSMSCodeServiceImpl.java b/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/feign/fallback/SendSMSCodeServiceImpl.java index 3252210..0b8797c 100644 --- a/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/feign/fallback/SendSMSCodeServiceImpl.java +++ b/oneLineTxt/v1/onelinetextplublic/api-passinger/src/main/java/mashibing/online/text/feign/fallback/SendSMSCodeServiceImpl.java @@ -5,6 +5,7 @@ import com.mashibing.dto.ResponseResult; import lombok.extern.slf4j.Slf4j; import mashibing.online.text.feign.SendSMSCodeService; import org.springframework.stereotype.Service; +import org.springframework.web.bind.annotation.RequestParam; /** * Copyright© 2020.10.20 by 博纳德集团有限公司.All rights reserved. @@ -26,8 +27,8 @@ public class SendSMSCodeServiceImpl implements SendSMSCodeService { @Override - public ResponseResult getSmsCode(int size) { + public String getSmsCode(int size, String phoneNumber) { log.info("获取短信验证码失败"); - return ResponseResult.fail(CommonStatusEnum.FAIL.getCode(), "获取短信验证码失败"); + return "";//ResponseResult.fail(CommonStatusEnum.FAIL.getCode(), "获取短信验证码失败"); } } diff --git a/oneLineTxt/v1/onelinetextplublic/pom.xml b/oneLineTxt/v1/onelinetextplublic/pom.xml index cbe5cd1..21c6cbd 100644 --- a/oneLineTxt/v1/onelinetextplublic/pom.xml +++ b/oneLineTxt/v1/onelinetextplublic/pom.xml @@ -52,6 +52,16 @@ java-jwt 3.10.3 + + org.apache.commons + commons-pool2 + 2.6.2 + + + org.springframework.boot + spring-boot-starter-data-redis + 2.2.1.RELEASE + diff --git a/oneLineTxt/v1/onelinetextplublic/send-server/src/main/java/com/mashibing/sendserver/controller/SendSmsCodeController.java b/oneLineTxt/v1/onelinetextplublic/send-server/src/main/java/com/mashibing/sendserver/controller/SendSmsCodeController.java index af7ce25..f61695b 100644 --- a/oneLineTxt/v1/onelinetextplublic/send-server/src/main/java/com/mashibing/sendserver/controller/SendSmsCodeController.java +++ b/oneLineTxt/v1/onelinetextplublic/send-server/src/main/java/com/mashibing/sendserver/controller/SendSmsCodeController.java @@ -75,11 +75,11 @@ public class SendSmsCodeController { * @return */ @GetMapping("/acquireSMSCode") - public String acquireSMSCode(@RequestParam(name = "size") int size){ + public String acquireSMSCode(@RequestParam(name = "size") int size,@RequestParam(name = "phoneNumber") String phoneNumber){ int radom = (int)(((Math.random()*9)+1) * Math.pow(10,(size-1))); String msg="【我来啦】尊敬的用户,您的验证码为:"+radom+",2分钟有效"; - Map params = prepareParams(smsAccount.getAccount(), smsAccount.getPassword(), "13710620759", msg, null, null, + Map params = prepareParams(smsAccount.getAccount(), smsAccount.getPassword(), phoneNumber, msg, null, null, UUID.randomUUID().toString()); String rutlet=post(smsAccount.getUrl() + PATH_SEND_SMS, params); log.info("==="+rutlet);