This commit is contained in:
van
2026-05-20 00:02:23 +08:00
parent 4aec0177dd
commit 8fcd7be160

View File

@@ -24,12 +24,12 @@ import java.util.regex.PatternSyntaxException;
/** /**
* 小红书/抖音内容生成Service业务层处理 * 小红书/抖音内容生成Service业务层处理
* *
* @author ruoyi * @author ruoyi
* @date 2025-01-XX * @date 2025-01-XX
*/ */
@Service @Service
public class SocialMediaServiceImpl implements ISocialMediaService public class SocialMediaServiceImpl implements ISocialMediaService
{ {
private static final Logger log = LoggerFactory.getLogger(SocialMediaServiceImpl.class); private static final Logger log = LoggerFactory.getLogger(SocialMediaServiceImpl.class);
@@ -194,7 +194,7 @@ public class SocialMediaServiceImpl implements ISocialMediaService
@Override @Override
public Map<String, Object> extractKeywords(String productName) { public Map<String, Object> extractKeywords(String productName) {
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new HashMap<>();
if (StringUtils.isEmpty(productName)) { if (StringUtils.isEmpty(productName)) {
result.put("success", false); result.put("success", false);
result.put("error", "商品名称不能为空"); result.put("error", "商品名称不能为空");
@@ -206,15 +206,15 @@ public class SocialMediaServiceImpl implements ISocialMediaService
String url = jarvisBaseUrl + "/jarvis/social-media/extract-keywords"; String url = jarvisBaseUrl + "/jarvis/social-media/extract-keywords";
JSONObject requestBody = new JSONObject(); JSONObject requestBody = new JSONObject();
requestBody.put("productName", productName); requestBody.put("productName", productName);
log.info("调用jarvis_java提取关键词接口URL: {}, 参数: {}", url, requestBody.toJSONString()); log.info("调用jarvis_java提取关键词接口URL: {}, 参数: {}", url, requestBody.toJSONString());
String response = HttpUtils.sendJsonPost(url, requestBody.toJSONString()); String response = HttpUtils.sendJsonPost(url, requestBody.toJSONString());
log.info("jarvis_java响应: {}", response); log.info("jarvis_java响应: {}", response);
if (StringUtils.isEmpty(response)) { if (StringUtils.isEmpty(response)) {
throw new Exception("jarvis_java返回空结果"); throw new Exception("jarvis_java返回空结果");
} }
// 解析响应 // 解析响应
Object parsed = JSON.parse(response); Object parsed = JSON.parse(response);
if (parsed instanceof JSONObject) { if (parsed instanceof JSONObject) {
@@ -233,11 +233,11 @@ public class SocialMediaServiceImpl implements ISocialMediaService
return result; return result;
} }
} }
result.put("success", false); result.put("success", false);
result.put("error", "响应格式错误"); result.put("error", "响应格式错误");
return result; return result;
} catch (Exception e) { } catch (Exception e) {
log.error("提取关键词失败", e); log.error("提取关键词失败", e);
result.put("success", false); result.put("success", false);
@@ -250,10 +250,10 @@ public class SocialMediaServiceImpl implements ISocialMediaService
* 生成文案 * 生成文案
*/ */
@Override @Override
public Map<String, Object> generateContent(String productName, Object originalPrice, public Map<String, Object> generateContent(String productName, Object originalPrice,
Object finalPrice, String keywords, String style) { Object finalPrice, String keywords, String style) {
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new HashMap<>();
if (StringUtils.isEmpty(productName)) { if (StringUtils.isEmpty(productName)) {
result.put("success", false); result.put("success", false);
result.put("error", "商品名称不能为空"); result.put("error", "商品名称不能为空");
@@ -277,15 +277,15 @@ public class SocialMediaServiceImpl implements ISocialMediaService
if (StringUtils.isNotEmpty(style)) { if (StringUtils.isNotEmpty(style)) {
requestBody.put("style", style); requestBody.put("style", style);
} }
log.info("调用jarvis_java生成文案接口URL: {}, 参数: {}", url, requestBody.toJSONString()); log.info("调用jarvis_java生成文案接口URL: {}, 参数: {}", url, requestBody.toJSONString());
String response = HttpUtils.sendJsonPost(url, requestBody.toJSONString()); String response = HttpUtils.sendJsonPost(url, requestBody.toJSONString());
log.info("jarvis_java响应: {}", response); log.info("jarvis_java响应: {}", response);
if (StringUtils.isEmpty(response)) { if (StringUtils.isEmpty(response)) {
throw new Exception("jarvis_java返回空结果"); throw new Exception("jarvis_java返回空结果");
} }
// 解析响应 // 解析响应
Object parsed = JSON.parse(response); Object parsed = JSON.parse(response);
if (parsed instanceof JSONObject) { if (parsed instanceof JSONObject) {
@@ -304,11 +304,11 @@ public class SocialMediaServiceImpl implements ISocialMediaService
return result; return result;
} }
} }
result.put("success", false); result.put("success", false);
result.put("error", "响应格式错误"); result.put("error", "响应格式错误");
return result; return result;
} catch (Exception e) { } catch (Exception e) {
log.error("生成文案失败", e); log.error("生成文案失败", e);
result.put("success", false); result.put("success", false);
@@ -324,7 +324,7 @@ public class SocialMediaServiceImpl implements ISocialMediaService
public Map<String, Object> generateCompleteContent(String productImageUrl, String productName, public Map<String, Object> generateCompleteContent(String productImageUrl, String productName,
Object originalPrice, Object finalPrice, String style) { Object originalPrice, Object finalPrice, String style) {
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new HashMap<>();
if (StringUtils.isEmpty(productName)) { if (StringUtils.isEmpty(productName)) {
result.put("success", false); result.put("success", false);
result.put("error", "商品名称不能为空"); result.put("error", "商品名称不能为空");
@@ -348,15 +348,15 @@ public class SocialMediaServiceImpl implements ISocialMediaService
if (StringUtils.isNotEmpty(style)) { if (StringUtils.isNotEmpty(style)) {
requestBody.put("style", style); requestBody.put("style", style);
} }
log.info("调用jarvis_java生成完整内容接口URL: {}, 参数: {}", url, requestBody.toJSONString()); log.info("调用jarvis_java生成完整内容接口URL: {}, 参数: {}", url, requestBody.toJSONString());
String response = HttpUtils.sendJsonPost(url, requestBody.toJSONString()); String response = HttpUtils.sendJsonPost(url, requestBody.toJSONString());
log.info("jarvis_java响应: {}", response); log.info("jarvis_java响应: {}", response);
if (StringUtils.isEmpty(response)) { if (StringUtils.isEmpty(response)) {
throw new Exception("jarvis_java返回空结果"); throw new Exception("jarvis_java返回空结果");
} }
// 解析响应 // 解析响应
Object parsed = JSON.parse(response); Object parsed = JSON.parse(response);
if (parsed instanceof JSONObject) { if (parsed instanceof JSONObject) {
@@ -375,11 +375,11 @@ public class SocialMediaServiceImpl implements ISocialMediaService
return result; return result;
} }
} }
result.put("success", false); result.put("success", false);
result.put("error", "响应格式错误"); result.put("error", "响应格式错误");
return result; return result;
} catch (Exception e) { } catch (Exception e) {
log.error("生成完整内容失败", e); log.error("生成完整内容失败", e);
result.put("success", false); result.put("success", false);
@@ -395,20 +395,20 @@ public class SocialMediaServiceImpl implements ISocialMediaService
public AjaxResult listPromptTemplates() { public AjaxResult listPromptTemplates() {
try { try {
Map<String, Object> templates = new HashMap<>(); Map<String, Object> templates = new HashMap<>();
for (String key : TEMPLATE_KEYS) { for (String key : TEMPLATE_KEYS) {
Map<String, Object> templateInfo = new HashMap<>(); Map<String, Object> templateInfo = new HashMap<>();
templateInfo.put("key", key); templateInfo.put("key", key);
templateInfo.put("description", TEMPLATE_DESCRIPTIONS.get(key)); templateInfo.put("description", TEMPLATE_DESCRIPTIONS.get(key));
String stored = getTemplateFromRedis(key); String stored = getTemplateFromRedis(key);
boolean isDefault = StringUtils.isEmpty(stored); boolean isDefault = StringUtils.isEmpty(stored);
templateInfo.put("template", isDefault ? DEFAULT_PROMPT_TEMPLATES.getOrDefault(key, "") : stored); templateInfo.put("template", isDefault ? DEFAULT_PROMPT_TEMPLATES.getOrDefault(key, "") : stored);
templateInfo.put("isDefault", isDefault); templateInfo.put("isDefault", isDefault);
templates.put(key, templateInfo); templates.put(key, templateInfo);
} }
return AjaxResult.success(templates); return AjaxResult.success(templates);
} catch (Exception e) { } catch (Exception e) {
log.error("获取提示词模板列表失败", e); log.error("获取提示词模板列表失败", e);
@@ -425,7 +425,7 @@ public class SocialMediaServiceImpl implements ISocialMediaService
if (!isValidKey(key)) { if (!isValidKey(key)) {
return AjaxResult.error("无效的模板键名"); return AjaxResult.error("无效的模板键名");
} }
String stored = getTemplateFromRedis(key); String stored = getTemplateFromRedis(key);
boolean isDefault = StringUtils.isEmpty(stored); boolean isDefault = StringUtils.isEmpty(stored);
Map<String, Object> data = new HashMap<>(); Map<String, Object> data = new HashMap<>();
@@ -433,7 +433,7 @@ public class SocialMediaServiceImpl implements ISocialMediaService
data.put("description", TEMPLATE_DESCRIPTIONS.get(key)); data.put("description", TEMPLATE_DESCRIPTIONS.get(key));
data.put("template", isDefault ? DEFAULT_PROMPT_TEMPLATES.getOrDefault(key, "") : stored); data.put("template", isDefault ? DEFAULT_PROMPT_TEMPLATES.getOrDefault(key, "") : stored);
data.put("isDefault", isDefault); data.put("isDefault", isDefault);
return AjaxResult.success(data); return AjaxResult.success(data);
} catch (Exception e) { } catch (Exception e) {
log.error("获取提示词模板失败", e); log.error("获取提示词模板失败", e);
@@ -449,19 +449,19 @@ public class SocialMediaServiceImpl implements ISocialMediaService
try { try {
String key = (String) request.get("key"); String key = (String) request.get("key");
String template = (String) request.get("template"); String template = (String) request.get("template");
if (!isValidKey(key)) { if (!isValidKey(key)) {
return AjaxResult.error("无效的模板键名"); return AjaxResult.error("无效的模板键名");
} }
if (StringUtils.isEmpty(template)) { if (StringUtils.isEmpty(template)) {
return AjaxResult.error("模板内容不能为空"); return AjaxResult.error("模板内容不能为空");
} }
if (redisTemplate == null) { if (redisTemplate == null) {
return AjaxResult.error("Redis未配置无法保存模板"); return AjaxResult.error("Redis未配置无法保存模板");
} }
String redisKey = REDIS_KEY_PREFIX + key; String redisKey = REDIS_KEY_PREFIX + key;
String templateValue = template.trim(); String templateValue = template.trim();
if (StringUtils.isEmpty(templateValue)) { if (StringUtils.isEmpty(templateValue)) {
@@ -475,10 +475,10 @@ public class SocialMediaServiceImpl implements ISocialMediaService
} }
} }
redisTemplate.opsForValue().set(redisKey, templateValue); redisTemplate.opsForValue().set(redisKey, templateValue);
log.info("保存提示词模板成功: {}", key); log.info("保存提示词模板成功: {}", key);
return AjaxResult.success("保存成功"); return AjaxResult.success("保存成功");
} catch (Exception e) { } catch (Exception e) {
log.error("保存提示词模板失败", e); log.error("保存提示词模板失败", e);
return AjaxResult.error("保存失败: " + e.getMessage()); return AjaxResult.error("保存失败: " + e.getMessage());
@@ -494,17 +494,17 @@ public class SocialMediaServiceImpl implements ISocialMediaService
if (!isValidKey(key)) { if (!isValidKey(key)) {
return AjaxResult.error("无效的模板键名"); return AjaxResult.error("无效的模板键名");
} }
if (redisTemplate == null) { if (redisTemplate == null) {
return AjaxResult.error("Redis未配置无法删除模板"); return AjaxResult.error("Redis未配置无法删除模板");
} }
String redisKey = REDIS_KEY_PREFIX + key; String redisKey = REDIS_KEY_PREFIX + key;
redisTemplate.delete(redisKey); redisTemplate.delete(redisKey);
log.info("删除提示词模板成功: {}", key); log.info("删除提示词模板成功: {}", key);
return AjaxResult.success("删除成功,已恢复默认模板"); return AjaxResult.success("删除成功,已恢复默认模板");
} catch (Exception e) { } catch (Exception e) {
log.error("删除提示词模板失败", e); log.error("删除提示词模板失败", e);
return AjaxResult.error("删除失败: " + e.getMessage()); return AjaxResult.error("删除失败: " + e.getMessage());
@@ -518,7 +518,7 @@ public class SocialMediaServiceImpl implements ISocialMediaService
if (redisTemplate == null) { if (redisTemplate == null) {
return null; return null;
} }
try { try {
String redisKey = REDIS_KEY_PREFIX + key; String redisKey = REDIS_KEY_PREFIX + key;
String template = redisTemplate.opsForValue().get(redisKey); String template = redisTemplate.opsForValue().get(redisKey);
@@ -1021,7 +1021,7 @@ public class SocialMediaServiceImpl implements ISocialMediaService
// 标题行 // 标题行
StringBuilder daixiadanBuilder = new StringBuilder(); StringBuilder daixiadanBuilder = new StringBuilder();
daixiadanBuilder.append("(一键代下) ").append(cleanTitle).append("\n"); daixiadanBuilder.append(cleanTitle).append("\n");
// 型号行(可选) // 型号行(可选)
if (StringUtils.isNotEmpty(cleanRemark)) { if (StringUtils.isNotEmpty(cleanRemark)) {
daixiadanBuilder.append("型号:").append(cleanRemark).append("\n"); daixiadanBuilder.append("型号:").append(cleanRemark).append("\n");
@@ -1030,7 +1030,7 @@ public class SocialMediaServiceImpl implements ISocialMediaService
// 教你下单版 // 教你下单版
StringBuilder jiaonixiadanBuilder = new StringBuilder(); StringBuilder jiaonixiadanBuilder = new StringBuilder();
jiaonixiadanBuilder.append("【教你下单】 ").append(cleanTitle).append("\n"); jiaonixiadanBuilder.append(cleanTitle).append("\n");
if (StringUtils.isNotEmpty(cleanRemark)) { if (StringUtils.isNotEmpty(cleanRemark)) {
jiaonixiadanBuilder.append("型号:").append(cleanRemark).append("\n"); jiaonixiadanBuilder.append("型号:").append(cleanRemark).append("\n");
} }