Commit 7466cacc authored by zhouxudong's avatar zhouxudong

短信次数限制

parent a372d7c1
......@@ -113,13 +113,21 @@ public class Constants {
public static final String[] JOB_ERROR_STR = { "java.net.URL", "javax.naming.InitialContext", "org.yaml.snakeyaml",
"org.springframework", "org.apache", "com.ruoyi.common.utils.file", "com.ruoyi.common.config" };
//token信息
public static final String token="Bearer ";
//手机验证码 规则: 手机验证码:验证码类型:手机号
public static final String MSG_KEY="phone:message:";
public static final String TOKEN_HEADER = "Authorization";
//秒
public static final int INTERVAL_TIME =4*60;
//用户 数据详情查看额度 map存储: 格式: key phone + ":" + type value
//------------------redis key--------------------------
public static final String token="Bearer ";
//手机验证码 规则: 手机验证码:验证码类型:手机号
public static final String MSG_KEY="phone:message:";
//用户查看数据详情 使用额度 map存储: 格式: key phone + ":" + type value
public static final String LIMIT_NUM="limit_num";
public static final String LIMIT_SMS="limit_sms:";
//---------------------end------------------------------
}
package com.lyy.user.moudle.user.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.ReUtil;
......@@ -31,6 +32,7 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
......@@ -56,8 +58,13 @@ public class SysUserInfoServiceImpl extends ServiceImpl<SysUserInfoMapper, SysUs
private final SysSmsLogService sysSmsLogService;
@Autowired private SendMsgUtils sendMsgUtils;
@Autowired
private BusinessService businessService;
@Autowired private BusinessService businessService;
@Value("${sms.limit}")
private Integer limitSms;
@Value("${sms.intervalTime}")
private Integer intervalTime;
public boolean checkPassword(String realPassword, String password) {
String md5Password =
......@@ -158,6 +165,24 @@ public class SysUserInfoServiceImpl extends ServiceImpl<SysUserInfoMapper, SysUs
lambdaQueryWrapper.eq(SysUserInfo::getPhonenumber, phone);
return this.count(lambdaQueryWrapper) > 0;
}
/**
* @description: 手机号每日发送次数
* @date: 2023/11/27 15:32
* @param: [phone]
* @return: void
*/
private void limitSms(String phone) {
String key = Constants.LIMIT_SMS + phone;
DateTime now = DateUtil.date();
DateTime startTime = DateUtil.offsetDay(now, this.intervalTime * -1);
RedisUtil.delZset(key, 0, startTime.getTime());
Long count = RedisUtil.countZset(key, startTime.getTime(), now.getTime());
if (count == null || count <= limitSms) {
RedisUtil.addZset(key, phone, now.getTime());
} else {
throw new ServiceException("短信发送频繁,每日短信发送次数为" + limitSms);
}
}
/**
* @description: 发送短信
......@@ -171,13 +196,13 @@ public class SysUserInfoServiceImpl extends ServiceImpl<SysUserInfoMapper, SysUs
if (StringUtils.isEmpty(SendMsgTypeEnum.getTemplateId(sendPhoneVo.getType()))) {
throw new ServiceException("参数有误");
}
limitSms(sendPhoneVo.getPhone());
String key = Constants.MSG_KEY + sendPhoneVo.getType() + ":" + sendPhoneVo.getPhone();
Object code = RedisUtil.get(key);
//
Long expire = RedisUtil.getExpire(key);
// 间隔 1分钟内
if (expire != null && expire > Constants.INTERVAL_TIME) {
throw new ServiceException("短信以发送,请稍后再发");
throw new ServiceException("短信已发送,请稍后重试");
}
int randomInt = RandomUtil.randomInt(100000, 999999);
List<Integer> temp = new ArrayList<>();
......@@ -241,15 +266,21 @@ public class SysUserInfoServiceImpl extends ServiceImpl<SysUserInfoMapper, SysUs
DigestUtils.md5DigestAsHex(
(registerVo.getPassword() + lyyConfig.getPasswordSalt()).getBytes()));
userInfo.setSalt(lyyConfig.getPasswordSalt());
//给当前用户存储 解锁次数
RedisUtil.hPut(Constants.LIMIT_NUM, phone + ":" + BusinessTypeEnum.PARK.getCode(), BusinessTypeEnum.PARK.getNumber());
RedisUtil.hPut(Constants.LIMIT_NUM, phone + ":" + BusinessTypeEnum.CARRIER.getCode(), BusinessTypeEnum.CARRIER.getNumber());
//保存一条线索信息
Map<String,Object> saveThread=new HashMap<>();
saveThread.put("dataSource",1);
saveThread.put("phone",phone);
// 给当前用户存储 解锁次数
RedisUtil.hPut(
Constants.LIMIT_NUM,
phone + ":" + BusinessTypeEnum.PARK.getCode(),
BusinessTypeEnum.PARK.getNumber());
RedisUtil.hPut(
Constants.LIMIT_NUM,
phone + ":" + BusinessTypeEnum.CARRIER.getCode(),
BusinessTypeEnum.CARRIER.getNumber());
// 保存一条线索信息
Map<String, Object> saveThread = new HashMap<>();
saveThread.put("dataSource", 1);
saveThread.put("phone", phone);
CompletableFuture.runAsync(()-> businessService.savThread(saveThread));
CompletableFuture.runAsync(() -> businessService.savThread(saveThread));
return this.save(userInfo);
}
......@@ -258,25 +289,27 @@ public class SysUserInfoServiceImpl extends ServiceImpl<SysUserInfoMapper, SysUs
* @date: 2023/11/23 10:21
* @param: [forgetVo]
* @return: boolean
**/
*/
@Override
public boolean forget(ForgetVo forgetVo,Integer type) {
public boolean forget(ForgetVo forgetVo, Integer type) {
String phone = forgetVo.getPhone();
LambdaQueryWrapper<SysUserInfo> userQuery=new LambdaQueryWrapper<>();
userQuery.eq(SysUserInfo::getPhonenumber,phone);
LambdaQueryWrapper<SysUserInfo> userQuery = new LambdaQueryWrapper<>();
userQuery.eq(SysUserInfo::getPhonenumber, phone);
SysUserInfo sysUserInfo = this.getOne(userQuery);
if(ObjectUtils.isEmpty(sysUserInfo)){
if (ObjectUtils.isEmpty(sysUserInfo)) {
throw new ServiceException("该用户不存在,请先注册");
}
String key = Constants.MSG_KEY + type + ":" + forgetVo.getPhone();
Object code = RedisUtil.get(key);
if(ObjectUtils.isEmpty(code)){
if (ObjectUtils.isEmpty(code)) {
throw new ServiceException("验证码无效,请重新发送");
}
if(!code.equals(forgetVo.getCode())){
if (!code.equals(forgetVo.getCode())) {
throw new ServiceException("验证码错误");
}
String newPassword= DigestUtils.md5DigestAsHex((forgetVo.getPassword() + lyyConfig.getPasswordSalt()).getBytes());
String newPassword =
DigestUtils.md5DigestAsHex(
(forgetVo.getPassword() + lyyConfig.getPasswordSalt()).getBytes());
sysUserInfo.setPassword(newPassword);
return this.updateById(sysUserInfo);
}
......
......@@ -299,4 +299,32 @@ public class RedisUtil {
public static Object listGetR(final String key) {
return redisTemplate.opsForList().rightPop(key);
}
/**
* @description: 移除有序集合中给定的分数区间的所有成员
* @date: 2023/11/27 15:19
* @param: [key, start, end]
* @return: Long 移除的成员数
**/
public static Long delZset(String key,double start,double end){
return redisTemplate.opsForZSet().removeRangeByScore(key, start, end);
}
/**
* @description: 计算在有序集合中指定区间分数的成员数
* @date: 2023/11/27 15:21
* @param: [key, start, end]
* @return: java.lang.Long
**/
public static Long countZset(String key,double start,double end){
return redisTemplate.opsForZSet().count(key, start, end);
}
/**
* @description: 向有序集合添加一个或多个成员,或者更新已存在成员的分数
* @date: 2023/11/27 15:26
* @param: [key, fileds, end]
* @return: boolean
**/
public static boolean addZset(String key,Object fileds,double end){
return Boolean.TRUE.equals(redisTemplate.opsForZSet().add(key, fileds, end));
}
}
......@@ -41,4 +41,8 @@ sms:
appUrl: https://smsapi.cn-north-4.myhuaweicloud.com:443/sms/batchSendSms/v1
sender: 8823112010314
signature: 立业云
#短信每日发送次数
limit: 10
#短信时间间隔(每天限制次数) 单位 天
intervalTime: 1
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment