add: 实现功能

master
msb_89821 2 years ago
parent 1f32f06038
commit 01db3ae4d7

@ -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";
}

@ -1,23 +1,40 @@
package mashibing.online.text.controller; package mashibing.online.text.controller;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.mashibing.dto.PassengerRequestDto; import com.mashibing.dto.PassengerRequestDto;
import com.mashibing.dto.ResponseResult; 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.ObjectUtils;
import org.apache.commons.lang3.StringUtils; 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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
/** /**
* @author zxx * @author zxx
* @Description * @Description
* @date 2022/10/5 * @date 2022/10/5
*/ */
@RestController @RestController
public class PassengerController { public class PassengerController {
@Resource
private SendSMSCodeService sendSMSCodeService;
@Resource
private StringRedisTemplate stringRedisTemplate;
@PostMapping("/getPhoneNumberCode") @PostMapping("/getPhoneNumberCode")
public ResponseResult getPhoneNumberCode(@RequestBody PassengerRequestDto passengerRequestDto){ public ResponseResult getPhoneNumberCode(@RequestBody PassengerRequestDto passengerRequestDto){
// 校验参数 // 校验参数
@ -27,9 +44,11 @@ public class PassengerController {
} }
// 调服务获取验证码 // 调服务获取验证码
String smsCode = sendSMSCodeService.getSmsCode(6,passengerRequestDto.getPhoneNumber());
System.out.println(""+ JSON.toJSONString(smsCode));
// 存入redis 有限时间 // 存入redis 有限时间
stringRedisTemplate.opsForValue().set(PassengerConsonant.phoneNumberCodePreKey+passengerRequestDto.getPhoneNumber(),
smsCode,2, TimeUnit.MINUTES);
return ResponseResult.success("成功!"); return ResponseResult.success("成功!");
@ -39,15 +58,29 @@ public class PassengerController {
@PostMapping("/checkPhoneNumberCode") @PostMapping("/checkPhoneNumberCode")
public ResponseResult checkPhoneNumberCode(@RequestBody PassengerRequestDto passengerRequestDto){ 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 // 返回token
String token = JwtUtils.genaroteToken(phone, PassengerConsonant.PassengerIdendity);
UserInfo userInfo =new UserInfo();
return ResponseResult.success("成功!"); userInfo.setPhoneNumber(phone);
userInfo.setToken(token);
return ResponseResult.success(userInfo);
} }
} }

@ -0,0 +1,11 @@
package mashibing.online.text.dto;
import lombok.Data;
@Data
public class UserInfo {
private String phoneNumber;
private String token;
}

@ -22,5 +22,5 @@ public interface SendSMSCodeService {
@GetMapping("/send/acquireSMSCode") @GetMapping("/send/acquireSMSCode")
public ResponseResult getSmsCode(@RequestParam("size") int size); public String getSmsCode(@RequestParam("size") int size,@RequestParam(name = "phoneNumber") String phoneNumber);
} }

@ -5,6 +5,7 @@ import com.mashibing.dto.ResponseResult;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import mashibing.online.text.feign.SendSMSCodeService; import mashibing.online.text.feign.SendSMSCodeService;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
/** /**
* Copyright© 2020.10.20 by .All rights reserved. * Copyright© 2020.10.20 by .All rights reserved.
@ -26,8 +27,8 @@ public class SendSMSCodeServiceImpl implements SendSMSCodeService {
@Override @Override
public ResponseResult getSmsCode(int size) { public String getSmsCode(int size, String phoneNumber) {
log.info("获取短信验证码失败"); log.info("获取短信验证码失败");
return ResponseResult.fail(CommonStatusEnum.FAIL.getCode(), "获取短信验证码失败"); return "";//ResponseResult.fail(CommonStatusEnum.FAIL.getCode(), "获取短信验证码失败");
} }
} }

@ -52,6 +52,16 @@
<artifactId>java-jwt</artifactId> <artifactId>java-jwt</artifactId>
<version>3.10.3</version> <version>3.10.3</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version> 2.6.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version> 2.2.1.RELEASE</version>
</dependency>
</dependencies> </dependencies>

@ -75,11 +75,11 @@ public class SendSmsCodeController {
* @return * @return
*/ */
@GetMapping("/acquireSMSCode") @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))); int radom = (int)(((Math.random()*9)+1) * Math.pow(10,(size-1)));
String msg="【我来啦】尊敬的用户,您的验证码为:"+radom+"2分钟有效"; String msg="【我来啦】尊敬的用户,您的验证码为:"+radom+"2分钟有效";
Map<String, Object> params = prepareParams(smsAccount.getAccount(), smsAccount.getPassword(), "13710620759", msg, null, null, Map<String, Object> params = prepareParams(smsAccount.getAccount(), smsAccount.getPassword(), phoneNumber, msg, null, null,
UUID.randomUUID().toString()); UUID.randomUUID().toString());
String rutlet=post(smsAccount.getUrl() + PATH_SEND_SMS, params); String rutlet=post(smsAccount.getUrl() + PATH_SEND_SMS, params);
log.info("==="+rutlet); log.info("==="+rutlet);

Loading…
Cancel
Save