| New file | 
 |  |  | 
 |  |  | 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() { | 
 |  |  |     } | 
 |  |  | } |