feat:新增配置类,实现动态配置管理,自动化注入配置功能

This commit is contained in:
2025-05-30 15:55:21 +08:00
parent 28398be217
commit 9b7fa58fcf
24 changed files with 191 additions and 170 deletions
@@ -3,10 +3,10 @@ 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.zlt.wechat.entity.WechatConfig;
import com.zlt.wechat.service.PrivateKeyService;
import com.zlt.wechat.service.WechatConfigService;
import com.zlt.wechat.service.WechatPayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
@@ -14,10 +14,10 @@ import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.util.Map;
@Slf4j
@@ -27,13 +27,13 @@ import java.util.Map;
@RequestMapping("/wechat/config")
public class SysWechatConfigController {
@Autowired
@Resource
private WechatConfigService wechatConfigService;
@Autowired
@Resource
private WechatPayService wechatPayService;
@Autowired
@Resource
private PrivateKeyService privateKeyService;
@Operation(summary = "查询列表")
@@ -109,4 +109,10 @@ public class SysWechatConfigController {
public Result<WechatConfig> getCurrentConfig(@PathVariable String type) {
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);
}
}
@@ -0,0 +1,35 @@
package com.central.user.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zlt.wechat.entity.WechatConfig;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
import java.util.Map;
@Mapper
public interface WechatConfigMapper extends BaseMapper<WechatConfig> {
@Select("<script>" +
"SELECT * FROM wechat_config " +
"<where>" +
" <if test='p.type != null and p.type != \"\"'>" +
" AND type = #{p.type}" +
" </if>" +
" <if test='p.merchantId != null and p.merchantId != \"\"'>" +
" AND merchant_id = #{p.merchantId}" +
" </if>" +
" <if test='p.status != null'>" +
" AND status = #{p.status}" +
" </if>" +
" <if test='p.tenantId != null and p.tenantId != \"\"'>" +
" AND tenant_id = #{p.tenantId}" +
" </if>" +
"</where>" +
"ORDER BY create_time DESC" +
"</script>")
List<WechatConfig> findList(Page<WechatConfig> page, @Param("p") Map<String, Object> params);
}
@@ -0,0 +1,37 @@
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);
}
@@ -1,34 +1,53 @@
package com.central.user.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.central.user.model.SysWechatConfig;
import com.central.common.model.PageResult;
import com.zlt.wechat.entity.WechatConfig;
import java.util.Map;
public interface WechatConfigService extends IService<WechatConfig> {
/**
* 列表
* @param params
* @return
*/
PageResult<WechatConfig> findList(Map<String, Object> params);
public interface SysWechatConfigService extends IService<SysWechatConfig> {
/**
* 获取微信配置
*
* @param type 配置类型
* @param tenantId 商户ID
* @return 微信配置
*/
SysWechatConfig getConfig(String type);
WechatConfig getConfig(String type, String tenantId);
/**
* 获取微信配置
*
* @param appId 商户ID
* @return 微信配置
*/
WechatConfig getConfig(String appId);
/**
* 保存微信配置
*
* @param config 微信配置
* @return 是否成功
*/
boolean saveConfig(SysWechatConfig config);
boolean saveConfig(WechatConfig config);
/**
* 更新微信配置
*
* @param config 微信配置
* @return 是否成功
*/
boolean updateConfig(SysWechatConfig config);
boolean updateConfig(WechatConfig config);
/**
* 删除微信配置
*
@@ -36,20 +55,13 @@ public interface SysWechatConfigService extends IService<SysWechatConfig> {
* @return 是否成功
*/
boolean deleteConfig(Long id);
/**
* 切换当前使用的微信配置
*
* @param type 配置类型
* @param merchantId 商户ID
* @return 是否成功
*/
boolean switchConfig(String type);
/**
* 获取当前使用的微信配置
*
* @param type 配置类型
* @return 微信配置
*/
SysWechatConfig getCurrentConfig(String type);
}
boolean switchConfig(String type, String merchantId);
}
@@ -0,0 +1,93 @@
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<String, String> config);
}
@@ -0,0 +1,61 @@
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);
}
}
@@ -1,61 +0,0 @@
package com.central.user.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.central.common.context.TenantContextHolder;
import com.central.user.mapper.SysWechatConfigMapper;
import com.central.user.model.SysWechatConfig;
import com.central.user.service.SysWechatConfigService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class SysWechatConfigServiceImpl extends ServiceImpl<SysWechatConfigMapper, SysWechatConfig>
implements SysWechatConfigService {
private static final String CACHE_NAME = "wechat:config";
private static final String CACHE_KEY_PATTERN = "#type";
@Override
@Cacheable(value = CACHE_NAME, key = CACHE_KEY_PATTERN, unless = "#result == null")
public SysWechatConfig getConfig(String type) {
return getOne(new LambdaQueryWrapper<SysWechatConfig>()
.eq(SysWechatConfig::getType, type)
.eq(SysWechatConfig::getTenantId, TenantContextHolder.getTenant())
.eq(SysWechatConfig::getStatus, 1));
}
@Override
@CacheEvict(value = CACHE_NAME, key = CACHE_KEY_PATTERN)
public boolean saveConfig(SysWechatConfig config) {
config.setTenantId(TenantContextHolder.getTenant());
return save(config);
}
@Override
@CacheEvict(value = CACHE_NAME, key = CACHE_KEY_PATTERN)
public boolean updateConfig(SysWechatConfig config) {
config.setTenantId(TenantContextHolder.getTenant());
return updateById(config);
}
@Override
@CacheEvict(value = CACHE_NAME, allEntries = true)
public boolean deleteConfig(Long id) {
return removeById(id);
}
@Override
@CacheEvict(value = CACHE_NAME, key = CACHE_KEY_PATTERN)
public boolean switchConfig(String type) {
// 这里可以添加切换配置的具体逻辑
return true;
}
@Override
@Cacheable(value = CACHE_NAME, key = "'current:' + #type", unless = "#result == null")
public SysWechatConfig getCurrentConfig(String type) {
return getConfig(type);
}
}
@@ -0,0 +1,73 @@
package com.central.user.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.zlt.wechat.entity.WechatConfig;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.MapUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
@RequiredArgsConstructor
@Service
public class WechatConfigServiceImpl extends ServiceImpl<WechatConfigMapper, WechatConfig> implements WechatConfigService {
private static final String CACHE_NAME = "wechat:config";
private static final String CACHE_KEY_PATTERN = "#type + ':' + #tenantId";
@Override
public PageResult<WechatConfig> findList(Map<String, Object> params) {
Page<WechatConfig> page = new Page<>(MapUtils.getInteger(params, "page", 1), MapUtils.getInteger(params, "limit", 10));
List<WechatConfig> list = baseMapper.findList(page, params);
return PageResult.<WechatConfig>builder().data(list).resp_code(0).count(page.getTotal()).build();
}
@Override
@Cacheable(value = CACHE_NAME, key = CACHE_KEY_PATTERN, unless = "#result == null")
public WechatConfig getConfig(String type, String tenantId) {
return getOne(new LambdaQueryWrapper<WechatConfig>()
.eq(WechatConfig::getType, type)
.eq(WechatConfig::getTenantId, tenantId)
.eq(WechatConfig::getStatus, 1));
}
@Override
public WechatConfig getConfig(String appId) {
return getOne(new LambdaQueryWrapper<WechatConfig>()
.eq(WechatConfig::getAppId, appId)
.eq(WechatConfig::getStatus, 1));
}
@Override
@CacheEvict(value = CACHE_NAME, key = CACHE_KEY_PATTERN)
public boolean saveConfig(WechatConfig config) {
return save(config);
}
@Override
@CacheEvict(value = CACHE_NAME, key = CACHE_KEY_PATTERN)
public boolean updateConfig(WechatConfig config) {
return updateById(config);
}
@Override
@CacheEvict(value = CACHE_NAME, allEntries = true)
public boolean deleteConfig(Long id) {
return removeById(id);
}
@Override
@CacheEvict(value = CACHE_NAME, key = CACHE_KEY_PATTERN)
public boolean switchConfig(String type, String tenantId) {
// 这里可以添加切换配置的具体逻辑
return true;
}
}
@@ -0,0 +1,130 @@
package com.central.user.service.impl;//package com.zlt.wechat.service.impl;
//
//import com.wechat.pay.java.core.Config;
//import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
//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.RefundService;
//import com.wechat.pay.java.service.refund.model.Refund;
//import com.wechat.pay.java.service.refund.model.RefundNotification;
//import com.wechat.pay.java.service.refund.model.RefundRequest;
//import com.wechat.pay.java.service.refund.model.RefundResponse;
//import com.zlt.wechat.entity.WechatConfig;
//import com.zlt.wechat.service.PrivateKeyService;
//import com.zlt.wechat.service.WechatConfigService;
//import com.zlt.wechat.service.WechatPayService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import java.io.ByteArrayInputStream;
//import java.util.Map;
//
//@Service
//public class WechatPayServiceImpl implements WechatPayService {
//
// @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<String, String> 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;
// }
// }
//}