接口访问次数限制
This commit is contained in:
parent
85f71935cd
commit
ed4720d992
|
|
@ -1,13 +1,9 @@
|
||||||
package com.sqx.common.utils;
|
package com.sqx.common.utils;
|
||||||
|
|
||||||
|
import cn.hutool.core.date.DateUtil;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
import java.util.Date;
|
||||||
import java.time.DayOfWeek;
|
|
||||||
import java.time.LocalDate;
|
|
||||||
import java.time.ZoneOffset;
|
|
||||||
import java.time.temporal.TemporalAdjusters;
|
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -117,7 +113,7 @@ public class ApiAccessLimitUtil {
|
||||||
redisUtils.set(redisKey, 1, expireAt);
|
redisUtils.set(redisKey, 1, expireAt);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if ((int) countObj < count) {
|
if (Integer.parseInt(countObj.toString()) < count) {
|
||||||
// 访问次数未达上限,次数加1
|
// 访问次数未达上限,次数加1
|
||||||
redisUtils.incr(redisKey);
|
redisUtils.incr(redisKey);
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -131,17 +127,19 @@ public class ApiAccessLimitUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static long calculateExpireAt(String timePeriod) {
|
private static long calculateExpireAt(String timePeriod) {
|
||||||
LocalDate now = LocalDate.now();
|
Date now = DateUtil.beginOfDay(DateUtil.date());
|
||||||
LocalDate expireDate = null;
|
Date expireDate = null;
|
||||||
if ("day".equals(timePeriod)) {
|
if ("day".equals(timePeriod)) {
|
||||||
expireDate = now.plusDays(1).atStartOfDay().toLocalDate();
|
expireDate = DateUtil.endOfDay(now);
|
||||||
} else if ("week".equals(timePeriod)) {
|
} else if ("week".equals(timePeriod)) {
|
||||||
expireDate = now.plusWeeks(0).with(TemporalAdjusters.nextOrSame(java.time.DayOfWeek.SUNDAY));
|
expireDate = DateUtil.endOfWeek(now);
|
||||||
} else if ("month".equals(timePeriod)) {
|
} else if ("month".equals(timePeriod)) {
|
||||||
expireDate = now.plusMonths(1).withDayOfMonth(1).minusDays(1);
|
expireDate = DateUtil.endOfMonth(now);
|
||||||
} else if ("year".equals(timePeriod)) {
|
} else if ("year".equals(timePeriod)) {
|
||||||
expireDate = now.plusYears(1).withDayOfYear(1).minusDays(1);
|
expireDate = DateUtil.endOfYear(now);
|
||||||
}
|
}
|
||||||
return Objects.requireNonNull(expireDate).atTime(23, 59, 59).toEpochSecond(ZoneOffset.UTC);
|
long endTimeStamp = DateUtil.endOfDay(expireDate).getTime() / 1000L;
|
||||||
|
long currentTimeStamp = DateUtil.currentSeconds();
|
||||||
|
return endTimeStamp - currentTimeStamp;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -2,6 +2,7 @@ package com.sqx.modules.app.controller.app;
|
||||||
|
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
import com.sqx.common.annotation.Debounce;
|
import com.sqx.common.annotation.Debounce;
|
||||||
import com.sqx.common.utils.ApiAccessLimitUtil;
|
import com.sqx.common.utils.ApiAccessLimitUtil;
|
||||||
import com.sqx.common.utils.Result;
|
import com.sqx.common.utils.Result;
|
||||||
|
|
@ -10,6 +11,7 @@ import com.sqx.modules.app.annotation.LoginUser;
|
||||||
import com.sqx.modules.app.entity.UserEntity;
|
import com.sqx.modules.app.entity.UserEntity;
|
||||||
import com.sqx.modules.app.service.AppService;
|
import com.sqx.modules.app.service.AppService;
|
||||||
import com.sqx.modules.app.service.UserService;
|
import com.sqx.modules.app.service.UserService;
|
||||||
|
import com.sqx.modules.message.entity.MessageInfo;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import org.apache.commons.codec.digest.DigestUtils;
|
import org.apache.commons.codec.digest.DigestUtils;
|
||||||
|
|
@ -20,7 +22,6 @@ import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* APP登录授权
|
* APP登录授权
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/app/user")
|
@RequestMapping("/app/user")
|
||||||
|
|
@ -74,8 +75,16 @@ public class AppController {
|
||||||
@ApiOperation("用户修改个人信息")
|
@ApiOperation("用户修改个人信息")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public Result updateUserImageUrl(@RequestAttribute("userId") Long userId, String zhiFuBao, String zhiFuBaoName) {
|
public Result updateUserImageUrl(@RequestAttribute("userId") Long userId, String zhiFuBao, String zhiFuBaoName) {
|
||||||
if(!ApiAccessLimitUtil.isAccessAllowed(userId.toString(), "updateZFB", 2, "month")){
|
int count = userService.count(new QueryWrapper<UserEntity>()
|
||||||
return Result.error("每月仅支持修改两次,请联系管理员");
|
.ne( "user_id", userId)
|
||||||
|
.eq("zhi_fu_bao_name", zhiFuBaoName)
|
||||||
|
.eq("zhi_fu_bao", zhiFuBao));
|
||||||
|
|
||||||
|
if (count > 0) {
|
||||||
|
return Result.error("一个支付宝账号仅可绑定一个支付宝用户");
|
||||||
|
}
|
||||||
|
if (!ApiAccessLimitUtil.isAccessAllowed(userId.toString(), "updateZFB", 3, "month")) {
|
||||||
|
return Result.error("每月仅支持修改三次,请联系管理员");
|
||||||
}
|
}
|
||||||
UserEntity userEntity = new UserEntity();
|
UserEntity userEntity = new UserEntity();
|
||||||
userEntity.setZhiFuBao(zhiFuBao);
|
userEntity.setZhiFuBao(zhiFuBao);
|
||||||
|
|
@ -86,7 +95,6 @@ public class AppController {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Login
|
@Login
|
||||||
@RequestMapping(value = "/updateUsers", method = RequestMethod.POST)
|
@RequestMapping(value = "/updateUsers", method = RequestMethod.POST)
|
||||||
@ApiOperation("用户修改个人信息")
|
@ApiOperation("用户修改个人信息")
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ spring:
|
||||||
database: 0
|
database: 0
|
||||||
host: localhost
|
host: localhost
|
||||||
port: 6379
|
port: 6379
|
||||||
password: # 密码(默认为空)
|
password: 111111 # 密码(默认为空)
|
||||||
timeout: 6000ms # 连接超时时长(毫秒)
|
timeout: 6000ms # 连接超时时长(毫秒)
|
||||||
jedis:
|
jedis:
|
||||||
pool:
|
pool:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue