|
|
@@ -0,0 +1,67 @@
|
|
|
+package cn.tr.module.smart.wx.register;
|
|
|
+
|
|
|
+import cn.tr.core.exception.ServiceException;
|
|
|
+import cn.tr.core.exception.TRExcCode;
|
|
|
+import cn.tr.module.smart.wx.dto.WxTemplateDTO;
|
|
|
+import cn.tr.module.smart.wx.handler.AbstractWxSendTemplateMsgHandler;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import me.chanjar.weixin.common.error.WxErrorException;
|
|
|
+import me.chanjar.weixin.mp.api.WxMpService;
|
|
|
+import org.springframework.aop.support.AopUtils;
|
|
|
+import org.springframework.beans.BeansException;
|
|
|
+import org.springframework.beans.factory.config.BeanPostProcessor;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.atomic.AtomicReference;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author wangzl
|
|
|
+ * @description: TODO
|
|
|
+ * @date 2025/5/23 11:02
|
|
|
+ */
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+public class WxSendTemplateMsgRegister implements BeanPostProcessor {
|
|
|
+ private final Map<Class<?>, AbstractWxSendTemplateMsgHandler<?>> map = new HashMap<>();
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
|
|
+ if (bean instanceof AbstractWxSendTemplateMsgHandler) {
|
|
|
+
|
|
|
+ AbstractWxSendTemplateMsgHandler<?> handler = (AbstractWxSendTemplateMsgHandler<?>) bean;
|
|
|
+ Class<?> clazz = handler.getSupportedType();
|
|
|
+ if (clazz == null) {
|
|
|
+ throw new ServiceException(TRExcCode.SYSTEM_ERROR_B0001, "Handler 未正确实现 getSupportedType(): " + handler.getClass());
|
|
|
+ }
|
|
|
+ map.put(((AbstractWxSendTemplateMsgHandler<?>) bean).getSupportedType(), (AbstractWxSendTemplateMsgHandler<?>) bean);
|
|
|
+ log.info("注册 Handler: " + clazz + " -> " + handler.getClass());
|
|
|
+ }
|
|
|
+ return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ public <T extends WxTemplateDTO> String sendMessage(WxMpService wxMpService, String unionId, String appId, String url, T msg) throws WxErrorException {
|
|
|
+ if (msg == null) {
|
|
|
+ throw new ServiceException(TRExcCode.SYSTEM_ERROR_B0001, "参数对象msg不能为空");
|
|
|
+ }
|
|
|
+ Class<?> clazz = AopUtils.isAopProxy(msg) ? AopUtils.getTargetClass(msg) : msg.getClass();
|
|
|
+ AtomicReference<AbstractWxSendTemplateMsgHandler<T>> handler = new AtomicReference<>((AbstractWxSendTemplateMsgHandler<T>) map.get(clazz));
|
|
|
+
|
|
|
+ // 如果直接没找到,尝试找父类匹配的 handler
|
|
|
+ if (handler.get() == null) {
|
|
|
+ map.entrySet().stream()
|
|
|
+ .filter(entry -> entry.getKey().isAssignableFrom(clazz))
|
|
|
+ .findFirst()
|
|
|
+ .ifPresent(entry -> handler.set((AbstractWxSendTemplateMsgHandler<T>) entry.getValue()));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if (handler.get() == null) {
|
|
|
+ throw new ServiceException(TRExcCode.SYSTEM_ERROR_B0001, "没有找到对handler对象" + clazz);
|
|
|
+ }
|
|
|
+ return handler.get().sendMessage(wxMpService, unionId, url, appId, msg);
|
|
|
+
|
|
|
+ }
|
|
|
+}
|