This commit is contained in:
雷欧(林平凡)
2025-08-15 17:39:31 +08:00
parent 26c2b51d72
commit 488eb5f64c
2 changed files with 143 additions and 23 deletions

View File

@@ -1,8 +1,7 @@
package cn.van.business.controller.jd;
import cn.van.business.controller.wx.WXListener;
import cn.van.business.util.JDProductService;
import cn.van.business.util.JDUtil;
import com.alibaba.fastjson2.JSONArray;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -12,8 +11,6 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
@@ -42,13 +39,13 @@ public class JDInnerController {
}
@RequestMapping("/generatePromotionContent")
public HashMap<String, List<String>> generatePromotionContent(@RequestBody Map<String, String> requestBody) {
public JSONArray generatePromotionContent(@RequestBody Map<String, String> requestBody) {
String promotionContent = requestBody.get("promotionContent");
logger.info("generatePromotionContent message:{}", promotionContent);
if (!checkSkey(skey)) {
return null;
}
return jdProductService.generatePromotionContent(promotionContent);
return jdProductService.generatePromotionContentAsJsonArray(promotionContent);
}
}

View File

@@ -5,7 +5,10 @@ import cn.van.business.model.cj.XbMessageItem;
import cn.van.business.repository.XbMessageItemRepository;
import cn.van.business.repository.XbMessageRepository;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.jd.open.api.sdk.DefaultJdClient;
import com.jd.open.api.sdk.JdClient;
import com.jd.open.api.sdk.domain.kplunion.CouponService.request.get.CreateGiftCouponReq;
@@ -127,23 +130,9 @@ public class JDProductService {
itemMap.put("spuid", String.valueOf(productInfo.getData()[0].getSpuid()));
itemMap.put("commission", String.valueOf(productInfo.getData()[0].getCommissionInfo().getCommission()));
itemMap.put("commissionShare", String.valueOf(productInfo.getData()[0].getCommissionInfo().getCommissionShare()));
//for (HashMap.Entry<String, String> entry : itemMap.entrySet()) {
//couponInfo.append(" ").append(entry.getKey()).append(": ").append(entry.getValue()).append("\n");
//}
couponInfo.append("店铺: ").append(itemMap.get("shopName")).append("\n").append("标题: ").append(replaceAll).append("\n").append("自营 POP ").append(itemMap.get("owner").equals("g") ? " 自营 " : " POP ").append("\n").append("佣金比例: ").append(itemMap.get("commissionShare")).append("\n").append("佣金: ").append(itemMap.get("commission")).append("\n");
//StringBuilder images = new StringBuilder();
if (productInfo.getData()[0].getImageInfo() != null) {
//images.append(" ").append("图片信息:\n");
//int index = 1;
for (UrlInfo image : productInfo.getData()[0].getImageInfo().getImageList()) {
//images.append("图片 ").append(index++).append("\n").append(image.getUrl()).append("\n");
imagesList.add(image.getUrl());
}
}
//textList.add(String.valueOf(images));
resultList.add(itemMap);
String title = "";
@@ -193,7 +182,7 @@ public class JDProductService {
finallyMessage.put("urlList", urlList);
} catch (Exception e) {
logger.error("处理商品链接时发生异常:{}", url, e);
log.error("处理商品链接时发生异常:{}", url, e);
couponInfo.append(" 处理商品链接时发生异常:").append(url).append("\n");
textList.add(String.valueOf(couponInfo));
finallyMessage.put("text", textList);
@@ -217,6 +206,140 @@ public class JDProductService {
}
/**
* 生成转链和方案的方法JSON数组格式
*
* @param message 方案内容,包含商品链接
* @return 处理后的方案以标准JSON数组格式返回每个商品及其文案为一个独立对象
*/
public synchronized JSONArray generatePromotionContentAsJsonArray(String message) {
JSONArray resultArray = new JSONArray();
// 提取方案中的所有 u.jd.com 链接
List<String> urls = extractUJDUrls(message);
if (urls.isEmpty()) {
JSONObject errorObj = new JSONObject();
errorObj.put("error", "方案中未找到有效的商品链接,请检查格式是否正确。");
resultArray.add(errorObj);
return resultArray;
}
String format = null;
DateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
for (String url : urls) {
try {
// 新建格式好日期
format = dateFormat.format(new Date());
// 查询商品信息
GoodsQueryResult productInfo = queryProductInfoByUJDUrl(url);
if (productInfo == null || productInfo.getCode() != 200) {
JSONObject errorObj = new JSONObject();
errorObj.put("url", url);
errorObj.put("error", "链接查询失败");
resultArray.add(errorObj);
continue;
}
long totalCount = productInfo.getTotalCount();
if (totalCount == 0) {
JSONObject errorObj = new JSONObject();
errorObj.put("url", url);
errorObj.put("error", "未找到商品信息");
resultArray.add(errorObj);
continue;
}
// 创建商品对象
JSONObject productObj = new JSONObject();
productObj.put("url", url);
// 商品基本信息
productObj.put("materialUrl", productInfo.getData()[0].getMaterialUrl());
productObj.put("oriItemId", productInfo.getData()[0].getOriItemId());
productObj.put("owner", productInfo.getData()[0].getOwner());
productObj.put("shopId", String.valueOf(productInfo.getData()[0].getShopInfo().getShopId()));
productObj.put("shopName", productInfo.getData()[0].getShopInfo().getShopName());
productObj.put("skuName", productInfo.getData()[0].getSkuName());
String cleanSkuName = productInfo.getData()[0].getSkuName().replaceAll("以旧|政府|换新|领取|国家|补贴|15%|20%|国补|立减|【|】", "");
productObj.put("cleanSkuName", cleanSkuName);
productObj.put("spuid", String.valueOf(productInfo.getData()[0].getSpuid()));
productObj.put("commission", String.valueOf(productInfo.getData()[0].getCommissionInfo().getCommission()));
productObj.put("commissionShare", String.valueOf(productInfo.getData()[0].getCommissionInfo().getCommissionShare()));
// 图片信息
JSONArray imageArray = new JSONArray();
if (productInfo.getData()[0].getImageInfo() != null) {
for (UrlInfo image : productInfo.getData()[0].getImageInfo().getImageList()) {
imageArray.add(image.getUrl());
}
}
productObj.put("images", imageArray);
// 文案信息
JSONArray wenanArray = new JSONArray();
String title = "";
try {
if (!message.equals(url)) {
String[] lines = message.split("\\r?\\n");
if (lines.length > 0) {
title = lines[0];
// 有的换行了
if (lines[1].length() > 3 && !lines[1].contains("u.jd")) {
title = title + lines[1];
}
title = title.replaceAll("@|所有人", "");
}
}
} catch (Exception e) {
log.error("文案首行异常", e);
}
// 生成各种文案
JSONObject wenan1 = new JSONObject();
wenan1.put("type", "标价到手-方案1");
wenan1.put("content", "(标价到手) " + title + cleanSkuName + "\n" + WENAN_FANAN_LQD.replaceAll("更新", format + "更新"));
wenanArray.add(wenan1);
JSONObject wenan2 = new JSONObject();
wenan2.put("type", "一键代下");
wenan2.put("content", "(一键代下) " + title + cleanSkuName + "\n" + WENAN_ZCXS);
wenanArray.add(wenan2);
JSONObject wenan3 = new JSONObject();
wenan3.put("type", "标价到手-方案2");
wenan3.put("content", "(标价到手) " + title + cleanSkuName + "\n" + WENAN_FANAN_HG.replaceAll("更新", format + "更新"));
wenanArray.add(wenan3);
JSONObject wenan4 = new JSONObject();
wenan4.put("type", "教你下单");
wenan4.put("content", "【教你下单】 " + title + cleanSkuName + "\n" + WENAN_FANAN_BX.replaceAll("信息更新日期:", "信息更新日期:" + format));
wenanArray.add(wenan4);
productObj.put("wenan", wenanArray);
// 添加通用文案
JSONObject commonWenan = new JSONObject();
commonWenan.put("type", "通用文案");
commonWenan.put("content", format + FANAN_COMMON + message);
wenanArray.add(commonWenan);
resultArray.add(productObj);
} catch (Exception e) {
log.error("处理商品链接时发生异常:{}", url, e);
JSONObject errorObj = new JSONObject();
errorObj.put("url", url);
errorObj.put("error", "处理商品链接时发生异常:" + e.getMessage());
resultArray.add(errorObj);
}
}
return resultArray;
}
/**
* 查询商品信息
*
@@ -394,7 +517,7 @@ public class JDProductService {
public void saveGiftCouponToRedis(String skuId, String giftKey, String skuName, String owner) {
String key = "gift_coupon:" + skuId;
String hashKey = giftKey;
LocalDateTime expireTime = LocalDateTime.now().plus("g".equals(owner) ? 1 : 7, TimeUnit.DAYS.toChronoUnit());
LocalDateTime expireTime = LocalDateTime.now().plus(owner.equals("g") ? 0 : 7, java.time.temporal.ChronoUnit.DAYS);
Map<String, Object> data = new HashMap<>();
data.put("giftKey", giftKey);