fix(服务通知):微信服务号通知逻辑实现
This commit is contained in:
+47
@@ -1,10 +1,14 @@
|
|||||||
package com.central.order.listener;
|
package com.central.order.listener;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.central.common.context.LoginUserContextHolder;
|
||||||
|
import com.central.common.model.LoginAppUser;
|
||||||
|
import com.central.common.model.SysUser;
|
||||||
import com.central.order.enums.MessageTemplateEnum;
|
import com.central.order.enums.MessageTemplateEnum;
|
||||||
import com.central.order.enums.OrderStatus;
|
import com.central.order.enums.OrderStatus;
|
||||||
import com.central.order.enums.OrderStatusChangeEnum;
|
import com.central.order.enums.OrderStatusChangeEnum;
|
||||||
import com.central.order.event.OrderStatusChangeEvent;
|
import com.central.order.event.OrderStatusChangeEvent;
|
||||||
|
import com.central.order.feign.UserCenterService;
|
||||||
import com.central.order.model.*;
|
import com.central.order.model.*;
|
||||||
import com.central.order.service.*;
|
import com.central.order.service.*;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
@@ -30,6 +34,8 @@ public class OrderStatusChangeListener {
|
|||||||
private final IQrCodeService qrCodeService;
|
private final IQrCodeService qrCodeService;
|
||||||
private final IVideoEditService videoEditService;
|
private final IVideoEditService videoEditService;
|
||||||
private final IOrderFileService orderFileService;
|
private final IOrderFileService orderFileService;
|
||||||
|
private final IWxTemplateMessageService wxTemplateMessageService;
|
||||||
|
private final UserCenterService userCenterService;
|
||||||
|
|
||||||
@EventListener
|
@EventListener
|
||||||
@Async
|
@Async
|
||||||
@@ -69,6 +75,11 @@ public class OrderStatusChangeListener {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 处理支付成功状态
|
||||||
|
if (isPaymentSuccess(event.getOldStatus(), event.getNewStatus())) {
|
||||||
|
handlePaymentSuccess(order, tourism);
|
||||||
|
}
|
||||||
|
|
||||||
// 构建消息内容
|
// 构建消息内容
|
||||||
String content = changeEnum.getContentTemplate()
|
String content = changeEnum.getContentTemplate()
|
||||||
.replace("#{orderNo}", event.getOrderNo())
|
.replace("#{orderNo}", event.getOrderNo())
|
||||||
@@ -119,6 +130,42 @@ public class OrderStatusChangeListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断是否为支付成功状态变更
|
||||||
|
*/
|
||||||
|
private boolean isPaymentSuccess(Integer oldStatus, Integer newStatus) {
|
||||||
|
return OrderStatus.UNPAID.getId()
|
||||||
|
.equals(oldStatus) && OrderStatus.PAID.getId().equals(newStatus);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理支付成功逻辑
|
||||||
|
*/
|
||||||
|
private void handlePaymentSuccess(Order order, Tourism tourism) {
|
||||||
|
try {
|
||||||
|
// 1. 发送微信通知
|
||||||
|
|
||||||
|
LoginAppUser user = LoginUserContextHolder.getUser();
|
||||||
|
SysUser sysUser = userCenterService.findUserById(user.getId());
|
||||||
|
if (sysUser.getOpenId() != null) {
|
||||||
|
wxTemplateMessageService.sendPaymentSuccessNotice(
|
||||||
|
order.getOrderNo(),
|
||||||
|
sysUser.getOpenId(),
|
||||||
|
order.getOrderAmount(),
|
||||||
|
tourism.getTourismName()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 记录支付成功日志
|
||||||
|
log.info("订单支付成功: orderNo={}, amount={}, tourismName={}",
|
||||||
|
order.getOrderNo(), order.getOrderAmount(), tourism.getTourismName());
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
// 通知失败不影响主流程
|
||||||
|
log.error("处理支付成功通知失败: orderNo={}", order.getOrderNo(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理订单完成逻辑
|
* 处理订单完成逻辑
|
||||||
*/
|
*/
|
||||||
|
|||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
package com.central.order.service;
|
||||||
|
|
||||||
|
public interface IWxTemplateMessageService {
|
||||||
|
/**
|
||||||
|
* 发送订单支付成功通知
|
||||||
|
* @param orderNo 订单号
|
||||||
|
* @param openId 用户openId
|
||||||
|
* @param amount 支付金额
|
||||||
|
* @param tourismName 景区名称
|
||||||
|
*/
|
||||||
|
void sendPaymentSuccessNotice(String orderNo, String openId,
|
||||||
|
String amount, String tourismName);
|
||||||
|
}
|
||||||
+56
@@ -0,0 +1,56 @@
|
|||||||
|
package com.central.order.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.date.DateUtil;
|
||||||
|
import com.central.order.service.IWxTemplateMessageService;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import me.chanjar.weixin.mp.api.WxMpService;
|
||||||
|
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
|
||||||
|
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class WxTemplateMessageServiceImpl implements IWxTemplateMessageService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private WxMpService wxMpService;
|
||||||
|
|
||||||
|
@Value("${wx.mp.templateId.paymentSuccess}")
|
||||||
|
private String paymentSuccessTemplateId;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendPaymentSuccessNotice(String orderNo, String openId,
|
||||||
|
String amount, String tourismName) {
|
||||||
|
try {
|
||||||
|
// 构建模板消息
|
||||||
|
WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
|
||||||
|
.toUser(openId)
|
||||||
|
.templateId(paymentSuccessTemplateId)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// 设置模板数据
|
||||||
|
templateMessage.addData(new WxMpTemplateData("first", "您的订单支付成功!", "#173177"))
|
||||||
|
.addData(new WxMpTemplateData("keyword1", orderNo))
|
||||||
|
.addData(new WxMpTemplateData("keyword2", amount + "元"))
|
||||||
|
.addData(new WxMpTemplateData("keyword3", tourismName))
|
||||||
|
.addData(new WxMpTemplateData("keyword4", DateUtil.formatDateTime(new Date())))
|
||||||
|
.addData(new WxMpTemplateData("remark",
|
||||||
|
"感谢您的支付!\n请前往景区出示订单完成体验。\n如有问题请联系客服。"));
|
||||||
|
|
||||||
|
// 发送模板消息
|
||||||
|
wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
|
||||||
|
|
||||||
|
log.info("发送支付成功通知成功: orderNo={}, openId={}, amount={}",
|
||||||
|
orderNo, openId, amount);
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("发送支付成功通知失败: orderNo={}, openId={}", orderNo, openId, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -83,3 +83,8 @@ aliyun:
|
|||||||
region: cn-beijing
|
region: cn-beijing
|
||||||
bucket: goflyapp # 使用实际存在的bucket名称
|
bucket: goflyapp # 使用实际存在的bucket名称
|
||||||
|
|
||||||
|
wx:
|
||||||
|
mp:
|
||||||
|
templateId:
|
||||||
|
paymentSuccess: 你的支付成功通知模板ID
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user