fix(服务通知):实现小程序订阅号通知逻辑,订单支付成功通知,订单排队提醒,一键成片完成提醒
This commit is contained in:
+38
-3
@@ -2,8 +2,10 @@ package com.central.order.controller;
|
||||
|
||||
import com.central.common.model.PageResult;
|
||||
import com.central.common.model.Result;
|
||||
import com.central.common.model.SysUser;
|
||||
import com.central.order.constants.EditTaskStatus;
|
||||
import com.central.order.enums.OrderStatus;
|
||||
import com.central.order.feign.UserCenterService;
|
||||
import com.central.order.model.EditTask;
|
||||
import com.central.order.model.Order;
|
||||
import com.central.order.model.OrderFile;
|
||||
@@ -41,6 +43,11 @@ public class AutoCutVideoController {
|
||||
private IVideoEditService videoEditService;
|
||||
@Autowired
|
||||
private IQrCodeService qrCodeService;
|
||||
@Autowired
|
||||
private WxMiniSubscribeService wxMiniSubscribeService;
|
||||
|
||||
@Autowired
|
||||
private UserCenterService userCenterService;
|
||||
|
||||
@Operation(summary = "根据订单号生成视频")
|
||||
@PostMapping("/generate")
|
||||
@@ -114,7 +121,7 @@ public class AutoCutVideoController {
|
||||
);
|
||||
|
||||
// 发送通知
|
||||
notifyUser(task.getUserId().toString(), "视频制作完成");
|
||||
notifyUser(task.getUserId().toString(), task.getOrderSn());
|
||||
}
|
||||
// 4. 处理失败回调
|
||||
else {
|
||||
@@ -158,8 +165,36 @@ public class AutoCutVideoController {
|
||||
return false; // Placeholder return, actual implementation needed
|
||||
}
|
||||
|
||||
private void notifyUser(String userId, String message) {
|
||||
// Implementation of notifyUser method
|
||||
private void notifyUser(String userId,String orderNo) {
|
||||
try {
|
||||
// 1. 获取用户信息
|
||||
SysUser user = userCenterService.findUserById(Long.valueOf(userId));
|
||||
if (user == null || user.getOpenId() == null) {
|
||||
log.warn("未找到用户或用户未绑定小程序openId,无法发送成片完成通知: userId={}", userId);
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. 获取任务和订单信息
|
||||
// 假设 message 里包含订单号或任务名,实际可根据 EditTask 查找
|
||||
Order order = orderService.getOrderByNumber(orderNo);
|
||||
if (order == null) {
|
||||
log.warn("未找到订单信息,无法发送成片完成通知: orderNo={}", orderNo);
|
||||
return;
|
||||
}
|
||||
|
||||
String taskName = "AI一键成片";
|
||||
String result = "您的视频已制作完成,可前往相册页查看和下载。";
|
||||
|
||||
wxMiniSubscribeService.sendVideoEditCompleteMsg(
|
||||
user.getOpenId(),
|
||||
taskName,
|
||||
orderNo,
|
||||
result
|
||||
);
|
||||
log.info("已发送一键成片完成通知: userId={}, openId={}, orderNo={}", userId, user.getOpenId(), orderNo);
|
||||
} catch (Exception e) {
|
||||
log.error("发送一键成片完成通知失败: userId={}, message={}", userId, orderNo, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+60
-2
@@ -46,6 +46,8 @@ public class OrderStatusChangeListener {
|
||||
private UserCenterService userCenterService;
|
||||
@Autowired
|
||||
private IEditTaskService editTaskService;
|
||||
@Autowired
|
||||
private WxMiniSubscribeService wxMiniSubscribeService;
|
||||
|
||||
@EventListener
|
||||
@Async
|
||||
@@ -167,6 +169,22 @@ public class OrderStatusChangeListener {
|
||||
);
|
||||
}
|
||||
|
||||
// 发送小程序订阅消息
|
||||
if (sysUser.getOpenId() != null) {
|
||||
String payTime = order.getPayTime() != null ? order.getPayTime().toString() : "";
|
||||
String tip = "您的订单已支付成功,您当前排在第"+queue+"位,欢迎体验!";
|
||||
String amount = yuanToFen(order.getOrderAmount());
|
||||
String orderName = tourism.getTourismName();
|
||||
wxMiniSubscribeService.sendPaySuccessMsg(
|
||||
sysUser.getOpenId(),
|
||||
orderName,
|
||||
order.getOrderNo(),
|
||||
tip,
|
||||
amount,
|
||||
payTime
|
||||
);
|
||||
}
|
||||
|
||||
// 2. 记录支付成功日志
|
||||
log.info("订单支付成功: orderNo={}, amount={}, tourismName={}",
|
||||
order.getOrderNo(), order.getOrderAmount(), tourism.getTourismName());
|
||||
@@ -206,9 +224,29 @@ public class OrderStatusChangeListener {
|
||||
task.setStatus(EditTaskStatus.PROCESSING);
|
||||
editTaskService.saveOrUpdateTask(task);
|
||||
|
||||
// 新增:查找下一个排队用户
|
||||
Order nextOrder = orderService.getNextQueueOrder(order.getTourismId(), order.getId());
|
||||
if (nextOrder != null) {
|
||||
SysUser nextUser = userCenterService.findUserById(nextOrder.getUserId());
|
||||
Tourism tourism = tourismService.getTourismById(nextOrder.getTourismId());
|
||||
if (nextUser != null && nextUser.getOpenId() != null && tourism != null) {
|
||||
String place = tourism.getTourismAddress(); // 活动地点
|
||||
String activityName = tourism.getTourismName(); // 活动名称
|
||||
String remark = "请您准备,马上轮到您啦!";
|
||||
String activityNo = nextOrder.getOrderNo(); // 活动编号
|
||||
wxMiniSubscribeService.sendActivityStartMsg(
|
||||
nextUser.getOpenId(),
|
||||
place,
|
||||
activityName,
|
||||
remark,
|
||||
activityNo
|
||||
);
|
||||
log.info("已通知下一个排队用户: userId={}, openId={}, orderNo={}", nextUser.getId(), nextUser.getOpenId(), activityNo);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
// 这里只记录错误,不抛出异常,避免影响订单状态变更的主流程
|
||||
log.error("触发一键成片失败: orderNo={}", order.getOrderNo(), e);
|
||||
log.error("触发一键成片或排队通知失败: orderNo={}", order.getOrderNo(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -221,4 +259,24 @@ public class OrderStatusChangeListener {
|
||||
&& qrCode.getEnableVideoEdit() == 1
|
||||
&& qrCode.getTemplateId() != null; // 确保有模板ID
|
||||
}
|
||||
|
||||
private String yuanToFen(String orderAmountStr) {
|
||||
if (orderAmountStr != null && !orderAmountStr.isEmpty()) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
int length = orderAmountStr.length();
|
||||
if (length >= 3) {
|
||||
// 字符串长度大于等于 3,正常插入小数点
|
||||
result.append(orderAmountStr, 0, length - 2).append(".").append(orderAmountStr.substring(length - 2));
|
||||
} else if (length == 2) {
|
||||
// 字符串长度为 2,前面补 0
|
||||
result.append("0.").append(orderAmountStr);
|
||||
} else {
|
||||
// 字符串长度为 1,前面补 0
|
||||
result.append("0.0").append(orderAmountStr);
|
||||
}
|
||||
return result.toString() + "元";
|
||||
} else {
|
||||
return "0.00元";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,5 +86,7 @@ public interface IOrderService extends ISuperService<Order> {
|
||||
void autoUpdateOrderStatusToComplete();
|
||||
|
||||
Long getUnCompleteOrderCount(String deviceSn);
|
||||
|
||||
Order getNextQueueOrder(Long tourismId, Long currentOrderId);
|
||||
}
|
||||
|
||||
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.central.order.service;
|
||||
|
||||
public interface WxMiniSubscribeService {
|
||||
/**
|
||||
* 支付成功后发送小程序订阅消息
|
||||
* @param openId 用户openid
|
||||
* @param orderName 订单名称
|
||||
* @param orderNo 订单编号
|
||||
* @param tip 温馨提示
|
||||
* @param amount 已付金额
|
||||
* @param orderTime 下单时间
|
||||
*/
|
||||
void sendPaySuccessMsg(String openId, String orderName, String orderNo, String tip, String amount, String orderTime);
|
||||
|
||||
/**
|
||||
* 活动即将开始提醒
|
||||
*/
|
||||
void sendActivityStartMsg(String openId, String place, String activityName, String remark, String activityNo);
|
||||
|
||||
/**
|
||||
* 一键成片制作完成通知
|
||||
*/
|
||||
void sendVideoEditCompleteMsg(String openId, String taskName, String orderNo, String result);
|
||||
}
|
||||
+20
@@ -292,5 +292,25 @@ public class OrderServiceImpl extends SuperServiceImpl<OrderMapper, Order> imple
|
||||
return tourismId + currentDate + formattedSequence;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Order getNextQueueOrder(Long tourismId, Long currentOrderId) {
|
||||
// 1. 查询当前订单
|
||||
Order currentOrder = baseMapper.selectById(currentOrderId);
|
||||
if (currentOrder == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 2. 查询下一个排队订单
|
||||
// 假设排队顺序按 createTime 升序,且 orderStatus=1(排队中)
|
||||
// 你可以根据实际业务调整 orderStatus 的值
|
||||
QueryWrapper<Order> wrapper = new QueryWrapper<>();
|
||||
wrapper.eq("tourism_id", tourismId)
|
||||
.eq("order_status", OrderStatus.PAID.getId()) // 1=排队中,具体值请根据你的枚举定义
|
||||
.gt("pay_time", currentOrder.getPayTime())
|
||||
.orderByAsc("pay_time")
|
||||
.last("limit 1");
|
||||
|
||||
return baseMapper.selectOne(wrapper);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+116
@@ -0,0 +1,116 @@
|
||||
package com.central.order.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.central.order.service.WxMiniSubscribeService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class WxMiniSubscribeServiceImpl implements WxMiniSubscribeService {
|
||||
|
||||
@Value("${wx.mini.app-id}")
|
||||
private String appId;
|
||||
@Value("${wx.mini.secret}")
|
||||
private String secret;
|
||||
@Value("${wx.mini.template.pay-success}")
|
||||
private String templateId;
|
||||
@Value("${wx.mini.template.activity-start}")
|
||||
private String activityStartTemplateId;
|
||||
@Value("${wx.mini.template.video-edit-complete}")
|
||||
private String videoEditCompleteTemplateId;
|
||||
|
||||
@Resource
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
/**
|
||||
* 获取小程序access_token
|
||||
*/
|
||||
private String getAccessToken() {
|
||||
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential"
|
||||
+ "&appid=" + appId
|
||||
+ "&secret=" + secret;
|
||||
String resp = restTemplate.getForObject(url, String.class);
|
||||
JSONObject json = JSONObject.parseObject(resp);
|
||||
return json.getString("access_token");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendPaySuccessMsg(String openId, String orderName, String orderNo, String tip, String amount, String orderTime) {
|
||||
try {
|
||||
String accessToken = getAccessToken();
|
||||
String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken;
|
||||
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
data.put("thing1", Map.of("value", orderName)); // 订单名称
|
||||
data.put("character_string2", Map.of("value", orderNo)); // 订单编号
|
||||
data.put("thing4", Map.of("value", tip)); // 温馨提示
|
||||
data.put("amount9", Map.of("value", amount)); // 已付金额
|
||||
data.put("time10", Map.of("value", orderTime)); // 下单时间
|
||||
|
||||
Map<String, Object> body = new HashMap<>();
|
||||
body.put("touser", openId);
|
||||
body.put("template_id", templateId);
|
||||
body.put("data", data);
|
||||
// body.put("page", "pages/order/detail?orderNo=" + orderNo); // 可选跳转
|
||||
|
||||
String resp = restTemplate.postForObject(url, body, String.class);
|
||||
log.info("小程序支付成功订阅消息发送结果: {}", resp);
|
||||
} catch (Exception e) {
|
||||
log.error("小程序支付成功订阅消息发送失败: openId={}, orderNo={}, error={}", openId, orderNo, e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendActivityStartMsg(String openId, String place, String activityName, String remark, String activityNo) {
|
||||
try {
|
||||
String accessToken = getAccessToken();
|
||||
String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken;
|
||||
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
data.put("thing4", Map.of("value", place)); // 活动地点
|
||||
data.put("thing1", Map.of("value", activityName)); // 活动名称
|
||||
data.put("thing5", Map.of("value", remark)); // 备注
|
||||
data.put("character_string10", Map.of("value", activityNo)); // 活动编号
|
||||
|
||||
Map<String, Object> body = new HashMap<>();
|
||||
body.put("touser", openId);
|
||||
body.put("template_id", activityStartTemplateId);
|
||||
body.put("data", data);
|
||||
|
||||
String resp = restTemplate.postForObject(url, body, String.class);
|
||||
log.info("小程序活动即将开始提醒发送结果: {}", resp);
|
||||
} catch (Exception e) {
|
||||
log.error("小程序活动即将开始提醒发送失败: openId={}, activityNo={}, error={}", openId, activityNo, e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendVideoEditCompleteMsg(String openId, String taskName, String orderNo, String result) {
|
||||
try {
|
||||
String accessToken = getAccessToken();
|
||||
String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken;
|
||||
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
data.put("thing7", Map.of("value", taskName)); // 任务名称
|
||||
data.put("character_string13", Map.of("value", orderNo)); // 订单编号
|
||||
data.put("thing14", Map.of("value", result)); // 制作结果
|
||||
|
||||
Map<String, Object> body = new HashMap<>();
|
||||
body.put("touser", openId);
|
||||
body.put("template_id", videoEditCompleteTemplateId);
|
||||
body.put("data", data);
|
||||
|
||||
String resp = restTemplate.postForObject(url, body, String.class);
|
||||
log.info("小程序一键成片完成通知发送结果: {}", resp);
|
||||
} catch (Exception e) {
|
||||
log.error("小程序一键成片完成通知发送失败: openId={}, orderNo={}, error={}", openId, orderNo, e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -91,10 +91,18 @@ aliyun:
|
||||
|
||||
wx:
|
||||
mp:
|
||||
app-id: wx0ea85b13966d88e1
|
||||
secret: a90637566b2b73f41f8b1dabd64dc32b
|
||||
app-id: wx0ea85b13966d88e1 # 服务号应用id
|
||||
secret: a90637566b2b73f41f8b1dabd64dc32b # 服务号应用密钥
|
||||
template-id:
|
||||
payment-success: uscmjgLdw0cKBRq8vFyCOPbYj7p-fHuEOIhFwwIAe4A # 支付成功通知模板ID
|
||||
video-complete: TEMPLATE_ID_2 # 视频完成通知模板ID
|
||||
video-failure: TEMPLATE_ID_3 # 视频失败通知模板ID
|
||||
|
||||
mini:
|
||||
app-id: ${zlt.wxpay.appId}
|
||||
secret: ${zlt.security.tencent.app-secret}
|
||||
template:
|
||||
pay-success: nSy7NGq1443YCfhgR81fgkeOVv9cSM5Ysp3nMoQYVpc
|
||||
activity-start: lgwkRnbdDc3mEwk3RMob43LpLHnLfglZtTkbFRPsPCA
|
||||
order-complete: ZeIlAA6ImQLxBiLmo-53lM7wkL4KnuKKTGKWBZwYAdM
|
||||
video-edit-complete: IXpL-Voztm3GM-zFj3sxA39qy8oRtAcxTiT7_Qsyz9Q
|
||||
|
||||
Reference in New Issue
Block a user