From 7de1396e315896dbc72a9d54e44f77434ea90f18 Mon Sep 17 00:00:00 2001
From: wlzboy <66905212@qq.com>
Date: 星期日, 14 十二月 2025 23:47:34 +0800
Subject: [PATCH] feat:增加企业微信自动登录

---
 ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/QyWechatLoginService.java |   89 ++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 89 insertions(+), 0 deletions(-)

diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/QyWechatLoginService.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/QyWechatLoginService.java
new file mode 100644
index 0000000..bb88703
--- /dev/null
+++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/QyWechatLoginService.java
@@ -0,0 +1,89 @@
+package com.ruoyi.framework.web.service;
+
+import com.ruoyi.common.constant.Constants;
+import com.ruoyi.common.core.domain.entity.SysUser;
+import com.ruoyi.common.core.domain.model.LoginUser;
+import com.ruoyi.framework.manager.AsyncManager;
+import com.ruoyi.framework.manager.factory.AsyncFactory;
+import com.ruoyi.framework.security.QyWechatAuthenticationToken;
+import com.ruoyi.system.service.ISysUserService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.authentication.AuthenticationManager;
+import org.springframework.security.authentication.BadCredentialsException;
+import org.springframework.security.core.Authentication;
+import org.springframework.stereotype.Component;
+
+/**
+ * 浼佷笟寰俊鐧诲綍鏍¢獙鏂规硶
+ * 绫讳技浜嶴ysLoginService
+ * 
+ * @author ruoyi
+ */
+@Component
+public class QyWechatLoginService
+{
+    @Autowired
+    private TokenService tokenService;
+
+    @Autowired
+    private AuthenticationManager authenticationManager;
+
+    @Autowired
+    private SysLoginService sysLoginService;
+
+    @Autowired
+    private ISysUserService userService;
+
+    /**
+     * 浼佷笟寰俊鐢ㄦ埛ID鐧诲綍楠岃瘉
+     * 
+     * @param qyUserId 浼佷笟寰俊鐢ㄦ埛ID
+     * @param corpId 浼佷笟寰俊CorpID
+     * @return token
+     */
+    public String loginByQyUserId(String qyUserId, String corpId)
+    {
+        try
+        {
+            qyUserId = "qywechat__"+qyUserId;
+            // 鍒涘缓浼佷笟寰俊璁よ瘉Token
+            QyWechatAuthenticationToken authenticationToken = new QyWechatAuthenticationToken(qyUserId, corpId);
+            
+            // 浣跨敤AuthenticationManager杩涜璁よ瘉
+            Authentication authentication = authenticationManager.authenticate(authenticationToken);
+            
+            // 璁よ瘉鎴愬姛,鑾峰彇LoginUser
+            LoginUser loginUser = (LoginUser) authentication.getPrincipal();
+            
+            // 璁板綍鐧诲綍鎴愬姛鏃ュ織
+            AsyncManager.me().execute(AsyncFactory.recordLogininfor(
+                loginUser.getUsername(), 
+                Constants.LOGIN_SUCCESS, 
+                "浼佷笟寰俊鐢ㄦ埛ID鐧诲綍鎴愬姛"));
+            
+            // 璁板綍鐧诲綍淇℃伅(IP鍜屾椂闂�)
+            sysLoginService.recordLoginInfo(loginUser.getUserId());
+            
+            // 鐢熸垚token
+            return tokenService.createToken(loginUser);
+        }
+        catch (BadCredentialsException e)
+        {
+            // 璁板綍鐧诲綍澶辫触鏃ュ織
+            AsyncManager.me().execute(AsyncFactory.recordLogininfor(
+                qyUserId, 
+                Constants.LOGIN_FAIL, 
+                e.getMessage()));
+            throw e;
+        }
+        catch (Exception e)
+        {
+            // 璁板綍鐧诲綍澶辫触鏃ュ織
+            AsyncManager.me().execute(AsyncFactory.recordLogininfor(
+                qyUserId, 
+                Constants.LOGIN_FAIL, 
+                e.getMessage()));
+            throw new BadCredentialsException(e.getMessage());
+        }
+    }
+}
\ No newline at end of file

--
Gitblit v1.9.1