|
@@ -5,6 +5,7 @@ import com.ruoyi.common.core.domain.R;
|
|
|
import com.ruoyi.common.core.ip.IPUtils;
|
|
|
import com.ruoyi.common.core.redis.RedisCache;
|
|
|
import com.ruoyi.common.core.sms.SendMessage;
|
|
|
+import com.ruoyi.common.utils.RedisKeys;
|
|
|
import com.ruoyi.common.utils.StringUtils;
|
|
|
import com.ruoyi.system.domain.UserInfo;
|
|
|
import com.ruoyi.system.domain.vo.SmsResponseVo;
|
|
@@ -19,7 +20,6 @@ import org.springframework.web.bind.annotation.RestController;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import java.time.LocalDate;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
-import java.util.UUID;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
@@ -29,13 +29,14 @@ public class SmsController {
|
|
|
|
|
|
@Autowired
|
|
|
private RedisCache redisCache;
|
|
|
+
|
|
|
@Autowired
|
|
|
private RedisTemplate redisTemplate;
|
|
|
|
|
|
|
|
|
@PostMapping("/send")
|
|
|
public R<SmsResponseVo> sendSms(@RequestBody UserInfo param, HttpServletRequest request) {
|
|
|
- if (StringUtils.isEmpty(param.getUserPhone()) || Pattern.matches("^1[3-9]\\d{9}$\n",param.getUserPhone())){
|
|
|
+ if (StringUtils.isEmpty(param.getUserPhone()) || Pattern.matches("^1[3-9]\\d{9}$\n", param.getUserPhone())) {
|
|
|
return R.fail("手机号格式不正确");
|
|
|
}
|
|
|
|
|
@@ -44,9 +45,9 @@ public class SmsController {
|
|
|
String ip = IPUtils.getIpAddr(request);
|
|
|
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");
|
|
|
String strDate = dtf.format(LocalDate.now());
|
|
|
- String ipKey = strDate + "_" + ip;
|
|
|
- String phoneIconKey = strDate + "_"+"icon" + param.getUserPhone();
|
|
|
- String phone = "phone_"+param.getUserPhone();
|
|
|
+ String ipKey = RedisKeys.INCR_IP + strDate + "_" + ip;
|
|
|
+ String phoneIconKey = RedisKeys.INCR_PHONE + strDate + "_" + param.getUserPhone();
|
|
|
+ String phone = RedisKeys.SMS_PHONE_TIME + param.getUserPhone();
|
|
|
//一分钟发送一次
|
|
|
if (redisTemplate.hasKey(phone)) {
|
|
|
//存在
|
|
@@ -59,7 +60,7 @@ public class SmsController {
|
|
|
//手机号
|
|
|
if (redisTemplate.hasKey(phoneIconKey)) {
|
|
|
//存在
|
|
|
- Long increment = redisTemplate.opsForValue().increment(phoneIconKey, 1);
|
|
|
+ Integer increment = redisCache.getCacheObject(phoneIconKey);
|
|
|
if (increment > 10) {
|
|
|
return R.fail("该手机号发送次数过多,已禁止发送!");
|
|
|
}
|
|
@@ -71,7 +72,7 @@ public class SmsController {
|
|
|
//ip
|
|
|
if (redisTemplate.hasKey(ipKey)) {
|
|
|
//存在
|
|
|
- Long increment = redisTemplate.opsForValue().increment(ipKey, 1);
|
|
|
+ Integer increment = redisCache.getCacheObject(ipKey);
|
|
|
if (increment > 100) {
|
|
|
return R.fail("发送次数过多,已禁止发送!");
|
|
|
}
|
|
@@ -88,13 +89,12 @@ public class SmsController {
|
|
|
SendSmsResponse sendSmsResponse = SendMessage.sendSms(param.getUserPhone(), code);
|
|
|
|
|
|
if (sendSmsResponse.getMessage().equals("OK")) {
|
|
|
- String uuid = UUID.randomUUID().toString().replaceAll("-", "");
|
|
|
- redisCache.setCacheObject(uuid, code, 5, TimeUnit.MINUTES);
|
|
|
+ redisCache.setCacheObject(RedisKeys.SMS_PHONE_CODE + param.getUserPhone(), code, 5, TimeUnit.MINUTES);
|
|
|
smsResponse.setCode(sendSmsResponse.getCode());
|
|
|
smsResponse.setMessage(sendSmsResponse.getMessage());
|
|
|
- smsResponse.setKeyCode(uuid);
|
|
|
+ smsResponse.setKeyCode(RedisKeys.SMS_PHONE_CODE + param.getUserPhone());
|
|
|
|
|
|
- }else {
|
|
|
+ } else {
|
|
|
return R.fail(sendSmsResponse.getMessage());
|
|
|
}
|
|
|
} catch (Exception e) {
|