给手机发送验证码
@PostMapping("/code")
public ResponseEntity<Void> sendCode(@RequestParam("phone") String phone) {
if (StringUtils.isBlank(phone)) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
this.userService.sendCode(phone);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
public void sendCode(String phone) {
String key = KEY_PREFIX + phone;
String code = NumberUtils.generateCode(6);
this.redisTemplate.opsForValue().set(key, code, 5, TimeUnit.MINUTES);
Map<String, String> msg = new HashMap<>();
msg.put("phone", phone);
msg.put("code", code);
this.amqpTemplate.convertAndSend("ly.sms.exchange", "sms.verify.code", msg);
}
@PostMapping("/sendCode")
@ApiOperation(value = "发送验证码--注册使用", httpMethod = "POST")
public ApiReturnObject sendCode(SysUser user) throws Exception {
if (user.getSysPhone() != null) {
String verifyCode3 = (int) ((Math.random() * 9 + 1) * 100000) + "";
redisTemplate.opsForHash().put(user.getSysPhone(), "code", verifyCode3);
redisTemplate.boundValueOps(user.getSysPhone()).expire(600000, TimeUnit.SECONDS);
SendSmsUtil.sendSms(user.getSysPhone(), verifyCode3);
return new ApiReturnObject("200", "验证码发送成功", "");
} else {
return new ApiReturnObject("201", "手机号为空 请输入手机号", "");
}
}