|
@@ -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;
|
|
|
}
|
|
|
}
|
|
|
|