修复调用失效问题

This commit is contained in:
蓝白 committed 2026-02-04 13:56:19 +08:00
1 parent ee9e51947e
commit 25c6b49775
11 files changed
+79 -61

No files matched your search

@@ -1,5 +1,6 @@
package cn.crabc.core.app.config;
import cn.crabc.core.app.entity.dto.ApiInfoDTO;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.CacheManager;
@@ -28,9 +29,9 @@ public class CacheConfig {
* @return
*/
@Bean("apiCache")
public Cache<String, Object> apiCaffeine() {
public Cache<String, ApiInfoDTO> apiCaffeine() {
return Caffeine.newBuilder()
.expireAfterAccess(10, TimeUnit.HOURS)
.expireAfterAccess(1, TimeUnit.MINUTES)
.build();
}
@@ -1,34 +0,0 @@
package cn.crabc.core.app.config;
import cn.crabc.core.app.service.system.IBaseApiInfoService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* 加载数据
*/
@Component
public class LoadingData implements InitializingBean {
@Autowired
private IBaseApiInfoService iBaseApiInfoService;
/**
* 启动加载
* @throws Exception
*/
@Override
public void afterPropertiesSet() throws Exception {
iBaseApiInfoService.initApi();
}
/**
* 定时加载
*/
@Scheduled(cron = "${crabc.corn.api:0 0/5 * * * ?}") // 每5分钟全量加载一次
public void task() {
iBaseApiInfoService.initApi();
}
}
@@ -35,9 +35,6 @@ public class CommonController {
public Result detail(Long apiId) {
Integer applyCount = 1;
BaseApiInfoVO apiDetail = apiInfoService.getApiDetail(apiId);
if ("0".equals(apiDetail.getShowSqlScript())){
apiDetail.setSqlScript("***");
}
apiDetail.setApplyed(applyCount);
return Result.success(apiDetail);
}
@@ -80,11 +80,6 @@ public class BaseApiInfo extends BaseEntity {
*/
private String sqlScript;
/**
* 是否显示脚本1/0
*/
private String showSqlScript;
/**
* 分页设置,不分页:0、只分页:page、分页并统计:pageCount
*/
@@ -104,6 +99,8 @@ public class BaseApiInfo extends BaseEntity {
* schema
*/
private String schemaName;
// 表名
private String tableName;
/**
* 数据源类型
@@ -40,6 +40,8 @@ public class BaseApiSql extends BaseEntity {
*/
private String schemaName;
private String tableName;
/**
* 数据源类型
*/
@@ -71,8 +71,6 @@ public class BaseApiInfoVO extends BaseEntity {
private Integer pageSetup;
private String sqlScript;
private String showSqlScript;
/**
* 发布时间
*/
@@ -3,6 +3,7 @@ package cn.crabc.core.app.filter;
import cn.crabc.core.app.entity.BaseApiLog;
import cn.crabc.core.app.entity.BaseApp;
import cn.crabc.core.app.entity.dto.ApiInfoDTO;
import cn.crabc.core.app.service.system.IBaseApiInfoService;
import cn.crabc.core.app.service.system.IBaseApiLogService;
import cn.crabc.core.app.util.ApiThreadLocal;
import cn.crabc.core.app.util.RequestUtils;
@@ -40,25 +41,30 @@ public class AuthInterceptor implements HandlerInterceptor {
private static final String API_PRE = "/api/web/";
@Autowired
private IBaseApiLogService iBaseApiLogService;
@Autowired
private IBaseApiInfoService iBaseApiInfoService;
@Value("${crabc.auth.expiresTime:10}")
private Integer expiresTime;
@Autowired
private ObjectMapper objectMapper;
@Autowired
@Qualifier("apiCache")
private Cache<String, Object> apiCache;
private Cache<String, ApiInfoDTO> apiCache;
private ApiInfoDTO getApiData(String key) {
String[] split = key.split("_");
return iBaseApiInfoService.getApiInfoCache(split[0], split[1]);
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String path = request.getRequestURI();
String method = request.getMethod();
Object apiData = apiCache.getIfPresent(method + "_" + path.replace(API_PRE, ""));
if (apiData == null) {
ApiInfoDTO apiInfo = apiCache.get(method + "_" + path.replace(API_PRE, ""), this::getApiData);
if (apiInfo == null) {
setErrorResponse(request, response,ErrorStatusEnum.API_INVALID.getCode(),ErrorStatusEnum.API_INVALID.getMassage());
return false;
}
ApiInfoDTO apiInfo = (ApiInfoDTO) apiData;
if (apiInfo.getEnabled() == 0) {
setErrorResponse(request, response,ErrorStatusEnum.API_OFFLINE.getCode(),ErrorStatusEnum.API_OFFLINE.getMassage());
return false;
@@ -74,6 +74,13 @@ public interface BaseApiInfoMapper {
*/
List<ApiInfoDTO> selectApiDetail(@Param("apiId") Long apiId);
/**
* 根据apiId查询信息
* @param
* @return
*/
ApiInfoDTO getApiDetail(@Param("method") String method, @Param("apiPath") String apiPath);
/**
* 插入API
*
@@ -18,7 +18,13 @@ import java.util.List;
*/
public interface IBaseApiInfoService {
void initApi();
/**
* 根据接口url获取API关联数据
*
* @param
* @return
*/
ApiInfoDTO getApiInfoCache(String method, String apiPath);
/**
* 获取API关联数据
@@ -49,11 +49,23 @@ public class BaseApiInfoServiceImpl implements IBaseApiInfoService {
private BaseApiParamMapper apiParamMapper;
@Autowired
@Qualifier("apiCache")
Cache<String, Object> apiInfoCache;
Cache<String, ApiInfoDTO> apiInfoCache;
@Override
public void initApi() {
updateCache(null);
public ApiInfoDTO getApiInfoCache(String method, String apiPath) {
ApiInfoDTO apiInfo = apiInfoMapper.getApiDetail(method, apiPath);
if (apiInfo == null) {
return null;
}
// 应用
List<BaseApp> appApis = baseAppMapper.selectApiApp(apiInfo.getApiId());
// 请求参数
List<BaseApiParam> baseApiParams = apiParamMapper.selectReqParams(apiInfo.getApiId());
apiInfo.setAppList(appApis == null ? new ArrayList<>() : appApis);
apiInfo.setRequestParams(baseApiParams == null ? new ArrayList<>() : baseApiParams);
return apiInfo;
}
public void updateCache(Long apiId) {
@@ -150,6 +162,7 @@ public class BaseApiInfoServiceImpl implements IBaseApiInfoService {
api.setApiType("SQL");
api.setDatasourceId(sql.getDatasourceId());
api.setSchemaName(sql.getSchemaName());
api.setTableName(sql.getTableName());
api.setDatasourceType(sql.getDatasourceType());
api.setSqlScript(sql.getSqlScript());
api.setCreateTime(date);
@@ -183,6 +196,7 @@ public class BaseApiInfoServiceImpl implements IBaseApiInfoService {
api.setEnabled(0);
api.setDatasourceId(sql.getDatasourceId());
api.setSchemaName(sql.getSchemaName());
api.setTableName(sql.getTableName());
api.setDatasourceType(sql.getDatasourceType());
api.setSqlScript(sql.getSqlScript());
api.setUpdateTime(updateTime);
@@ -23,7 +23,6 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
i.update_by,
i.update_time,
i.release_time,
i.show_sql_script,
g.group_name
FROM
base_api_info i
@@ -90,7 +89,6 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
remarks,
version,
page_setup,
show_sql_script,
datasource_id,
datasource_type,
schema_name,
@@ -106,6 +104,33 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</if>
</select>
<select id="getApiDetail" resultType="cn.crabc.core.app.entity.dto.ApiInfoDTO">
SELECT
api_id,
api_name,
api_path,
api_type,
api_method,
auth_type,
api_status,
enabled,
group_id,
tenant_id,
remarks,
version,
page_setup,
datasource_id,
datasource_type,
schema_name,
sql_script,
sql_type,
result_type
FROM
base_api_info
WHERE
parent_id = 0 AND api_status ='release' and api_path =#{apiPath} and api_method =#{method}
</select>
<select id="selectApiById" resultType="cn.crabc.core.app.entity.BaseApiInfo">
SELECT
api_id,
@@ -127,8 +152,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
datasource_id,
datasource_type,
schema_name,
table_name,
sql_script,
show_sql_script,
page_setup,
sql_type,
result_type
@@ -153,7 +178,6 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
b.page_setup,
b.create_time,
b.sql_script,
b.show_sql_script,
b.update_time,
b.release_time,
b.version
@@ -186,10 +210,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="sqlType != null and sqlType != ''">sql_type,</if>
<if test="resultType != null and resultType != ''">result_type,</if>
<if test="sqlScript != null and sqlScript !=''">sql_script,</if>
<if test="showSqlScript != null and showSqlScript !=''">show_sql_script,</if>
<if test="datasourceId != null and datasourceId != ''">datasource_id,</if>
<if test="datasourceType != null and datasourceType != ''">datasource_type,</if>
<if test="schemaName != null and schemaName != ''">schema_name,</if>
<if test="tableName != null and tableName != ''">table_name,</if>
<if test="pageSetup != null">page_setup,</if>
enabled,
create_by,
@@ -210,10 +234,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="sqlType != null and sqlType != ''">#{sqlType},</if>
<if test="resultType != null and resultType != ''">#{resultType},</if>
<if test="sqlScript != null and sqlScript !=''">#{sqlScript},</if>
<if test="showSqlScript != null and showSqlScript !=''">#{showSqlScript},</if>
<if test="datasourceId != null and datasourceId != ''">#{datasourceId},</if>
<if test="datasourceType != null and datasourceType != ''">#{datasourceType},</if>
<if test="schemaName != null and schemaName != ''">#{schemaName},</if>
<if test="tableName != null and tableName != ''">#{tableName},</if>
<if test="pageSetup != null">#{pageSetup},</if>
#{enabled},
#{createBy},
@@ -240,10 +264,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="sqlType != null and sqlType != ''">sql_type = #{sqlType},</if>
<if test="resultType != null and resultType != ''">result_type = #{resultType},</if>
<if test="sqlScript != null and sqlScript !=''">sql_script = #{sqlScript},</if>
<if test="showSqlScript != null and showSqlScript !=''">show_sql_script =#{showSqlScript},</if>
<if test="datasourceId != null and datasourceId != ''">datasource_id =#{datasourceId},</if>
<if test="datasourceType != null and datasourceType != ''">datasource_type =#{datasourceType},</if>
<if test="schemaName != null and schemaName != ''">schema_name =#{schemaName},</if>
<if test="tableName != null and tableName != ''">table_name =#{tableName},</if>
<if test="pageSetup != null">page_setup =#{pageSetup},</if>
<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
update_time = #{updateTime}