继续写这个接口的商品信息,够用了

This commit is contained in:
Leo
2025-04-02 00:34:37 +08:00
parent 5c72c3da3e
commit 6ef1bb0412

View File

@@ -6,14 +6,16 @@ import cn.van.business.model.jd.ProductOrder;
import cn.van.business.repository.OrderRowRepository;
import cn.van.business.repository.ProductOrderRepository;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
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;
import com.jd.open.api.sdk.domain.kplunion.CouponService.request.stop.StopGiftCouponReq;
import com.jd.open.api.sdk.domain.kplunion.GoodsService.request.query.BigFieldGoodsReq;
import com.jd.open.api.sdk.domain.kplunion.GoodsService.response.query.BigFieldGoodsResp;
import com.jd.open.api.sdk.domain.kplunion.GoodsService.request.query.GoodsReq;
import com.jd.open.api.sdk.domain.kplunion.GoodsService.response.query.BigfieldQueryResult;
import com.jd.open.api.sdk.domain.kplunion.GoodsService.response.query.GoodsQueryResult;
import com.jd.open.api.sdk.domain.kplunion.OrderService.request.query.OrderRowReq;
import com.jd.open.api.sdk.domain.kplunion.promotionbysubunioni.PromotionService.request.get.PromotionCodeReq;
import com.jd.open.api.sdk.request.kplunion.*;
@@ -28,7 +30,6 @@ import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
@@ -199,12 +200,12 @@ public class JDUtil {
contents.add(content);
content = new StringBuilder();
content.append("礼金\r");
content.append("转链\r");
contents.add(content);
//content = new StringBuilder();
//
//content.append("礼金\r");
//content.append("转链\r");
//
//contents.add(content);
break;
case "测试指令": {
//test01();
@@ -725,7 +726,6 @@ public class JDUtil {
wxUtil.sendTextMessage(fromWxid, "请输入数量1-1000", 1, fromWxid);
break;
case STEP_QUANTITY:
logger.debug("用户 {} 输入数量:{}", fromWxid, message); // 新增
if (!isValidQuantity(message)) {
@@ -762,6 +762,353 @@ public class JDUtil {
state.reset();
}
}
/**
* 生成转链和文案的方法
*
* @param message 文案内容,包含商品链接
* @return 处理后的文案,附带商品信息
*/
public String generatePromotionContent(String message,Boolean needImagesAndSkuName) {
// 提取文案中的所有 u.jd.com 链接
List<String> urls = extractUJDUrls(message);
if (urls.isEmpty()) {
return "文案中未找到有效的商品链接,请检查格式是否正确。\n" + message;
}
// 存储解析后的商品信息
StringBuilder enrichedContent = new StringBuilder();
enrichedContent.append("✨ 商品推广文案:\n\n").append(message).append("\n\n");
/**
* {
* "jd_union_open_goods_query_responce": {
* "code": "0",
* "queryResult": {
* "code": 200,
* "data": [
* {
* "brandCode": "16407",
* "brandName": "松下Panasonic",
* "categoryInfo": {
* "cid1": 737,
* "cid1Name": "家用电器",
* "cid2": 794,
* "cid2Name": "大 家 电",
* "cid3": 880,
* "cid3Name": "洗衣机"
* },
* "comments": 10000,
* "commissionInfo": {
* "commission": 599.95,
* "commissionShare": 5,
* "couponCommission": 592.95,
* "endTime": 1743609599000,
* "isLock": 1,
* "plusCommissionShare": 5,
* "startTime": 1742486400000
* },
* "couponInfo": {
* "couponList": [
* {
* "bindType": 1,
* "couponStatus": 0,
* "couponStyle": 0,
* "discount": 100,
* "getEndTime": 1746028799000,
* "getStartTime": 1743436800000,
* "isBest": 1,
* "isInputCoupon": 1,
* "link": "https://coupon.m.jd.com/coupons/show.action?linkKey=AAROH_xIpeffAs_-naABEFoeVJzBK6Q6mfry4nz4ylRYHXsp_NnipDSrReNwGZGTxXhj94SGi9SkzR_7xaWEXLpO2boqow",
* "platformType": 0,
* "quota": 5000,
* "useEndTime": 1746028799000,
* "useStartTime": 1743436800000
* },
* {
* "bindType": 1,
* "couponStatus": 0,
* "couponStyle": 0,
* "discount": 50,
* "getEndTime": 1746028799000,
* "getStartTime": 1743436800000,
* "hotValue": 0,
* "isBest": 0,
* "isInputCoupon": 0,
* "link": "https://coupon.m.jd.com/coupons/show.action?linkKey=AAROH_xIpeffAs_-naABEFoervD9YaNNXlsj6OBbZoWSFI1sZXw31PSjK04AH6tFP_Iu0YJlePWBT6sZfNvp14W0QK2K6A",
* "platformType": 0,
* "quota": 5000,
* "useEndTime": 1746028799000,
* "useStartTime": 1743436800000
* },
* {
* "bindType": 1,
* "couponStatus": 0,
* "couponStyle": 0,
* "discount": 40,
* "getEndTime": 1746028799000,
* "getStartTime": 1743436800000,
* "hotValue": 0,
* "isBest": 0,
* "isInputCoupon": 0,
* "link": "https://coupon.m.jd.com/coupons/show.action?linkKey=AAROH_xIpeffAs_-naABEFoeqQdmhZbv1TiAn0QqI5gnT4g_zAgV5887llN8j6TatV-zpDfReipMr-hKkwQasE9NNUV2uQ",
* "platformType": 0,
* "quota": 500,
* "useEndTime": 1746028799000,
* "useStartTime": 1743436800000
* },
* {
* "bindType": 1,
* "couponStatus": 0,
* "couponStyle": 0,
* "discount": 20,
* "getEndTime": 1746028799000,
* "getStartTime": 1743436800000,
* "hotValue": 0,
* "isBest": 0,
* "isInputCoupon": 0,
* "link": "https://coupon.m.jd.com/coupons/show.action?linkKey=AAROH_xIpeffAs_-naABEFoepukjRCuLpbwceODRbBw5HpNLRTVe5Olp99d34Izdo0s9lDtTIdyYZ-uJRCEXnc5N3OZ5LA",
* "platformType": 0,
* "quota": 2000,
* "useEndTime": 1746028799000,
* "useStartTime": 1743436800000
* },
* {
* "bindType": 1,
* "couponStatus": 0,
* "couponStyle": 0,
* "discount": 10,
* "getEndTime": 1746028799000,
* "getStartTime": 1743436800000,
* "hotValue": 0,
* "isBest": 0,
* "isInputCoupon": 0,
* "link": "https://coupon.m.jd.com/coupons/show.action?linkKey=AAROH_xIpeffAs_-naABEFoeKIIbuvk-VsfJj_m1o3PRHqRaEBIbXHwooEMMt3z44T3cNQTkQsS8TjGnEeIz1obKp_t_mQ",
* "platformType": 0,
* "quota": 1000,
* "useEndTime": 1746028799000,
* "useStartTime": 1743436800000
* }
* ]
* },
* "deliveryType": 1,
* "eliteType": [],
* "forbidTypes": [
* 0
* ],
* "goodCommentsShare": 99,
* "imageInfo": {
* "imageList": [
* {
* "url": "https://img14.360buyimg.com/pop/jfs/t1/273873/17/14072/113544/67ebd215Feef3f56b/fc5156bf59a25ba3.jpg"
* },
* {
* "url": "https://img14.360buyimg.com/pop/jfs/t1/273802/16/14273/84419/67ebd224F72dbcc8d/8ffe6f99aeeeb8fd.jpg"
* },
* {
* "url": "https://img14.360buyimg.com/pop/jfs/t1/278931/40/12819/81341/67ea0227F41ffc604/ab1c6727e5d4f224.jpg"
* },
* {
* "url": "https://img14.360buyimg.com/pop/jfs/t1/276989/12/13440/79310/67ea0226F68c2ed40/8acdeda05aa3596b.jpg"
* },
* {
* "url": "https://img14.360buyimg.com/pop/jfs/t1/284503/25/12384/59498/67ea0225F8be60c83/b29ea34abc64346e.jpg"
* },
* {
* "url": "https://img14.360buyimg.com/pop/jfs/t1/284667/29/12481/56118/67ea0225F97d9c729/f4c81d77064957bd.jpg"
* },
* {
* "url": "https://img14.360buyimg.com/pop/jfs/t1/270959/35/13852/50552/67ea0224F911b8f00/248f9a7751549db7.jpg"
* },
* {
* "url": "https://img14.360buyimg.com/pop/jfs/t1/274981/3/13326/48019/67ea0224Fe64bca69/b062218fc8db9b29.jpg"
* },
* {
* "url": "https://img14.360buyimg.com/pop/jfs/t1/283691/1/12478/82267/67ea0223Fd4ecb0f5/2c32de327d8aa440.jpg"
* },
* {
* "url": "https://img14.360buyimg.com/pop/jfs/t1/281457/31/12476/94335/67ea0222Fde76593a/cdddcd59b0b20c9a.jpg"
* }
* ]
* },
* "inOrderComm30Days": 633170.8,
* "inOrderCount30Days": 3000,
* "inOrderCount30DaysSku": 100,
* "isHot": 1,
* "isJdSale": 1,
* "isOversea": 0,
* "itemId": "BsrqLq5CfIziE7BSl3ItPp8q_3DFaNVYJqfkRRLc7HR",
* "jxFlags": [],
* "materialUrl": "jingfen.jd.com/detail/BsrqLq5CfIziE7BSl3ItPp8q_3DFaNVYJqfkRRLc7HR.html",
* "oriItemId": "BMrqLq5CfIz9X04KC3ItPp8q_3DFaNVYJqfkRRLc7HR",
* "owner": "g",
* "pinGouInfo": {},
* "pingGouInfo": {},
* "priceInfo": {
* "lowestCouponPrice": 11899,
* "lowestPrice": 11999,
* "lowestPriceType": 1,
* "price": 11999
* },
* "purchasePriceInfo": {
* "basisPriceType": 1,
* "code": 200,
* "couponList": [
* {
* "bindType": 1,
* "couponStatus": 0,
* "couponStyle": 0,
* "discount": 100,
* "isBest": 0,
* "link": "https://coupon.m.jd.com/coupons/show.action?linkKey=AAROH_xIpeffAs_-naABEFoeVJzBK6Q6mfry4nz4ylRYHXsp_NnipDSrReNwGZGTxXhj94SGi9SkzR_7xaWEXLpO2boqow",
* "platformType": 0,
* "quota": 5000
* },
* {
* "bindType": 1,
* "couponStatus": 0,
* "couponStyle": 0,
* "discount": 40,
* "isBest": 0,
* "link": "https://coupon.m.jd.com/coupons/show.action?linkKey=AAROH_xIpeffAs_-naABEFoeqQdmhZbv1TiAn0QqI5gnT4g_zAgV5887llN8j6TatV-zpDfReipMr-hKkwQasE9NNUV2uQ",
* "platformType": 0,
* "quota": 500
* }
* ],
* "message": "success",
* "purchaseNum": 1,
* "purchasePrice": 11859,
* "thresholdPrice": 11999
* },
* "shopInfo": {
* "shopId": 1000001741,
* "shopLabel": "0",
* "shopLevel": 4.9,
* "shopName": "松下洗衣机京东自营旗舰店"
* },
* "skuName": "松下Panasonic白月光4.0Ultra 洗烘套装 10kg滚筒洗衣机+变频热泵烘干机 除毛升级2.0 水氧SPA护理 8532N+8532NR",
* "skuTagList": [
* {
* "index": 1,
* "name": "自营",
* "type": 11
* },
* {
* "index": 1,
* "name": "plus",
* "type": 6
* },
* {
* "index": 3,
* "name": "7天无理由退货",
* "type": 12
* }
* ],
* "spuid": 100137629936,
* "videoInfo": {}
* }
* ],
* "message": "success",
* "requestId": "o_0b721560_m8yp5pqj_88394507",
* "totalCount": 1
* }
* }
* }
*
* */
//"materialUrl": "jingfen.jd.com/detail/BsrqLq5CfIziE7BSl3ItPp8q_3DFaNVYJqfkRRLc7HR.html",
//"oriItemId": "BMrqLq5CfIz9X04KC3ItPp8q_3DFaNVYJqfkRRLc7HR",
//"owner": "g",
//"shopInfo": {
// "shopId": 1000001741,
// "shopLabel": "0",
// "shopLevel": 4.9,
// "shopName": "松下洗衣机京东自营旗舰店"
//},
//"skuName": "松下Panasonic白月光4.0Ultra 洗烘套装 10kg滚筒洗衣机+变频热泵烘干机 除毛升级2.0 水氧SPA护理 8532N+8532NR",
//"spuid": 100137629936,
ArrayList<HashMap<String, String>> resultList = new ArrayList<>();
for (String url : urls) {
try {
// 查询商品信息
GoodsQueryResult productInfo = queryProductInfoByUJDUrl(url);
if (productInfo == null || productInfo.getCode() != 200) {
enrichedContent.append("⚠️ 商品链接查询失败:").append(url).append("\n");
continue;
}
long totalCount = productInfo.getTotalCount();
HashMap<String, String> itemMap = new HashMap<>();
itemMap.put("url", url);
if (totalCount == 0) {
itemMap.put("type", "coupon");
}
else {
itemMap.put("type", "goods");
itemMap.put("data", JSONObject.toJSONString(productInfo.getData()));
}
resultList.add(itemMap);
} catch (Exception e) {
logger.error("处理商品链接时发生异常:{}", url, e);
enrichedContent.append("❌ 处理商品链接时发生异常:").append(url).append("\n");
}
}
return enrichedContent.toString();
}
/**
* 提取文案中的所有 u.jd.com 链接
*
* @param message 文案内容
* @return 包含所有 u.jd.com 链接的列表
*/
private List<String> extractUJDUrls(String message) {
List<String> urls = new ArrayList<>();
Pattern pattern = Pattern.compile("https://u\\.jd\\.com/[^\\s]+");
Matcher matcher = pattern.matcher(message);
while (matcher.find()) {
urls.add(matcher.group());
}
return urls;
}
/**
* 通过这个可以将u.jd.*** 查询到对应的商品信息 和 商品图片,甚至可以查询到是不是自营的商品
*
* */
public UnionOpenGoodsQueryResponse getUnionOpenGoodsQueryRequest(String uJDUrl) throws Exception {
JdClient client = new DefaultJdClient(SERVER_URL, ACCESS_TOKEN, LPF_APP_KEY_WZ, LPF_APP_KEY_WZ);
UnionOpenGoodsQueryRequest request = new UnionOpenGoodsQueryRequest();
GoodsReq goodsReq = new GoodsReq();
//goodsReq.setSkuIds(List.of(Long.parseLong(skuId)).toArray(new Long[0])); // 传入skuId集合
/**
* 查询域集合不填写则查询全部目目前支持categoryInfo类目信息,imageInfo图片信息,
* baseBigFieldInfo基础大字段信息,bookBigFieldInfo图书大字段信息,
* videoBigFieldInfo影音大字段信息,detailImages商详图
* */
//goodsReq.setFields(new String[]{"categoryInfo", "imageInfo", "baseBigFieldInfo"}); // 设置需要查询的字段
goodsReq.setKeyword(uJDUrl);
request.setGoodsReqDTO(goodsReq);
request.setVersion("1.0");
return client.execute(request);
}
public GoodsQueryResult queryProductInfoByUJDUrl(String skuId) throws Exception {
UnionOpenGoodsQueryResponse response = getUnionOpenGoodsQueryRequest(skuId);
if (response == null || response.getQueryResult() == null) {
return null;
}
GoodsQueryResult queryResult = response.getQueryResult();
if (queryResult.getCode() != 200) {
return null;
}
return queryResult;
}
// 在JDUtil类中新增方法实现商品详情查询接口
public UnionOpenGoodsBigfieldQueryResponse getUnionOpenGoodsBigfieldQueryResponse(String skuId) throws Exception {