feat: 功能优化
This commit is contained in:
@@ -59,36 +59,103 @@ public class TikHupServiceImpl implements TikHupService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postTikHup(String type,String methodType,String urlParams,String paramType){
|
||||
public Object postTikHup(String type, String methodType, String urlParams, String paramType) {
|
||||
// 1. 参数校验
|
||||
if (StringUtils.isBlank(type)) {
|
||||
log.error("postTikHup: type 参数为空");
|
||||
return CommonResult.error(400, "接口类型不能为空");
|
||||
}
|
||||
if (StringUtils.isBlank(methodType)) {
|
||||
log.error("postTikHup: methodType 参数为空");
|
||||
return CommonResult.error(400, "请求方法类型不能为空");
|
||||
}
|
||||
|
||||
// 2. 获取接口配置信息
|
||||
TikTokenVO tikTokenVO = tikTokenMapper.getInterfaceUrl(type);
|
||||
String Authorization = tikTokenVO.getPlatformToken();
|
||||
if (tikTokenVO == null) {
|
||||
log.error("postTikHup: 未找到接口类型 {} 的配置信息", type);
|
||||
return CommonResult.error(404, "未找到接口类型 " + type + " 的配置信息");
|
||||
}
|
||||
|
||||
String authorization = tikTokenVO.getPlatformToken();
|
||||
String url = tikTokenVO.getPlatformUrl();
|
||||
try{
|
||||
|
||||
if (StringUtils.isBlank(authorization)) {
|
||||
log.error("postTikHup: 接口类型 {} 的 token 为空", type);
|
||||
return CommonResult.error(500, "接口配置错误:token 为空");
|
||||
}
|
||||
if (StringUtils.isBlank(url)) {
|
||||
log.error("postTikHup: 接口类型 {} 的 URL 为空", type);
|
||||
return CommonResult.error(500, "接口配置错误:URL 为空");
|
||||
}
|
||||
|
||||
// 3. 统一转换为小写进行比较(兼容大小写)
|
||||
String methodTypeLower = methodType.toLowerCase();
|
||||
String paramTypeLower = paramType != null ? paramType.toLowerCase() : "";
|
||||
|
||||
try {
|
||||
Unirest.setTimeouts(0, 0);
|
||||
HttpResponse<String> response;
|
||||
if("post".equals(methodType) && "json".equals(paramType)){
|
||||
|
||||
// 4. 根据请求方法和参数类型构建请求
|
||||
if ("post".equals(methodTypeLower) && "json".equals(paramTypeLower)) {
|
||||
// POST + JSON: 将 urlParams 作为 JSON body
|
||||
log.debug("postTikHup: POST JSON 请求, URL: {}, Body: {}", url, urlParams);
|
||||
response = Unirest.post(url)
|
||||
.header("Authorization", "Bearer "+Authorization)
|
||||
.header("Authorization", "Bearer " + authorization)
|
||||
.header("Content-Type", "application/json")
|
||||
.body(urlParams)
|
||||
.asString();
|
||||
} else if("post".equals(methodType)){
|
||||
} else if ("post".equals(methodTypeLower)) {
|
||||
// POST + 表单参数: 将 urlParams 作为 URL 查询参数
|
||||
log.debug("postTikHup: POST 表单请求, URL: {}?{}", url, urlParams);
|
||||
response = Unirest.post(url + "?" + urlParams)
|
||||
.header("Authorization", "Bearer "+Authorization)
|
||||
.header("Authorization", "Bearer " + authorization)
|
||||
.asString();
|
||||
} else {
|
||||
response = Unirest.get(url + "?" + urlParams)
|
||||
.header("Authorization", "Bearer "+Authorization)
|
||||
// GET 或其他方法: 将 urlParams 作为 URL 查询参数
|
||||
// 处理 URL 拼接:如果 URL 已包含查询参数,使用 & 连接,否则使用 ? 连接
|
||||
String finalUrl = url;
|
||||
if (urlParams != null && !urlParams.trim().isEmpty()) {
|
||||
if (url.contains("?")) {
|
||||
// URL 已包含查询参数,使用 & 连接
|
||||
finalUrl = url + "&" + urlParams;
|
||||
} else {
|
||||
// URL 不包含查询参数,使用 ? 连接
|
||||
finalUrl = url + "?" + urlParams;
|
||||
}
|
||||
}
|
||||
log.info("postTikHup: GET 请求, 原始URL: {}, 参数: {}, 最终URL: {}", url, urlParams, finalUrl);
|
||||
response = Unirest.get(finalUrl)
|
||||
.header("Authorization", "Bearer " + authorization)
|
||||
.asString();
|
||||
}
|
||||
Long userId = SecurityFrameworkUtils.getLoginUser().getId();
|
||||
if(response.getBody() != null){
|
||||
return response.getBody();
|
||||
|
||||
// 5. 检查响应状态码
|
||||
int statusCode = response.getStatus();
|
||||
String responseBody = response.getBody();
|
||||
|
||||
if (statusCode == 200) {
|
||||
if (StringUtils.isNotBlank(responseBody)) {
|
||||
// 尝试解析为 JSON,如果失败则直接返回字符串
|
||||
try {
|
||||
return JSON.parseObject(responseBody);
|
||||
} catch (Exception e) {
|
||||
// 如果不是 JSON,直接返回字符串
|
||||
return responseBody;
|
||||
}
|
||||
} else {
|
||||
log.warn("postTikHup: 接口返回空响应, URL: {}", url);
|
||||
return CommonResult.error(500, "接口返回空响应");
|
||||
}
|
||||
} else {
|
||||
log.error("postTikHup: 接口调用失败, URL: {}, 状态码: {}, 响应: {}", url, statusCode, responseBody);
|
||||
return CommonResult.error(statusCode, "接口调用失败: " + (StringUtils.isNotBlank(responseBody) ? responseBody : "HTTP " + statusCode));
|
||||
}
|
||||
}catch (Exception e){
|
||||
log.error("{}接口调用异常",url);
|
||||
} catch (Exception e) {
|
||||
log.error("postTikHup: 接口调用异常, URL: {}, 错误信息: {}", url, e.getMessage(), e);
|
||||
return CommonResult.error(500, "接口调用异常: " + e.getMessage());
|
||||
}
|
||||
return new HashMap<>();
|
||||
}
|
||||
|
||||
private final String appKey = "sldJ4XSpYp3rKALZ ";
|
||||
|
||||
@@ -32,9 +32,36 @@ public class AppUserPromptController {
|
||||
|
||||
@PostMapping("/create")
|
||||
@Operation(summary = "创建用户提示词")
|
||||
public CommonResult<Long> createUserPrompt(@Valid @RequestBody UserPromptSaveReqVO createReqVO) {
|
||||
// 设置当前登录用户ID
|
||||
createReqVO.setUserId(getLoginUserId());
|
||||
public CommonResult<Long> createUserPrompt(@RequestBody UserPromptSaveReqVO createReqVO) {
|
||||
// 先设置当前登录用户ID(在验证之前设置,避免 @NotNull 验证失败)
|
||||
Long userId = getLoginUserId();
|
||||
if (userId == null) {
|
||||
return CommonResult.error(401, "用户未登录");
|
||||
}
|
||||
createReqVO.setUserId(userId);
|
||||
|
||||
// 手动验证必要字段
|
||||
if (createReqVO.getName() == null || createReqVO.getName().trim().isEmpty()) {
|
||||
return CommonResult.error(400, "提示词名称不能为空");
|
||||
}
|
||||
if (createReqVO.getContent() == null || createReqVO.getContent().trim().isEmpty()) {
|
||||
return CommonResult.error(400, "提示词内容不能为空");
|
||||
}
|
||||
if (createReqVO.getStatus() == null) {
|
||||
return CommonResult.error(400, "状态不能为空");
|
||||
}
|
||||
|
||||
// 设置默认值(如果前端没有传递)
|
||||
if (createReqVO.getIsPublic() == null) {
|
||||
createReqVO.setIsPublic(false); // 默认私有
|
||||
}
|
||||
if (createReqVO.getSort() == null) {
|
||||
createReqVO.setSort(0); // 默认排序为 0
|
||||
}
|
||||
if (createReqVO.getUseCount() == null) {
|
||||
createReqVO.setUseCount(0); // 默认使用次数为 0
|
||||
}
|
||||
|
||||
return success(userPromptService.createUserPrompt(createReqVO));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user