pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/webFilter/WXDataSourceNameSetFilter.java
File was deleted pipIrr-platform/pipIrr-web/pipIrr-web-wechat/src/main/java/com/dy/pipIrrWechat/config/WechatFilter.java
New file @@ -0,0 +1,76 @@ package com.dy.pipIrrWechat.config; import com.dy.common.multiDataSource.DataSourceContext; import com.dy.common.util.AES; import com.dy.pipIrrGlobal.pojoSe.SeWechatpay; import com.dy.pipIrrWechat.wechatpay.PayInfo; import com.dy.pipIrrWechat.wechatpay.PaymentSv; import jakarta.servlet.*; import jakarta.servlet.http.HttpServletRequest; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.springframework.context.ApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; /** * @author ZhuBaoMin * @date 2024-10-23 17:13 * @LastEditTime 2024-10-23 17:13 * @Description */ @Slf4j public class WechatFilter implements Filter { private PaymentSv paymentSv; @Override public void init(FilterConfig filterConfig) throws ServletException { // 从ServletContext获取ApplicationContext ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(filterConfig.getServletContext()); // 从ApplicationContext中获取服务bean paymentSv = applicationContext.getBean(PaymentSv.class); } @SneakyThrows @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) { HttpServletRequest httpRequest = (HttpServletRequest) servletRequest; // 配置数据源 String wxDataSourceName = httpRequest.getHeader("tag"); if (wxDataSourceName != null && wxDataSourceName.trim().length() > 0) { log.info("微信开发,设置数据源名称为:" + wxDataSourceName); //把组织单位标签作为数据源名称 DataSourceContext.set(wxDataSourceName); } else { log.info("用户未选择数据源"); } // 初始化微信支付信息 String appId = httpRequest.getHeader("appId"); if (appId == null || appId.trim().length() == 0) { log.info("小程序唯一标识不能为空"); } else { appId = AES.encrypt(appId); SeWechatpay seWechatpay = paymentSv.getWechatpayByAppId(appId); if (seWechatpay != null) { PayInfo.appid = AES.decrypt(seWechatpay.getAppId()); PayInfo.secret = AES.decrypt(seWechatpay.getAppSecret()); PayInfo.mchid = AES.decrypt(seWechatpay.getMchId()); PayInfo.key = AES.decrypt(seWechatpay.getMchKey()); PayInfo.serial_no = AES.decrypt(seWechatpay.getSerialNo()); PayInfo.notifyUrl = AES.decrypt(seWechatpay.getNotifyUrl()); log.info(PayInfo.appid + " ; " + PayInfo.secret); } else { log.info("小程序唯一标识错误或其信息不存在"); } } filterChain.doFilter(servletRequest, servletResponse); } @Override public void destroy() { } } pipIrr-platform/pipIrr-web/pipIrr-web-wechat/src/main/java/com/dy/pipIrrWechat/config/WechatFilterConfiguration.java
File was renamed from pipIrr-platform/pipIrr-web/pipIrr-web-wechat/src/main/java/com/dy/pipIrrWechat/config/WebFilterConfiguration.java @@ -1,6 +1,5 @@ package com.dy.pipIrrWechat.config; import com.dy.common.webFilter.WXDataSourceNameSetFilter; import jakarta.servlet.Filter; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; @@ -8,21 +7,21 @@ /** * @author ZhuBaoMin * @date 2024-05-07 14:51 * @LastEditTime 2024-05-07 14:51 * @date 2024-10-23 17:24 * @LastEditTime 2024-10-23 17:24 * @Description */ @Configuration public class WebFilterConfiguration { public class WechatFilterConfiguration { @Bean public FilterRegistrationBean<? extends Filter> RegFilter() { FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<>(); filterRegistrationBean.setFilter(new WXDataSourceNameSetFilter()); filterRegistrationBean.setFilter(new WechatFilter()); filterRegistrationBean.addUrlPatterns("/*");//配置过滤规则 filterRegistrationBean.setName("WXDataSourceNameSetFilter");//设置过滤器名称 filterRegistrationBean.setName("WechatFilter");//设置过滤器名称 filterRegistrationBean.setOrder(1);//执行次序 return filterRegistrationBean; } } } pipIrr-platform/pipIrr-web/pipIrr-web-wechat/src/main/java/com/dy/pipIrrWechat/wechatpay/PaymentCtrl.java
@@ -562,31 +562,4 @@ } return BaseResponseUtils.buildSuccess(true) ; } /** * 初始化微信支付信息 * @param appId * @return */ @PostMapping(path = "init_wechatpay") public BaseResponse<Boolean> initWechatpay(@RequestParam("appId") String appId) throws Exception { if(appId == null || appId.trim().length() == 0) { return BaseResponseUtils.buildErrorMsg("小程序唯一标识不能为空"); } appId = AES.encrypt(appId); SeWechatpay seWechatpay = paymentSv.getWechatpayByAppId(appId); if(seWechatpay != null) { PayInfo.appid = AES.decrypt(seWechatpay.getAppId()); PayInfo.secret = AES.decrypt(seWechatpay.getAppSecret()); PayInfo.mchid = AES.decrypt(seWechatpay.getMchId()); PayInfo.key = AES.decrypt(seWechatpay.getMchKey()); PayInfo.serial_no = AES.decrypt(seWechatpay.getSerialNo()); PayInfo.notifyUrl = AES.decrypt(seWechatpay.getNotifyUrl()); return BaseResponseUtils.buildSuccess(true) ; }else { return BaseResponseUtils.buildErrorMsg("小程序唯一标识错误或其信息不存在"); } } } pipIrr-platform/pipIrr-web/pipIrr-web-wechat/src/main/java/com/dy/pipIrrWechat/wechatpay/PaymentSv.java
@@ -101,7 +101,7 @@ * @param po * @return */ Long addWechatpay(SeWechatpay po) { public Long addWechatpay(SeWechatpay po) { seWechatpayMapper.insert(po); return po.getId(); } @@ -111,7 +111,7 @@ * @param appId * @return */ SeWechatpay getWechatpayByAppId(String appId) { public SeWechatpay getWechatpayByAppId(String appId) { return seWechatpayMapper.getWechatpayByAppId(appId); } }