浏览代码

api次数逻辑优化

dsx 2 年之前
父节点
当前提交
de04bdc290

+ 3 - 1
src/main/java/com/fdkankan/openApi/aop/ValidateApiAOP.java

@@ -4,6 +4,7 @@ import cn.hutool.core.bean.BeanUtil;
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
 import com.fdkankan.common.constant.ErrorCode;
+import com.fdkankan.common.constant.ServerCode;
 import com.fdkankan.openApi.constant.HttpStatus;
 import com.fdkankan.openApi.constant.RedisKey;
 import com.fdkankan.openApi.exception.ApiBusinessException;
@@ -80,6 +81,7 @@ public class ValidateApiAOP {
                 log.info("前置减次数后,{}", decrStock);
                 if (decrStock < 0) {
                     localOverCache.setTimedCache(authorization, true);
+                    //次数用完后,redis次数会变成负一,这里需要返还一次,变为0,客户增加次数时,从0增加
                     redisUtil.incr(String.format(RedisKey.API_METHOD_COUNT, authorization), 1);
                     throw new ApiBusinessException(HttpStatus.COUNT_OVER, "");
                 }
@@ -105,7 +107,7 @@ public class ValidateApiAOP {
         HttpServletRequest request = servletRequestAttributes.getRequest();
         String authorization = request.getHeader("Authorization");
         //方法签名
-        if (BeanUtil.toBean(jsonResult, ResultData.class).getCode() == 0) {
+        if (BeanUtil.toBean(jsonResult, ResultData.class).getCode() == ServerCode.SUCCESS.code()) {
             if (counting) {
                 log.info("进入队列,同步数据库");
                 callApiSender.callApi(new CallApiDTO(authorization));

+ 1 - 1
src/main/java/com/fdkankan/openApi/mq/listener/CallApiListener.java

@@ -55,7 +55,7 @@ public class CallApiListener {
             log.info("callApi开始,id:{},deliveryTag:{},消息体:{}", messageId, deliveryTag, msg);
             CallApiDTO param = JSONObject.parseObject(msg, CallApiDTO.class);
             if (StrUtil.isNotEmpty(param.getAppKey())){
-                userAuthService.decrCallCount(param.getAppKey());
+                userAuthService.incrCallCount(param.getAppKey());
             }
             channel.basicAck(deliveryTag, false);
         } catch (DeadlockLoserDataAccessException e) {

+ 2 - 2
src/main/java/com/fdkankan/openApi/service/system/IUserAuthInfoService.java

@@ -25,7 +25,7 @@ public interface IUserAuthInfoService extends IService<UserAuthInfo> {
 
     boolean updateCallCounts(String appKey, Integer count);
 
-    boolean decrCallCount(String appKey);
+    boolean incrCallCount(String appKey);
 
     boolean updateCallCountsByType(String appKey, Integer count, Integer type);
 
@@ -33,7 +33,7 @@ public interface IUserAuthInfoService extends IService<UserAuthInfo> {
 
     void checkValidAppKey(String appKey);
 
-    void setReidsCount(String appKey, int count, boolean isInfinite);
+    void setReidsCount(String appKey, Integer count, boolean isInfinite);
 
     UserAuthInfo findByAccountId(long accountId);
 

+ 11 - 2
src/main/java/com/fdkankan/openApi/service/system/impl/AccountServiceImpl.java

@@ -125,7 +125,7 @@ public class AccountServiceImpl extends ServiceImpl<IAccountMapper, Account> imp
            throw new BusinessException(ErrorCode.FAILURE_CODE_3021);
         }
         if(param.getState() != CommonStatus.NO.code().intValue()
-                || param.getState() != CommonStatus.YES.code().intValue()){
+                && param.getState() != CommonStatus.YES.code().intValue()){
            throw new BusinessException(ErrorCode.PARAM_ERROR);
         }
         Integer count = param.getCount();
@@ -148,11 +148,14 @@ public class AccountServiceImpl extends ServiceImpl<IAccountMapper, Account> imp
         userAuthInfo.setAppKey(TokenUtil.createToken());
         userAuthInfo.setTotalCount(count);
         userAuthInfo.setCallCount(0);
-        userAuthInfo.setState(userAuthInfo.getState());
+        userAuthInfo.setState(param.getState());
         userAuthInfo.setEffectTime(param.getEffectTime());
         userAuthInfo.setCreaterId(param.getCreaterId());
         userAuthService.save(userAuthInfo);
 
+        //设置redis缓存
+        userAuthService.setReidsCount(userAuthInfo.getAppKey(), count, CountType.NO_LIMIT.code() == param.getCountType());
+
         return ResultData.ok();
     }
 
@@ -198,10 +201,16 @@ public class AccountServiceImpl extends ServiceImpl<IAccountMapper, Account> imp
 
         userAuthInfo.setUserId(account.getUserId());
         userAuthInfo.setTotalCount(totalCount);
+        if(totalCount == -1){//如果由限制改为无限制,调用次数置为初始状态0
+            userAuthInfo.setCallCount(0);
+        }
         userAuthInfo.setState(param.getState());
         userAuthInfo.setUpdaterId(param.getUpdaterId());
         userAuthService.updateById(userAuthInfo);
 
+        //设置redis缓存
+        userAuthService.setReidsCount(userAuthInfo.getAppKey(), count, CountType.NO_LIMIT.code() == param.getCountType());
+
         return ResultData.ok();
     }
 

+ 5 - 3
src/main/java/com/fdkankan/openApi/service/system/impl/UserAuthInfoInfoServiceImpl.java

@@ -57,7 +57,8 @@ public class UserAuthInfoInfoServiceImpl extends ServiceImpl<IUserAuthInfoMapper
         userAuthInfo.setEffectTime(Calendar.getInstance().getTime());
         userAuthService.save(userAuthInfo);
 
-        this.setReidsCount(userAuthInfo.getAppKey(), -1, true);
+        //设置redis缓存
+        this.setReidsCount(userAuthInfo.getAppKey(), null, true);
 
         return userAuthInfo;
     }
@@ -145,16 +146,17 @@ public class UserAuthInfoInfoServiceImpl extends ServiceImpl<IUserAuthInfoMapper
     }
 
     @Override
-    public void setReidsCount(String appKey, int count, boolean isInfinite) {
+    public void setReidsCount(String appKey, Integer count, boolean isInfinite) {
         if(isInfinite){
             redisUtil.sSet(RedisKey.API_METHOD_COUNT_INFINITE, appKey);
+            redisUtil.del(String.format(RedisKey.API_METHOD_COUNT, appKey));
             return;
         }
         redisUtil.incr(String.format(RedisKey.API_METHOD_COUNT, appKey), count);
     }
 
     @Override
-    public boolean decrCallCount(String appKey) {
+    public boolean incrCallCount(String appKey) {
         return this.update(new LambdaUpdateWrapper<UserAuthInfo>().setSql("call_count = call_count + " + 1).eq(UserAuthInfo::getAppKey, appKey));
     }