测试
This commit is contained in:
@@ -36,9 +36,13 @@ import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static cn.van.business.util.JDUtil.UserInteractionState.GiftMoneyStep.*;
|
||||
import static cn.van.business.util.JDUtil.UserInteractionState.ProcessState.*;
|
||||
import static cn.van.business.util.WXUtil.super_admins;
|
||||
|
||||
/**
|
||||
@@ -1073,18 +1077,28 @@ public class JDUtil {
|
||||
state.updateLastInteractionTime();
|
||||
|
||||
switch (state.getCurrentState()) {
|
||||
case "INIT":
|
||||
case INIT:
|
||||
if ("京礼金".equals(message)) {
|
||||
state.setCurrentState(GIFT_MONEY_FLOW);
|
||||
state.setCurrentStep(STEP_PRODUCT_LINK);
|
||||
wxUtil.sendTextMessage(fromWxid, "请输入商品链接:", 1, fromWxid);
|
||||
logger.info("进入礼金开通流程 - 商品链接步骤");
|
||||
}
|
||||
if ("登记".equals(message)) {
|
||||
// 开始登记新的订单
|
||||
state.setCurrentState("DISINFECTANT_CABINET");
|
||||
state.setCurrentState(DISINFECTANT_CABINET);
|
||||
state.setCurrentField("orderId");
|
||||
wxUtil.sendTextMessage(fromWxid, "请输入订单号:", 1, fromWxid);
|
||||
logger.debug("User {} entered DISINFECTANT_CABINET state", fromWxid);
|
||||
}
|
||||
break;
|
||||
case "DISINFECTANT_CABINET":
|
||||
case GIFT_MONEY_FLOW:
|
||||
handleGiftMoneyFlow(fromWxid, message, state);
|
||||
break;
|
||||
|
||||
case DISINFECTANT_CABINET:
|
||||
if ("退出".equals(message)) {
|
||||
state.setCurrentState("INIT");
|
||||
state.setCurrentState(INIT);
|
||||
wxUtil.sendTextMessage(fromWxid, "退出登记", 1, fromWxid);
|
||||
logger.debug("User {} exited DISINFECTANT_CABINET state", fromWxid);
|
||||
} else {
|
||||
@@ -1098,7 +1112,7 @@ public class JDUtil {
|
||||
} else if (state.getCurrentField().equals("recipientPhone")) {
|
||||
// 所有字段收集完毕,保存订单
|
||||
saveProductOrder(state.getCollectedFields());
|
||||
state.setCurrentState("INIT");
|
||||
state.setCurrentState(INIT);
|
||||
wxUtil.sendTextMessage(fromWxid, "订单已登记", 1, fromWxid);
|
||||
logger.debug("User {} completed order registration", fromWxid);
|
||||
}
|
||||
@@ -1106,7 +1120,7 @@ public class JDUtil {
|
||||
break;
|
||||
default:
|
||||
wxUtil.sendTextMessage(fromWxid, "无效的状态,请重新开始对话", 1, fromWxid);
|
||||
state.setCurrentState("INIT");
|
||||
state.setCurrentState(INIT);
|
||||
logger.debug("User {} reset to INIT state due to invalid state", fromWxid);
|
||||
break;
|
||||
}
|
||||
@@ -1131,28 +1145,139 @@ public class JDUtil {
|
||||
productOrderRepository.save(productOrder);
|
||||
logger.debug("Saved product order: {}", productOrder);
|
||||
}
|
||||
// 新增礼金流程处理方法
|
||||
private void handleGiftMoneyFlow(String fromWxid, String message, UserInteractionState state) {
|
||||
if ("京礼金".equals(message)) {
|
||||
state.reset(); // 重置流程
|
||||
wxUtil.sendTextMessage(fromWxid, "流程已重置,请重新开始", 1, fromWxid);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
switch (state.getCurrentStep()) {
|
||||
case STEP_PRODUCT_LINK:
|
||||
// 解析商品链接获取SKU
|
||||
String skuId = parseSkuFromUrl(message);
|
||||
Map<String, String> productInfo = queryProductInfo(skuId);
|
||||
|
||||
state.getCollectedFields().put("skuId", skuId);
|
||||
state.getCollectedFields().put("productInfo",
|
||||
productInfo.get("name") + "\n价格:" + productInfo.get("price"));
|
||||
|
||||
state.setCurrentStep(STEP_AMOUNT);
|
||||
wxUtil.sendTextMessage(fromWxid,
|
||||
"商品信息:\n" + productInfo.get("name") +
|
||||
"\n当前价格:" + productInfo.get("price") +
|
||||
"\n请输入开通金额(元):", 1, fromWxid);
|
||||
break;
|
||||
|
||||
case STEP_AMOUNT:
|
||||
if (!isValidAmount(message)) {
|
||||
wxUtil.sendTextMessage(fromWxid, "金额格式错误,请重新输入", 1, fromWxid);
|
||||
return;
|
||||
}
|
||||
state.getCollectedFields().put("amount", message);
|
||||
state.setCurrentStep(STEP_QUANTITY);
|
||||
wxUtil.sendTextMessage(fromWxid, "请输入开通数量:", 1, fromWxid);
|
||||
break;
|
||||
|
||||
case STEP_QUANTITY:
|
||||
if (!isValidQuantity(message)) {
|
||||
wxUtil.sendTextMessage(fromWxid, "数量格式错误,请重新输入", 1, fromWxid);
|
||||
return;
|
||||
}
|
||||
state.getCollectedFields().put("quantity", message);
|
||||
|
||||
// 调用开通接口
|
||||
boolean result = activateGiftMoney(
|
||||
state.getCollectedFields().get("skuId"),
|
||||
Double.parseDouble(state.getCollectedFields().get("amount")),
|
||||
Integer.parseInt(message)
|
||||
);
|
||||
|
||||
String response = result ? "开通成功!" : "开通失败,请稍后重试";
|
||||
wxUtil.sendTextMessage(fromWxid, response, 1, fromWxid);
|
||||
state.reset(); // 完成流程
|
||||
break;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("礼金流程处理异常", e);
|
||||
wxUtil.sendTextMessage(fromWxid, "系统异常,流程已终止", 1, fromWxid);
|
||||
state.reset();
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, String> queryProductInfo(String skuId) {
|
||||
// 调用京东商品查询API(需要实现)
|
||||
return Map.of("name", "示例商品", "price", "299.00");
|
||||
}
|
||||
|
||||
private boolean isValidQuantity(String input) {
|
||||
return input.matches("^\\d+$");
|
||||
}
|
||||
|
||||
private boolean activateGiftMoney(String skuId, double amount, int quantity) {
|
||||
// 实现实际的开通接口调用
|
||||
return true;
|
||||
}
|
||||
// 定义一个内部类来存储用户交互状态
|
||||
@Getter
|
||||
@Setter
|
||||
private static class UserInteractionState {
|
||||
static class UserInteractionState {
|
||||
// 推荐使用枚举管理状态
|
||||
public enum ProcessState {
|
||||
INIT,
|
||||
GIFT_MONEY_FLOW,
|
||||
DISINFECTANT_CABINET
|
||||
}
|
||||
|
||||
public enum GiftMoneyStep {
|
||||
STEP_PRODUCT_LINK,
|
||||
STEP_AMOUNT,
|
||||
STEP_QUANTITY
|
||||
}
|
||||
private GiftMoneyStep currentStep; // 新增当前步骤字段
|
||||
|
||||
private String lastInteractionTime;
|
||||
private String currentState;
|
||||
private ProcessState currentState;
|
||||
private Map<String, String> collectedFields; // 用于存储收集到的字段值
|
||||
private String currentField; // 当前正在询问的字段
|
||||
|
||||
public UserInteractionState() {
|
||||
this.lastInteractionTime = LocalDateTime.now().format(DATE_TIME_FORMATTER);
|
||||
this.currentState = "INIT";
|
||||
this.currentState = INIT;
|
||||
this.collectedFields = new HashMap<>();
|
||||
this.currentField = null;
|
||||
|
||||
}
|
||||
|
||||
public void updateLastInteractionTime() {
|
||||
this.lastInteractionTime = LocalDateTime.now().format(DATE_TIME_FORMATTER);
|
||||
}
|
||||
}
|
||||
// UserInteractionState类缺少reset方法
|
||||
public void reset() {
|
||||
this.currentState = INIT;
|
||||
this.collectedFields.clear();
|
||||
this.currentStep = null;
|
||||
updateLastInteractionTime();
|
||||
}
|
||||
|
||||
}
|
||||
// 增强京东链接解析的正则表达式
|
||||
private String parseSkuFromUrl(String url) {
|
||||
Pattern pattern = Pattern.compile("/(\\d+)(\\.html|\\?)"); // 支持更多链接格式
|
||||
Matcher matcher = pattern.matcher(url);
|
||||
return matcher.find() ? matcher.group(1) : null;
|
||||
}
|
||||
// 改进金额验证提示
|
||||
private boolean isValidAmount(String input) {
|
||||
String fromWxid = "";
|
||||
if (!input.matches("^\\d+(\\.\\d{1,2})?$")) {
|
||||
wxUtil.sendTextMessage(fromWxid, "金额格式错误,请输入数字(可包含两位小数)\n示例:50 或 29.9", 1, fromWxid);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// 限流异常类(需自定义)
|
||||
public static class RateLimitExceededException extends RuntimeException {
|
||||
public RateLimitExceededException(String message) {
|
||||
|
||||
Reference in New Issue
Block a user