This commit is contained in:
Van0313
2025-04-27 12:05:38 +08:00
parent 2fdf300849
commit 040f99d285

View File

@@ -96,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;
@@ -711,9 +711,9 @@ public class JDUtil {
private void handlePromotionState(String wxid, String message, UserInteractionState state) {
if ("content—withOutPic".equals(state.getCurrentField())) {
processContentInput(wxid, message, state,false);
processContentInput(wxid, message, state, false);
} else if ("content—withPic".equals(state.getCurrentField())) {
processContentInput(wxid, message, state,true);
processContentInput(wxid, message, state, true);
} else if ("confirm".equals(state.getCurrentField())) {
handleGiftMoneyConfirmation(wxid, message, state);
@@ -840,7 +840,7 @@ public class JDUtil {
// 2. 生成推广内容
HashMap<String, List<String>> contentResult = generatePromotionContent(message, false);
if (withPic){
if (withPic) {
// 3. 发送文本内容
for (String text : contentResult.get("text")) {
wxUtil.sendTextMessage(wxid, text, 1, wxid, true);
@@ -854,11 +854,11 @@ public class JDUtil {
}
}
}
}else {
} else {
// 3. 发送文本内容
for (String text : contentResult.get("text")) {
if (text.contains("链接类型")){
if (text.contains("链接类型")) {
wxUtil.sendTextMessage(wxid, text, 1, wxid, true);
}
}
@@ -944,6 +944,27 @@ public class JDUtil {
saveState(INTERACTION_STATE_PREFIX + wxid, state);
}
public static List<String> extractPrices(String text) {
List<String> prices = new ArrayList<>();
if (text.contains("🔥")) {
// 处理包含 🔥 的文本
Pattern pattern = Pattern.compile("\\d+(\\.\\d+)?💰");
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
String priceStr = matcher.group().replace("💰", "");
prices.add(priceStr);
}
} else {
// 处理不包含 🔥 的文本
Pattern pattern = Pattern.compile("💰(\\d+)");
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
prices.add(matcher.group(1));
}
}
return prices;
}
/**
* 生成转链和方案的方法
*
@@ -951,20 +972,22 @@ public class JDUtil {
* @return 处理后的方案,附带商品信息
*/
public synchronized HashMap<String, List<String>> generatePromotionContent(String message, Boolean isCj) {
HashMap<String, List<String>> finallMessage = new HashMap<>();
HashMap<String, List<String>> finallyMessage = new HashMap<>();
List<String> textList = new ArrayList<>();
List<String> imagesList = new ArrayList<>();
List<String> dataList = new ArrayList<>();
// 最终的方案
List<String> finalWenAn = new ArrayList<>();
// 提取方案中的所有 u.jd.com 链接
List<String> urlList = new ArrayList<>();
List<String> priceList = new ArrayList<>();
// 提取方案中的所有 u.jd.com 链接
List<String> urls = extractUJDUrls(message);
if (urls.isEmpty()) {
textList.add("方案中未找到有效的商品链接,请检查格式是否正确。");
finallMessage.put("text", textList);
return finallMessage;
finallyMessage.put("text", textList);
return finallyMessage;
}
/**
@@ -1210,7 +1233,13 @@ public class JDUtil {
String format = null;
DateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
if (!isCj) {
try {
priceList = extractPrices(message);
finallyMessage.put("priceList", priceList);
} catch (Exception ignored) {
}
}
for (String url : urls) {
try {
// 重置。不然会一直追加文本内容
@@ -1229,6 +1258,7 @@ public class JDUtil {
if (totalCount == 0) {
couponInfo.append("链接类型:优惠券\n\n");
} else {
urlList.add(url);
couponInfo.append("链接类型:商品\n\n");
JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(productInfo.getData()[0]));
jsonObject.put("url", url);
@@ -1269,7 +1299,7 @@ public class JDUtil {
if (!isCj) {
try {
if (!message.equals(url)){
if (!message.equals(url)) {
String[] lines = message.split("\\r?\\n");
if (lines.length > 0) {
title = lines[0];
@@ -1305,14 +1335,15 @@ public class JDUtil {
}
textList.add(String.valueOf(couponInfo));
finallMessage.put("data", dataList);
finallyMessage.put("data", dataList);
finallyMessage.put("urlList", urlList);
} catch (Exception e) {
logger.error("处理商品链接时发生异常:{}", url, e);
couponInfo.append(" 处理商品链接时发生异常:").append(url).append("\n");
textList.add(String.valueOf(couponInfo));
finallMessage.put("text", textList);
finallyMessage.put("text", textList);
}
}
/**
@@ -1324,10 +1355,10 @@ public class JDUtil {
finalWenAn.add(String.valueOf(wenan));
finallMessage.put("text", textList);
finallMessage.put("images", imagesList);
finallMessage.put("finalWenAn", finalWenAn);
return finallMessage;
finallyMessage.put("text", textList);
finallyMessage.put("images", imagesList);
finallyMessage.put("finalWenAn", finalWenAn);
return finallyMessage;
}
@@ -1509,16 +1540,16 @@ public class JDUtil {
public void sendOrderToWxByOrderD(String order, String fromWxid) {
if (order == null || order.trim().isEmpty()) {
wxUtil.sendTextMessage(fromWxid, "输入格式为:\n 单\n型号\n数量\n地址", 1, fromWxid, false);
wxUtil.sendTextMessage(fromWxid, "输入格式为:\n 单\n分销标记\n型号\n转链链接\n数量\n地址", 1, fromWxid, false);
return;
}
String[] split = order.split("\n");
logger.info("sendOrderToWxByOrderD.split 数组长度{}", split.length);
for (String s : split){
for (String s : split) {
logger.info("sendOrderToWxByOrderD.split 内容 {}", s);
}
if (split.length != 4) {
wxUtil.sendTextMessage(fromWxid, "输入格式为:\n 单\n型号\n数量\n地址", 1, fromWxid, false);
if (split.length != 6) {
wxUtil.sendTextMessage(fromWxid, "输入格式为:\n 单\n分销标记\n型号\n转链链接\n数量\n地址", 1, fromWxid, false);
return;
}
String temp = WENAN_D;
@@ -1532,10 +1563,11 @@ public class JDUtil {
String s = redisTemplate.opsForValue().get(redisKey);
Integer num = 1;
try {
num = Integer.parseInt(split[2].replaceAll(" ", ""));
} catch (NumberFormatException ignored) {
num = Integer.parseInt(split[4].replaceAll(" ", ""));
} catch (NumberFormatException e) {
logger.error("sendOrderToWxByOrderD 订单数量格式错误,请输入正确的数字", e);
}
for (int i = 0; i < num; i++){
for (int i = 0; i < num; i++) {
if (s != null) {
count = Integer.parseInt(s) + 1; // 递增计数器
}
@@ -1548,11 +1580,12 @@ public class JDUtil {
// 替换模板中的占位符
temp = temp.replace("{单号}", orderID);
temp = temp.replace("{地址}", split[3]);
temp = temp.replace("{型号}", split[1]);
temp = temp.replace("{分销标记}", split[1]);
temp = temp.replace("{链接}", split[3]);
temp = temp.replace("{地址}", split[5]);
temp = temp.replace("{型号}", split[2]);
temp = temp.replaceAll("[|]", "");
// 发送订单信息到微信
wxUtil.sendTextMessage(fromWxid, temp, 1, fromWxid, true);
}