From 252abfe5be453026dd87e84eac00dcc65750f0c0 Mon Sep 17 00:00:00 2001 From: yubaolee Date: Sat, 27 Jun 2026 23:08:53 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=84refactor:=20=E6=9B=BF=E6=8D=A2EF?= =?UTF-8?q?=E4=B8=BASqlSugar?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OpenAuth.App/AuthContextFactory.cs | 25 +++++++++++++++++++++---- OpenAuth.App/SSO/LoginParse.cs | 24 ++++++++++++++++++++---- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/OpenAuth.App/AuthContextFactory.cs b/OpenAuth.App/AuthContextFactory.cs index 803433bd..1eb41529 100644 --- a/OpenAuth.App/AuthContextFactory.cs +++ b/OpenAuth.App/AuthContextFactory.cs @@ -18,6 +18,10 @@ using Infrastructure; using OpenAuth.Repository; using OpenAuth.Repository.Domain; using OpenAuth.Repository.Interface; +using SqlSugar; +using Infrastructure.Extensions.AutofacManager; +using Infrastructure.Utilities; +using Microsoft.AspNetCore.Http; namespace OpenAuth.App { @@ -29,15 +33,28 @@ namespace OpenAuth.App { private SystemAuthStrategy _systemAuth; private NormalAuthStrategy _normalAuthStrategy; - private readonly IUnitWork _unitWork; + + protected ISqlSugarClient SugarClient; public AuthContextFactory(SystemAuthStrategy sysStrategy , NormalAuthStrategy normalAuthStrategy - , IUnitWork unitWork) + , ISqlSugarClient client) { _systemAuth = sysStrategy; _normalAuthStrategy = normalAuthStrategy; - _unitWork = unitWork; + string tenantId = Define.DEFAULT_TENANT_ID; + var httpContextAccessor = AutofacContainerModule.GetService(); + if (httpContextAccessor != null) + { + tenantId = httpContextAccessor.GetTenantId(); + } + + if(tenantId != Define.DEFAULT_TENANT_ID) //如果不是默认租户,则使用租户id获取连接 + { + client = client.AsTenant().GetConnection(tenantId); + } + + SugarClient = client; } public AuthStrategyContext GetAuthStrategyContext(string username) @@ -52,7 +69,7 @@ namespace OpenAuth.App else { service = _normalAuthStrategy; - service.User = _unitWork.FirstOrDefault(u => u.Account == username); + service.User = SugarClient.Queryable().First(u => u.Account == username); } return new AuthStrategyContext(service); diff --git a/OpenAuth.App/SSO/LoginParse.cs b/OpenAuth.App/SSO/LoginParse.cs index 45254cca..9ef93a92 100644 --- a/OpenAuth.App/SSO/LoginParse.cs +++ b/OpenAuth.App/SSO/LoginParse.cs @@ -9,6 +9,10 @@ using Infrastructure.Helpers; using OpenAuth.Repository; using OpenAuth.Repository.Domain; using OpenAuth.Repository.Interface; +using SqlSugar; +using Infrastructure.Extensions.AutofacManager; +using Infrastructure.Utilities; +using Microsoft.AspNetCore.Http; namespace OpenAuth.App.SSO @@ -17,15 +21,27 @@ namespace OpenAuth.App.SSO { //这个地方使用IRepository 而不使用UserManagerApp是防止循环依赖 - public IRepository _app; + protected ISqlSugarClient SugarClient; private ICacheContext _cacheContext; private AppManager _appInfoService; - public LoginParse( AppManager infoService, ICacheContext cacheContext, IRepository userApp) + public LoginParse( AppManager infoService, ICacheContext cacheContext, ISqlSugarClient client) { _appInfoService = infoService; _cacheContext = cacheContext; - _app = userApp; + string tenantId = Define.DEFAULT_TENANT_ID; + var httpContextAccessor = AutofacContainerModule.GetService(); + if (httpContextAccessor != null) + { + tenantId = httpContextAccessor.GetTenantId(); + } + + if(tenantId != Define.DEFAULT_TENANT_ID) //如果不是默认租户,则使用租户id获取连接 + { + client = client.AsTenant().GetConnection(tenantId); + } + + SugarClient = client; } public LoginResult Do(PassportLoginRequest model) @@ -54,7 +70,7 @@ namespace OpenAuth.App.SSO } else { - sysUserInfo = _app.FirstOrDefault(u =>u.Account == model.Account); + sysUserInfo = SugarClient.Queryable().First(u =>u.Account == model.Account); } if (sysUserInfo == null)