多链接礼金第一版

This commit is contained in:
Leo
2025-04-14 20:24:33 +08:00
parent c8efd8512e
commit f8c817fa8c
2 changed files with 80 additions and 52 deletions

View File

@@ -4,6 +4,7 @@ package cn.van.business.util;
import cn.van.business.model.jd.OrderRow;
import cn.van.business.repository.OrderRowRepository;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jd.open.api.sdk.DefaultJdClient;
@@ -13,8 +14,12 @@ import com.jd.open.api.sdk.domain.kplunion.GoodsService.request.query.GoodsReq;
import com.jd.open.api.sdk.domain.kplunion.GoodsService.response.query.GoodsQueryResult;
import com.jd.open.api.sdk.domain.kplunion.GoodsService.response.query.UrlInfo;
import com.jd.open.api.sdk.domain.kplunion.promotionbysubunioni.PromotionService.request.get.PromotionCodeReq;
import com.jd.open.api.sdk.request.kplunion.*;
import com.jd.open.api.sdk.response.kplunion.*;
import com.jd.open.api.sdk.request.kplunion.UnionOpenCouponGiftGetRequest;
import com.jd.open.api.sdk.request.kplunion.UnionOpenGoodsQueryRequest;
import com.jd.open.api.sdk.request.kplunion.UnionOpenPromotionBysubunionidGetRequest;
import com.jd.open.api.sdk.response.kplunion.UnionOpenCouponGiftGetResponse;
import com.jd.open.api.sdk.response.kplunion.UnionOpenGoodsQueryResponse;
import com.jd.open.api.sdk.response.kplunion.UnionOpenPromotionBysubunionidGetResponse;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@@ -42,7 +47,8 @@ import java.util.stream.Stream;
import static cn.van.business.util.JDUtil.UserInteractionState.GiftMoneyStep.*;
import static cn.van.business.util.JDUtil.UserInteractionState.ProcessState.INIT;
import static cn.van.business.util.WXUtil.*;
import static cn.van.business.util.WXUtil.chatRoom_BY;
import static cn.van.business.util.WXUtil.getSuperAdmins;
/**
* @author Leo
@@ -90,7 +96,7 @@ public class JDUtil {
* <p>
* 订单号:
*/
private static final String WENAN_D = "单:\n" + "{单号} \n" + "分销标记(标记用,勿改):\n" + "型号:\n" +"\n" +"链接:\n" +"\n" + "下单付款:\n" + "\n" + "后返金额:\n" + "\n" + "地址:\n" + "\n" + "物流链接:\n" + "\n" + "订单号:\n" + "\n" + "下单人:\n" + "\n" ;
private static final String WENAN_D = "单:\n" + "{单号} \n" + "分销标记(标记用,勿改):\n" + "型号:\n" + "\n" + "链接:\n" + "\n" + "下单付款:\n" + "\n" + "后返金额:\n" + "\n" + "地址:\n" + "\n" + "物流链接:\n" + "\n" + "订单号:\n" + "\n" + "下单人:\n" + "\n";
final WXUtil wxUtil;
private final StringRedisTemplate redisTemplate;
@@ -733,7 +739,7 @@ public class JDUtil {
default:
state.setCurrentStep(STEP_AMOUNT);
wxUtil.sendTextMessage(wxid, "请输入金额1-50元", 1, wxid, false);
wxUtil.sendTextMessage(wxid, "[Packet] 请输入金额1-50元", 1, wxid, false);
}
}
@@ -753,45 +759,70 @@ public class JDUtil {
wxUtil.sendTextMessage(wxid, "数量格式错误请输入1-100的整数", 1, wxid, false);
return;
}
String cachedData = cacheMap.get("productData" + wxid);
// 从缓存获取商品数据
String productDataJson = cacheMap.get("productData" + wxid);
String finalWenAn = cacheMap.get("finalWenAn" + wxid);
if (cachedData == null) {
if (productDataJson == null || finalWenAn == null) {
logger.error("数据丢失 - productData: {}, finalWenAn: {}",
productDataJson != null, finalWenAn != null);
wxUtil.sendTextMessage(wxid, "数据丢失,请重新开始流程", 1, wxid, false);
resetState(wxid, state);
return;
}
try {
JSONObject jsonObject = JSON.parseObject(cachedData);
String skuId = jsonObject.getString("materialUrl");
String owner = jsonObject.getString("owner");
String skuName = jsonObject.getString("skuName");
// 解析商品数据
/**
* // 取出来的每一个String都是JSONObject
* List<String> data = contentResult.get("data");
* JSONArray jsonObjectArr = new JSONArray(data);
*
*
* cacheMap.put("productData" + wxid, jsonObjectArr.toJSONString());*/
List<String> productList = JSON.parseArray(productDataJson, String.class);
double amount = Double.parseDouble(state.getCollectedFields().get("amount"));
int quantity = Integer.parseInt(message);
String giftKey = createGiftCoupon(skuId, amount, quantity, owner, skuName);
if (giftKey == null) {
wxUtil.sendTextMessage(wxid, "礼金创建失败,请重试", 1, wxid, false);
return;
StringBuilder result = new StringBuilder();
String updatedContent = finalWenAn;
for (String productJSONString : productList) {
// 序列化好了的
JSONObject product = JSON.parseObject(productJSONString);
String skuId = product.getString("materialUrl");
String owner = product.getString("owner");
String skuName = product.getString("skuName");
String originalUrl = product.getString("url");
String giftKey = createGiftCoupon(skuId, amount, quantity, owner, skuName);
if (giftKey == null) {
result.append(" ").append(skuName).append(" 礼金创建失败\n");
continue;
}
String transferUrl = transfer(skuId, giftKey);
if (transferUrl != null) {
updatedContent = updatedContent.replace(originalUrl, transferUrl);
result.append(" ").append(skuName).append(" 礼金创建成功\n");
} else {
result.append(" ").append(skuName).append(" 转链失败\n");
}
}
// 记录成功日志
logger.info("礼金创建成功 - 用户: {}, SKU: {}, 金额: {}, 数量: {}", wxid, skuId, amount, quantity);
// 生成转链
String transferUrl = transfer(skuId, giftKey);
//wxUtil.sendTextMessage(wxid, "附带礼金的链接:\n" + transferUrl, 1, wxid, false);
//wxUtil.sendTextMessage(wxid, "附带礼金的方案:\n", 1, wxid, false);
wxUtil.sendTextMessage(wxid, finalWenAn.replaceAll(jsonObject.getString("url"), transferUrl), 1, wxid, true);
wxUtil.sendTextMessage(wxid, result.toString(), 1, wxid, false);
wxUtil.sendTextMessage(wxid, updatedContent, 1, wxid, true);
} catch (Exception e) {
logger.error("礼金处理异常 - 用户: {}", wxid, e);
wxUtil.sendTextMessage(wxid, "处理异常,请重试", 1, wxid, false);
logger.error("礼金处理异常", e);
wxUtil.sendTextMessage(wxid, "处理异常" + e.getMessage(), 1, wxid, false);
} finally {
resetState(wxid, state);
cacheMap.remove("text" + wxid);
cacheMap.remove("productData" + wxid);
cacheMap.remove("finalWenAn" + wxid);
}
}
@@ -828,13 +859,19 @@ public class JDUtil {
// 5. 缓存商品数据
if (!contentResult.get("data").isEmpty()) {
String productData = contentResult.get("data").get(0);
cacheMap.put("productData" + wxid, productData);
// 取出来的每一个String都是JSONObject
List<String> data = contentResult.get("data");
JSONArray jsonObjectArr = new JSONArray(data);
cacheMap.put("productData" + wxid, jsonObjectArr.toJSONString());
cacheMap.put("finalWenAn" + wxid, contentResult.get("finalWenAn").get(0));
// 6. 进入确认礼金步骤
state.setCurrentField("confirm");
wxUtil.sendTextMessage(wxid, "是否需要开通礼金?\n回复 1 - 是\n回复 2 - 否", 1, wxid, false);
wxUtil.sendTextMessage(wxid,
"检测到" + jsonObjectArr.size() + "个商品\n" +
"是否需要开通礼金?\n回复 1 - 是\n回复 2 - 否",
1, wxid, false);
} else {
wxUtil.sendTextMessage(wxid, "未获取到商品数据,请检查链接格式", 1, wxid, false);
state.reset();
@@ -861,8 +898,7 @@ public class JDUtil {
state.setCurrentStep(UserInteractionState.GiftMoneyStep.STEP_AMOUNT);
state.getCollectedFields().clear();
wxUtil.sendTextMessage(wxid, "请输入开通金额1-50元支持小数点后两位\n" + "示例20.50", 1, wxid, false);
wxUtil.sendTextMessage(wxid, "当前选择" + state.getSelectedSkuIds().size() + "个商品\n" + "请输入开通金额1-50元支持小数点后两位\n" + "示例20.50", 1, wxid, false);
} else if ("2".equals(message)) {
// 用户选择不开通礼金
String cachedData = cacheMap.get("productData" + wxid);
@@ -1178,22 +1214,9 @@ public class JDUtil {
couponInfo.append("链接类型:优惠券\n");
} else {
couponInfo.append("链接类型:商品\n");
//"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,
JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(productInfo.getData()[0]));
jsonObject.put("url", url);
dataList.add(JSONObject.toJSONString(jsonObject));
finallMessage.put("data", dataList);
dataList.add(jsonObject.toString());
HashMap<String, String> itemMap = new HashMap<>();
itemMap.put("url", url);
@@ -1238,6 +1261,7 @@ public class JDUtil {
textList.add(String.valueOf(sb2));
}
textList.add(String.valueOf(couponInfo));
finallMessage.put("data", dataList);
} catch (Exception e) {
@@ -1459,7 +1483,7 @@ public class JDUtil {
String orderID = today + String.format("%03d", count); // 格式化为 3 位数字,不足补零
// 替换模板中的占位符
temp = temp.replace("{单号}", orderID );
temp = temp.replace("{单号}", orderID);
// 发送订单信息到微信
wxUtil.sendTextMessage(fromWxid, temp, 1, fromWxid, true);
@@ -1475,6 +1499,8 @@ public class JDUtil {
@Getter
@Setter
static class UserInteractionState {
private List<String> selectedSkuIds; // 新增字段存储多个商品SKU ID
private List<String> selectedSkuNames; // 新增字段,存储多个商品名称
private GiftMoneyStep currentStep; // 新增当前步骤字段
private String lastInteractionTime;
private ProcessState currentState;
@@ -1487,6 +1513,8 @@ public class JDUtil {
this.currentState = INIT;
this.collectedFields = new HashMap<>();
this.currentField = null;
this.selectedSkuIds = new ArrayList<>();
this.selectedSkuNames = new ArrayList<>();
}

View File

@@ -151,10 +151,10 @@ public class WXUtil {
jdidToWxidMap.put(admin6.getUnionId(), admin6.getWxid());
jdidToRemarkMap.put(admin6.getUnionId(), admin6.getName());
//SuperAdmin admin7 = new SuperAdmin("wxid_ytpc72mdoskt22", "燕敏", "2033493067", "", "");
//super_admins.put(admin7.getWxid() + admin7.getUnionId(), admin7);
//jdidToWxidMap.put(admin7.getUnionId(), admin7.getWxid());
//jdidToRemarkMap.put(admin7.getUnionId(), admin7.getName());
SuperAdmin admin7 = new SuperAdmin("wxid_ytpc72mdoskt22", "燕敏", "2033493067", "66618e24a5e9f44156253c4eb9892388", "15846e2334934482a703417d5058f842");
super_admins.put(admin7.getWxid() + admin7.getUnionId(), admin7);
jdidToWxidMap.put(admin7.getUnionId(), admin7.getWxid());
jdidToRemarkMap.put(admin7.getUnionId(), admin7.getName());