接口访问次数限制
This commit is contained in:
parent
85f71935cd
commit
ed4720d992
|
|
@ -1,13 +1,9 @@
|
|||
package com.sqx.common.utils;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.DayOfWeek;
|
||||
import java.time.LocalDate;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.temporal.TemporalAdjusters;
|
||||
import java.util.Objects;
|
||||
import java.util.Date;
|
||||
|
||||
|
||||
/**
|
||||
|
|
@ -117,7 +113,7 @@ public class ApiAccessLimitUtil {
|
|||
redisUtils.set(redisKey, 1, expireAt);
|
||||
return true;
|
||||
}
|
||||
if ((int) countObj < count) {
|
||||
if (Integer.parseInt(countObj.toString()) < count) {
|
||||
// 访问次数未达上限,次数加1
|
||||
redisUtils.incr(redisKey);
|
||||
return true;
|
||||
|
|
@ -131,17 +127,19 @@ public class ApiAccessLimitUtil {
|
|||
}
|
||||
|
||||
private static long calculateExpireAt(String timePeriod) {
|
||||
LocalDate now = LocalDate.now();
|
||||
LocalDate expireDate = null;
|
||||
Date now = DateUtil.beginOfDay(DateUtil.date());
|
||||
Date expireDate = null;
|
||||
if ("day".equals(timePeriod)) {
|
||||
expireDate = now.plusDays(1).atStartOfDay().toLocalDate();
|
||||
expireDate = DateUtil.endOfDay(now);
|
||||
} 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)) {
|
||||
expireDate = now.plusMonths(1).withDayOfMonth(1).minusDays(1);
|
||||
expireDate = DateUtil.endOfMonth(now);
|
||||
} 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.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.sqx.common.annotation.Debounce;
|
||||
import com.sqx.common.utils.ApiAccessLimitUtil;
|
||||
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.service.AppService;
|
||||
import com.sqx.modules.app.service.UserService;
|
||||
import com.sqx.modules.message.entity.MessageInfo;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
|
|
@ -20,7 +22,6 @@ import javax.servlet.http.HttpServletRequest;
|
|||
|
||||
/**
|
||||
* APP登录授权
|
||||
*
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/app/user")
|
||||
|
|
@ -34,14 +35,14 @@ public class AppController {
|
|||
|
||||
@PostMapping("/authenticationRegister")
|
||||
@ApiOperation("认证创建账号")
|
||||
public Result authenticationRegister(@RequestBody JSONObject jsonObject, HttpServletRequest request){
|
||||
return userService.authenticationRegister(jsonObject,request);
|
||||
public Result authenticationRegister(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
|
||||
return userService.authenticationRegister(jsonObject, request);
|
||||
}
|
||||
|
||||
@Login
|
||||
@PostMapping("/getNewUserRed")
|
||||
@ApiOperation("领取新用户红包")
|
||||
public Result getNewUserRed(@RequestAttribute Long userId){
|
||||
public Result getNewUserRed(@RequestAttribute Long userId) {
|
||||
return userService.getNewUserRed(userId);
|
||||
}
|
||||
|
||||
|
|
@ -49,11 +50,11 @@ public class AppController {
|
|||
@RequestMapping(value = "/updatePwd", method = RequestMethod.POST)
|
||||
@ResponseBody
|
||||
@ApiOperation("用户端修改密码")
|
||||
public Result updatePwd(@LoginUser UserEntity user,String pwd,String oldPwd) {
|
||||
if(!user.getPassword().equals(DigestUtils.sha256Hex(oldPwd))){
|
||||
public Result updatePwd(@LoginUser UserEntity user, String pwd, String oldPwd) {
|
||||
if (!user.getPassword().equals(DigestUtils.sha256Hex(oldPwd))) {
|
||||
return Result.error("原始密码不正确!");
|
||||
}
|
||||
if(pwd.equals(oldPwd)){
|
||||
if (pwd.equals(oldPwd)) {
|
||||
return Result.error("新密码不能与旧密码相同!");
|
||||
}
|
||||
user.setPassword(DigestUtils.sha256Hex(pwd));
|
||||
|
|
@ -65,19 +66,27 @@ public class AppController {
|
|||
@RequestMapping(value = "/updatePhone", method = RequestMethod.POST)
|
||||
@ApiOperation("用户端换绑手机号")
|
||||
@ResponseBody
|
||||
public Result updatePhone(@RequestAttribute("userId") Long userId,@RequestParam String phone, @RequestParam String msg) {
|
||||
return userService.updatePhone(phone, msg,userId);
|
||||
public Result updatePhone(@RequestAttribute("userId") Long userId, @RequestParam String phone, @RequestParam String msg) {
|
||||
return userService.updatePhone(phone, msg, userId);
|
||||
}
|
||||
|
||||
@Login
|
||||
@RequestMapping(value = "/updateUser", method = RequestMethod.POST)
|
||||
@ApiOperation("用户修改个人信息")
|
||||
@ResponseBody
|
||||
public Result updateUserImageUrl(@RequestAttribute("userId") Long userId,String zhiFuBao,String zhiFuBaoName) {
|
||||
if(!ApiAccessLimitUtil.isAccessAllowed(userId.toString(), "updateZFB", 2, "month")){
|
||||
return Result.error("每月仅支持修改两次,请联系管理员");
|
||||
public Result updateUserImageUrl(@RequestAttribute("userId") Long userId, String zhiFuBao, String zhiFuBaoName) {
|
||||
int count = userService.count(new QueryWrapper<UserEntity>()
|
||||
.ne( "user_id", userId)
|
||||
.eq("zhi_fu_bao_name", zhiFuBaoName)
|
||||
.eq("zhi_fu_bao", zhiFuBao));
|
||||
|
||||
if (count > 0) {
|
||||
return Result.error("一个支付宝账号仅可绑定一个支付宝用户");
|
||||
}
|
||||
UserEntity userEntity=new UserEntity();
|
||||
if (!ApiAccessLimitUtil.isAccessAllowed(userId.toString(), "updateZFB", 3, "month")) {
|
||||
return Result.error("每月仅支持修改三次,请联系管理员");
|
||||
}
|
||||
UserEntity userEntity = new UserEntity();
|
||||
userEntity.setZhiFuBao(zhiFuBao);
|
||||
userEntity.setZhiFuBaoName(zhiFuBaoName);
|
||||
userEntity.setUserId(userId);
|
||||
|
|
@ -86,12 +95,11 @@ public class AppController {
|
|||
}
|
||||
|
||||
|
||||
|
||||
@Login
|
||||
@RequestMapping(value = "/updateUsers", method = RequestMethod.POST)
|
||||
@ApiOperation("用户修改个人信息")
|
||||
@ResponseBody
|
||||
public Result updateUsers(@RequestAttribute("userId") Long userId,@RequestBody UserEntity userEntity) {
|
||||
public Result updateUsers(@RequestAttribute("userId") Long userId, @RequestBody UserEntity userEntity) {
|
||||
userEntity.setUserId(userId);
|
||||
userService.updateById(userEntity);
|
||||
return Result.success();
|
||||
|
|
@ -117,7 +125,7 @@ public class AppController {
|
|||
@RequestMapping(value = "/updateUserImageUrl", method = RequestMethod.POST)
|
||||
@ApiOperation("用户修改头像")
|
||||
@ResponseBody
|
||||
public Result updateUserImageUrl(@LoginUser UserEntity user,String avatar) {
|
||||
public Result updateUserImageUrl(@LoginUser UserEntity user, String avatar) {
|
||||
user.setAvatar(avatar);
|
||||
userService.updateById(user);
|
||||
return Result.success();
|
||||
|
|
@ -127,7 +135,7 @@ public class AppController {
|
|||
@RequestMapping(value = "/updateUserName", method = RequestMethod.POST)
|
||||
@ApiOperation("用户修改昵称")
|
||||
@ResponseBody
|
||||
public Result updateUserName(@LoginUser UserEntity user,String userName) {
|
||||
public Result updateUserName(@LoginUser UserEntity user, String userName) {
|
||||
user.setUserName(userName);
|
||||
userService.updateById(user);
|
||||
return Result.success();
|
||||
|
|
@ -138,7 +146,7 @@ public class AppController {
|
|||
@ApiOperation("获取用户详细信息")
|
||||
@ResponseBody
|
||||
public Result selectUserById(@LoginUser UserEntity user) {
|
||||
return Result.success().put("data",user);
|
||||
return Result.success().put("data", user);
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -146,21 +154,21 @@ public class AppController {
|
|||
@ApiOperation("升级检测")
|
||||
@ResponseBody
|
||||
public Result selectNewApp() {
|
||||
return Result.success().put("data",appService.selectNewApp());
|
||||
return Result.success().put("data", appService.selectNewApp());
|
||||
}
|
||||
|
||||
@GetMapping("/openId/{code:.+}/{userId}")
|
||||
@ApiOperation("根据code获取openid")
|
||||
public Result getOpenid(@PathVariable("code") String code,@PathVariable("userId")Long userId) {
|
||||
return userService.getOpenId(code,userId);
|
||||
public Result getOpenid(@PathVariable("code") String code, @PathVariable("userId") Long userId) {
|
||||
return userService.getOpenId(code, userId);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/updateClientId", method = RequestMethod.GET)
|
||||
@ApiOperation("绑定ClientId")
|
||||
@ResponseBody
|
||||
public Result updateClientId(String clientId,Long userId,Integer sysPhone ) {
|
||||
public Result updateClientId(String clientId, Long userId, Integer sysPhone) {
|
||||
userService.updateUserClientIdIsNull(clientId);
|
||||
UserEntity userEntity=new UserEntity();
|
||||
UserEntity userEntity = new UserEntity();
|
||||
userEntity.setSysPhone(sysPhone);
|
||||
userEntity.setUserId(userId);
|
||||
userEntity.setClientid(clientId);
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ spring:
|
|||
database: 0
|
||||
host: localhost
|
||||
port: 6379
|
||||
password: # 密码(默认为空)
|
||||
password: 111111 # 密码(默认为空)
|
||||
timeout: 6000ms # 连接超时时长(毫秒)
|
||||
jedis:
|
||||
pool:
|
||||
|
|
|
|||
Loading…
Reference in New Issue