【功能新增】微信小程序的订阅消息

This commit is contained in:
YunaiV
2024-08-01 13:08:35 +08:00
parent e5c1ce7640
commit 3ceb4d4685
41 changed files with 548 additions and 80 deletions

View File

@@ -0,0 +1,14 @@
package cn.iocoder.yudao.module.pay.enums;
/**
* 通知模板枚举类
*
* @author HUIHUI
*/
public interface MessageTemplateConstants {
// ======================= 小程序订阅消息 =======================
String WXA_WALLET_RECHARGER_PAID = "充值成功通知";
}

View File

@@ -1,7 +1,10 @@
package cn.iocoder.yudao.module.pay.framework.rpc.config;
import cn.iocoder.yudao.module.system.api.social.SocialClientApi;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Configuration;
@Configuration(proxyBeanMethods = false)
@EnableFeignClients(clients = {SocialClientApi.class})
public class RpcConfiguration {
}

View File

@@ -1,6 +1,8 @@
package cn.iocoder.yudao.module.pay.service.wallet;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.lang.Assert;
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;
@@ -18,8 +20,11 @@ import cn.iocoder.yudao.module.pay.enums.refund.PayRefundStatusEnum;
import cn.iocoder.yudao.module.pay.enums.wallet.PayWalletBizTypeEnum;
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.SocialWxaSubscribeMessageSendReqDTO;
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;
@@ -31,8 +36,10 @@ import static cn.hutool.core.util.ObjectUtil.notEqual;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.addTime;
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.WXA_WALLET_RECHARGER_PAID;
import static cn.iocoder.yudao.module.pay.enums.refund.PayRefundStatusEnum.*;
/**
@@ -61,6 +68,8 @@ public class PayWalletRechargeServiceImpl implements PayWalletRechargeService {
private PayRefundService payRefundService;
@Resource
private PayWalletRechargePackageService payWalletRechargePackageService;
@Resource
public SocialClientApi socialClientApi;
@Override
@Transactional(rollbackFor = Exception.class)
@@ -96,7 +105,7 @@ public class PayWalletRechargeServiceImpl implements PayWalletRechargeService {
@Override
public PageResult<PayWalletRechargeDO> getWalletRechargePackagePage(Long userId, Integer userType,
PageParam pageReqVO, Boolean payStatus) {
PageParam pageReqVO, Boolean payStatus) {
PayWalletDO wallet = payWalletService.getOrCreateWallet(userId, userType);
return walletRechargeMapper.selectPage(pageReqVO, wallet.getId(), payStatus);
}
@@ -126,6 +135,24 @@ public class PayWalletRechargeServiceImpl implements PayWalletRechargeService {
// TODO 需要钱包中加个可提现余额
payWalletService.addWalletBalance(walletRecharge.getWalletId(), String.valueOf(id),
PayWalletBizTypeEnum.RECHARGE, walletRecharge.getTotalPrice());
// 4. 发送订阅消息
getSelf().sendWalletRechargerPaidMessage(payOrderId, walletRecharge);
}
@Async
public void sendWalletRechargerPaidMessage(Long payOrderId, PayWalletRechargeDO walletRecharge) {
// 1. 获得会员钱包信息
PayWalletDO wallet = payWalletService.getWallet(walletRecharge.getWalletId());
// 2. 构建并发送模版消息
socialClientApi.sendWxaSubscribeMessage(new SocialWxaSubscribeMessageSendReqDTO()
.setUserId(wallet.getUserId()).setUserType(wallet.getUserType())
.setTemplateTitle(WXA_WALLET_RECHARGER_PAID)
.setPage("pages/user/wallet/money") // 钱包详情界面
.addMessage("character_string1", String.valueOf(payOrderId)) // 支付单编号
.addMessage("amount2", fenToYuanStr(walletRecharge.getTotalPrice())) // 充值金额
.addMessage("time3", LocalDateTimeUtil.formatNormal(walletRecharge.getCreateTime())) // 充值时间
.addMessage("phrase4", "充值成功")); // 充值状态
}
@Override
@@ -282,4 +309,13 @@ public class PayWalletRechargeServiceImpl implements PayWalletRechargeService {
return payOrder;
}
/**
* 获得自身的代理对象,解决 AOP 生效问题
*
* @return 自己
*/
private PayWalletRechargeServiceImpl getSelf() {
return SpringUtil.getBean(getClass());
}
}