通知消息 跳转 接口对接

This commit is contained in:
Jack 2025-09-27 21:09:53 +08:00
parent f9f7fc1688
commit 62b5d71b3b
3 changed files with 102 additions and 8 deletions

View File

@ -40,6 +40,6 @@ public class WxOrderShippingController {
if (!result.getFirst()) {
throw new ApiException(result.getSecond());
}
return CommonResult.success();
return CommonResult.success(result.getSecond());
}
}

View File

@ -36,5 +36,20 @@ public interface WxOrderShippingService {
* 配置订单详情路径
* 参考https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/business-capabilities/order_center/order_center.html
*/
Pair<Boolean, String> updateOrderDetailPath(String orderId);
/**
* 更新订单详情路径
* 参考https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/business-capabilities/order_center/order_center.html
*
* @param path 包括参数名=${商品订单号}
* @return
*/
Pair<Boolean, String> updateOrderDetailPath(String path);
/**
* 消息跳转路径设置接口
*
* @param orderId 订单号
* @return
*/
Pair<Boolean, String> setMsgJumpPath(String orderId);
}

View File

@ -40,7 +40,8 @@ import java.util.Date;
@Service
public class WxOrderShippingServiceImpl implements WxOrderShippingService {
private final String ORDER_DETAIL_PATH = "https://api.weixin.qq.com/wxa/business/shipping";
private final String ORDER_DETAIL_PATH = "member/order/detail";
@Lazy
@Autowired
private WxUtil wxUtil;
@ -160,6 +161,9 @@ public class WxOrderShippingServiceImpl implements WxOrderShippingService {
return Pair.of(false, "发货信息录入失败: " + errorMsg);
}
// 跳转链接设置
setMsgJumpPath(orderId);
log.info("发货信息录入成功, 订单ID: {}", orderId);
return Pair.of(true, "发货信息录入成功");
@ -231,6 +235,10 @@ public class WxOrderShippingServiceImpl implements WxOrderShippingService {
return Pair.of(false, "通知微信用户确认收货失败: " + errorMsg);
}
// 跳转链接设置
setMsgJumpPath(orderId);
log.info("通知微信用户确认收货成功, 订单ID: {}", orderId);
return Pair.of(true, "通知微信用户确认收货成功");
@ -254,9 +262,8 @@ public class WxOrderShippingServiceImpl implements WxOrderShippingService {
public Pair<Boolean, String> updateOrderDetailPath(String path) {
log.debug("开始配置订单详情路径,路径: {}", path);
if (StrUtil.isBlank(path)) {
log.warn("订单详情路径为空,默认:{}", ORDER_DETAIL_PATH);
// return Pair.of(false, "详情路径 path 不能为空");
path = ORDER_DETAIL_PATH;
path = String.format("%s?on=${商品订单号}", ORDER_DETAIL_PATH);
log.warn("订单详情路径为空,默认:{}", path);
}
try {
@ -267,9 +274,13 @@ public class WxOrderShippingServiceImpl implements WxOrderShippingService {
return Pair.of(false, "获取AccessToken失败");
}
if (!StrUtil.contains(path, "?")) {
path = String.format("%s?on=${商品订单号}", path);
}
// 构造请求参数
JSONObject paramsJSON = new JSONObject()
.set("path", String.format("%s?on=${商品订单号}", path));
.set("path", path);
// 构造请求URL
String postUrl = "https://api.weixin.qq.com/wxa/sec/order/update_order_detail_path?access_token=" + accessToken;
@ -295,7 +306,7 @@ public class WxOrderShippingServiceImpl implements WxOrderShippingService {
}
log.info("配置订单详情路径成功,路径: {}", path);
return Pair.of(true, "配置订单详情路径成功");
return Pair.of(true, path);
} catch (HttpClientErrorException e) {
log.error("HTTP请求错误状态码: {}, 错误信息: {}", e.getStatusCode(), e.getMessage(), e);
@ -305,4 +316,72 @@ public class WxOrderShippingServiceImpl implements WxOrderShippingService {
return Pair.of(false, "配置订单详情路径失败: " + e.getMessage());
}
}
/**
* 消息跳转路径设置接口
* 参考https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/business-capabilities/order-shipping/order-shipping.html
* 用于设置微信订单消息中的跳转路径用户点击消息时可跳转到指定页面
*
* @param orderId 订单号
* @return 设置结果包含成功状态和跳转路径
*/
@Override
public Pair<Boolean, String> setMsgJumpPath(String orderId) {
log.debug("开始设置消息跳转路径订单ID: {}", orderId);
if (StrUtil.isBlank(orderId)) {
log.warn("订单号不能为空");
return Pair.of(false, "订单号不能为空");
}
try {
// 获取微信访问令牌
String accessToken = wxUtil.getAccessToken();
if (StrUtil.isBlank(accessToken)) {
log.error("获取AccessToken失败订单ID: {}", orderId);
return Pair.of(false, "获取AccessToken失败");
}
// 构造跳转路径
String path = String.format("%s?on=%s", ORDER_DETAIL_PATH, orderId);
log.debug("构造消息跳转路径: {}", path);
// 构造请求参数
JSONObject paramsJSON = new JSONObject()
.set("path", path);
// 构造请求URL
String postUrl = "https://api.weixin.qq.com/wxa/sec/order/set_msg_jump_path?access_token=" + accessToken;
log.debug("消息跳转路径设置请求: {} \n {}", postUrl, paramsJSON);
// 发送请求到微信API
JSONObject respObj = RestTemplateHttpUtil.sendPost(
postUrl,
null, paramsJSON, JSONObject.class
);
// 处理响应结果
if (respObj == null) {
log.error("消息跳转路径设置失败订单ID: {}, 返回结果为空", orderId);
return Pair.of(false, "消息跳转路径设置失败,返回结果为空");
}
int errCode = respObj.getInt("errcode", -1);
if (errCode != 0) {
String errorMsg = respObj.getStr("errmsg", "未知错误");
log.error("消息跳转路径设置失败订单ID: {}, 错误码: {}, 错误信息: {}", orderId, errCode, errorMsg);
return Pair.of(false, "消息跳转路径设置失败: " + errorMsg);
}
log.info("消息跳转路径设置成功订单ID: {}, 路径: {}", orderId, path);
return Pair.of(true, path);
} catch (HttpClientErrorException e) {
log.error("HTTP请求错误订单ID: {}, 状态码: {}, 错误信息: {}", orderId, e.getStatusCode(), e.getMessage(), e);
return Pair.of(false, "HTTP请求错误: " + e.getMessage());
} catch (Exception e) {
log.error("消息跳转路径设置失败订单ID: {}, 错误信息: {}", orderId, e.getMessage(), e);
return Pair.of(false, "消息跳转路径设置失败: " + e.getMessage());
}
}
}