1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| @RequestMapping("/sendSms") public CommonResult<String> sendSms(@RequestBody String phoneNumber) { log.info("接收到手机号: {}", phoneNumber); String code = String.valueOf((int) ((Math.random() * 9 + 1) * 100000)); boolean success = sendVerificationCode(phoneNumber, code); if (success) { redisUtil.set("phoneCode", code, 300); return CommonResultEnum.SUCCESS.setData("验证码发送成功"); } else { return CommonResultEnum.ERROR.setData("验证码发送失败"); } } public boolean sendVerificationCode(String phoneNumber, String code) { try { SendSmsResponse response = smsConfig.sendSms( phoneNumber, "阿里云短信测试", "SMS_154950909", "{\"code\":\"" + code + "\"}" ); log.info(response.getCode()); return "OK".equals(response.getCode()); } catch (Exception e) { e.printStackTrace(); return false; } }
|