1
This commit is contained in:
@@ -2889,8 +2889,7 @@ public class JDUtil {
|
|||||||
Map<String, String> fields = new HashMap<>();
|
Map<String, String> fields = new HashMap<>();
|
||||||
|
|
||||||
// 适配新模板格式的字段提取
|
// 适配新模板格式的字段提取
|
||||||
// 新格式:单: -> 分销标记: -> 第三方单号: -> 下单链接(必须用这个): -> 下单地址(注意带分机): -> 型号: -> 下单人(需填): -> 下单付款(注意核对): -> 后返金额(注意核对): -> 订单号(需填): -> 物流链接(需填): -> 备注(下单号码有变动/没法带分机号的写这里):
|
// 按顺序提取各个字段,支持分隔线"—————————"作为字段分隔符
|
||||||
// 使用更灵活的正则,支持分隔线"—————————"
|
|
||||||
extractFieldWithSeparator(input, fields, "单:", "分销标记:");
|
extractFieldWithSeparator(input, fields, "单:", "分销标记:");
|
||||||
extractFieldWithSeparator(input, fields, "分销标记:", "第三方单号:");
|
extractFieldWithSeparator(input, fields, "分销标记:", "第三方单号:");
|
||||||
extractFieldWithSeparator(input, fields, "第三方单号:", "下单链接(必须用这个):");
|
extractFieldWithSeparator(input, fields, "第三方单号:", "下单链接(必须用这个):");
|
||||||
@@ -2904,14 +2903,22 @@ public class JDUtil {
|
|||||||
extractFieldWithSeparator(input, fields, "物流链接(需填):", "备注(下单号码有变动/没法带分机号的写这里):");
|
extractFieldWithSeparator(input, fields, "物流链接(需填):", "备注(下单号码有变动/没法带分机号的写这里):");
|
||||||
|
|
||||||
// 手动提取"备注"字段(最后一个字段,可能后面有分隔线或"京粉实际价格")
|
// 手动提取"备注"字段(最后一个字段,可能后面有分隔线或"京粉实际价格")
|
||||||
Pattern remarkPattern = Pattern.compile("备注(下单号码有变动/没法带分机号的写这里):\\s*(.*?)\\s*(?=—————————|京粉实际价格|\\Z)", Pattern.DOTALL);
|
Pattern remarkPattern = Pattern.compile("备注(下单号码有变动/没法带分机号的写这里):[\\s\\n]*([\\s\\S]*?)[\\s\\n]*(?=—————————|京粉实际价格|\\Z)", Pattern.DOTALL);
|
||||||
Matcher remarkMatcher = remarkPattern.matcher(input);
|
Matcher remarkMatcher = remarkPattern.matcher(input);
|
||||||
if (remarkMatcher.find()) {
|
if (remarkMatcher.find()) {
|
||||||
String remarkValue = remarkMatcher.group(1).trim();
|
String remarkValue = remarkMatcher.group(1);
|
||||||
// 去掉可能的分隔线
|
if (remarkValue != null) {
|
||||||
remarkValue = remarkValue.replaceAll("^—————————\\s*", "").trim();
|
remarkValue = remarkValue.trim();
|
||||||
fields.put("备注", remarkValue);
|
// 去掉可能的分隔线
|
||||||
|
remarkValue = remarkValue.replaceAll("^—————————[\\s\\n]*", "").trim();
|
||||||
|
if (!remarkValue.isEmpty()) {
|
||||||
|
fields.put("备注", remarkValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 调试:打印提取到的字段(可选,生产环境可以注释掉)
|
||||||
|
// logger.debug("解析到的字段: {}", fields);
|
||||||
|
|
||||||
// 构建 JDOrder 对象
|
// 构建 JDOrder 对象
|
||||||
JDOrder order = new JDOrder();
|
JDOrder order = new JDOrder();
|
||||||
@@ -2970,17 +2977,35 @@ public class JDUtil {
|
|||||||
|
|
||||||
// 提取字段的方法(支持分隔线)
|
// 提取字段的方法(支持分隔线)
|
||||||
private void extractFieldWithSeparator(String input, Map<String, String> map, String startKeyword, String endKeyword) {
|
private void extractFieldWithSeparator(String input, Map<String, String> map, String startKeyword, String endKeyword) {
|
||||||
// 支持分隔线"—————————"作为字段分隔符
|
try {
|
||||||
Pattern pattern = Pattern.compile(startKeyword + "\\s*(.*?)\\s*(?=" + Pattern.quote(endKeyword) + "|—————————|\\Z)", Pattern.DOTALL);
|
// 构建正则表达式,匹配从startKeyword到endKeyword或分隔线之间的内容
|
||||||
Matcher matcher = pattern.matcher(input);
|
String escapedStart = Pattern.quote(startKeyword);
|
||||||
|
String escapedEnd = Pattern.quote(endKeyword);
|
||||||
|
|
||||||
|
// 匹配模式:startKeyword + 可选的空白和换行 + 字段值(非贪婪,支持多行) + 可选的空白和换行 + 后面是endKeyword或分隔线或结束
|
||||||
|
// 使用[\s\S]来匹配包括换行在内的所有字符
|
||||||
|
String patternStr = escapedStart + "[\\s\\n]*([\\s\\S]*?)(?=[\\s\\n]*" + escapedEnd + "|[\\s\\n]*—————————|\\Z)";
|
||||||
|
Pattern pattern = Pattern.compile(patternStr, Pattern.DOTALL);
|
||||||
|
Matcher matcher = pattern.matcher(input);
|
||||||
|
|
||||||
if (matcher.find()) {
|
if (matcher.find()) {
|
||||||
String value = matcher.group(1).trim();
|
String value = matcher.group(1);
|
||||||
// 去掉可能的分隔线
|
if (value != null) {
|
||||||
value = value.replaceAll("^—————————\\s*", "").replaceAll("\\s*—————————$", "").trim();
|
// 去掉开头和结尾的空白字符和换行
|
||||||
// 去掉字段标签本身(如果被匹配到了)
|
value = value.trim();
|
||||||
value = value.replaceAll("^" + Pattern.quote(startKeyword), "").trim();
|
// 去掉可能的分隔线(在值的前后)
|
||||||
map.put(startKeyword.replace(":", ""), value);
|
value = value.replaceAll("^—————————[\\s\\n]*", "").replaceAll("[\\s\\n]*—————————$", "").trim();
|
||||||
|
// 去掉字段标签本身(如果被匹配到了)
|
||||||
|
value = value.replaceAll("^" + escapedStart, "").trim();
|
||||||
|
// 保存字段值,使用去掉冒号的key
|
||||||
|
String key = startKeyword.replace(":", "").trim();
|
||||||
|
if (!value.isEmpty()) {
|
||||||
|
map.put(key, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.warn("提取字段失败: startKeyword={}, endKeyword={}, error={}", startKeyword, endKeyword, e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user