|
@@ -4,50 +4,60 @@ import cn.hutool.core.util.ObjectUtil;
|
|
import com.baomidou.dynamic.datasource.annotation.DS;
|
|
import com.baomidou.dynamic.datasource.annotation.DS;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
+import com.fdkankan.common.constant.CommonStatus;
|
|
|
|
+import com.fdkankan.common.constant.ErrorCode;
|
|
|
|
+import com.fdkankan.common.exception.BusinessException;
|
|
import com.fdkankan.openApi.constant.RedisKey;
|
|
import com.fdkankan.openApi.constant.RedisKey;
|
|
import com.fdkankan.openApi.entity.system.UserAuthInfo;
|
|
import com.fdkankan.openApi.entity.system.UserAuthInfo;
|
|
-import com.fdkankan.openApi.mapper.system.IUserAuthMapper;
|
|
|
|
-import com.fdkankan.openApi.service.system.IUserAuthService;
|
|
|
|
-import com.fdkankan.openApi.util.JwtUtil;
|
|
|
|
|
|
+import com.fdkankan.openApi.exception.ApiBusinessException;
|
|
|
|
+import com.fdkankan.openApi.mapper.system.IUserAuthInfoMapper;
|
|
|
|
+import com.fdkankan.openApi.service.system.IUserAuthInfoService;
|
|
import com.fdkankan.openApi.util.TokenUtil;
|
|
import com.fdkankan.openApi.util.TokenUtil;
|
|
import com.fdkankan.redis.util.RedisUtil;
|
|
import com.fdkankan.redis.util.RedisUtil;
|
|
-import org.checkerframework.checker.units.qual.A;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Isolation;
|
|
import org.springframework.transaction.annotation.Isolation;
|
|
import org.springframework.transaction.annotation.Propagation;
|
|
import org.springframework.transaction.annotation.Propagation;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
+import java.util.Calendar;
|
|
import java.util.Objects;
|
|
import java.util.Objects;
|
|
|
|
|
|
@DS("system")
|
|
@DS("system")
|
|
@Service
|
|
@Service
|
|
-public class UserAuthServiceImpl extends ServiceImpl<IUserAuthMapper, UserAuthInfo> implements IUserAuthService {
|
|
|
|
|
|
+public class UserAuthInfoInfoServiceImpl extends ServiceImpl<IUserAuthInfoMapper, UserAuthInfo> implements IUserAuthInfoService {
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
RedisUtil redisUtil;
|
|
RedisUtil redisUtil;
|
|
@Autowired
|
|
@Autowired
|
|
- private IUserAuthService userAuthService;
|
|
|
|
|
|
+ private IUserAuthInfoService userAuthService;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public UserAuthInfo findByUserId(Integer userId) {
|
|
|
|
|
|
+ public UserAuthInfo findByUserId(long userId) {
|
|
LambdaQueryWrapper<UserAuthInfo> wrapper = new LambdaQueryWrapper<>();
|
|
LambdaQueryWrapper<UserAuthInfo> wrapper = new LambdaQueryWrapper<>();
|
|
wrapper.eq(UserAuthInfo::getUserId, userId);
|
|
wrapper.eq(UserAuthInfo::getUserId, userId);
|
|
return getOne((wrapper));
|
|
return getOne((wrapper));
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public UserAuthInfo findByUserIdCreateIfNotExits(Integer userId) {
|
|
|
|
|
|
+ public UserAuthInfo findByUserIdCreateIfNotExits(long accountId, long userId) {
|
|
UserAuthInfo userAuthInfo = this.findByUserId(userId);
|
|
UserAuthInfo userAuthInfo = this.findByUserId(userId);
|
|
if(Objects.nonNull(userAuthInfo)){
|
|
if(Objects.nonNull(userAuthInfo)){
|
|
return userAuthInfo;
|
|
return userAuthInfo;
|
|
}
|
|
}
|
|
userAuthInfo = new UserAuthInfo();
|
|
userAuthInfo = new UserAuthInfo();
|
|
- userAuthInfo.setUserId((long)userId);
|
|
|
|
|
|
+ userAuthInfo.setAccountId(accountId);
|
|
|
|
+ userAuthInfo.setUserId(userId);
|
|
userAuthInfo.setAppKey(TokenUtil.createToken());
|
|
userAuthInfo.setAppKey(TokenUtil.createToken());
|
|
- userAuthInfo.setCallCounts(0);
|
|
|
|
|
|
+ userAuthInfo.setTotalCount(-1);
|
|
|
|
+ userAuthInfo.setCallCount(0);
|
|
|
|
+ userAuthInfo.setState(CommonStatus.YES.code().intValue());
|
|
|
|
+ userAuthInfo.setEffectTime(Calendar.getInstance().getTime());
|
|
userAuthService.save(userAuthInfo);
|
|
userAuthService.save(userAuthInfo);
|
|
|
|
+
|
|
|
|
+ this.setReidsCount(userAuthInfo.getAppKey(), -1, true);
|
|
|
|
+
|
|
return userAuthInfo;
|
|
return userAuthInfo;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -108,4 +118,37 @@ public class UserAuthServiceImpl extends ServiceImpl<IUserAuthMapper, UserAuthIn
|
|
}
|
|
}
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public UserAuthInfo getEffectUserAuthInfo(String appKey) {
|
|
|
|
+ UserAuthInfo userAuthInfo = this.findByAppKey(appKey);
|
|
|
|
+ if(Objects.isNull(userAuthInfo)){
|
|
|
|
+ throw new BusinessException(ErrorCode.APP_KEY_ILLEGAL);
|
|
|
|
+ }
|
|
|
|
+ if(CommonStatus.NO.code().intValue() == userAuthInfo.getState()){
|
|
|
|
+ throw new BusinessException(ErrorCode.FAILURE_CODE_10000);
|
|
|
|
+ }
|
|
|
|
+ if(userAuthInfo.getEffectTime().before(Calendar.getInstance().getTime())){
|
|
|
|
+ throw new BusinessException(ErrorCode.FAILURE_CODE_10001);
|
|
|
|
+ }
|
|
|
|
+ return userAuthInfo;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void checkValidAppKey(String appKey) {
|
|
|
|
+ String key = String.format(RedisKey.USER_APP_KEY_INFO, appKey);
|
|
|
|
+ if (!redisUtil.hasKey(key)){
|
|
|
|
+ this.getEffectUserAuthInfo(appKey);
|
|
|
|
+ }
|
|
|
|
+ redisUtil.set(key, appKey, 3600);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void setReidsCount(String appKey, int count, boolean isInfinite) {
|
|
|
|
+ if(isInfinite){
|
|
|
|
+ redisUtil.sSet(RedisKey.API_METHOD_COUNT_INFINITE, appKey);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ redisUtil.incr(String.format(RedisKey.API_METHOD_COUNT, appKey), count);
|
|
|
|
+ }
|
|
}
|
|
}
|