【优化】SYSTEM: 根据代码评审优化订阅消息

This commit is contained in:
puhui999
2024-07-30 12:19:31 +08:00
parent 345f41b153
commit f10840afb8
12 changed files with 131 additions and 257 deletions

View File

@ -11,19 +11,4 @@ public interface MessageTemplateConstants {
String PAY_WALLET_CHANGE = "充值成功通知";
// TODO @puhui999这种建议不枚举直接写~嘿嘿。
/**
* 充值成功通知模版参数
*
* @author HUIHUI
*/
class PayWalletChangeTemplateParams {
public static final String NO = "character_string1"; // 流水编号
public static final String PRICE = "amount2"; // 充值金额
public static final String PAY_TIME = "time3"; // 充值时间
public static final String STATUS = "phrase4"; // 充值状态
}
}

View File

@ -1 +0,0 @@
package cn.iocoder.yudao.module.pay.message;

View File

@ -1,56 +0,0 @@
package cn.iocoder.yudao.module.pay.message.subscribe;
import cn.iocoder.yudao.module.system.api.social.SocialClientApi;
import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.util.Map;
import static cn.iocoder.yudao.module.pay.enums.MessageTemplateConstants.PAY_WALLET_CHANGE;
// TODO @puhui999建议可以先直接调用不要新建一个 client。
/**
* 订阅消息
*
* @author HUIHUI
*/
@Component
@Slf4j
public class SubscribeMessageClient {
public static final String WALLET_MONEY_PATH = "pages/user/wallet/money"; // 钱包详情页
@Resource
public SocialClientApi socialClientApi;
/**
* 发送钱包充值通知
*
* @param messages 消息
* @param userType 用户类型
* @param userId 用户编号
*/
@Async
public void sendPayWalletChangeMessage(Map<String, String> messages, Integer userType, Long userId) {
sendWxMessage(PAY_WALLET_CHANGE, messages, userType, userId, WALLET_MONEY_PATH);
}
/**
* 发送微信订阅消息
*
* @param templateTitle 模版标题
* @param messages 消息
* @param userType 用户类型
* @param userId 用户编号
* @param path 点击模板卡片后的跳转页面,仅限本小程序内的页面
*/
private void sendWxMessage(String templateTitle, Map<String, String> messages, Integer userType, Long userId,
String path) {
socialClientApi.sendSubscribeMessage(templateTitle, messages, userType, userId, SocialTypeEnum.WECHAT_MINI_APP.getType(), path);
}
}

View File

@ -2,7 +2,7 @@ package cn.iocoder.yudao.module.pay.service.wallet;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.pay.core.enums.refund.PayRefundStatusRespEnum;
@ -15,21 +15,22 @@ import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletDO;
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletRechargeDO;
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletRechargePackageDO;
import cn.iocoder.yudao.module.pay.dal.mysql.wallet.PayWalletRechargeMapper;
import cn.iocoder.yudao.module.pay.enums.MessageTemplateConstants;
import cn.iocoder.yudao.module.pay.enums.order.PayOrderStatusEnum;
import cn.iocoder.yudao.module.pay.enums.refund.PayRefundStatusEnum;
import cn.iocoder.yudao.module.pay.enums.wallet.PayWalletBizTypeEnum;
import cn.iocoder.yudao.module.pay.message.subscribe.SubscribeMessageClient;
import cn.iocoder.yudao.module.pay.service.order.PayOrderService;
import cn.iocoder.yudao.module.pay.service.refund.PayRefundService;
import cn.iocoder.yudao.module.system.api.social.SocialClientApi;
import cn.iocoder.yudao.module.system.api.social.dto.SocialWxSubscribeMessageSendReqDTO;
import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.Objects;
import static cn.hutool.core.util.ObjectUtil.notEqual;
@ -39,6 +40,7 @@ import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString
import static cn.iocoder.yudao.framework.common.util.number.MoneyUtils.fenToYuanStr;
import static cn.iocoder.yudao.module.pay.convert.wallet.PayWalletRechargeConvert.INSTANCE;
import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.*;
import static cn.iocoder.yudao.module.pay.enums.MessageTemplateConstants.PAY_WALLET_CHANGE;
import static cn.iocoder.yudao.module.pay.enums.refund.PayRefundStatusEnum.*;
/**
@ -57,6 +59,8 @@ public class PayWalletRechargeServiceImpl implements PayWalletRechargeService {
private static final String WALLET_RECHARGE_ORDER_SUBJECT = "钱包余额充值";
public static final String WALLET_MONEY_PATH = "pages/user/wallet/money"; // 钱包详情页
@Resource
private PayWalletRechargeMapper walletRechargeMapper;
@Resource
@ -68,7 +72,7 @@ public class PayWalletRechargeServiceImpl implements PayWalletRechargeService {
@Resource
private PayWalletRechargePackageService payWalletRechargePackageService;
@Resource
private SubscribeMessageClient subscribeMessageClient;
public SocialClientApi socialClientApi;
@Override
@Transactional(rollbackFor = Exception.class)
@ -136,21 +140,21 @@ public class PayWalletRechargeServiceImpl implements PayWalletRechargeService {
PayWalletBizTypeEnum.RECHARGE, walletRecharge.getTotalPrice());
// 4. 发送订阅消息
sendPayWalletChangeMessage(payOrderId, walletRecharge);
getSelf().sendPayWalletChangeMessage(payOrderId, walletRecharge);
}
// TODO @puhui999发送使用异步发送@Async
private void sendPayWalletChangeMessage(Long payOrderId, PayWalletRechargeDO walletRecharge) {
@Async
public void sendPayWalletChangeMessage(Long payOrderId, PayWalletRechargeDO walletRecharge) {
// 1. 获得会员钱包信息
PayWalletDO wallet = payWalletService.getWallet(walletRecharge.getWalletId());
// TODO @puhui999可以使用 MapUtil.builder();另外,不应该是并发 hashmap 哈
Map<String, String> messages = MapUtil.newConcurrentHashMap(4);
messages.put(MessageTemplateConstants.PayWalletChangeTemplateParams.NO, String.valueOf(payOrderId));
messages.put(MessageTemplateConstants.PayWalletChangeTemplateParams.PRICE,
fenToYuanStr(walletRecharge.getTotalPrice()));
messages.put(MessageTemplateConstants.PayWalletChangeTemplateParams.STATUS, "充值成功");
messages.put(MessageTemplateConstants.PayWalletChangeTemplateParams.PAY_TIME,
LocalDateTimeUtil.formatNormal(LocalDateTime.now()));
subscribeMessageClient.sendPayWalletChangeMessage(messages, wallet.getUserType(), wallet.getUserId());
// 2. 构建并发送模版消息
socialClientApi.sendSubscribeMessage(new SocialWxSubscribeMessageSendReqDTO().setPage(WALLET_MONEY_PATH)
.setUserId(wallet.getUserId()).setUserType(wallet.getUserType()).setTemplateTitle(PAY_WALLET_CHANGE)
.setSocialType(SocialTypeEnum.WECHAT_MINI_APP.getType())
// 添加模版消息
.addMessage("phrase4", "充值成功").addMessage("character_string1", String.valueOf(payOrderId))
.addMessage("amount2", fenToYuanStr(walletRecharge.getTotalPrice()))
.addMessage("time3", LocalDateTimeUtil.formatNormal(LocalDateTime.now())));
}
@Override
@ -307,4 +311,13 @@ public class PayWalletRechargeServiceImpl implements PayWalletRechargeService {
return payOrder;
}
/**
* 获得自身的代理对象,解决 AOP 生效问题
*
* @return 自己
*/
private PayWalletRechargeServiceImpl getSelf() {
return SpringUtil.getBean(getClass());
}
}