package cn.iocoder.yudao.module.wxcp.service.listener; import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum; import cn.iocoder.yudao.module.system.event.SocialLoginEvent; import cn.iocoder.yudao.module.wxcp.service.notify.WxCpNotifyService; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import org.springframework.transaction.event.TransactionPhase; import org.springframework.transaction.event.TransactionalEventListener; /** * 企业微信登录事件监听器 * * 监听社交登录成功事件,当登录方式为企业微信时,发送登录通知消息 * * @author 超级管理员 */ @Component @Slf4j public class WxCpLoginEventListener { @Resource private WxCpNotifyService wxCpNotifyService; /** * 监听社交登录事件 * * 使用 AFTER_COMMIT:确保事务提交后再发送通知, * 否则异步线程查询绑定关系时会因事务未提交而查不到记录(首次自动注册场景尤其明显) * fallbackExecution = true:调用方无事务时也正常触发 */ @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT, fallbackExecution = true) public void onSocialLogin(SocialLoginEvent event) { // 仅处理企业微信登录事件 if (!SocialTypeEnum.WECHAT_ENTERPRISE.getType().equals(event.getSocialType())) { return; } log.info("[onSocialLogin][收到企业微信登录事件 userId={} username={}]", event.getUserId(), event.getUsername()); // 发送企业微信登录通知 wxCpNotifyService.sendLoginNotification( event.getUserId(), event.getUsername(), event.getLoginType()); } }