From 3b405079c11858c221414d14692adc3f57ebfaaa Mon Sep 17 00:00:00 2001 From: xRain Date: Wed, 20 May 2026 09:38:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=91=BD=E5=90=8D=E7=A9=BA?= =?UTF-8?q?=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SimApiAuthCenter.cs | 111 +++++++++++++----- .../SimApiAuthCenterDto.cs | 29 ++++- .../SimApiAuthCenterMiddleware.cs | 4 +- .../SimApiAuthClient.cs | 4 +- .../SimApiIam.cs => AuthSDK/SimApiAuthIam.cs | 6 +- .../SimApiAuthIamDto.cs | 4 +- Controllers/SimApiAuthController.cs | 7 -- Controllers/SimApiCommonController.cs | 8 ++ Helpers/SimApiError.cs | 2 +- Middlewares/SimApiExceptionMiddleware.cs | 8 +- SimApiExtensions.cs | 12 +- 11 files changed, 139 insertions(+), 56 deletions(-) rename AuthGate/SimApiAuthGate.cs => AuthSDK/SimApiAuthCenter.cs (50%) rename AuthGate/SimApiAuthGateDto.cs => AuthSDK/SimApiAuthCenterDto.cs (53%) rename AuthGate/SimApiAuthGateMiddleware.cs => AuthSDK/SimApiAuthCenterMiddleware.cs (89%) rename AuthGate/SimApiAuthGateClient.cs => AuthSDK/SimApiAuthClient.cs (78%) rename AuthGate/SimApiIam.cs => AuthSDK/SimApiAuthIam.cs (87%) rename AuthGate/SimApiIamDto.cs => AuthSDK/SimApiAuthIamDto.cs (81%) diff --git a/AuthGate/SimApiAuthGate.cs b/AuthSDK/SimApiAuthCenter.cs similarity index 50% rename from AuthGate/SimApiAuthGate.cs rename to AuthSDK/SimApiAuthCenter.cs index a481c04..345bee7 100644 --- a/AuthGate/SimApiAuthGate.cs +++ b/AuthSDK/SimApiAuthCenter.cs @@ -1,15 +1,16 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Net.Http; using System.Net.Http.Json; using SimApi.Communications; using static SimApi.Helpers.SimApiError; -namespace SimApi.AuthGate; +namespace SimApi.AuthSDK; -public class SimApiAuthGate(SimApiAuthGateClient simapi) +public class SimApiAuthCenter(SimApiAuthClient simapi) { - #region AuthGate公开接口 + public SimApiAuthClient Client { get; } = simapi; + + #region 公共 /// /// 委托AuthCenter进行应用签名验证 @@ -21,13 +22,68 @@ public class SimApiAuthGate(SimApiAuthGateClient simapi) public void VerifySign(string appId, string timestamp, string nonce, string sign) { var http = new HttpClient(); - var url = $"{simapi.Server}/api/auth/sign/verify?appId={appId}×tamp={timestamp}&nonce={nonce}&sign={sign}"; + var url = $"{Client.Server}/api/auth/sign/verify?appId={appId}×tamp={timestamp}&nonce={nonce}&sign={sign}"; var result = http.PostAsJsonAsync(url, new { }).Result; var resp = result.Content.ReadFromJsonAsync().Result; ErrorWhenNull(resp, 400, "签名验证失败"); ErrorWhenFalse(resp.Code == 200, 400, "签名验证失败"); } + #endregion + + + #region 群组相关 + + /// + /// 根据profileId获取群组列表 + /// + /// + /// + public SimApiAuthCenterDto.GroupRelatedItem[]? GroupRelated(string profileId) + { + return Client.SignQuery("/api/auth/group/related", new { profileId }); + } + + /// + /// 根据关键字搜索群组,输入群组ID精准搜索 + /// + /// + /// + /// + /// + public SimApiAuthCenterDto.AppAndProfileItem[]? GroupSearch(string keyword, int skip = 0, int take = 20) + { + return Client.SignQuery("/api/auth/group/search", + new { keyword, skip, take }); + } + + /// + /// 使用组ID以及组内成员,管理员profile获取组的详细树结构 + /// + /// + /// + /// + public SimApiAuthCenterDto.GroupDetailTreeNode? GroupDetail(string groupId, string profileId) + { + return Client.SignQuery("/api/auth/group/detail", + new { profileId, groupId }); + } + + /// + /// 获取profile在本组的所有子组 + /// + /// + /// + /// + public string[]? GroupRelatedIndex(string groupId, string profileId) + { + return Client.SignQuery("/api/auth/internal/group/get-related-groupid", new { groupId, profileId }); + } + + #endregion + + #region Profile相关 + /// /// 根据关键字,搜索用户Profile,参数支持精准ID,用户手机号,用户邮箱,Profile名称模糊搜索 /// @@ -35,9 +91,9 @@ public class SimApiAuthGate(SimApiAuthGateClient simapi) /// /// /// - public SimApiAuthGateDto.AppAndProfileItem[]? ProfileSearch(string keyword, int skip = 0, int take = 20) + public SimApiAuthCenterDto.AppAndProfileItem[]? ProfileSearch(string keyword, int skip = 0, int take = 20) { - return simapi.SignQuery("/api/auth/profile/search", + return Client.SignQuery("/api/auth/profile/search", new { keyword, skip, take }); } @@ -46,9 +102,9 @@ public class SimApiAuthGate(SimApiAuthGateClient simapi) /// /// /// - public SimApiAuthGateDto.AppAndProfileItem[]? ProfileList(string[] ids) + public SimApiAuthCenterDto.AppAndProfileItem[]? ProfileList(string[] ids) { - return simapi.SignQuery("/api/auth/profile/list", new { ids }); + return Client.SignQuery("/api/auth/profile/list", new { ids }); } #endregion @@ -63,7 +119,7 @@ public class SimApiAuthGate(SimApiAuthGateClient simapi) /// public bool CheckIsAppOwner(string profileId, string applicationId) { - return simapi.SignQuery("/api/auth/internal/apps/check-owner", new + return Client.SignQuery("/api/auth/internal/apps/check-owner", new { ProfileId = profileId, AppId = applicationId, @@ -76,9 +132,9 @@ public class SimApiAuthGate(SimApiAuthGateClient simapi) /// /// /// - public SimApiAuthGateDto.AppAndProfileItem[]? GetAppList(string profileId, IEnumerable appIds) + public SimApiAuthCenterDto.AppAndProfileItem[]? GetAppList(string profileId, IEnumerable appIds) { - return simapi.SignQuery("/api/auth/internal/apps/related", new + return Client.SignQuery("/api/auth/internal/apps/related", new { ProfileId = profileId, AllowedAppIds = appIds, @@ -96,16 +152,17 @@ public class SimApiAuthGate(SimApiAuthGateClient simapi) /// /// /// - public SimApiAuthGateDto.GetCodeResponse GetLoginCode(string? scene = null, Dictionary? data = null, + public SimApiAuthCenterDto.GetCodeResponse GetLoginCode(string? scene = null, + Dictionary? data = null, string? backUrl = null) { - var code = simapi.SignQuery("/api/auth/login/code", + var code = Client.SignQuery("/api/auth/login/code", new { scene, data, backUrl }); - return new SimApiAuthGateDto.GetCodeResponse() + return new SimApiAuthCenterDto.GetCodeResponse() { Code = code!, - Server = simapi.Server, - FullUrl = $"{simapi.Server}/auth?code={code}" + Server = Client.Server, + FullUrl = $"{Client.Server}/auth?code={code}" }; } @@ -115,9 +172,9 @@ public class SimApiAuthGate(SimApiAuthGateClient simapi) /// /// /// - public SimApiAuthGateDto.LoginInfoResponse GetLoginInfo(string code, string? scene = null) + public SimApiAuthCenterDto.LoginInfoResponse GetLoginInfo(string code, string? scene = null) { - var resp = simapi.SignQuery("/api/auth/login/get", new { code }); + var resp = Client.SignQuery("/api/auth/login/get", new { code }); ErrorWhenNull(resp, 400232, "登录信息获取失败"); ErrorWhen(resp.Scene != scene, 403003, "登录场景不匹配"); return resp; @@ -135,17 +192,17 @@ public class SimApiAuthGate(SimApiAuthGateClient simapi) /// /// /// - public SimApiAuthGateDto.GetCodeResponse GetConfirmCode(string scene, string userId, + public SimApiAuthCenterDto.GetCodeResponse GetConfirmCode(string scene, string userId, Dictionary? data = null, string? backUrl = null) { - var code = simapi.SignQuery("/api/auth/confirm/code", + var code = Client.SignQuery("/api/auth/confirm/code", new { scene, data, backUrl, profileId = userId }); - return new SimApiAuthGateDto.GetCodeResponse() + return new SimApiAuthCenterDto.GetCodeResponse() { Code = code!, - Server = simapi.Server, - FullUrl = $"{simapi.Server}/confirm?code={code}" + Server = Client.Server, + FullUrl = $"{Client.Server}/confirm?code={code}" }; } @@ -156,9 +213,9 @@ public class SimApiAuthGate(SimApiAuthGateClient simapi) /// /// /// - public SimApiAuthGateDto.ConfirmResponse Confirm(string code, string scene, string? userId = null) + public SimApiAuthCenterDto.ConfirmResponse Confirm(string code, string scene, string? userId = null) { - var resp = simapi.SignQuery("/api/auth/confirm/get", new { code }); + var resp = Client.SignQuery("/api/auth/confirm/get", new { code }); ErrorWhenNull(resp, 403001, "安全确认码无效"); ErrorWhen(resp.ProfileId != userId, 403002, "安全确认身份不匹配"); ErrorWhen(resp.Scene != scene, 403003, "安全确认场景不匹配"); diff --git a/AuthGate/SimApiAuthGateDto.cs b/AuthSDK/SimApiAuthCenterDto.cs similarity index 53% rename from AuthGate/SimApiAuthGateDto.cs rename to AuthSDK/SimApiAuthCenterDto.cs index c2e4f32..3e476d9 100644 --- a/AuthGate/SimApiAuthGateDto.cs +++ b/AuthSDK/SimApiAuthCenterDto.cs @@ -1,14 +1,14 @@ using System; using System.Collections.Generic; -namespace SimApi.AuthGate; +namespace SimApi.AuthSDK; -public class SimApiAuthGateDto +public class SimApiAuthCenterDto { public class AppAndProfileItem { - public string Id { get; set; } = ""; - public string Name { get; set; } = ""; + public required string Id { get; set; } + public required string Name { get; set; } public string? Image { get; set; } public string? Description { get; set; } } @@ -37,4 +37,25 @@ public class SimApiAuthGateDto public required string Server { get; set; } public required string FullUrl { get; set; } } + + public class GroupRelatedItem + { + public required string Id { get; set; } + public required string Name { get; set; } + public string? Image { get; set; } + public string? Description { get; set; } + public bool IsOwner { get; set; } + public bool IsAdmin { get; set; } + public bool IsMember { get; set; } + } + + public class GroupDetailTreeNode + { + public required string Id { get; set; } + public required string Name { get; set; } + public string? Image { get; set; } + public string? Description { get; set; } + public int Sort { get; set; } + public List Children { get; set; } = []; + } } \ No newline at end of file diff --git a/AuthGate/SimApiAuthGateMiddleware.cs b/AuthSDK/SimApiAuthCenterMiddleware.cs similarity index 89% rename from AuthGate/SimApiAuthGateMiddleware.cs rename to AuthSDK/SimApiAuthCenterMiddleware.cs index d6821a1..3da6c1d 100644 --- a/AuthGate/SimApiAuthGateMiddleware.cs +++ b/AuthSDK/SimApiAuthCenterMiddleware.cs @@ -5,9 +5,9 @@ using SimApi.Communications; using SimApi.Configurations; using SimApi.Helpers; -namespace SimApi.AuthGate; +namespace SimApi.AuthSDK; -public class SimApiAuthGateMiddleware(RequestDelegate next, ILogger logger) +public class SimApiAuthCenterMiddleware(RequestDelegate next, ILogger logger) { public Task Invoke(HttpContext httpContext, SimApiOptions simApiOptions) { diff --git a/AuthGate/SimApiAuthGateClient.cs b/AuthSDK/SimApiAuthClient.cs similarity index 78% rename from AuthGate/SimApiAuthGateClient.cs rename to AuthSDK/SimApiAuthClient.cs index a41929f..a7dc563 100644 --- a/AuthGate/SimApiAuthGateClient.cs +++ b/AuthSDK/SimApiAuthClient.cs @@ -2,9 +2,9 @@ using SimApi.Configurations; using SimApi.Helpers; -namespace SimApi.AuthGate; +namespace SimApi.AuthSDK; -public class SimApiAuthGateClient(SimApiOptions apiOptions, ILogger logger) +public class SimApiAuthClient(SimApiOptions apiOptions, ILogger logger) : SimApiHttpClient(apiOptions, logger) { public override string Server { get; init; } = apiOptions.SimApiAuthGateOptions.Server ?? string.Empty; diff --git a/AuthGate/SimApiIam.cs b/AuthSDK/SimApiAuthIam.cs similarity index 87% rename from AuthGate/SimApiIam.cs rename to AuthSDK/SimApiAuthIam.cs index 033ec3e..c18b13a 100644 --- a/AuthGate/SimApiIam.cs +++ b/AuthSDK/SimApiAuthIam.cs @@ -1,15 +1,15 @@ using Microsoft.Extensions.Logging; using static SimApi.Helpers.SimApiError; -namespace SimApi.AuthGate; +namespace SimApi.AuthSDK; -public class SimApiIam(SimApiAuthGateClient simapi, ILogger logger) +public class SimApiAuthIam(SimApiAuthClient simapi, ILogger logger) { /// /// 向Iam注册权限 /// /// - public void RegisterPermissions(SimApiIamDto.PermissionItem[] permissions) + public void RegisterPermissions(SimApiAuthIamDto.PermissionItem[] permissions) { var log = $"检测到 {permissions.Length} 个权限接口,正在注册..: "; foreach (var permission in permissions) diff --git a/AuthGate/SimApiIamDto.cs b/AuthSDK/SimApiAuthIamDto.cs similarity index 81% rename from AuthGate/SimApiIamDto.cs rename to AuthSDK/SimApiAuthIamDto.cs index 8a3683c..9a3ccf3 100644 --- a/AuthGate/SimApiIamDto.cs +++ b/AuthSDK/SimApiAuthIamDto.cs @@ -1,6 +1,6 @@ -namespace SimApi.AuthGate; +namespace SimApi.AuthSDK; -public class SimApiIamDto +public class SimApiAuthIamDto { public class PermissionItem { diff --git a/Controllers/SimApiAuthController.cs b/Controllers/SimApiAuthController.cs index fb7e6f7..bc8e481 100644 --- a/Controllers/SimApiAuthController.cs +++ b/Controllers/SimApiAuthController.cs @@ -19,11 +19,4 @@ public class SimApiAuthController(SimApiAuth auth) : SimApiBaseController auth.Logout(value!); } } - - /// - /// 获取已登录用户信息 - /// - /// - [HttpPost, SimApiAuth, SimApiDoc("认证", "获取已登录用户信息")] - public SimApiLoginItem UserInfo() => LoginInfo; } \ No newline at end of file diff --git a/Controllers/SimApiCommonController.cs b/Controllers/SimApiCommonController.cs index 24b95ab..57dcf28 100644 --- a/Controllers/SimApiCommonController.cs +++ b/Controllers/SimApiCommonController.cs @@ -31,4 +31,12 @@ public class SimApiCommonController : SimApiBaseController { "App", SimApiUtil.AppVersion } }; } + + + /// + /// 获取已登录用户信息 + /// + /// + [HttpPost, SimApiAuth, SimApiDoc("认证", "获取已登录用户信息")] + public SimApiLoginItem UserInfo() => LoginInfo; } \ No newline at end of file diff --git a/Helpers/SimApiError.cs b/Helpers/SimApiError.cs index 77bd715..c5491c7 100644 --- a/Helpers/SimApiError.cs +++ b/Helpers/SimApiError.cs @@ -65,7 +65,7 @@ public static class SimApiError /// 错误代码 /// 错误描述 public static void ErrorWhenNull([NotNull] object? condition, int code = 404, - string message = "请求的资源不存在") + string message = "") { ErrorWhen(condition == null, code, message); } diff --git a/Middlewares/SimApiExceptionMiddleware.cs b/Middlewares/SimApiExceptionMiddleware.cs index 66c5ae7..2b0a5da 100644 --- a/Middlewares/SimApiExceptionMiddleware.cs +++ b/Middlewares/SimApiExceptionMiddleware.cs @@ -45,11 +45,15 @@ public class SimApiExceptionMiddleware( response = string.IsNullOrEmpty(simEx.Message) ? new SimApiBaseResponse(simEx.Code) : new SimApiBaseResponse(simEx.Code, simEx.Message); + if (context.Response.StatusCode == 404) + { + response.Message = "接口不存在"; + } } else { - log.LogError(ex, "服务器异常"); - response = new SimApiBaseResponse(500, "服务器错误"); + log.LogError(ex, ex.Message); + response = new SimApiBaseResponse(500); } await ErrorResponseAsync(context, response); diff --git a/SimApiExtensions.cs b/SimApiExtensions.cs index 03515b8..d4a33e4 100644 --- a/SimApiExtensions.cs +++ b/SimApiExtensions.cs @@ -16,7 +16,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using SimApi.Attributes; -using SimApi.AuthGate; +using SimApi.AuthSDK; using SimApi.Configurations; using SimApi.Interfaces; using SimApi.Logger; @@ -331,11 +331,11 @@ public static class SimApiExtensions if (simApiOptions.EnableSimApiAuthGate) { - builder.AddSingleton(); - builder.AddSingleton(); + builder.AddSingleton(); + builder.AddSingleton(); if (simApiOptions.SimApiAuthGateOptions.UseIam) { - builder.AddSingleton(); + builder.AddSingleton(); } } @@ -434,7 +434,7 @@ public static class SimApiExtensions { if (options.SimApiAuthGateOptions.UseMiddleware) { - builder.UseMiddleware(); + builder.UseMiddleware(); } } } @@ -463,7 +463,7 @@ public static class SimApiExtensions builder.MapControllerRoute(name: "UserInfo", pattern: options.SimApiRouteOptions.UserInfoRoute, defaults: new { - controller = "SimApiAuth", + controller = "SimApiCommon", action = "UserInfo" }); }