fix(小程序通知):小程序通知使用同一个模板进行消息发送

This commit is contained in:
2025-05-12 10:15:53 +08:00
parent 5789b9bacd
commit 8fc80be482
6 changed files with 91 additions and 69 deletions
@@ -6,13 +6,11 @@ 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;
import com.central.order.model.QrCode;
import com.central.order.model.*;
import com.central.order.model.dto.CallbackDTO;
import com.central.order.model.params.EditTaskQueryParams;
import com.central.order.service.*;
import com.central.order.util.StringUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
@@ -49,6 +47,9 @@ public class AutoCutVideoController {
@Autowired
private UserCenterService userCenterService;
@Autowired
private ITourismService tourismService;
@Operation(summary = "根据订单号生成视频")
@PostMapping("/generate")
public Result generateVideo(@RequestParam String orderNo) {
@@ -182,14 +183,16 @@ public class AutoCutVideoController {
return;
}
String taskName = "AI一键成片";
String result = "您的视频已制作完成,可前往相册页查看和下载。";
Tourism tourism = tourismService.getTourismById(order.getTourismId());
String result = "您的视频已制作完成,可前往相册页查看和下载!";
wxMiniSubscribeService.sendVideoEditCompleteMsg(
wxMiniSubscribeService.sendPaySuccessMsg(
user.getOpenId(),
taskName,
tourism.getTourismName(),
orderNo,
result
result,
StringUtils.fenToYuan(order.getOrderAmount()),
order.getPayTime().toString()
);
log.info("已发送一键成片完成通知: userId={}, openId={}, orderNo={}", userId, user.getOpenId(), orderNo);
} catch (Exception e) {
@@ -10,6 +10,7 @@ import com.central.order.event.OrderStatusChangeEvent;
import com.central.order.feign.UserCenterService;
import com.central.order.model.*;
import com.central.order.service.*;
import com.central.order.util.StringUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@@ -160,20 +161,12 @@ public class OrderStatusChangeListener {
SysUser sysUser = userCenterService.findUserById(order.getUserId());
// 2. 查询订单排队信息
Integer queue = orderService.getOrderRankingByOrderId(order.getId(),tourism.getId());
if (sysUser.getUnionId() != null) {
wxTemplateMessageService.sendPaymentSuccessNotice(
sysUser.getUnionId(),
order.getOrderNo(),
tourism.getTourismName(),
queue
);
}
// 发送小程序订阅消息
if (sysUser.getOpenId() != null) {
String payTime = order.getPayTime() != null ? order.getPayTime().toString() : "";
String tip = "您的订单已支付成功,您当前排在第"+queue+"位,欢迎体验!";
String amount = yuanToFen(order.getOrderAmount());
String amount = StringUtils.fenToYuan(order.getOrderAmount());
String orderName = tourism.getTourismName();
wxMiniSubscribeService.sendPaySuccessMsg(
sysUser.getOpenId(),
@@ -230,16 +223,16 @@ public class OrderStatusChangeListener {
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 remark = "请您准备,当前排队序列的下一位就轮到您啦!";
String activityNo = nextOrder.getOrderNo(); // 活动编号
wxMiniSubscribeService.sendActivityStartMsg(
wxMiniSubscribeService.sendPaySuccessMsg(
nextUser.getOpenId(),
place,
activityName,
activityNo,
remark,
activityNo
activityNo,
nextOrder.getPayTime().toString()
);
log.info("已通知下一个排队用户: userId={}, openId={}, orderNo={}", nextUser.getId(), nextUser.getOpenId(), activityNo);
}
@@ -259,24 +252,4 @@ 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元";
}
}
}
@@ -1,8 +1,9 @@
package com.central.order.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.central.order.service.IWxAccessTokenService;
import com.central.order.service.WxMiniSubscribeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@@ -15,10 +16,6 @@ import java.util.Map;
@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}")
@@ -29,24 +26,17 @@ public class WxMiniSubscribeServiceImpl implements WxMiniSubscribeService {
@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");
}
@Autowired
private IWxAccessTokenService wxAccessTokenService;
@Override
public void sendPaySuccessMsg(String openId, String orderName, String orderNo, String tip, String amount, String orderTime) {
try {
String accessToken = getAccessToken();
String accessToken = wxAccessTokenService.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)); // 订单编号
@@ -70,7 +60,7 @@ public class WxMiniSubscribeServiceImpl implements WxMiniSubscribeService {
@Override
public void sendActivityStartMsg(String openId, String place, String activityName, String remark, String activityNo) {
try {
String accessToken = getAccessToken();
String accessToken = wxAccessTokenService.getAccessToken();
String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken;
Map<String, Object> data = new HashMap<>();
@@ -94,7 +84,7 @@ public class WxMiniSubscribeServiceImpl implements WxMiniSubscribeService {
@Override
public void sendVideoEditCompleteMsg(String openId, String taskName, String orderNo, String result) {
try {
String accessToken = getAccessToken();
String accessToken = wxAccessTokenService.getAccessToken();
String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken;
Map<String, Object> data = new HashMap<>();
@@ -1,11 +1,11 @@
package com.central.order.utils;
package com.central.order.util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
@Slf4j
public class ExceptionUtils {
/**
* 获取异常的根本原因
*/
@@ -13,7 +13,7 @@ public class ExceptionUtils {
Throwable rootCause = getRootCause(throwable);
return rootCause.getMessage();
}
/**
* 获取异常的根本原因对象
*/
@@ -24,7 +24,7 @@ public class ExceptionUtils {
}
return cause;
}
/**
* 格式化异常信息
*/
@@ -37,7 +37,7 @@ public class ExceptionUtils {
// 限制长度
return message.length() > 200 ? message.substring(0, 200) : message;
}
/**
* 判断是否为业务异常
*/
@@ -48,4 +48,4 @@ public class ExceptionUtils {
|| message.contains("SIGN_ERROR")
|| message.contains("INVALID_REQUEST");
}
}
}
@@ -0,0 +1,29 @@
package com.central.order.util;
/**
* 字符串处理
* @date
* @author lsl
*/
public class StringUtils {
public static String fenToYuan(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元";
}
}
}
+28 -1
View File
@@ -19,7 +19,10 @@ server {
# 添加必要的跨域和缓存控制头
add_header Access-Control-Allow-Origin *;
add_header Cache-Control "no-cache, no-store, must-revalidate";
# 调整缓存策略,例如对于静态资源可以缓存
location ~* \.(css|js|svg|png|jpg|jpeg)$ {
expires 365d;
}
# 添加安全相关头
add_header X-Frame-Options SAMEORIGIN;
@@ -37,6 +40,14 @@ server {
# 确保 Authorization 头可以传递
proxy_set_header Authorization $http_authorization;
proxy_pass_header Authorization;
# 处理 OPTIONS 请求
if ($request_method = 'OPTIONS') {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
return 204;
}
}
# 用户服务代理
@@ -49,6 +60,14 @@ server {
# 确保 Authorization 头可以传递
proxy_set_header Authorization $http_authorization;
proxy_pass_header Authorization;
# 处理 OPTIONS 请求
if ($request_method = 'OPTIONS') {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
return 204;
}
}
# 其他 API 服务代理(如果需要)
@@ -75,5 +94,13 @@ server {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 处理 OPTIONS 请求
if ($request_method = 'OPTIONS') {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
return 204;
}
}
}