From 5acbec182b4ea6ec357a17d8c10e7d4a39f0029c Mon Sep 17 00:00:00 2001 From: GYJ <1157756119@qq.com> Date: Tue, 7 Jan 2025 12:44:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=AE=9E=E5=90=8D=E8=AE=A4?= =?UTF-8?q?=E8=AF=81=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sqx/common/utils/ApiAccessLimitUtil.java | 55 ++++++-- .../app/controller/app/AppController.java | 118 +++++++----------- .../controller/app/AppLoginController.java | 10 -- 3 files changed, 87 insertions(+), 96 deletions(-) diff --git a/src/main/java/com/sqx/common/utils/ApiAccessLimitUtil.java b/src/main/java/com/sqx/common/utils/ApiAccessLimitUtil.java index e6e5c944..5b5dc62d 100644 --- a/src/main/java/com/sqx/common/utils/ApiAccessLimitUtil.java +++ b/src/main/java/com/sqx/common/utils/ApiAccessLimitUtil.java @@ -34,8 +34,9 @@ public class ApiAccessLimitUtil { /** * 默认 当月5次 - * @param id 唯一值 - * @param key 接口名称 sys:limit:接口名称 + * + * @param id 唯一值 + * @param key 接口名称 sys:limit:接口名称 * @return */ public static boolean isAccessAllowed(String id, String key) { @@ -58,9 +59,10 @@ public class ApiAccessLimitUtil { /** * 默认月 month/月/自然月 - * @param id 唯一值 - * @param key 接口名称 sys:limit:接口名称 - * @param count 次数限制 + * + * @param id 唯一值 + * @param key 接口名称 sys:limit:接口名称 + * @param count 次数限制 * @return */ public static boolean isAccessAllowed(String id, String key, Integer count) { @@ -82,9 +84,10 @@ public class ApiAccessLimitUtil { /** * 默认 5次 + * * @param id 唯一值 * @param key 接口名称 sys:limit:接口名称 - * @param timeFormat day/天/自然天 week/周/本周日 month/月/自然月 year/年/自然年 + * @param timeFormat day/天/自然天 week/周/本周日 month/月/自然月 year/年/自然年 * @return */ public static boolean isAccessAllowed(String id, String key, String timeFormat) { @@ -108,7 +111,7 @@ public class ApiAccessLimitUtil { * @param id 唯一值 * @param key 接口名称 sys:limit:接口名称 * @param count 次数限制 - * @param timeFormat day/天/自然天 week/周/本周日 month/月/自然月 year/年/自然年 + * @param timeFormat day/天/自然天 week/周/本周日 month/月/自然月 year/年/自然年 * @return */ public static boolean isAccessAllowed(String id, String key, Integer count, String timeFormat) { @@ -118,7 +121,7 @@ public class ApiAccessLimitUtil { long expireAt; if (StrUtil.isBlank(timeFormat)) { expireAt = count; - }else { + } else { // 根据不同时间周期设置过期时间并初始化访问次数为1 expireAt = calculateExpireAt(timeFormat); } @@ -133,14 +136,40 @@ public class ApiAccessLimitUtil { return false; } + public static boolean getCertAuthIsAccessAllowed(String id, String key, Integer count) { + String redisKey = generateRedisKey(key, id); + Object countObj = redisUtils.get(redisKey); + if (countObj == null) { + return true; + } + return Integer.parseInt(countObj.toString()) < count; + } + + public static void setCertAuthIsAccessAllowed(String id, String key, Integer count, String timeFormat) { + String redisKey = generateRedisKey(key, id); + Object countObj = redisUtils.get(redisKey); + if (countObj == null) { + long expireAt; + if (StrUtil.isBlank(timeFormat)) { + expireAt = count; + } else { + // 根据不同时间周期设置过期时间并初始化访问次数为1 + expireAt = calculateExpireAt(timeFormat); + } + redisUtils.set(redisKey, 1, expireAt); + } else { + redisUtils.incr(redisKey); + } + } + public static void removeKey(String id, String key) { String redisKey = generateRedisKey(key, id); redisUtils.delete(redisKey); } - public static T runFunAndCheckKey(Supplier supplier, String lockKey, Integer seconds) { - try{ + public static T runFunAndCheckKey(Supplier supplier, String lockKey, Integer seconds) { + try { // 创建线程id, 用作判断 String clientId = UUID.randomUUID().toString(); // 设置分布式锁 @@ -154,12 +183,12 @@ public class ApiAccessLimitUtil { lock = Boolean.TRUE.equals(redisUtils.setIfAbsent(lockKey, clientId, seconds)); } return supplier.get(); - } catch (RuntimeException e){ + } catch (RuntimeException e) { log.error("执行出错", e); throw e; } catch (InterruptedException e) { throw new RuntimeException(e); - } finally{ + } finally { redisUtils.delete(lockKey); } } @@ -189,7 +218,7 @@ public class ApiAccessLimitUtil { Date now = DateUtil.beginOfDay(DateUtil.date()); Date expireDate = null; if ("day".equals(timePeriod)) { - expireDate = DateUtil.endOfDay(now); + expireDate = DateUtil.endOfDay(now); } else if ("week".equals(timePeriod)) { expireDate = DateUtil.endOfWeek(now); } else if ("month".equals(timePeriod)) { diff --git a/src/main/java/com/sqx/modules/app/controller/app/AppController.java b/src/main/java/com/sqx/modules/app/controller/app/AppController.java index c570ce67..28f32d34 100644 --- a/src/main/java/com/sqx/modules/app/controller/app/AppController.java +++ b/src/main/java/com/sqx/modules/app/controller/app/AppController.java @@ -9,7 +9,6 @@ import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.sqx.common.annotation.Debounce; -import com.sqx.common.exception.SqxException; import com.sqx.common.utils.ApiAccessLimitUtil; import com.sqx.common.utils.DataLimitUtil; import com.sqx.common.utils.Result; @@ -33,6 +32,7 @@ import java.util.Map; /** * APP登录授权 + * @author mac */ @RestController @RequestMapping("/app/user") @@ -104,87 +104,75 @@ public class AppController { UserInfo userInfo = userInfoService.getByUserId(userId); UserEntity userEntity = userService.getById(userId); - String errMsg = null; if (StrUtil.isNotBlank(zhiFuBao) && (!zhiFuBao.equals(userEntity.getZhiFuBao())) || !certName.equals(userEntity.getZhiFuBaoName())) { int count = userService.count(new QueryWrapper() .ne("user_id", userId) .eq("zhi_fu_bao_name", certName) .eq("zhi_fu_bao", zhiFuBao)); if (count > 0) { - errMsg = "支付宝信息修改失败: 一个支付宝账号仅可绑定一个用户"; + return Result.error("支付宝信息修改失败: 此支付宝账号已被绑定"); } - if (errMsg == null && StrUtil.isNotBlank(userInfo.getCertName()) && !certName.equals(userInfo.getCertName())) { - errMsg = "支付宝信息修改失败: 姓名与实名认证信息不相符"; + if (StrUtil.isNotBlank(userInfo.getCertName()) && !certName.equals(userInfo.getCertName())) { + return Result.error("支付宝信息修改失败: 姓名与实名认证信息不相符"); } - if (errMsg == null && !DataLimitUtil.isAccessAllowed(zhiFuBao+certName, Integer.parseInt(commonRepository.findOne(924).getValue()), "month")) { - errMsg = "支付宝信息修改失败: 相同支付宝账号每月可绑定次数已用完"; + if (!DataLimitUtil.isAccessAllowed(zhiFuBao + certName, Integer.parseInt(commonRepository.findOne(924).getValue()), "month")) { + return Result.error("支付宝信息修改失败: 相同支付宝账号每月可绑定次数已用完"); } - if (errMsg == null && !ApiAccessLimitUtil.isAccessAllowed(userId.toString(), "updateZFB", Integer.parseInt(commonRepository.findOne(925).getValue()), "month")) { - errMsg = "支付宝信息修改失败: 每月可修改次数已用完,请联系管理员"; + if (!ApiAccessLimitUtil.isAccessAllowed(userId.toString(), "updateZFB", Integer.parseInt(commonRepository.findOne(925).getValue()), "month")) { + return Result.error("支付宝信息修改失败: 每月可修改次数已用完,请联系管理员"); } - if (errMsg == null) { - userEntity.setZhiFuBao(zhiFuBao); - userEntity.setZhiFuBaoName(certName); - userService.update(userEntity, new LambdaQueryWrapper().eq(UserEntity::getUserId, userId)); - } + userEntity.setZhiFuBao(zhiFuBao); + userEntity.setZhiFuBaoName(certName); + userService.update(userEntity, new LambdaQueryWrapper().eq(UserEntity::getUserId, userId)); + } + + if (StrUtil.isBlank(certNum)) { + return Result.success(); } - String authErrMsg = null; - if (StrUtil.isNotBlank(certNum) && (!certNum.equals(userInfo.getCertNo()) || !certName.equals(userInfo.getCertName()))) { + if (!certNum.equals(userInfo.getCertNo()) || !certName.equals(userInfo.getCertName())) { if (StrUtil.isNotBlank(userEntity.getZhiFuBaoName()) && !certName.equals(userEntity.getZhiFuBaoName())) { - authErrMsg = "实名修改失败: 姓名与绑定支付宝信息不相符"; + return Result.error("实名修改失败: 姓名与绑定支付宝信息不相符"); } - if (authErrMsg == null && !IdcardUtil.isValidCard(certNum)) { - authErrMsg = "实名修改失败: 身份证号码有误"; + if (!IdcardUtil.isValidCard(certNum)) { + return Result.error("实名修改失败: 身份证号码有误"); } - if (authErrMsg == null) { - Integer idCount = userInfoService.countCertCount(certName, certNum); - if (idCount >= 1) { - authErrMsg = "实名修改失败: 此实名信息已存在"; - } - } - - if (authErrMsg == null && !ApiAccessLimitUtil.isAccessAllowed(String.valueOf(userId), "updateAuthCertInfo", 1, "month")) { - authErrMsg = "实名修改失败: 账号每月可修改次数已用完,请联系管理员"; - } - - if (authErrMsg == null && !ApiAccessLimitUtil.isAccessAllowed(certNum, "updateAuthCertInfoByIdCard", 1, "month")) { - authErrMsg = "实名修改失败: 此身份证信息次月已绑定过,请联系管理员"; - } - - if (authErrMsg == null) { - try { - aliService.authCertNo(certName, certNum); - userInfo.setCertName(certName); - userInfo.setCertNo(certNum); - userInfo.setUpdateTime(DateUtil.date()); - boolean update = userInfoService.update(userInfo, new LambdaQueryWrapper().eq(UserInfo::getUserId, userId).eq(UserInfo::getId, userInfo.getId())); - if (!update) { - ApiAccessLimitUtil.removeKey(String.valueOf(userId), "updateAuthCertInfo"); - ApiAccessLimitUtil.removeKey(certNum, "updateAuthCertInfoByIdCard"); - } - }catch (Exception e) { - ApiAccessLimitUtil.removeKey(String.valueOf(userId), "updateAuthCertInfo"); - ApiAccessLimitUtil.removeKey(certNum, "updateAuthCertInfoByIdCard"); - authErrMsg = "实名修改失败: 身份证信息不匹配"; + Integer idCount = userInfoService.countCertCount(certName, certNum); + if (idCount >= 1) { + return Result.error("实名修改失败: 此身份证信息已绑定过"); + } + + if (!ApiAccessLimitUtil.getCertAuthIsAccessAllowed(String.valueOf(userId), "updateAuthCertInfo", 1)) { + return Result.error("实名修改失败: 每月可修改次数已用完,请联系管理员"); + } + + if (!ApiAccessLimitUtil.getCertAuthIsAccessAllowed(certNum, "updateAuthCertInfoByIdCard", 1)) { + return Result.error("实名修改失败: 每月可修改次数已用完,请联系管理员"); + } + + try { + aliService.authCertNo(certName, certNum); + userInfo.setCertName(certName); + userInfo.setCertNo(certNum); + userInfo.setUpdateTime(DateUtil.date()); + boolean update = userInfoService.update(userInfo, new LambdaQueryWrapper().eq(UserInfo::getUserId, userId).eq(UserInfo::getId, userInfo.getId())); + if (!update) { + return Result.error("实名修改失败: 请稍后重试"); } + ApiAccessLimitUtil.setCertAuthIsAccessAllowed(String.valueOf(userId), "updateAuthCertInfo", 1, "month"); + ApiAccessLimitUtil.setCertAuthIsAccessAllowed(certNum, "updateAuthCertInfoByIdCard", 1, "month"); + return Result.success(); + } catch (Exception e) { + return Result.error("实名修改失败: 身份证信息不匹配"); } } - if (errMsg != null || authErrMsg != null) { - authErrMsg = authErrMsg == null ? "" : authErrMsg; - return Result.error(errMsg == null ? authErrMsg : errMsg + " " + authErrMsg); - } return Result.success(); - // 去除首绑支付宝奖励 -// if (bool && isFirstBind) { -// userService.firstBindAwardsMoney(old); -// } } @@ -198,22 +186,6 @@ public class AppController { return Result.success(); } - - /*@Login - @RequestMapping(value = "/updateUsers", method = RequestMethod.POST) - @ApiOperation("用户修改个人信息") - @ResponseBody - public Result updateUsers(@RequestAttribute("userId") Long userId,String userName,String avatar,String phone) { - UserEntity userEntity=new UserEntity(); - userEntity.setUserId(userId); - userEntity.setUserName(userName); - userEntity.setAvatar(avatar); - userEntity.setPhone(phone); - userService.updateById(userEntity); - return Result.success(); - }*/ - - @Login @RequestMapping(value = "/updateUserImageUrl", method = RequestMethod.POST) @ApiOperation("用户修改头像") diff --git a/src/main/java/com/sqx/modules/app/controller/app/AppLoginController.java b/src/main/java/com/sqx/modules/app/controller/app/AppLoginController.java index a621ad32..f6d55dd1 100644 --- a/src/main/java/com/sqx/modules/app/controller/app/AppLoginController.java +++ b/src/main/java/com/sqx/modules/app/controller/app/AppLoginController.java @@ -167,16 +167,6 @@ public class AppLoginController { return userService.sendMsg(phone, state,null); } - @Login - @Debounce(value = "#authCertNoDTO.idNum") - @PostMapping("/authCertNo") - public Result authCertNo(@RequestBody @Validated AuthCertNoDTO authCertNoDTO, @RequestAttribute("userId") long userId) { - if (!ApiAccessLimitUtil.isAccessAllowed(String.valueOf(userId), "updateAuthCertInfo", 1, "month")) { - return Result.error("每月可修改次数已用完,请联系管理员"); - } - return Result.success().put("data", userService.authCertNo(userId, authCertNoDTO)); - } - @ApiOperation("解密手机号") @RequestMapping(value = "/selectPhone",method = RequestMethod.POST) public Result getPhoneNumberBeanS5(@RequestBody WxPhone wxPhone) {