diff --git a/src/main/java/com/gxwebsoft/common/system/mapper/PaymentMapper.java b/src/main/java/com/gxwebsoft/common/system/mapper/PaymentMapper.java index 53213e9..c58aeb8 100644 --- a/src/main/java/com/gxwebsoft/common/system/mapper/PaymentMapper.java +++ b/src/main/java/com/gxwebsoft/common/system/mapper/PaymentMapper.java @@ -1,5 +1,6 @@ package com.gxwebsoft.common.system.mapper; +import com.baomidou.mybatisplus.annotation.InterceptorIgnore; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.gxwebsoft.common.system.entity.Payment; @@ -34,4 +35,6 @@ public interface PaymentMapper extends BaseMapper { */ List selectListRel(@Param("param") PaymentParam param); + @InterceptorIgnore(tenantLine = "true") + Payment getByType(@Param("param") PaymentParam paymentParam); } diff --git a/src/main/java/com/gxwebsoft/common/system/mapper/xml/PaymentMapper.xml b/src/main/java/com/gxwebsoft/common/system/mapper/xml/PaymentMapper.xml index b4e096c..d54c4bc 100644 --- a/src/main/java/com/gxwebsoft/common/system/mapper/xml/PaymentMapper.xml +++ b/src/main/java/com/gxwebsoft/common/system/mapper/xml/PaymentMapper.xml @@ -77,4 +77,14 @@ + + + diff --git a/src/main/java/com/gxwebsoft/common/system/service/PaymentService.java b/src/main/java/com/gxwebsoft/common/system/service/PaymentService.java index 918bd12..ae4f571 100644 --- a/src/main/java/com/gxwebsoft/common/system/service/PaymentService.java +++ b/src/main/java/com/gxwebsoft/common/system/service/PaymentService.java @@ -39,4 +39,5 @@ public interface PaymentService extends IService { */ Payment getByIdRel(Integer id); + Payment getByType(PaymentParam paymentParam); } diff --git a/src/main/java/com/gxwebsoft/common/system/service/impl/PaymentServiceImpl.java b/src/main/java/com/gxwebsoft/common/system/service/impl/PaymentServiceImpl.java index 767c632..9e2d798 100644 --- a/src/main/java/com/gxwebsoft/common/system/service/impl/PaymentServiceImpl.java +++ b/src/main/java/com/gxwebsoft/common/system/service/impl/PaymentServiceImpl.java @@ -44,4 +44,9 @@ public class PaymentServiceImpl extends ServiceImpl impl return param.getOne(baseMapper.selectListRel(param)); } + @Override + public Payment getByType(PaymentParam paymentParam) { + return baseMapper.getByType(paymentParam); + } + } diff --git a/src/main/java/com/gxwebsoft/payment/controller/PaymentController.java b/src/main/java/com/gxwebsoft/payment/controller/PaymentController.java index bc409fa..9a37a37 100644 --- a/src/main/java/com/gxwebsoft/payment/controller/PaymentController.java +++ b/src/main/java/com/gxwebsoft/payment/controller/PaymentController.java @@ -2,6 +2,7 @@ package com.gxwebsoft.payment.controller; import com.gxwebsoft.common.core.web.ApiResult; import com.gxwebsoft.common.core.web.BaseController; +import com.gxwebsoft.common.system.entity.User; import com.gxwebsoft.payment.constants.PaymentConstants; import com.gxwebsoft.payment.dto.PaymentRequest; import com.gxwebsoft.payment.dto.PaymentResponse; @@ -45,7 +46,16 @@ public class PaymentController extends BaseController { @PostMapping("/create") public ApiResult createPayment(@Valid @RequestBody PaymentRequest request) { log.info("收到支付请求: {}", request); + final User loginUser = getLoginUser(); + if(loginUser == null){ + return fail("请先登录"); + } + + request.setUserId(loginUser.getUserId()); + if(request.getTenantId() == null){ + request.setTenantId(loginUser.getTenantId()); + } try { PaymentResponse response = paymentService.createPayment(request); return this.success("支付订单创建成功", response); diff --git a/src/main/java/com/gxwebsoft/payment/service/WxPayConfigService.java b/src/main/java/com/gxwebsoft/payment/service/WxPayConfigService.java index 7014da3..9b54fb0 100644 --- a/src/main/java/com/gxwebsoft/payment/service/WxPayConfigService.java +++ b/src/main/java/com/gxwebsoft/payment/service/WxPayConfigService.java @@ -1,10 +1,13 @@ package com.gxwebsoft.payment.service; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.gxwebsoft.common.core.config.CertificateProperties; import com.gxwebsoft.common.core.service.CertificateService; import com.gxwebsoft.common.core.utils.RedisUtil; import com.gxwebsoft.common.core.utils.WxNativeUtil; import com.gxwebsoft.common.system.entity.Payment; +import com.gxwebsoft.common.system.param.PaymentParam; +import com.gxwebsoft.common.system.service.PaymentService; import com.gxwebsoft.payment.exception.PaymentException; import com.wechat.pay.java.core.Config; import com.wechat.pay.java.core.RSAAutoCertificateConfig; @@ -15,6 +18,7 @@ import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.io.IOException; +import java.util.concurrent.TimeUnit; /** * 微信支付配置服务 @@ -36,6 +40,9 @@ public class WxPayConfigService { @Resource private CertificateProperties certificateProperties; + @Resource + private PaymentService paymentService; + @Value("${spring.profiles.active:dev}") private String activeProfile; @@ -92,22 +99,41 @@ public class WxPayConfigService { /** * 获取支付配置信息 + * 优先从缓存获取,缓存没有则查询数据库,最后兜底到开发环境测试配置 */ private Payment getPaymentConfig(Integer tenantId) throws PaymentException { String cacheKey = "Payment:wxPay:" + tenantId; Payment payment = redisUtil.get(cacheKey, Payment.class); + System.out.println("payment = " + payment); + if (payment != null) { + log.debug("从缓存获取支付配置成功,租户ID: {}", tenantId); + return payment; + } - if (payment == null && !"dev".equals(activeProfile)) { - throw PaymentException.systemError("微信支付配置未找到,租户ID: " + tenantId, null); + // 缓存中没有,尝试从数据库查询 + try { + final PaymentParam paymentParam = new PaymentParam(); + paymentParam.setType(1); + paymentParam.setTenantId(tenantId); + payment = paymentService.getByType(paymentParam); + System.out.println("payment1 = " + payment); + if (payment != null) { + log.info("从数据库获取支付配置成功,租户ID: {},将缓存配置", tenantId); + // 将查询到的配置缓存起来,缓存1小时 + redisUtil.set(cacheKey, payment, 1L,TimeUnit.DAYS); + return payment; + } + } catch (Exception e) { + log.warn("从数据库查询支付配置失败,租户ID: {},错误: {}", tenantId, e.getMessage()); } - if (payment != null) { - log.debug("从缓存获取支付配置成功,租户ID: {}", tenantId); - } else { - log.debug("开发环境模式,将使用测试配置,租户ID: {}", tenantId); + // 数据库也没有配置 + if (!"dev".equals(activeProfile)) { + throw PaymentException.systemError("微信支付配置未找到,租户ID: " + tenantId + ",请检查数据库配置", null); } - return payment; + log.debug("开发环境模式,将使用测试配置,租户ID: {}", tenantId); + return null; // 开发环境返回null,使用测试配置 } /** @@ -126,7 +152,8 @@ public class WxPayConfigService { */ private String getDevCertificatePath(Integer tenantId) throws PaymentException { try { - String certPath = "cert/wechat-pay/apiclient_key.pem"; + // 根据租户ID构建证书路径 + String certPath = "dev/wechat/" + tenantId + "/apiclient_key.pem"; ClassPathResource resource = new ClassPathResource(certPath); if (!resource.exists()) {