抽离礼金

This commit is contained in:
Leo
2025-04-08 16:00:57 +08:00
parent 9c48e02531
commit ef3f50259e

View File

@@ -186,13 +186,15 @@ public void sendOrderToWxByOrderDefault(String order, String fromWxid) {
logger.info("执行 sendOrderToWxByOrderDefault 方法order: {}, fromWxid: {}", order, fromWxid);
if ("".equals(order)) {
// 确保缓存中有消息内容
String cachedMessage = cacheMap.get("giftMessage" + fromWxid);
if (cachedMessage == null || cachedMessage.trim().isEmpty()) {
wxUtil.sendTextMessage(fromWxid, "未找到商品链接,请重新输入包含商品链接的消息。", 1, fromWxid, false);
return;
}
handleGiftCommand(fromWxid);
// 设置状态为等待文案输入
String key = INTERACTION_STATE_PREFIX + fromWxid;
UserInteractionState state = loadOrCreateState(key);
state.setCurrentState(UserInteractionState.ProcessState.GIFT_MONEY_FLOW);
state.setCurrentStep(UserInteractionState.GiftMoneyStep.STEP_WAIT_FOR_CONTENT);
saveState(key, state);
// 提示用户输入文案
wxUtil.sendTextMessage(fromWxid, "请输入包含商品链接的文案:", 1, fromWxid, false);
} else {
handleUserInteraction(fromWxid, order);
}
@@ -200,6 +202,7 @@ public void sendOrderToWxByOrderDefault(String order, String fromWxid) {
/**
* 处理“礼”指令,直接进入礼金创建流程
*/
@@ -264,80 +267,47 @@ private void handleGiftCommand(String fromWxid) {
*/
private void processGiftMoneyFlow(String wxid, String message, UserInteractionState state) {
try {
// 获取缓存的商品信息
String cachedData = cacheMap.get("validProducts" + wxid);
if (cachedData == null) {
wxUtil.sendTextMessage(wxid, "数据丢失,请重新开始流程", 1, wxid, false);
resetState(wxid, state);
// 提取文案中的 u.jd.com 链接
List<String> urls = extractUJDUrls(message);
if (urls.isEmpty()) {
wxUtil.sendTextMessage(wxid, "未找到有效的商品链接,请重新输入包含商品链接的文案。", 1, wxid, false);
return;
}
List<GoodsQueryResult> validProducts = JSON.parseArray(cachedData, GoodsQueryResult.class);
int currentIndex = Integer.parseInt(state.getCollectedFields().get("productIndex"));
int totalProducts = Integer.parseInt(state.getCollectedFields().get("totalProducts"));
// 根据当前步骤处理
switch (state.getCurrentStep()) {
case STEP_AMOUNT:
if (!isValidAmount(message)) {
wxUtil.sendTextMessage(wxid, "金额格式错误请输入1-50元", 1, wxid, false);
return;
// 查询商品信息,筛选有效商品
List<GoodsQueryResult> validProducts = new ArrayList<>();
for (String url : urls) {
try {
GoodsQueryResult productInfo = queryProductInfoByUJDUrl(url);
if (productInfo != null && productInfo.getCode() == 200 && productInfo.getTotalCount() > 0) {
validProducts.add(productInfo);
}
state.getCollectedFields().put("amount", message);
state.setCurrentStep(UserInteractionState.GiftMoneyStep.STEP_QUANTITY);
wxUtil.sendTextMessage(wxid, "请输入数量1-100", 1, wxid, false);
break;
case STEP_QUANTITY:
if (!isValidQuantity(message)) {
wxUtil.sendTextMessage(wxid, "数量格式错误请输入1-100的整数", 1, wxid, false);
return;
}
// 记录数量
state.getCollectedFields().put("quantity", message);
// 开始循环创建礼金
double amount = Double.parseDouble(state.getCollectedFields().get("amount"));
int quantity = Integer.parseInt(message);
StringBuilder result = new StringBuilder();
for (int i = currentIndex; i < totalProducts; i++) {
GoodsQueryResult product = validProducts.get(i);
JSONObject productInfo = JSONObject.parseObject(JSON.toJSONString(product.getData()[0]));
String skuId = productInfo.getString("materialUrl");
String owner = productInfo.getString("owner");
String skuName = productInfo.getString("skuName");
// 调用礼金创建接口
String giftKey = createGiftCoupon(skuId, amount, quantity, owner, skuName);
if (giftKey != null) {
result.append("商品:").append(skuName).append("\n")
.append("礼金创建成功批次ID").append(giftKey).append("\n\n");
} else {
result.append("商品:").append(skuName).append("\n")
.append("礼金创建失败,请稍后重试。\n\n");
}
// 更新当前索引
state.getCollectedFields().put("productIndex", String.valueOf(i + 1));
saveState(INTERACTION_STATE_PREFIX + wxid, state);
}
// 返回结果
wxUtil.sendTextMessage(wxid, "礼金创建完成:\n" + result.toString(), 1, wxid, false);
resetState(wxid, state);
break;
default:
wxUtil.sendTextMessage(wxid, "流程异常,请重新开始", 1, wxid, false);
resetState(wxid, state);
} catch (Exception e) {
logger.error("查询商品信息失败:{}", url, e);
}
}
if (validProducts.isEmpty()) {
wxUtil.sendTextMessage(wxid, "未找到有效的商品信息,请检查链接是否正确。", 1, wxid, false);
return;
}
// 缓存商品信息
cacheMap.put("validProducts" + wxid, JSON.toJSONString(validProducts));
// 更新状态
state.setCurrentStep(UserInteractionState.GiftMoneyStep.STEP_AMOUNT);
state.getCollectedFields().clear();
state.getCollectedFields().put("productIndex", "0"); // 当前处理的商品索引
state.getCollectedFields().put("totalProducts", String.valueOf(validProducts.size())); // 总商品数
saveState(INTERACTION_STATE_PREFIX + wxid, state);
// 提示用户输入金额
wxUtil.sendTextMessage(wxid, "请输入开通金额1-50元支持小数点后两位", 1, wxid, false);
} catch (Exception e) {
logger.error("礼金创建流程异常 - 用户: {}", wxid, e);
wxUtil.sendTextMessage(wxid, "处理异常,请重试", 1, wxid, false);
logger.error("处理礼金文案异常 - 用户: {}", wxid, e);
wxUtil.sendTextMessage(wxid, "处理请求时发生异常,请重试", 1, wxid, false);
resetState(wxid, state);
}
}
@@ -878,39 +848,39 @@ private void processGiftMoneyFlow(String wxid, String message, UserInteractionSt
});
}
private void handleUserInteraction(String fromWxid, String message) {
String key = INTERACTION_STATE_PREFIX + fromWxid;
UserInteractionState state = loadOrCreateState(key);
private void handleUserInteraction(String fromWxid, String message) {
String key = INTERACTION_STATE_PREFIX + fromWxid;
UserInteractionState state = loadOrCreateState(key);
try {
switch (state.getCurrentState()) {
case INIT:
handleInitState(fromWxid, message, state);
break;
try {
switch (state.getCurrentState()) {
case INIT:
handleInitState(fromWxid, message, state);
break;
case PRODUCT_PROMOTION:
handlePromotionState(fromWxid, message, state);
break;
case PRODUCT_PROMOTION:
handlePromotionState(fromWxid, message, state);
break;
case GIFT_MONEY_FLOW:
processGiftMoneyFlow(fromWxid, message, state);
break;
case GIFT_MONEY_FLOW:
processGiftMoneyFlow(fromWxid, message, state);
break;
default:
resetState(fromWxid, state);
wxUtil.sendTextMessage(fromWxid, "流程异常,请重新开始", 1, fromWxid, false);
}
} catch (Exception e) {
logger.error("流程处理异常 - 用户: {}, 状态: {}", fromWxid, state, e);
resetState(fromWxid, state);
wxUtil.sendTextMessage(fromWxid, "处理异常,请重新开始", 1, fromWxid, false);
} finally {
saveState(key, state);
}
default:
resetState(fromWxid, state);
wxUtil.sendTextMessage(fromWxid, "流程异常,请重新开始", 1, fromWxid, false);
}
} catch (Exception e) {
logger.error("流程处理异常 - 用户: {}, 状态: {}", fromWxid, state, e);
resetState(fromWxid, state);
wxUtil.sendTextMessage(fromWxid, "处理异常,请重", 1, fromWxid, false);
} finally {
saveState(key, state);
}
userInteractionStates.put(key, state);
logger.debug("Updated interaction state for user {}: {}", fromWxid, JSON.toJSONString(state));
}
userInteractionStates.put(key, state);
logger.debug("Updated interaction state for user {}: {}", fromWxid, JSON.toJSONString(state));
}
private UserInteractionState loadOrCreateState(String key) {
UserInteractionState state = loadState(key);
@@ -1556,7 +1526,9 @@ public HashMap<String, List<String>> generatePromotionContent(String message) {
INIT, GIFT_MONEY_FLOW, PRODUCT_PROMOTION
}
@Getter
public enum GiftMoneyStep {
STEP_WAIT_FOR_CONTENT(3),
STEP_CONFIRM_GIFT(0), // 显式指定序号
STEP_AMOUNT(1), STEP_QUANTITY(2);
private final int code;
@@ -1565,10 +1537,6 @@ public HashMap<String, List<String>> generatePromotionContent(String message) {
this.code = code;
}
public int getCode() {
return code;
}
}