SmsController.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package com.huaxu.controller;
  2. import com.huaxu.entity.User;
  3. import com.huaxu.entity.ValidateCode;
  4. import com.huaxu.exception.ServiceException;
  5. import com.huaxu.model.AjaxMessage;
  6. import com.huaxu.model.ResultStatus;
  7. import com.huaxu.service.UserService;
  8. import com.huaxu.util.ByteArrayUtils;
  9. import com.huaxu.util.VerifyCodeUtil;
  10. import io.swagger.annotations.Api;
  11. import io.swagger.annotations.ApiOperation;
  12. import io.swagger.annotations.ApiParam;
  13. import org.apache.commons.lang3.StringUtils;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.beans.factory.annotation.Value;
  16. import org.springframework.stereotype.Controller;
  17. import org.springframework.web.bind.annotation.PostMapping;
  18. import org.springframework.web.bind.annotation.RequestMapping;
  19. import org.springframework.web.bind.annotation.RequestParam;
  20. import org.springframework.web.bind.annotation.ResponseBody;
  21. import com.huaxu.util.RedisUtil;
  22. import java.time.LocalDate;
  23. @Controller
  24. @Api(tags = "短信")
  25. @RequestMapping("sms")
  26. public class SmsController {
  27. @Autowired
  28. private RedisUtil redisUtil;
  29. @Value("${spring.profiles.active}")
  30. private String active;
  31. @Autowired
  32. private UserService userService;
  33. @Value("${maxSendCodeNum}")
  34. private Integer maxSendCodeNum;
  35. @Value("${getSendCodeMax}")
  36. private Integer getSendCodeMax;
  37. @PostMapping("/send")
  38. @ResponseBody
  39. @ApiOperation(value = "发送短信")
  40. public AjaxMessage getCode(
  41. @ApiParam(value = "手机号码", required = true) @RequestParam(required = true) String phone
  42. ) {
  43. if (StringUtils.equals(phone, "18800000000") || StringUtils.equals(phone, "18800000001")) {
  44. return new AjaxMessage(ResultStatus.OK);
  45. }
  46. //检验该手机号码是否注册
  47. String key = "smsValidTime:" + phone;
  48. int user = getUser(phone, key);
  49. if(user==1){
  50. return new AjaxMessage(ResultStatus.PHONE_NUMBER_NOT_FOUND_ERROR);
  51. }else if(user==2){
  52. return new AjaxMessage(ResultStatus.UNABLE_SEND_ERROR);
  53. }
  54. verifyCode(phone,key);
  55. return countVerify(phone);
  56. }
  57. @PostMapping("/v2/send")
  58. @ResponseBody
  59. @ApiOperation(value = "发送短信")
  60. public AjaxMessage getCode(
  61. @ApiParam(value = "手机号码", required = true) @RequestParam(required = true) String mobile,
  62. @ApiParam(value = "验证码", required = true) @RequestParam(required = true) String code,
  63. @ApiParam(value = "随机数 uuid", required = true) @RequestParam String random
  64. ) {
  65. String validKey = "smsValidTime:" + mobile;
  66. int user = getUser(mobile, validKey);
  67. if(user==1){
  68. return new AjaxMessage(ResultStatus.PHONE_NUMBER_NOT_FOUND_ERROR);
  69. }else if(user==2){
  70. return new AjaxMessage(ResultStatus.UNABLE_SEND_ERROR);
  71. }
  72. String key = "validateCode:" + random;
  73. byte[] redisValidateCodeByte = redisUtil.get(key.getBytes());
  74. if (redisValidateCodeByte == null) {
  75. return new AjaxMessage(ResultStatus.VALIDATE_CODE_EXPIRED_ERROR);
  76. } else {
  77. ValidateCode validateCode = (ValidateCode) ByteArrayUtils.bytesToObject(redisValidateCodeByte).get();
  78. if (validateCode.isExpried() || !StringUtils.equals(validateCode.getCode(), code)) {
  79. return new AjaxMessage(ResultStatus.VALIDATE_CODE_ERROR);
  80. }
  81. }
  82. verifyCode(mobile,validKey);
  83. //清除验证码
  84. redisUtil.del(key.getBytes());
  85. return new AjaxMessage(ResultStatus.OK);
  86. }
  87. @PostMapping("test")
  88. @ResponseBody
  89. @ApiOperation(value = "发送短信")
  90. public void test(String phone){
  91. countVerify(phone);
  92. }
  93. private AjaxMessage countVerify(String phone){
  94. LocalDate now = LocalDate.now();
  95. AjaxMessage ajaxMessage=new AjaxMessage(ResultStatus.OK);
  96. String key = "countVerify:" + phone+now.toString();
  97. byte[] countVerifyByte = redisUtil.get(key.getBytes());
  98. if(countVerifyByte!=null){
  99. Integer count = (Integer) ByteArrayUtils.bytesToObject(countVerifyByte).get();
  100. if(count==getSendCodeMax){
  101. ajaxMessage=new AjaxMessage(ResultStatus.SMS_CODE_COUNT);
  102. }else if(count==maxSendCodeNum){
  103. ajaxMessage=new AjaxMessage(ResultStatus.SMS_CODE_LIMIT);
  104. }
  105. }else{
  106. redisUtil.set(key,"0");
  107. }
  108. redisUtil.incr(key);
  109. redisUtil.setExpire(key,60*60*24);
  110. return ajaxMessage;
  111. }
  112. private void verifyCode(String mobile,String validKey){
  113. String verifyCode = VerifyCodeUtil.generateTextCode(VerifyCodeUtil.TYPE_NUM_ONLY, 4, null);
  114. //保存redis
  115. String smsKey = "smsCode:" + mobile;
  116. redisUtil.setExpire(smsKey.getBytes(), ByteArrayUtils.objectToBytes(verifyCode).get(), 900);//15分钟过期
  117. VerifyCodeUtil.sendVerificationCodeSms(mobile, verifyCode);
  118. redisUtil.setExpire(validKey.getBytes(), "".getBytes(), 60);//60秒
  119. }
  120. private int getUser(String phone,String key){
  121. if (StringUtils.equals(phone, "18800000000") || StringUtils.equals(phone, "18800000001")) {
  122. return 0;
  123. }
  124. //检验该手机号码是否注册
  125. User userQuery=new User();
  126. userQuery.setPhone(phone);
  127. User user = userService.findUser(userQuery);
  128. if (user == null) {
  129. return 1;
  130. }
  131. byte[] redisValidateCodeByte = redisUtil.get(key.getBytes());
  132. if (redisValidateCodeByte != null) {
  133. return 2;
  134. }
  135. return 0;
  136. }
  137. }