From 1dec9d2035061381eb53a6f487ad7dedc01239f7 Mon Sep 17 00:00:00 2001 From: Iamasoldier <1272745473@qq.com> Date: Tue, 3 Jun 2025 15:56:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=8A=9F=E8=83=BD=E6=94=AF=E4=BB=98?= =?UTF-8?q?=E7=B1=BB=E6=A8=A1=E5=9D=97=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../order/config/TencentPayConfiguration.java | 15 +- .../central/order/service/PaymentService.java | 196 +++++++++--------- .../controller/SysWechatConfigController.java | 26 +-- .../user/mapper/WechatConfigMapper.java | 2 +- .../user/service/PrivateKeyService.java | 37 ---- .../user/service/WechatConfigService.java | 2 +- .../user/service/WechatPayService.java | 93 --------- .../service/impl/PrivateKeyServiceImpl.java | 61 ------ .../service/impl/WechatConfigServiceImpl.java | 2 +- .../service/impl/WechatPayServiceImpl.java | 154 -------------- .../{wechat => }/config/WechatProperties.java | 2 +- .../{wechat => }/entity/WechatConfig.java | 7 +- .../feign/WechatConfigService.java | 16 +- .../WechatConfigServiceFallbackFactory.java | 11 +- .../wechat/config/WxPayConfigFactory.java | 39 ---- .../wechat/feign/PrivateKeyService.java | 25 --- .../PrivateKeyServiceFallbackFactory.java | 25 --- ...ot.autoconfigure.AutoConfiguration.imports | 3 +- .../wechat/config/PrivateKeyUtils.java | 34 +++ .../config/WechatAutoConfiguration.java | 138 ++++++++++++ .../wechat/config/WxPayConfigFactory.java | 107 ++++++++++ .../config/WechatAutoConfiguration.java | 166 --------------- .../zlt/wechat/config/WxPayConfigFactory.java | 42 ---- .../main/resources/META-INF/spring.factories | 2 - ...ot.autoconfigure.AutoConfiguration.imports | 1 + zlt-uaa/pom.xml | 5 +- .../oauth/service/ZltUserDetailsService.java | 2 +- .../service/impl/UserDetailServiceImpl.java | 4 +- .../TencentAuthenticationConverter.java | 8 +- .../TencentAuthenticationProvider.java | 2 +- zlt-uaa/src/main/resources/application.yml | 4 +- 31 files changed, 433 insertions(+), 798 deletions(-) delete mode 100644 zlt-business/user-center/src/main/java/com/central/user/service/PrivateKeyService.java delete mode 100644 zlt-business/user-center/src/main/java/com/central/user/service/WechatPayService.java delete mode 100644 zlt-business/user-center/src/main/java/com/central/user/service/impl/PrivateKeyServiceImpl.java delete mode 100644 zlt-business/user-center/src/main/java/com/central/user/service/impl/WechatPayServiceImpl.java rename zlt-commons/zlt-wechat-common-core/src/main/java/com/central/wechat/{wechat => }/config/WechatProperties.java (98%) rename zlt-commons/zlt-wechat-common-core/src/main/java/com/central/wechat/{wechat => }/entity/WechatConfig.java (94%) rename zlt-commons/zlt-wechat-common-core/src/main/java/com/central/wechat/{wechat => }/feign/WechatConfigService.java (61%) rename zlt-commons/zlt-wechat-common-core/src/main/java/com/central/wechat/{wechat => }/feign/fallback/WechatConfigServiceFallbackFactory.java (71%) delete mode 100644 zlt-commons/zlt-wechat-common-core/src/main/java/com/central/wechat/wechat/config/WxPayConfigFactory.java delete mode 100644 zlt-commons/zlt-wechat-common-core/src/main/java/com/central/wechat/wechat/feign/PrivateKeyService.java delete mode 100644 zlt-commons/zlt-wechat-common-core/src/main/java/com/central/wechat/wechat/feign/fallback/PrivateKeyServiceFallbackFactory.java create mode 100644 zlt-commons/zlt-wechat-spring-boot-starter/src/main/java/com/central/wechat/config/PrivateKeyUtils.java create mode 100644 zlt-commons/zlt-wechat-spring-boot-starter/src/main/java/com/central/wechat/config/WechatAutoConfiguration.java create mode 100644 zlt-commons/zlt-wechat-spring-boot-starter/src/main/java/com/central/wechat/config/WxPayConfigFactory.java delete mode 100644 zlt-commons/zlt-wechat-spring-boot-starter/src/main/java/com/zlt/wechat/config/WechatAutoConfiguration.java delete mode 100644 zlt-commons/zlt-wechat-spring-boot-starter/src/main/java/com/zlt/wechat/config/WxPayConfigFactory.java delete mode 100644 zlt-commons/zlt-wechat-spring-boot-starter/src/main/resources/META-INF/spring.factories create mode 100644 zlt-commons/zlt-wechat-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports diff --git a/zlt-business/order-center/src/main/java/com/central/order/config/TencentPayConfiguration.java b/zlt-business/order-center/src/main/java/com/central/order/config/TencentPayConfiguration.java index e90f10e2..80c894ae 100644 --- a/zlt-business/order-center/src/main/java/com/central/order/config/TencentPayConfiguration.java +++ b/zlt-business/order-center/src/main/java/com/central/order/config/TencentPayConfiguration.java @@ -8,14 +8,9 @@ import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension; import com.wechat.pay.java.service.payments.jsapi.model.SettleInfo; import com.wechat.pay.java.service.profitsharing.ProfitsharingService; import com.wechat.pay.java.service.refund.RefundService; -import lombok.Getter; -import lombok.Setter; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.Executor; @@ -28,11 +23,11 @@ import java.util.concurrent.ThreadPoolExecutor; * @date 2025-01-14 * */ -@Getter -@Setter -@Configuration -@ConfigurationProperties(prefix = "tencent") -@EnableConfigurationProperties(ProfitSharingProperties.class) +//@Getter +//@Setter +//@Configuration +//@ConfigurationProperties(prefix = "tencent") +//@EnableConfigurationProperties(ProfitSharingProperties.class) public class TencentPayConfiguration { private String merchantId; diff --git a/zlt-business/order-center/src/main/java/com/central/order/service/PaymentService.java b/zlt-business/order-center/src/main/java/com/central/order/service/PaymentService.java index cfa28438..845909de 100644 --- a/zlt-business/order-center/src/main/java/com/central/order/service/PaymentService.java +++ b/zlt-business/order-center/src/main/java/com/central/order/service/PaymentService.java @@ -1,98 +1,98 @@ -//package com.central.order.service; -// -//import com.central.common.context.LoginUserContextHolder; -//import com.central.common.exception.BusinessException; -//import com.central.common.model.SysUser; -//import com.central.order.feign.UserCenterService; -//import com.central.order.model.Order; -//import com.central.order.model.QrCode; -//import com.central.wechat.wechat.entity.WechatConfig; -//import com.central.wechat.wechat.feign.WechatConfigService; -//import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension; -//import com.wechat.pay.java.service.payments.jsapi.model.*; -//import com.zlt.wechat.config.WxPayConfigFactory; -//import lombok.extern.slf4j.Slf4j; -//import org.springframework.beans.factory.annotation.Autowired; -//import org.springframework.stereotype.Service; -// -//@Slf4j -//@Service -//public class PaymentService { -// @Autowired -// private WxPayConfigFactory wxPayConfigFactory; -// @Autowired -// private UserCenterService userCenterService; -// @Autowired -// private IOrderService orderService; -// @Autowired -// private IQrCodeService qrCodeService; -// @Autowired -// private IProfitSharingService profitSharingService; -// @Autowired -// private WechatConfigService configService; -// -// -// -// public PrepayWithRequestPaymentResponse createPayment(Long orderId) { -// // 1. 获取用户和订单信息 -// SysUser user = userCenterService.findUserById(LoginUserContextHolder.getUser().getId()); -// Order order = orderService.getById(orderId); -// if (order == null) { -// throw new BusinessException("订单数据不存在"); -// } -// -// // 2. 获取二维码信息 -// QrCode qrCode = qrCodeService.getByCodeId(order.getCodeId()); -// if (qrCode == null) { -// throw new BusinessException("二维码不存在"); -// } -// -// // 3. 创建支付请求 -// PrepayRequest request = new PrepayRequest(); -// -// // 设置基本支付信息 -// Amount amount = new Amount(); -// amount.setTotal(Integer.parseInt(order.getOrderAmount())); -// request.setAmount(amount); -// -// // 从二维码配置中获取支付配置 -// WechatConfig payConfig = configService.getConfig("PAY", qrCode.getTenantId()); -// request.setAppid(payConfig.getAppId()); -// request.setMchid(payConfig.getMerchantId()); -// request.setDescription(order.getOrderNo()); -// request.setNotifyUrl(payConfig.getNotifyUrl()); -// request.setOutTradeNo(order.getOrderNo()); -// -// // 设置支付者信息 -// Payer payer = new Payer(); -// payer.setOpenid(user.getOpenId()); -// request.setPayer(payer); -// -// // 设置分账信息 -// SettleInfo settleInfo = new SettleInfo(); -// if (isEnableProfit(qrCode)) { -// settleInfo.setProfitSharing(true); -// request.setSettleInfo(settleInfo); -// // 创建分账记录 -// profitSharingService.createProfitSharing(order, qrCode); -// } else { -// settleInfo.setProfitSharing(false); -// request.setSettleInfo(settleInfo); -// } -// -// // 4. 创建对应租户的支付服务 -// JsapiServiceExtension jsapiService = wxPayConfigFactory.createJsapiService(qrCode.getTenantId()); -// -// // 5. 调用支付接口 -// return jsapiService.prepayWithRequestPayment(request); -// } -// -// private boolean isEnableProfit(QrCode qrCode) { -// return qrCode != null && -// Integer.valueOf(1).equals(qrCode.getEnableProfit()) && -// qrCode.getPayeeStatus() != null && -// qrCode.getPayeeStatus() == 1 && -// qrCode.getPayeeAccount() != null && -// qrCode.getProfitRate() != null; -// } -//} +package com.central.order.service; + +import com.central.common.context.LoginUserContextHolder; +import com.central.common.exception.BusinessException; +import com.central.common.model.SysUser; +import com.central.order.feign.UserCenterService; +import com.central.order.model.Order; +import com.central.order.model.QrCode; +import com.central.wechat.config.WxPayConfigFactory; +import com.central.wechat.entity.WechatConfig; +import com.central.wechat.feign.WechatConfigService; +import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension; +import com.wechat.pay.java.service.payments.jsapi.model.*; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Slf4j +@Service +public class PaymentService { + @Autowired + private WxPayConfigFactory wxPayConfigFactory; + @Autowired + private UserCenterService userCenterService; + @Autowired + private IOrderService orderService; + @Autowired + private IQrCodeService qrCodeService; + @Autowired + private IProfitSharingService profitSharingService; + @Autowired + private WechatConfigService configService; + + + + public PrepayWithRequestPaymentResponse createPayment(Long orderId) { + // 1. 获取用户和订单信息 + SysUser user = userCenterService.findUserById(LoginUserContextHolder.getUser().getId()); + Order order = orderService.getById(orderId); + if (order == null) { + throw new BusinessException("订单数据不存在"); + } + + // 2. 获取二维码信息 + QrCode qrCode = qrCodeService.getByCodeId(order.getCodeId()); + if (qrCode == null) { + throw new BusinessException("二维码不存在"); + } + + // 3. 创建支付请求 + PrepayRequest request = new PrepayRequest(); + + // 设置基本支付信息 + Amount amount = new Amount(); + amount.setTotal(Integer.parseInt(order.getOrderAmount())); + request.setAmount(amount); + + // 从二维码配置中获取支付配置 + WechatConfig payConfig = configService.getConfig("PAY", qrCode.getTenantId()); + request.setAppid(payConfig.getAppId()); + request.setMchid(payConfig.getMerchantId()); + request.setDescription(order.getOrderNo()); + request.setNotifyUrl(payConfig.getNotifyUrl()); + request.setOutTradeNo(order.getOrderNo()); + + // 设置支付者信息 + Payer payer = new Payer(); + payer.setOpenid(user.getOpenId()); + request.setPayer(payer); + + // 设置分账信息 + SettleInfo settleInfo = new SettleInfo(); + if (isEnableProfit(qrCode)) { + settleInfo.setProfitSharing(true); + request.setSettleInfo(settleInfo); + // 创建分账记录 + profitSharingService.createProfitSharing(order, qrCode); + } else { + settleInfo.setProfitSharing(false); + request.setSettleInfo(settleInfo); + } + + // 4. 创建对应租户的支付服务 + JsapiServiceExtension jsapiService = wxPayConfigFactory.createJsapiService(qrCode.getTenantId()); + + // 5. 调用支付接口 + return jsapiService.prepayWithRequestPayment(request); + } + + private boolean isEnableProfit(QrCode qrCode) { + return qrCode != null && + Integer.valueOf(1).equals(qrCode.getEnableProfit()) && + qrCode.getPayeeStatus() != null && + qrCode.getPayeeStatus() == 1 && + qrCode.getPayeeAccount() != null && + qrCode.getProfitRate() != null; + } +} diff --git a/zlt-business/user-center/src/main/java/com/central/user/controller/SysWechatConfigController.java b/zlt-business/user-center/src/main/java/com/central/user/controller/SysWechatConfigController.java index 09111ca3..d971a7ec 100644 --- a/zlt-business/user-center/src/main/java/com/central/user/controller/SysWechatConfigController.java +++ b/zlt-business/user-center/src/main/java/com/central/user/controller/SysWechatConfigController.java @@ -3,10 +3,8 @@ package com.central.user.controller; import com.central.common.context.TenantContextHolder; import com.central.common.model.PageResult; import com.central.common.model.Result; -import com.central.user.service.PrivateKeyService; import com.central.user.service.WechatConfigService; -import com.central.user.service.WechatPayService; -import com.central.wechat.wechat.entity.WechatConfig; +import com.central.wechat.entity.WechatConfig; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.Parameters; @@ -30,11 +28,6 @@ public class SysWechatConfigController { @Resource private WechatConfigService wechatConfigService; - @Resource - private WechatPayService wechatPayService; - - @Resource - private PrivateKeyService privateKeyService; @Operation(summary = "查询列表") @Parameters({ @@ -84,21 +77,15 @@ public class SysWechatConfigController { public Result uploadPrivateKey(@RequestParam("file") MultipartFile file) { try { String privateKey = new String(file.getBytes()); - return Result.succeed(wechatPayService.uploadPrivateKey( - TenantContextHolder.getTenant(), privateKey)); + WechatConfig wechatConfig = wechatConfigService.getConfig("PAY",TenantContextHolder.getTenant()); + wechatConfig.setPrivateKey(privateKey); + return Result.succeed(wechatConfigService.updateConfig(wechatConfig)); } catch (Exception e) { log.error("上传私钥文件失败", e); return Result.failed("上传私钥文件失败"); } } - @Operation(summary = "更新支付配置") - @PostMapping("/pay-config") - public Result updatePayConfig(@RequestBody Map config) { - return Result.succeed(wechatPayService.updatePayConfig( - TenantContextHolder.getTenant(), config)); - } - @Operation(summary = "切换微信配置") @PostMapping("/switch/{type}") public Result switchConfig(@PathVariable String type) { @@ -111,9 +98,4 @@ public class SysWechatConfigController { return Result.succeed(wechatConfigService.getConfig(type, TenantContextHolder.getTenant())); } - @Operation(summary = "获取租户密钥") - @GetMapping("/tenant/private-key") - public String getPrivateKey(@RequestParam("tenantId")String tenantId,@RequestParam String type) { - return privateKeyService.getPrivateKey(tenantId, type); - } } diff --git a/zlt-business/user-center/src/main/java/com/central/user/mapper/WechatConfigMapper.java b/zlt-business/user-center/src/main/java/com/central/user/mapper/WechatConfigMapper.java index 77c92d71..46bcf64f 100644 --- a/zlt-business/user-center/src/main/java/com/central/user/mapper/WechatConfigMapper.java +++ b/zlt-business/user-center/src/main/java/com/central/user/mapper/WechatConfigMapper.java @@ -2,7 +2,7 @@ package com.central.user.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; -import com.central.wechat.wechat.entity.WechatConfig; +import com.central.wechat.entity.WechatConfig; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; diff --git a/zlt-business/user-center/src/main/java/com/central/user/service/PrivateKeyService.java b/zlt-business/user-center/src/main/java/com/central/user/service/PrivateKeyService.java deleted file mode 100644 index ecdd63b8..00000000 --- a/zlt-business/user-center/src/main/java/com/central/user/service/PrivateKeyService.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.central.user.service; - -import java.io.InputStream; - -/** - * 私钥文件管理服务 - */ -public interface PrivateKeyService { - - /** - * 上传私钥文件 - * - * @param tenantId 租户ID - * @param type 配置类型 - * @param inputStream 文件输入流 - * @return 文件访问路径 - */ - String uploadPrivateKey(String tenantId, String type, InputStream inputStream); - - /** - * 获取私钥文件 - * - * @param tenantId 租户ID - * @param type 配置类型 - * @return 私钥文件内容 - */ - String getPrivateKey(String tenantId, String type); - - /** - * 删除私钥文件 - * - * @param tenantId 租户ID - * @param type 配置类型 - * @return 是否成功 - */ - boolean deletePrivateKey(String tenantId, String type); -} diff --git a/zlt-business/user-center/src/main/java/com/central/user/service/WechatConfigService.java b/zlt-business/user-center/src/main/java/com/central/user/service/WechatConfigService.java index 3b942a61..3dfca709 100644 --- a/zlt-business/user-center/src/main/java/com/central/user/service/WechatConfigService.java +++ b/zlt-business/user-center/src/main/java/com/central/user/service/WechatConfigService.java @@ -2,7 +2,7 @@ package com.central.user.service; import com.baomidou.mybatisplus.extension.service.IService; import com.central.common.model.PageResult; -import com.central.wechat.wechat.entity.WechatConfig; +import com.central.wechat.entity.WechatConfig; import java.util.Map; diff --git a/zlt-business/user-center/src/main/java/com/central/user/service/WechatPayService.java b/zlt-business/user-center/src/main/java/com/central/user/service/WechatPayService.java deleted file mode 100644 index c31efdf2..00000000 --- a/zlt-business/user-center/src/main/java/com/central/user/service/WechatPayService.java +++ /dev/null @@ -1,93 +0,0 @@ -package com.central.user.service; - -import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse; -import com.wechat.pay.java.service.payments.model.Transaction; -import com.wechat.pay.java.service.refund.model.Refund; -import com.wechat.pay.java.service.refund.model.RefundNotification; - -import java.util.Map; - -/** - * 微信支付服务 - */ -public interface WechatPayService { - - /** - * 创建JSAPI支付订单 - * - * @param tenantId 租户ID - * @param openId 用户openId - * @param amount 支付金额(分) - * @param description 商品描述 - * @param outTradeNo 商户订单号 - * @return 支付参数 - */ - PrepayWithRequestPaymentResponse createJsapiOrder(String tenantId, String openId, int amount, - String description, String outTradeNo); - - /** - * 查询订单 - * - * @param tenantId 租户ID - * @param outTradeNo 商户订单号 - * @return 订单信息 - */ - Transaction queryOrder(String tenantId, String outTradeNo); - - /** - * 关闭订单 - * - * @param tenantId 租户ID - * @param outTradeNo 商户订单号 - */ - void closeOrder(String tenantId, String outTradeNo); - - /** - * 申请退款 - * - * @param tenantId 租户ID - * @param outTradeNo 商户订单号 - * @param outRefundNo 商户退款单号 - * @param amount 退款金额(分) - * @param reason 退款原因 - * @return 退款结果 - */ -// RefundResponse refund(String tenantId, String outTradeNo, String outRefundNo, -// int amount, String reason); - - /** - * 查询退款 - * - * @param tenantId 租户ID - * @param outRefundNo 商户退款单号 - * @return 退款信息 - */ - Refund queryRefund(String tenantId, String outRefundNo); - - /** - * 解析退款通知 - * - * @param tenantId 租户ID - * @param notifyData 通知数据 - * @return 退款通知信息 - */ - RefundNotification parseRefundNotify(String tenantId, String notifyData); - - /** - * 上传私钥文件 - * - * @param tenantId 租户ID - * @param privateKey 私钥内容 - * @return 是否成功 - */ - boolean uploadPrivateKey(String tenantId, String privateKey); - - /** - * 更新支付配置 - * - * @param tenantId 租户ID - * @param config 配置信息 - * @return 是否成功 - */ - boolean updatePayConfig(String tenantId, Map config); -} diff --git a/zlt-business/user-center/src/main/java/com/central/user/service/impl/PrivateKeyServiceImpl.java b/zlt-business/user-center/src/main/java/com/central/user/service/impl/PrivateKeyServiceImpl.java deleted file mode 100644 index a81447e6..00000000 --- a/zlt-business/user-center/src/main/java/com/central/user/service/impl/PrivateKeyServiceImpl.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.central.user.service.impl; - -import com.central.user.service.PrivateKeyService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.redis.core.StringRedisTemplate; -import org.springframework.stereotype.Service; -import org.springframework.util.StringUtils; - -import java.io.BufferedReader; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.util.concurrent.TimeUnit; - -@Service -public class PrivateKeyServiceImpl implements PrivateKeyService { - - private static final String CACHE_KEY_PATTERN = "wechat:private_key:%s:%s"; - private static final long CACHE_EXPIRE_DAYS = 30; - - @Autowired - private StringRedisTemplate redisTemplate; - - @Override - public String uploadPrivateKey(String tenantId, String type, InputStream inputStream) { - try { - // 读取私钥内容 - BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); - StringBuilder content = new StringBuilder(); - String line; - while ((line = reader.readLine()) != null) { - content.append(line).append("\n"); - } - - // 缓存私钥内容 - String cacheKey = String.format(CACHE_KEY_PATTERN, tenantId, type); - redisTemplate.opsForValue().set(cacheKey, content.toString(), CACHE_EXPIRE_DAYS, TimeUnit.DAYS); - - return cacheKey; - } catch (Exception e) { - throw new RuntimeException("上传私钥文件失败", e); - } - } - - @Override - public String getPrivateKey(String tenantId, String type) { - String cacheKey = String.format(CACHE_KEY_PATTERN, tenantId, type); - String privateKey = redisTemplate.opsForValue().get(cacheKey); - - if (!StringUtils.hasText(privateKey)) { - throw new RuntimeException("私钥文件不存在"); - } - - return privateKey; - } - - @Override - public boolean deletePrivateKey(String tenantId, String type) { - String cacheKey = String.format(CACHE_KEY_PATTERN, tenantId, type); - return redisTemplate.delete(cacheKey); - } -} diff --git a/zlt-business/user-center/src/main/java/com/central/user/service/impl/WechatConfigServiceImpl.java b/zlt-business/user-center/src/main/java/com/central/user/service/impl/WechatConfigServiceImpl.java index c8df1eee..807e81e3 100644 --- a/zlt-business/user-center/src/main/java/com/central/user/service/impl/WechatConfigServiceImpl.java +++ b/zlt-business/user-center/src/main/java/com/central/user/service/impl/WechatConfigServiceImpl.java @@ -6,7 +6,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.central.common.model.PageResult; import com.central.user.mapper.WechatConfigMapper; import com.central.user.service.WechatConfigService; -import com.central.wechat.wechat.entity.WechatConfig; +import com.central.wechat.entity.WechatConfig; import lombok.RequiredArgsConstructor; import org.apache.commons.collections4.MapUtils; import org.springframework.cache.annotation.CacheEvict; diff --git a/zlt-business/user-center/src/main/java/com/central/user/service/impl/WechatPayServiceImpl.java b/zlt-business/user-center/src/main/java/com/central/user/service/impl/WechatPayServiceImpl.java deleted file mode 100644 index 3c119d77..00000000 --- a/zlt-business/user-center/src/main/java/com/central/user/service/impl/WechatPayServiceImpl.java +++ /dev/null @@ -1,154 +0,0 @@ -package com.central.user.service.impl;//package com.zlt.wechat.service.impl; - -import com.central.user.service.WechatPayService; -import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse; -import com.wechat.pay.java.service.payments.model.Transaction; -import com.wechat.pay.java.service.refund.model.Refund; -import com.wechat.pay.java.service.refund.model.RefundNotification; -import org.springframework.stereotype.Service; - -import java.util.Map; - -@Service -public class WechatPayServiceImpl implements WechatPayService { - @Override - public PrepayWithRequestPaymentResponse createJsapiOrder(String tenantId, String openId, int amount, String description, String outTradeNo) { - return null; - } - - @Override - public Transaction queryOrder(String tenantId, String outTradeNo) { - return null; - } - - @Override - public void closeOrder(String tenantId, String outTradeNo) { - - } - - @Override - public Refund queryRefund(String tenantId, String outRefundNo) { - return null; - } - - @Override - public RefundNotification parseRefundNotify(String tenantId, String notifyData) { - return null; - } - - @Override - public boolean uploadPrivateKey(String tenantId, String privateKey) { - return false; - } - - @Override - public boolean updatePayConfig(String tenantId, Map config) { - return false; - } - - /*@Autowired - private WechatConfigService configService; - - @Autowired - private PrivateKeyService privateKeyService; - - @Autowired - private JsapiServiceExtension jsapiService; - - @Autowired - private RefundService refundService; - - @Autowired - private Config wxPayConfig; - - @Override - public PrepayWithRequestPaymentResponse createJsapiOrder(String tenantId, String openId, - int amount, String description, String outTradeNo) { - // 切换租户配置 - configService.switchConfig("PAY", tenantId); - - // 创建支付订单 - *//*return jsapiService.prepayWithRequestPayment( - openId, - description, - outTradeNo, - amount, - "CNY" - );*//* - return new PrepayWithRequestPaymentResponse(); - } - - @Override - public Transaction queryOrder(String tenantId, String outTradeNo) { - configService.switchConfig("PAY", tenantId); - return jsapiService.queryOrderByOutTradeNo(outTradeNo); - } - - @Override - public void closeOrder(String tenantId, String outTradeNo) { - configService.switchConfig("PAY", tenantId); - jsapiService.closeOrder(outTradeNo); - } - - @Override - public RefundResponse refund(String tenantId, String outTradeNo, String outRefundNo, - int amount, String reason) { - configService.switchConfig("PAY", tenantId); - - RefundRequest request = new RefundRequest(); - request.setOutTradeNo(outTradeNo); - request.setOutRefundNo(outRefundNo); - request.setAmount(new RefundRequest.Amount(amount, "CNY")); - request.setReason(reason); - - return refundService.refund(request); - } - - @Override - public Refund queryRefund(String tenantId, String outRefundNo) { - configService.switchConfig("PAY", tenantId); - return refundService.queryByOutRefundNo(outRefundNo); - } - - @Override - public RefundNotification parseRefundNotify(String tenantId, String notifyData) { - configService.switchConfig("PAY", tenantId); - return refundService.parseRefundNotification(notifyData); - } - - @Override - public boolean uploadPrivateKey(String tenantId, String privateKey) { - try { - privateKeyService.uploadPrivateKey(tenantId, "PAY", - new ByteArrayInputStream(privateKey.getBytes())); - return true; - } catch (Exception e) { - return false; - } - } - - @Override - public boolean updatePayConfig(String tenantId, Map config) { - try { - WechatConfig wechatConfig = configService.getConfig("PAY", tenantId); - if (wechatConfig == null) { - wechatConfig = new WechatConfig(); - wechatConfig.setType("PAY"); - wechatConfig.setTenantId(tenantId); - } - - // 更新配置信息 - wechatConfig.setMerchantId(config.get("merchantId")); - wechatConfig.setAppId(config.get("appId")); - wechatConfig.setMchKey(config.get("mchKey")); - wechatConfig.setMchSerialNo(config.get("mchSerialNo")); - wechatConfig.setApiV3Key(config.get("apiV3Key")); - wechatConfig.setNotifyUrl(config.get("notifyUrl")); - wechatConfig.setStatus(1); - - return configService.saveOrUpdate(wechatConfig); - } catch (Exception e) { - return false; - } - }*/ -} diff --git a/zlt-commons/zlt-wechat-common-core/src/main/java/com/central/wechat/wechat/config/WechatProperties.java b/zlt-commons/zlt-wechat-common-core/src/main/java/com/central/wechat/config/WechatProperties.java similarity index 98% rename from zlt-commons/zlt-wechat-common-core/src/main/java/com/central/wechat/wechat/config/WechatProperties.java rename to zlt-commons/zlt-wechat-common-core/src/main/java/com/central/wechat/config/WechatProperties.java index 1eac684a..4411ea60 100644 --- a/zlt-commons/zlt-wechat-common-core/src/main/java/com/central/wechat/wechat/config/WechatProperties.java +++ b/zlt-commons/zlt-wechat-common-core/src/main/java/com/central/wechat/config/WechatProperties.java @@ -1,4 +1,4 @@ -package com.central.wechat.wechat.config; +package com.central.wechat.config; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; diff --git a/zlt-commons/zlt-wechat-common-core/src/main/java/com/central/wechat/wechat/entity/WechatConfig.java b/zlt-commons/zlt-wechat-common-core/src/main/java/com/central/wechat/entity/WechatConfig.java similarity index 94% rename from zlt-commons/zlt-wechat-common-core/src/main/java/com/central/wechat/wechat/entity/WechatConfig.java rename to zlt-commons/zlt-wechat-common-core/src/main/java/com/central/wechat/entity/WechatConfig.java index f60f8874..053f44b4 100644 --- a/zlt-commons/zlt-wechat-common-core/src/main/java/com/central/wechat/wechat/entity/WechatConfig.java +++ b/zlt-commons/zlt-wechat-common-core/src/main/java/com/central/wechat/entity/WechatConfig.java @@ -1,4 +1,4 @@ -package com.central.wechat.wechat.entity; +package com.central.wechat.entity; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; @@ -54,6 +54,11 @@ public class WechatConfig { */ private String privateKeyPath; + /** + * 商户私钥 + */ + private String privateKey; + /** * APIv3密钥 */ diff --git a/zlt-commons/zlt-wechat-common-core/src/main/java/com/central/wechat/wechat/feign/WechatConfigService.java b/zlt-commons/zlt-wechat-common-core/src/main/java/com/central/wechat/feign/WechatConfigService.java similarity index 61% rename from zlt-commons/zlt-wechat-common-core/src/main/java/com/central/wechat/wechat/feign/WechatConfigService.java rename to zlt-commons/zlt-wechat-common-core/src/main/java/com/central/wechat/feign/WechatConfigService.java index db756d95..74cd1946 100644 --- a/zlt-commons/zlt-wechat-common-core/src/main/java/com/central/wechat/wechat/feign/WechatConfigService.java +++ b/zlt-commons/zlt-wechat-common-core/src/main/java/com/central/wechat/feign/WechatConfigService.java @@ -1,11 +1,12 @@ -package com.central.wechat.wechat.feign; +package com.central.wechat.feign; import com.central.common.constant.ServiceNameConstants; -import com.central.wechat.wechat.entity.WechatConfig; -import com.central.wechat.wechat.feign.fallback.WechatConfigServiceFallbackFactory; +import com.central.wechat.entity.WechatConfig; +import com.central.wechat.feign.fallback.WechatConfigServiceFallbackFactory; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestParam; /** * @author zlt @@ -25,5 +26,14 @@ public interface WechatConfigService { @GetMapping(value = "/wechat/config/app/{id}") WechatConfig getConfigByAppId(@PathVariable("id") String appId); + /** + * feign rpc访问远程/wechat/config/{type}接口 + * 查询微信配置对象WechatConfig + * + * @param type + * @return + */ + @GetMapping(value = "/wechat/config/{type}") + String getPrivateKey(@PathVariable("type") String type, @RequestParam("tenantId") String tenantId); } diff --git a/zlt-commons/zlt-wechat-common-core/src/main/java/com/central/wechat/wechat/feign/fallback/WechatConfigServiceFallbackFactory.java b/zlt-commons/zlt-wechat-common-core/src/main/java/com/central/wechat/feign/fallback/WechatConfigServiceFallbackFactory.java similarity index 71% rename from zlt-commons/zlt-wechat-common-core/src/main/java/com/central/wechat/wechat/feign/fallback/WechatConfigServiceFallbackFactory.java rename to zlt-commons/zlt-wechat-common-core/src/main/java/com/central/wechat/feign/fallback/WechatConfigServiceFallbackFactory.java index 938d5b17..e60bc186 100644 --- a/zlt-commons/zlt-wechat-common-core/src/main/java/com/central/wechat/wechat/feign/fallback/WechatConfigServiceFallbackFactory.java +++ b/zlt-commons/zlt-wechat-common-core/src/main/java/com/central/wechat/feign/fallback/WechatConfigServiceFallbackFactory.java @@ -1,7 +1,7 @@ -package com.central.wechat.wechat.feign.fallback; +package com.central.wechat.feign.fallback; -import com.central.wechat.wechat.entity.WechatConfig; -import com.central.wechat.wechat.feign.WechatConfigService; +import com.central.wechat.entity.WechatConfig; +import com.central.wechat.feign.WechatConfigService; import lombok.extern.slf4j.Slf4j; import org.springframework.cloud.openfeign.FallbackFactory; @@ -27,6 +27,11 @@ public class WechatConfigServiceFallbackFactory implements FallbackFactory { - @Override - public PrivateKeyService create(Throwable throwable) { - return new PrivateKeyService() { - @Override - public String getPrivateKey(String tenantId,String type) { - - return ""; - } - }; - } -} diff --git a/zlt-commons/zlt-wechat-common-core/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/zlt-commons/zlt-wechat-common-core/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index ce935257..cc1ba790 100644 --- a/zlt-commons/zlt-wechat-common-core/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/zlt-commons/zlt-wechat-common-core/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1,4 +1,3 @@ -com.central.wechat.wechat.feign.fallback.PrivateKeyServiceFallbackFactory -com.central.wechat.wechat.feign.fallback.WechatConfigServiceFallbackFactory +com.central.wechat.feign.fallback.WechatConfigServiceFallbackFactory diff --git a/zlt-commons/zlt-wechat-spring-boot-starter/src/main/java/com/central/wechat/config/PrivateKeyUtils.java b/zlt-commons/zlt-wechat-spring-boot-starter/src/main/java/com/central/wechat/config/PrivateKeyUtils.java new file mode 100644 index 00000000..206cac3e --- /dev/null +++ b/zlt-commons/zlt-wechat-spring-boot-starter/src/main/java/com/central/wechat/config/PrivateKeyUtils.java @@ -0,0 +1,34 @@ +package com.central.wechat.config; + +import lombok.extern.slf4j.Slf4j; + +import java.security.KeyFactory; +import java.security.PrivateKey; +import java.security.spec.PKCS8EncodedKeySpec; +import java.util.Base64; + +/** + * 私钥串处理工具类 + * @author lsl + * @date 2025-06-03 + */ +@Slf4j +public class PrivateKeyUtils { + + public static PrivateKey getPrivateKey(String privateKeyStr) { + try { + String privateKeyPEM = privateKeyStr + .replace("-----BEGIN PRIVATE KEY-----", "") + .replace("-----END PRIVATE KEY-----", "") + .replaceAll("\\s+", ""); + + byte[] decoded = Base64.getDecoder().decode(privateKeyPEM); + PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(decoded); + KeyFactory keyFactory = KeyFactory.getInstance("RSA"); + return keyFactory.generatePrivate(keySpec); + } catch (Exception e) { + log.error("解析私钥失败", e); + throw new RuntimeException("解析私钥失败", e); + } + } +} diff --git a/zlt-commons/zlt-wechat-spring-boot-starter/src/main/java/com/central/wechat/config/WechatAutoConfiguration.java b/zlt-commons/zlt-wechat-spring-boot-starter/src/main/java/com/central/wechat/config/WechatAutoConfiguration.java new file mode 100644 index 00000000..504fd9cf --- /dev/null +++ b/zlt-commons/zlt-wechat-spring-boot-starter/src/main/java/com/central/wechat/config/WechatAutoConfiguration.java @@ -0,0 +1,138 @@ +package com.central.wechat.config; + +import cn.binarywang.wx.miniapp.api.WxMaService; +import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl; +import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl; +import com.central.wechat.entity.WechatConfig; +import com.central.wechat.feign.WechatConfigService; +import com.wechat.pay.java.core.Config; +import com.wechat.pay.java.core.notification.NotificationParser; +import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension; +import com.wechat.pay.java.service.payments.jsapi.model.SettleInfo; +import com.wechat.pay.java.service.refund.RefundService; +import com.wechat.pay.java.service.profitsharing.ProfitsharingService; +import lombok.extern.slf4j.Slf4j; +import me.chanjar.weixin.mp.api.WxMpService; +import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; +import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.cloud.openfeign.FeignAutoConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +import java.util.concurrent.Executor; +import java.util.concurrent.ThreadPoolExecutor; + +@Slf4j +@Configuration +@EnableConfigurationProperties(WechatProperties.class) +@AutoConfigureAfter({FeignAutoConfiguration.class, WebMvcAutoConfiguration.class}) +public class WechatAutoConfiguration { + + @Bean + @ConditionalOnBean(WechatConfigService.class) + public WxMpService wxMpService(WechatConfigService configService) { + log.info("WxMpService(公众号相关)被加载,tenantId:{}", getCurrentTenantId()); + return new WxMpServiceImpl() { + @Override + public WxMpDefaultConfigImpl getWxMpConfigStorage() { + WechatConfig config = configService.getConfig("MP", getCurrentTenantId()); + WxMpDefaultConfigImpl wxConfig = new WxMpDefaultConfigImpl(); + wxConfig.setAppId(config.getAppId()); + wxConfig.setSecret(config.getAppSecret()); + wxConfig.setToken(config.getToken()); + wxConfig.setAesKey(config.getAesKey()); + return wxConfig; + } + }; + } + + @Bean + @ConditionalOnBean(WechatConfigService.class) + public WxMaService wxMaService(WechatConfigService configService) { + log.info("WxMaService(小程序通知)被加载,tenantId:{}", getCurrentTenantId()); + return new WxMaServiceImpl() { + @Override + public WxMaDefaultConfigImpl getWxMaConfig() { + WechatConfig config = configService.getConfig("MINI", getCurrentTenantId()); + WxMaDefaultConfigImpl wxConfig = new WxMaDefaultConfigImpl(); + wxConfig.setAppid(config.getAppId()); + wxConfig.setSecret(config.getAppSecret()); + return wxConfig; + } + }; + } + + @Bean + @ConditionalOnBean({WechatConfigService.class}) + public WxPayConfigFactory wxPayConfigFactory(WechatConfigService configService) { + log.info("WxPayConfigFactory被加载,tenantId:{}", getCurrentTenantId()); + return new WxPayConfigFactory(configService); + } + + @Bean + @ConditionalOnBean(WxPayConfigFactory.class) + public Config wxPayConfig(WxPayConfigFactory factory) { + return factory.createConfig(getCurrentTenantId()); + } + + @Bean + @ConditionalOnBean(WxPayConfigFactory.class) + public JsapiServiceExtension jsapiService(WxPayConfigFactory factory) { + return factory.createJsapiService(getCurrentTenantId()); + } + + @Bean + @ConditionalOnBean(WxPayConfigFactory.class) + public NotificationParser notificationParser(WxPayConfigFactory factory) { + return factory.createNotificationParser(getCurrentTenantId()); + } + + @Bean + @ConditionalOnBean(WxPayConfigFactory.class) + public RefundService refundService(WxPayConfigFactory factory) { + return factory.createRefundService(getCurrentTenantId()); + } + + @Bean + @ConditionalOnBean(Config.class) + public ProfitsharingService profitsharingService(Config config) { + return new ProfitsharingService.Builder().config(config).build(); + } + + @Bean + @ConditionalOnBean(WechatConfigService.class) + public SettleInfo settleInfo(WechatConfigService configService) { + WechatConfig config = configService.getConfig("PAY", getCurrentTenantId()); + SettleInfo settleInfo = new SettleInfo(); + settleInfo.setProfitSharing(config.getProfitSharingEnabled()); + return settleInfo; + } + + @Bean + @ConditionalOnBean(WechatConfigService.class) + public Executor profitSharingExecutor(WechatConfigService configService) { + log.info("profitSharingExecutor被加载,tenantId:{}", getCurrentTenantId()); + WechatConfig config = configService.getConfig("PAY", getCurrentTenantId()); + if (!config.getAsyncEnabled()) { + return null; + } + + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + executor.setCorePoolSize(config.getAsyncPoolSize()); + executor.setMaxPoolSize(config.getAsyncPoolSize()); + executor.setQueueCapacity(100); + executor.setThreadNamePrefix("profit-sharing-"); + executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); + executor.initialize(); + return executor; + } + + private String getCurrentTenantId() { + return "webApp"; + } +} diff --git a/zlt-commons/zlt-wechat-spring-boot-starter/src/main/java/com/central/wechat/config/WxPayConfigFactory.java b/zlt-commons/zlt-wechat-spring-boot-starter/src/main/java/com/central/wechat/config/WxPayConfigFactory.java new file mode 100644 index 00000000..ca719d53 --- /dev/null +++ b/zlt-commons/zlt-wechat-spring-boot-starter/src/main/java/com/central/wechat/config/WxPayConfigFactory.java @@ -0,0 +1,107 @@ +package com.central.wechat.config; + +import com.central.wechat.entity.WechatConfig; +import com.central.wechat.feign.WechatConfigService; +import com.wechat.pay.java.core.Config; +import com.wechat.pay.java.core.RSAAutoCertificateConfig; +import com.wechat.pay.java.core.notification.NotificationConfig; +import com.wechat.pay.java.core.notification.NotificationParser; +import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension; +import com.wechat.pay.java.service.refund.RefundService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import java.security.PrivateKey; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +@Slf4j +@Component +public class WxPayConfigFactory { + private final WechatConfigService configService; + + // 缓存各个服务的实例 + private final Map configCache = new ConcurrentHashMap<>(); + private final Map jsapiServiceCache = new ConcurrentHashMap<>(); + private final Map notificationParserCache = new ConcurrentHashMap<>(); + private final Map refundServiceCache = new ConcurrentHashMap<>(); + + public WxPayConfigFactory(WechatConfigService configService) { + this.configService = configService; + } + + public Config createConfig(String tenantId) { + return configCache.computeIfAbsent(tenantId, this::buildConfig); + } + + private Config buildConfig(String tenantId) { + log.info("创建微信支付配置,tenantId:{}", tenantId); + WechatConfig config = configService.getConfig("PAY", tenantId); + PrivateKey privateKey = PrivateKeyUtils.getPrivateKey(config.getPrivateKey()); + + return new RSAAutoCertificateConfig.Builder() + .merchantId(config.getMerchantId()) + .privateKey(privateKey) + .merchantSerialNumber(config.getMchSerialNo()) + .apiV3Key(config.getApiV3Key()) + .build(); + } + + public JsapiServiceExtension createJsapiService(String tenantId) { + return jsapiServiceCache.computeIfAbsent(tenantId, this::buildJsapiService); + } + + private JsapiServiceExtension buildJsapiService(String tenantId) { + Config config = createConfig(tenantId); + return new JsapiServiceExtension.Builder().config(config).build(); + } + + public NotificationParser createNotificationParser(String tenantId) { + return notificationParserCache.computeIfAbsent(tenantId, this::buildNotificationParser); + } + + private NotificationParser buildNotificationParser(String tenantId) { + log.info("创建微信支付通知解析器,tenantId:{}", tenantId); + WechatConfig config = configService.getConfig("PAY", tenantId); + PrivateKey privateKey = PrivateKeyUtils.getPrivateKey(config.getPrivateKey()); + + NotificationConfig notificationConfig = new RSAAutoCertificateConfig.Builder() + .merchantId(config.getMerchantId()) + .privateKey(privateKey) + .merchantSerialNumber(config.getMchSerialNo()) + .apiV3Key(config.getApiV3Key()) + .build(); + + return new NotificationParser(notificationConfig); + } + + public RefundService createRefundService(String tenantId) { + return refundServiceCache.computeIfAbsent(tenantId, this::buildRefundService); + } + + private RefundService buildRefundService(String tenantId) { + log.info("创建微信退款服务,tenantId:{}", tenantId); + Config config = createConfig(tenantId); + return new RefundService.Builder().config(config).build(); + } + + /** + * 清除指定租户的缓存 + */ + public void clearCache(String tenantId) { + configCache.remove(tenantId); + jsapiServiceCache.remove(tenantId); + notificationParserCache.remove(tenantId); + refundServiceCache.remove(tenantId); + } + + /** + * 清除所有缓存 + */ + public void clearAllCache() { + configCache.clear(); + jsapiServiceCache.clear(); + notificationParserCache.clear(); + refundServiceCache.clear(); + } +} diff --git a/zlt-commons/zlt-wechat-spring-boot-starter/src/main/java/com/zlt/wechat/config/WechatAutoConfiguration.java b/zlt-commons/zlt-wechat-spring-boot-starter/src/main/java/com/zlt/wechat/config/WechatAutoConfiguration.java deleted file mode 100644 index 3148eee7..00000000 --- a/zlt-commons/zlt-wechat-spring-boot-starter/src/main/java/com/zlt/wechat/config/WechatAutoConfiguration.java +++ /dev/null @@ -1,166 +0,0 @@ -//package com.zlt.wechat.config; -// -//import cn.binarywang.wx.miniapp.api.WxMaService; -//import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl; -//import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl; -//import com.central.common.context.TenantContextHolder; -//import com.central.wechat.wechat.config.WechatProperties; -//import com.central.wechat.wechat.entity.WechatConfig; -//import com.central.wechat.wechat.feign.PrivateKeyService; -//import com.central.wechat.wechat.feign.WechatConfigService; -//import com.wechat.pay.java.core.Config; -//import com.wechat.pay.java.core.RSAAutoCertificateConfig; -//import com.wechat.pay.java.core.notification.NotificationConfig; -//import com.wechat.pay.java.core.notification.NotificationParser; -//import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension; -//import com.wechat.pay.java.service.payments.jsapi.model.SettleInfo; -//import com.wechat.pay.java.service.profitsharing.ProfitsharingService; -//import lombok.extern.slf4j.Slf4j; -//import me.chanjar.weixin.mp.api.WxMpService; -//import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; -//import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl; -//import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; -//import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -//import org.springframework.boot.context.properties.EnableConfigurationProperties; -//import org.springframework.context.annotation.Bean; -//import org.springframework.context.annotation.Configuration; -//import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; -// -//import java.util.concurrent.Executor; -//import java.util.concurrent.ThreadPoolExecutor; -// -//@Slf4j -//@Configuration -//@EnableConfigurationProperties(WechatProperties.class) -//public class WechatAutoConfiguration { -// -// @Bean -// @ConditionalOnMissingBean -// @ConditionalOnBean(WechatConfigService.class) -// public WxMpService wxMpService(WechatConfigService configService) { -// log.info("WxMpService(公众号相关)被加载,tenantId:{}", getCurrentTenantId()); -// return new WxMpServiceImpl() { -// @Override -// public WxMpDefaultConfigImpl getWxMpConfigStorage() { -// WechatConfig config = configService.getConfig("MP", getCurrentTenantId()); -// WxMpDefaultConfigImpl wxConfig = new WxMpDefaultConfigImpl(); -// wxConfig.setAppId(config.getAppId()); -// wxConfig.setSecret(config.getAppSecret()); -// wxConfig.setToken(config.getToken()); -// wxConfig.setAesKey(config.getAesKey()); -// return wxConfig; -// } -// }; -// } -// -// /** -// * 这个配置类在启动时可能会遇到问题,因为: -// * WechatAutoConfiguration是一个自动配置类 -// * 它依赖于WechatConfigService这个Feign客户端 -// * 如果Feign客户端还没有被创建,就会导致注入失败 -// * 因此使用@ConditionalOnBean注解确保配置类只在Feign客户端可用时才创建相关Bean -// */ -// @Bean -// @ConditionalOnMissingBean -// @ConditionalOnBean(WechatConfigService.class) -// public WxMaService wxMaService(WechatConfigService configService) { -// log.info("WxMaService(小程序通知)被加载,tenantId:{}", getCurrentTenantId()); -// // 小程序通知Service配置,type=MINI -// return new WxMaServiceImpl() { -// @Override -// public WxMaDefaultConfigImpl getWxMaConfig() { -// WechatConfig config = configService.getConfig("MINI", getCurrentTenantId()); -// WxMaDefaultConfigImpl wxConfig = new WxMaDefaultConfigImpl(); -// wxConfig.setAppid(config.getAppId()); -// wxConfig.setSecret(config.getAppSecret()); -// return wxConfig; -// } -// }; -// } -// -// @Bean -// @ConditionalOnBean({WechatConfigService.class, PrivateKeyService.class}) -// public WxPayConfigFactory wxPayConfigFactory( -// WechatConfigService configService, -// PrivateKeyService privateKeyService) { -// log.info("WxPayConfigFactory被加载,tenantId:{}", getCurrentTenantId()); -// return new WxPayConfigFactory(configService, privateKeyService); -// } -// -// @Bean -// @ConditionalOnBean(WxPayConfigFactory.class) -// public Config wxPayConfig(WxPayConfigFactory factory) { -// return factory.createConfig(getCurrentTenantId()); -// } -// -// @Bean -// @ConditionalOnBean(WxPayConfigFactory.class) -// public JsapiServiceExtension jsapiService(WxPayConfigFactory factory) { -// return factory.createJsapiService(getCurrentTenantId()); -// } -// -// @Bean -// @ConditionalOnMissingBean -// @ConditionalOnBean(WechatConfigService.class) -// public NotificationConfig notificationConfig(WechatConfigService configService, PrivateKeyService privateKeyService) { -// log.info("NotificationConfig被加载,tenantId:{}", getCurrentTenantId()); -// WechatConfig config = configService.getConfig("PAY", getCurrentTenantId()); -// String privateKey = privateKeyService.getPrivateKey(getCurrentTenantId(), "PAY"); -// return new RSAAutoCertificateConfig.Builder() -// .merchantId(config.getMerchantId()) -// .privateKey(privateKey) -// .merchantSerialNumber(config.getMchSerialNo()) -// .apiV3Key(config.getApiV3Key()) -// .build(); -// } -// -// @Bean -// @ConditionalOnMissingBean -// public NotificationParser notificationParser(NotificationConfig config) { -// return new NotificationParser(config); -// } -// -// @Bean -// @ConditionalOnMissingBean -// public ProfitsharingService profitsharingService(Config config) { -// return new ProfitsharingService.Builder().config(config).build(); -// } -// -// @Bean -// @ConditionalOnMissingBean -// @ConditionalOnBean(WechatConfigService.class) -// public SettleInfo settleInfo(WechatConfigService configService) { -// WechatConfig config = configService.getConfig("PAY", getCurrentTenantId()); -// SettleInfo settleInfo = new SettleInfo(); -// settleInfo.setProfitSharing(config.getProfitSharingEnabled()); -// return settleInfo; -// } -// -// @Bean -// @ConditionalOnMissingBean -// @ConditionalOnBean(WechatConfigService.class) -// public Executor profitSharingExecutor(WechatConfigService configService) { -// log.info("profitSharingExecutor被加载,tenantId:{}", getCurrentTenantId()); -// WechatConfig config = configService.getConfig("PAY", getCurrentTenantId()); -// if (!config.getAsyncEnabled()) { -// return null; -// } -// -// ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); -// executor.setCorePoolSize(config.getAsyncPoolSize()); -// executor.setMaxPoolSize(config.getAsyncPoolSize()); -// executor.setQueueCapacity(100); -// executor.setThreadNamePrefix("profit-sharing-"); -// executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); -// executor.initialize(); -// return executor; -// } -// /** -// * 获取当前商户ID -// * 这里需要根据实际业务场景实现,可以从ThreadLocal、请求上下文等获取 -// */ -// private String getCurrentTenantId() { -// // 获取当前租户的逻辑 -// return TenantContextHolder.getTenant(); -// } -//} diff --git a/zlt-commons/zlt-wechat-spring-boot-starter/src/main/java/com/zlt/wechat/config/WxPayConfigFactory.java b/zlt-commons/zlt-wechat-spring-boot-starter/src/main/java/com/zlt/wechat/config/WxPayConfigFactory.java deleted file mode 100644 index 2a70963c..00000000 --- a/zlt-commons/zlt-wechat-spring-boot-starter/src/main/java/com/zlt/wechat/config/WxPayConfigFactory.java +++ /dev/null @@ -1,42 +0,0 @@ -//package com.zlt.wechat.config; -// -//import com.central.wechat.wechat.entity.WechatConfig; -//import com.central.wechat.wechat.feign.PrivateKeyService; -//import com.central.wechat.wechat.feign.WechatConfigService; -//import com.wechat.pay.java.core.Config; -//import com.wechat.pay.java.core.RSAAutoCertificateConfig; -//import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension; -//import lombok.extern.slf4j.Slf4j; -//import org.springframework.context.annotation.Bean; -//import org.springframework.stereotype.Component; -// -//@Slf4j -//@Component -//public class WxPayConfigFactory { -// private final WechatConfigService configService; -// private final PrivateKeyService privateKeyService; -// -// public WxPayConfigFactory(WechatConfigService configService, PrivateKeyService privateKeyService) { -// this.configService = configService; -// this.privateKeyService = privateKeyService; -// } -// -// @Bean -// public Config createConfig(String tenantId) { -// log.info("创建微信支付配置,tenantId:{}", tenantId); -// WechatConfig config = configService.getConfig("PAY", tenantId); -// String privateKey = privateKeyService.getPrivateKey(tenantId, "PAY"); -// return new RSAAutoCertificateConfig.Builder() -// .merchantId(config.getMerchantId()) -// .privateKey(privateKey) -// .merchantSerialNumber(config.getMchSerialNo()) -// .apiV3Key(config.getApiV3Key()) -// .build(); -// } -// -// @Bean -// public JsapiServiceExtension createJsapiService(String tenantId) { -// Config config = createConfig(tenantId); -// return new JsapiServiceExtension.Builder().config(config).build(); -// } -//} diff --git a/zlt-commons/zlt-wechat-spring-boot-starter/src/main/resources/META-INF/spring.factories b/zlt-commons/zlt-wechat-spring-boot-starter/src/main/resources/META-INF/spring.factories deleted file mode 100644 index 1ec45a7f..00000000 --- a/zlt-commons/zlt-wechat-spring-boot-starter/src/main/resources/META-INF/spring.factories +++ /dev/null @@ -1,2 +0,0 @@ -org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ -com.zlt.wechat.config.WechatAutoConfiguration diff --git a/zlt-commons/zlt-wechat-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/zlt-commons/zlt-wechat-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 00000000..12f6ff0c --- /dev/null +++ b/zlt-commons/zlt-wechat-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +com.central.wechat.config.WechatAutoConfiguration diff --git a/zlt-uaa/pom.xml b/zlt-uaa/pom.xml index 17381e3c..dbf6a1f6 100644 --- a/zlt-uaa/pom.xml +++ b/zlt-uaa/pom.xml @@ -97,10 +97,7 @@ org.apache.tomcat.embed tomcat-embed-core - - com.zlt - zlt-wechat-spring-boot-starter - + diff --git a/zlt-uaa/src/main/java/com/central/oauth/service/ZltUserDetailsService.java b/zlt-uaa/src/main/java/com/central/oauth/service/ZltUserDetailsService.java index 6a07e22e..602bff38 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/service/ZltUserDetailsService.java +++ b/zlt-uaa/src/main/java/com/central/oauth/service/ZltUserDetailsService.java @@ -1,6 +1,6 @@ package com.central.oauth.service; -import com.central.wechat.wechat.entity.WechatConfig; +import com.central.wechat.entity.WechatConfig; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; diff --git a/zlt-uaa/src/main/java/com/central/oauth/service/impl/UserDetailServiceImpl.java b/zlt-uaa/src/main/java/com/central/oauth/service/impl/UserDetailServiceImpl.java index 5ec22850..1044d172 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/service/impl/UserDetailServiceImpl.java +++ b/zlt-uaa/src/main/java/com/central/oauth/service/impl/UserDetailServiceImpl.java @@ -5,8 +5,8 @@ import com.central.common.feign.UserService; import com.central.common.model.SysUser; import com.central.common.utils.LoginUserUtils; import com.central.oauth.service.ZltUserDetailsService; -import com.central.wechat.wechat.entity.WechatConfig; -import com.central.wechat.wechat.feign.WechatConfigService; +import com.central.wechat.entity.WechatConfig; +import com.central.wechat.feign.WechatConfigService; import lombok.extern.slf4j.Slf4j; import org.springframework.security.authentication.DisabledException; import org.springframework.security.authentication.InternalAuthenticationServiceException; diff --git a/zlt-uaa/src/main/java/com/central/oauth/support/tencent/TencentAuthenticationConverter.java b/zlt-uaa/src/main/java/com/central/oauth/support/tencent/TencentAuthenticationConverter.java index 4ef43783..b7c096aa 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/support/tencent/TencentAuthenticationConverter.java +++ b/zlt-uaa/src/main/java/com/central/oauth/support/tencent/TencentAuthenticationConverter.java @@ -18,6 +18,8 @@ import java.util.List; */ public class TencentAuthenticationConverter extends BaseAuthenticationConverter { + private String appCode = "wx30a3c8de94a85584"; + @Override protected String supportGrantType() { return TencentAuthenticationToken.GRANT_TYPE.getValue(); @@ -33,7 +35,11 @@ public class TencentAuthenticationConverter extends BaseAuthenticationConverter String code = OAuthEndpointUtils.getParam(parameters, OAuth2ParameterNames.CODE); String encryptedData = OAuthEndpointUtils.getParam(parameters,"encryptedData"); String iv = OAuthEndpointUtils.getParam(parameters,"iv"); - String appCode = OAuthEndpointUtils.getParam(parameters,"appCode"); + //如果参数缺省,默认使用平台提供的APPID + if (parameters.containsKey("appCode")) { + appCode = OAuthEndpointUtils.getParam(parameters,"appCode"); + } + return new TencentAuthenticationToken(code, encryptedData, iv, appCode); } diff --git a/zlt-uaa/src/main/java/com/central/oauth/support/tencent/TencentAuthenticationProvider.java b/zlt-uaa/src/main/java/com/central/oauth/support/tencent/TencentAuthenticationProvider.java index d727da34..02b64ca8 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/support/tencent/TencentAuthenticationProvider.java +++ b/zlt-uaa/src/main/java/com/central/oauth/support/tencent/TencentAuthenticationProvider.java @@ -6,7 +6,7 @@ import com.central.oauth.service.impl.UserDetailServiceFactory; import com.central.oauth.support.base.BaseAuthenticationProvider; import com.central.oauth2.common.properties.SecurityProperties; import com.central.oauth2.common.token.TencentAuthenticationToken; -import com.central.wechat.wechat.entity.WechatConfig; +import com.central.wechat.entity.WechatConfig; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.Getter; import lombok.Setter; diff --git a/zlt-uaa/src/main/resources/application.yml b/zlt-uaa/src/main/resources/application.yml index a91416f2..d4d77a7e 100644 --- a/zlt-uaa/src/main/resources/application.yml +++ b/zlt-uaa/src/main/resources/application.yml @@ -48,5 +48,5 @@ zlt: - com.central.oauth.mapper.ClientMapper.findList uaa: wx: - appId: - appsecret: + # 平台默认的APPID + appId: wx0c3cc6f1d297644d