206 lines
6.9 KiB
Java
206 lines
6.9 KiB
Java
package cn.van.business.controller;
|
|
|
|
import cn.van.business.service.SocialMediaService;
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* 小红书/抖音内容生成控制器
|
|
*
|
|
* @author System
|
|
*/
|
|
@Slf4j
|
|
@RestController
|
|
@RequestMapping("/jarvis/social-media")
|
|
public class SocialMediaController {
|
|
|
|
@Autowired
|
|
private SocialMediaService socialMediaService;
|
|
|
|
/**
|
|
* 提取关键词
|
|
*
|
|
* POST /jarvis/social-media/extract-keywords
|
|
*
|
|
* {
|
|
* "productName": "商品名称"
|
|
* }
|
|
*/
|
|
@PostMapping("/extract-keywords")
|
|
public JSONObject extractKeywords(@RequestBody Map<String, Object> request) {
|
|
JSONObject response = new JSONObject();
|
|
try {
|
|
String productName = (String) request.get("productName");
|
|
|
|
if (productName == null || productName.trim().isEmpty()) {
|
|
response.put("code", 400);
|
|
response.put("msg", "商品名称不能为空");
|
|
return response;
|
|
}
|
|
|
|
Map<String, Object> result = socialMediaService.extractKeywords(productName);
|
|
|
|
response.put("code", 200);
|
|
response.put("msg", "操作成功");
|
|
response.put("data", result);
|
|
|
|
} catch (Exception e) {
|
|
log.error("提取关键词失败", e);
|
|
response.put("code", 500);
|
|
response.put("msg", "提取关键词失败: " + e.getMessage());
|
|
}
|
|
return response;
|
|
}
|
|
|
|
/**
|
|
* 生成文案
|
|
*
|
|
* POST /jarvis/social-media/generate-content
|
|
*
|
|
* {
|
|
* "productName": "商品名称",
|
|
* "originalPrice": 499.0,
|
|
* "finalPrice": 199.0,
|
|
* "keywords": "关键词1、关键词2",
|
|
* "style": "xhs" // xhs/douyin/both
|
|
* }
|
|
*/
|
|
@PostMapping("/generate-content")
|
|
public JSONObject generateContent(@RequestBody Map<String, Object> request) {
|
|
JSONObject response = new JSONObject();
|
|
try {
|
|
String productName = (String) request.get("productName");
|
|
Object originalPriceObj = request.get("originalPrice");
|
|
Object finalPriceObj = request.get("finalPrice");
|
|
String keywords = (String) request.get("keywords");
|
|
String style = (String) request.getOrDefault("style", "both");
|
|
|
|
if (productName == null || productName.trim().isEmpty()) {
|
|
response.put("code", 400);
|
|
response.put("msg", "商品名称不能为空");
|
|
return response;
|
|
}
|
|
|
|
Double originalPrice = parseDouble(originalPriceObj);
|
|
Double finalPrice = parseDouble(finalPriceObj);
|
|
|
|
Map<String, Object> result = socialMediaService.generateContent(
|
|
productName, originalPrice, finalPrice, keywords, style
|
|
);
|
|
|
|
response.put("code", 200);
|
|
response.put("msg", "操作成功");
|
|
response.put("data", result);
|
|
|
|
} catch (Exception e) {
|
|
log.error("生成文案失败", e);
|
|
response.put("code", 500);
|
|
response.put("msg", "生成文案失败: " + e.getMessage());
|
|
}
|
|
return response;
|
|
}
|
|
|
|
/**
|
|
* 一键生成完整内容(关键词 + 文案 + 图片)
|
|
*
|
|
* POST /jarvis/social-media/generate-complete
|
|
*
|
|
* {
|
|
* "productImageUrl": "商品主图URL",
|
|
* "productName": "商品名称",
|
|
* "originalPrice": 499.0,
|
|
* "finalPrice": 199.0,
|
|
* "style": "xhs"
|
|
* }
|
|
*/
|
|
@PostMapping("/generate-complete")
|
|
public JSONObject generateComplete(@RequestBody Map<String, Object> request) {
|
|
JSONObject response = new JSONObject();
|
|
try {
|
|
String productImageUrl = (String) request.get("productImageUrl");
|
|
String productName = (String) request.get("productName");
|
|
Object originalPriceObj = request.get("originalPrice");
|
|
Object finalPriceObj = request.get("finalPrice");
|
|
String style = (String) request.getOrDefault("style", "both");
|
|
|
|
if (productName == null || productName.trim().isEmpty()) {
|
|
response.put("code", 400);
|
|
response.put("msg", "商品名称不能为空");
|
|
return response;
|
|
}
|
|
|
|
Double originalPrice = parseDouble(originalPriceObj);
|
|
Double finalPrice = parseDouble(finalPriceObj);
|
|
|
|
Map<String, Object> result = socialMediaService.generateCompleteContent(
|
|
productImageUrl, productName, originalPrice, finalPrice, style
|
|
);
|
|
|
|
response.put("code", 200);
|
|
response.put("msg", "操作成功");
|
|
response.put("data", result);
|
|
|
|
} catch (Exception e) {
|
|
log.error("生成完整内容失败", e);
|
|
response.put("code", 500);
|
|
response.put("msg", "生成完整内容失败: " + e.getMessage());
|
|
}
|
|
return response;
|
|
}
|
|
|
|
/**
|
|
* 调试:测试大模型连通性。
|
|
* POST /jarvis/social-media/llm/test
|
|
* body: { "profileId": "可选,不传则用当前激活或 yml 默认", "message": "可选,默认问 1+1" }
|
|
*/
|
|
@PostMapping("/llm/test")
|
|
public JSONObject testLlm(@RequestBody(required = false) Map<String, Object> request) {
|
|
JSONObject response = new JSONObject();
|
|
try {
|
|
Map<String, Object> req = request != null ? request : new HashMap<>();
|
|
Object pid = req.get("profileId");
|
|
String profileId = pid == null ? null : pid.toString().trim();
|
|
if (profileId != null && profileId.isEmpty()) {
|
|
profileId = null;
|
|
}
|
|
Object msg = req.get("message");
|
|
String message = msg == null ? null : msg.toString();
|
|
Map<String, Object> data = socialMediaService.testLlmChat(profileId, message);
|
|
response.put("code", 200);
|
|
response.put("msg", "操作成功");
|
|
response.put("data", data);
|
|
} catch (Exception e) {
|
|
log.error("LLM 测试接口异常", e);
|
|
response.put("code", 500);
|
|
response.put("msg", "测试失败: " + e.getMessage());
|
|
}
|
|
return response;
|
|
}
|
|
|
|
/**
|
|
* 解析Double值
|
|
*/
|
|
private Double parseDouble(Object value) {
|
|
if (value == null) {
|
|
return null;
|
|
}
|
|
if (value instanceof Double) {
|
|
return (Double) value;
|
|
}
|
|
if (value instanceof Number) {
|
|
return ((Number) value).doubleValue();
|
|
}
|
|
try {
|
|
return Double.parseDouble(value.toString());
|
|
} catch (Exception e) {
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|