瀏覽代碼

用户余额控制修改

xusonglin 5 年之前
父節點
當前提交
debca7621e

+ 1 - 1
src/main/java/com/jkcredit/invoice/hub/constant/CommonConstant.java

@@ -75,5 +75,5 @@ public class CommonConstant {
     public static final String CHARGE_QUEUE = "charge_queue";
 
     // 接口用户appKey
-    public static final String INVOICE_HUB_USER_APPKEY = "invoicehub:appkey:appsecret";
+    public static final String INVOICE_HUB_USER_APPKEY = "invoicehub:appkey:";
 }

+ 46 - 14
src/main/java/com/jkcredit/invoice/hub/service/apiUser/ApiUserServiceImpl.java

@@ -16,6 +16,7 @@ import com.jkcredit.invoice.hub.spi.rest.data.ApiResponseData;
 import com.jkcredit.invoice.hub.util.CommonUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
 import org.thymeleaf.util.ArrayUtils;
 
@@ -32,26 +33,57 @@ public class ApiUserServiceImpl extends BaseService implements ApiUserService {
     UserService userService;
     @Autowired
     UserBalanceService userBalanceService;
-
+    @Autowired
+    private RedisTemplate redisTemplate;
     @Override
     public Integer validateUser(ApiRequestParam param) {
+        Object object = redisTemplate.opsForValue().get(CommonConstant.INVOICE_HUB_USER_APPKEY + param.getAppKey());
+        Boolean availableUser;
+        String userId = "";
+        if (object != null) {
+            JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(object));
+            if (!param.getAppSecret().equals(jsonObject.get("appSecret"))) {
+                availableUser = validateUser(param.getAppKey(), param.getAppSecret(), userId);
+            } else {
+                userId = jsonObject.get("userId").toString();
+                availableUser = true;
+            }
+        } else {
+            availableUser = validateUser(param.getAppKey(), param.getAppSecret(), userId);
+        }
+
         // 无效用户
-        UserPo userPo = userService.getUserByAppKey(param.getAppKey());
-        // TODO 余额控制需要测试
-        if (userPo == null) {
-            return ApiResponseCodeEnum.CODE_1000.getValue();
-        } else if (!userPo.getAppSecret().equals(param.getAppSecret())) {
+        if (!availableUser) {
             return ApiResponseCodeEnum.CODE_1000.getValue();
-        } else if (!ArrayUtils.contains(CommonConstant.CAR_FREE_API_ARGS, param.getApi())) {
+        }
+        if (!ArrayUtils.contains(CommonConstant.CAR_FREE_API_ARGS, param.getApi())) {
             return ApiResponseCodeEnum.CODE_1080.getValue();
-        } else if (userPo.getBalance() == null || (CommonUtil.parseDouble(userPo.getBalance()) / CommonUtil.parseDouble(userPo.getPrice()) < CommonUtil.parseDouble(userPo.getPrice())
-                && (param.getApi().equals(CommonConstant.WAY_BILL_START) || param.getApi().equals(CommonConstant.WAY_BILL_HISTORY_START)))) {
-            return ApiResponseCodeEnum.CODE_1090.getValue();
+        }
+        if (param.getApi().equals(CommonConstant.WAY_BILL_START) || param.getApi().equals(CommonConstant.WAY_BILL_HISTORY_START)) {
+            UserPo userPo = userService.getUserByAppKey(param.getAppKey());
+            if (userPo.getBalance() == null || (CommonUtil.parseDouble(userPo.getBalance()) / CommonUtil.parseDouble(userPo.getPrice()) < CommonUtil.parseDouble(userPo.getPrice()))) {
+                return ApiResponseCodeEnum.CODE_1090.getValue();
+            }
+        }
+        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(param.getData()));
+        jsonObject.put("userId", userId);
+        param.setData(jsonObject);
+        return ApiResponseCodeEnum.CODE_200.getValue();
+    }
+
+    private Boolean validateUser(String appKey, String appSecret, String userId) {
+        UserPo userPo = userService.getUserByAppKey(appKey);
+        if (userPo == null) {
+            return false;
+        } else if (!userPo.getAppSecret().equals(appSecret)) {
+            return false;
         } else {
-            JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(param.getData()));
-            jsonObject.put("userId", userPo.getId());
-            param.setData(jsonObject);
-            return ApiResponseCodeEnum.CODE_200.getValue();
+            userId = userPo.getId().toString();
+            JSONObject jsonObject = new JSONObject();
+            jsonObject.put("appSecret", appSecret);
+            jsonObject.put("userId", userId);
+            redisTemplate.opsForValue().set(CommonConstant.INVOICE_HUB_USER_APPKEY + appKey, jsonObject);
+            return true;
         }
     }
 

+ 1 - 5
src/main/java/com/jkcredit/invoice/hub/service/user/UserServiceImpl.java

@@ -146,11 +146,7 @@ public class UserServiceImpl extends BaseService implements UserService {
 
     @Override
     public UserPo getUserByAppKey(String appKey) {
-        UserPo userPo = userMapper.getUserByAppKey(appKey);
-        if (userPo == null) {
-            throw new ServiceException(ExceptionMessage.USER_NOT_EXIST);
-        }
-        return userPo;
+        return userMapper.getUserByAppKey(appKey);
     }
 
     @Override