1
This commit is contained in:
@@ -543,8 +543,8 @@ public class JDOrderController extends BaseController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文本替换:根据选中的商品创建礼金并替换文案中的URL
|
* 文本替换:为每个URL单独查询商品并创建礼金券,然后替换
|
||||||
* 入参:{ content, selectedProducts: [{skuId/materialUrl, amount, quantity, owner, skuName, originalUrl}], ... }
|
* 入参:{ content, amount, quantity, owner }
|
||||||
* 返回:替换后的文本和礼金信息
|
* 返回:替换后的文本和礼金信息
|
||||||
*/
|
*/
|
||||||
@PostMapping("/replaceUrlsWithGiftCoupons")
|
@PostMapping("/replaceUrlsWithGiftCoupons")
|
||||||
@@ -555,9 +555,15 @@ public class JDOrderController extends BaseController {
|
|||||||
return AjaxResult.error("content is required");
|
return AjaxResult.error("content is required");
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("文本URL替换请求 - content长度={}, 参数: materialUrl={}, skuId={}, amount={}, quantity={}, owner={}",
|
Object amountObj = body.get("amount");
|
||||||
content.length(), body.get("materialUrl"), body.get("skuId"), body.get("amount"),
|
Object quantityObj = body.get("quantity");
|
||||||
body.get("quantity"), body.get("owner"));
|
String owner = body.get("owner") != null ? String.valueOf(body.get("owner")) : "g";
|
||||||
|
|
||||||
|
double amount = amountObj != null ? Double.parseDouble(String.valueOf(amountObj)) : 1.8;
|
||||||
|
int quantity = quantityObj != null ? Integer.parseInt(String.valueOf(quantityObj)) : 12;
|
||||||
|
|
||||||
|
logger.info("文本URL替换请求 - content长度={}, amount={}, quantity={}, owner={}",
|
||||||
|
content.length(), amount, quantity, owner);
|
||||||
|
|
||||||
// 提取文本中的所有URL(京东链接)
|
// 提取文本中的所有URL(京东链接)
|
||||||
java.util.List<String> urls = new java.util.ArrayList<>();
|
java.util.List<String> urls = new java.util.ArrayList<>();
|
||||||
@@ -576,94 +582,179 @@ public class JDOrderController extends BaseController {
|
|||||||
logger.info("文本URL替换 - 提取到{}个URL", urls.size());
|
logger.info("文本URL替换 - 提取到{}个URL", urls.size());
|
||||||
|
|
||||||
if (urls.isEmpty()) {
|
if (urls.isEmpty()) {
|
||||||
return AjaxResult.success(new JSONObject().fluentPut("replacedContent", content)
|
return AjaxResult.success(new JSONObject()
|
||||||
|
.fluentPut("replacedContent", content)
|
||||||
|
.fluentPut("originalContent", content)
|
||||||
|
.fluentPut("replacements", new JSONArray())
|
||||||
|
.fluentPut("totalUrls", 0)
|
||||||
|
.fluentPut("replacedCount", 0)
|
||||||
.fluentPut("message", "未找到URL,无需替换"));
|
.fluentPut("message", "未找到URL,无需替换"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 批量创建礼金券
|
// 为每个URL单独处理:查询商品 → 创建礼金 → 转链 → 替换
|
||||||
Map<String, Object> batchParams = new java.util.HashMap<>();
|
|
||||||
batchParams.put("materialUrl", body.get("materialUrl"));
|
|
||||||
batchParams.put("skuId", body.get("skuId"));
|
|
||||||
batchParams.put("amount", body.get("amount"));
|
|
||||||
batchParams.put("quantity", body.get("quantity"));
|
|
||||||
batchParams.put("batchSize", urls.size());
|
|
||||||
batchParams.put("owner", body.get("owner"));
|
|
||||||
batchParams.put("skuName", body.get("skuName"));
|
|
||||||
|
|
||||||
AjaxResult batchResult = batchCreateGiftCoupons(batchParams);
|
|
||||||
|
|
||||||
Integer code = (Integer) batchResult.get(AjaxResult.CODE_TAG);
|
|
||||||
if (code == null || code != 200) {
|
|
||||||
String msg = (String) batchResult.get(AjaxResult.MSG_TAG);
|
|
||||||
return AjaxResult.error("批量创建礼金失败: " + (msg != null ? msg : "未知错误"));
|
|
||||||
}
|
|
||||||
|
|
||||||
// 解析批量创建结果
|
|
||||||
Object data = batchResult.get(AjaxResult.DATA_TAG);
|
|
||||||
JSONObject batchData = null;
|
|
||||||
if (data instanceof JSONObject) {
|
|
||||||
batchData = (JSONObject) data;
|
|
||||||
} else if (data instanceof String) {
|
|
||||||
try {
|
|
||||||
batchData = JSON.parseObject((String) data);
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.error("解析批量创建结果失败", e);
|
|
||||||
return AjaxResult.error("解析批量创建结果失败");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (batchData == null || !batchData.containsKey("results")) {
|
|
||||||
return AjaxResult.error("批量创建结果格式错误");
|
|
||||||
}
|
|
||||||
|
|
||||||
JSONArray results = batchData.getJSONArray("results");
|
|
||||||
if (results == null || results.size() != urls.size()) {
|
|
||||||
logger.warn("批量创建礼金数量不匹配 - 期望={}, 实际={}", urls.size(), results != null ? results.size() : 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (results == null) {
|
|
||||||
return AjaxResult.error("批量创建结果为空");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 替换文本中的URL
|
|
||||||
String replacedContent = content;
|
String replacedContent = content;
|
||||||
JSONArray replacementInfo = new JSONArray();
|
JSONArray replacementInfo = new JSONArray();
|
||||||
|
int successCount = 0;
|
||||||
int minSize = Math.min(urls.size(), results.size());
|
|
||||||
for (int i = 0; i < minSize; i++) {
|
|
||||||
String originalUrl = urls.get(i);
|
|
||||||
JSONObject result = results.getJSONObject(i);
|
|
||||||
|
|
||||||
String newUrl = null;
|
|
||||||
if (Boolean.TRUE.equals(result.getBoolean("success"))) {
|
|
||||||
newUrl = result.getString("shortURL");
|
|
||||||
if (newUrl == null || newUrl.trim().isEmpty()) {
|
|
||||||
newUrl = originalUrl; // 如果转链失败,保持原URL
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
newUrl = originalUrl; // 创建失败,保持原URL
|
|
||||||
}
|
|
||||||
|
|
||||||
// 替换文本中的URL(支持多种格式)
|
for (int i = 0; i < urls.size(); i++) {
|
||||||
replacedContent = replacedContent.replace(originalUrl, newUrl != null ? newUrl : originalUrl);
|
String originalUrl = urls.get(i);
|
||||||
|
logger.info("处理第{}/{}个URL: {}", i + 1, urls.size(), originalUrl);
|
||||||
JSONObject info = new JSONObject();
|
|
||||||
info.put("index", i + 1);
|
try {
|
||||||
info.put("originalUrl", originalUrl);
|
// 1. 查询该URL的商品信息
|
||||||
info.put("newUrl", newUrl);
|
String queryUrl = requestUrl + "generatePromotionContent";
|
||||||
info.put("success", Boolean.TRUE.equals(result.getBoolean("success")));
|
JSONObject queryParam = new JSONObject();
|
||||||
info.put("giftCouponKey", result.getString("giftCouponKey"));
|
queryParam.put("skey", skey);
|
||||||
replacementInfo.add(info);
|
queryParam.put("promotionContent", originalUrl);
|
||||||
|
|
||||||
|
String queryResult = HttpUtils.sendJsonPost(queryUrl, queryParam.toJSONString());
|
||||||
|
logger.debug("商品查询响应: {}", queryResult);
|
||||||
|
|
||||||
|
Object parsed = JSON.parse(queryResult);
|
||||||
|
JSONObject product = null;
|
||||||
|
|
||||||
|
// 解析商品信息
|
||||||
|
if (parsed instanceof JSONArray) {
|
||||||
|
JSONArray arr = (JSONArray) parsed;
|
||||||
|
if (arr.size() > 0 && arr.get(0) instanceof JSONObject) {
|
||||||
|
product = arr.getJSONObject(0);
|
||||||
|
}
|
||||||
|
} else if (parsed instanceof JSONObject) {
|
||||||
|
JSONObject obj = (JSONObject) parsed;
|
||||||
|
if (obj.get("list") instanceof JSONArray) {
|
||||||
|
JSONArray list = obj.getJSONArray("list");
|
||||||
|
if (list.size() > 0) product = list.getJSONObject(0);
|
||||||
|
} else if (obj.get("data") instanceof JSONArray) {
|
||||||
|
JSONArray data = obj.getJSONArray("data");
|
||||||
|
if (data.size() > 0) product = data.getJSONObject(0);
|
||||||
|
} else if (obj.containsKey("materialUrl") || obj.containsKey("skuName")) {
|
||||||
|
product = obj;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (product == null || product.containsKey("error")) {
|
||||||
|
logger.warn("URL{}商品信息查询失败: {}", originalUrl, product != null ? product.getString("error") : "无数据");
|
||||||
|
JSONObject info = new JSONObject();
|
||||||
|
info.put("index", i + 1);
|
||||||
|
info.put("originalUrl", originalUrl);
|
||||||
|
info.put("newUrl", originalUrl);
|
||||||
|
info.put("success", false);
|
||||||
|
info.put("error", "商品信息查询失败");
|
||||||
|
replacementInfo.add(info);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 为该商品创建礼金券
|
||||||
|
String createUrl = requestUrl + "createGiftCoupon";
|
||||||
|
JSONObject createParam = new JSONObject();
|
||||||
|
createParam.put("skey", skey);
|
||||||
|
createParam.put("amount", amount);
|
||||||
|
createParam.put("quantity", quantity);
|
||||||
|
createParam.put("owner", owner);
|
||||||
|
createParam.put("skuName", product.getString("skuName"));
|
||||||
|
|
||||||
|
// 设置skuId或materialUrl
|
||||||
|
String skuId = product.getString("skuId");
|
||||||
|
String materialUrl = product.getString("materialUrl");
|
||||||
|
if (skuId != null && !skuId.trim().isEmpty()) {
|
||||||
|
createParam.put("skuId", skuId);
|
||||||
|
} else if (materialUrl != null && !materialUrl.trim().isEmpty()) {
|
||||||
|
createParam.put("materialUrl", materialUrl);
|
||||||
|
} else {
|
||||||
|
logger.warn("商品{}既无skuId也无materialUrl", product.getString("skuName"));
|
||||||
|
JSONObject info = new JSONObject();
|
||||||
|
info.put("index", i + 1);
|
||||||
|
info.put("originalUrl", originalUrl);
|
||||||
|
info.put("newUrl", originalUrl);
|
||||||
|
info.put("success", false);
|
||||||
|
info.put("error", "商品信息不完整");
|
||||||
|
replacementInfo.add(info);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
String createResult = HttpUtils.sendJsonPost(createUrl, createParam.toJSONString());
|
||||||
|
logger.debug("礼金创建响应: {}", createResult);
|
||||||
|
|
||||||
|
JSONObject createData = JSON.parseObject(createResult);
|
||||||
|
if (createData == null || createData.containsKey("error") ||
|
||||||
|
createData.getString("giftCouponKey") == null ||
|
||||||
|
createData.getString("giftCouponKey").trim().isEmpty()) {
|
||||||
|
logger.warn("URL{}礼金创建失败: {}", originalUrl, createData != null ? createData.getString("error") : "giftCouponKey为空");
|
||||||
|
JSONObject info = new JSONObject();
|
||||||
|
info.put("index", i + 1);
|
||||||
|
info.put("originalUrl", originalUrl);
|
||||||
|
info.put("newUrl", originalUrl);
|
||||||
|
info.put("success", false);
|
||||||
|
info.put("error", "礼金创建失败");
|
||||||
|
replacementInfo.add(info);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
String giftCouponKey = createData.getString("giftCouponKey");
|
||||||
|
|
||||||
|
// 3. 转链(带礼金)
|
||||||
|
String transferUrl = requestUrl + "transfer";
|
||||||
|
JSONObject transferParam = new JSONObject();
|
||||||
|
transferParam.put("skey", skey);
|
||||||
|
transferParam.put("materialUrl", originalUrl);
|
||||||
|
transferParam.put("giftCouponKey", giftCouponKey);
|
||||||
|
|
||||||
|
String transferResult = HttpUtils.sendJsonPost(transferUrl, transferParam.toJSONString());
|
||||||
|
logger.debug("转链响应: {}", transferResult);
|
||||||
|
|
||||||
|
JSONObject transferData = JSON.parseObject(transferResult);
|
||||||
|
if (transferData == null || transferData.containsKey("error") ||
|
||||||
|
transferData.getString("shortURL") == null ||
|
||||||
|
transferData.getString("shortURL").trim().isEmpty()) {
|
||||||
|
logger.warn("URL{}转链失败: {}", originalUrl, transferData != null ? transferData.getString("error") : "shortURL为空");
|
||||||
|
JSONObject info = new JSONObject();
|
||||||
|
info.put("index", i + 1);
|
||||||
|
info.put("originalUrl", originalUrl);
|
||||||
|
info.put("newUrl", originalUrl);
|
||||||
|
info.put("success", false);
|
||||||
|
info.put("giftCouponKey", giftCouponKey);
|
||||||
|
info.put("error", "转链失败");
|
||||||
|
replacementInfo.add(info);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
String shortURL = transferData.getString("shortURL");
|
||||||
|
|
||||||
|
// 4. 替换文本中的URL
|
||||||
|
replacedContent = replacedContent.replace(originalUrl, shortURL);
|
||||||
|
|
||||||
|
JSONObject info = new JSONObject();
|
||||||
|
info.put("index", i + 1);
|
||||||
|
info.put("originalUrl", originalUrl);
|
||||||
|
info.put("newUrl", shortURL);
|
||||||
|
info.put("success", true);
|
||||||
|
info.put("giftCouponKey", giftCouponKey);
|
||||||
|
info.put("skuName", product.getString("skuName"));
|
||||||
|
replacementInfo.add(info);
|
||||||
|
|
||||||
|
successCount++;
|
||||||
|
logger.info("URL{}处理成功: {} -> {}", i + 1, originalUrl, shortURL);
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("处理URL{}失败: {}", originalUrl, e.getMessage(), e);
|
||||||
|
JSONObject info = new JSONObject();
|
||||||
|
info.put("index", i + 1);
|
||||||
|
info.put("originalUrl", originalUrl);
|
||||||
|
info.put("newUrl", originalUrl);
|
||||||
|
info.put("success", false);
|
||||||
|
info.put("error", e.getMessage());
|
||||||
|
replacementInfo.add(info);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("文本URL替换完成 - 替换了{}个URL", minSize);
|
logger.info("文本URL替换完成 - 成功{}/{}", successCount, urls.size());
|
||||||
|
|
||||||
JSONObject response = new JSONObject();
|
JSONObject response = new JSONObject();
|
||||||
response.put("replacedContent", replacedContent);
|
response.put("replacedContent", replacedContent);
|
||||||
response.put("originalContent", content);
|
response.put("originalContent", content);
|
||||||
response.put("replacements", replacementInfo);
|
response.put("replacements", replacementInfo);
|
||||||
response.put("totalUrls", urls.size());
|
response.put("totalUrls", urls.size());
|
||||||
response.put("replacedCount", minSize);
|
response.put("replacedCount", successCount);
|
||||||
|
|
||||||
return AjaxResult.success(response);
|
return AjaxResult.success(response);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user