liu
2026-09-30 91491239c5d4611b5b1404a7abeb2bb23d0edad5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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());
    }
 
}