Compare commits

..
2 Commits
Author SHA1 Message Date
xrain 9a8d14142a version move to common 2025-11-01 21:38:55 +08:00
xrain cb89a1a003 doc only appear in current group 2025-11-01 18:40:51 +08:00
4 changed files with 82 additions and 59 deletions
+11 -16
View File
@@ -6,22 +6,22 @@ namespace SimApi.Configurations;
/// <summary> /// <summary>
/// 文档组配置 /// 文档组配置
/// </summary> /// </summary>
public class SimApiDocGroupOption public class SimApiDocGroupOption(string id, string name, string description = "")
{ {
/// <summary> /// <summary>
/// 文档标识 /// 文档标识
/// </summary> /// </summary>
public string Id { get; set; } = null!; public string Id { get; set; } = id;
/// <summary> /// <summary>
/// 文档名称 /// 文档名称
/// </summary> /// </summary>
public string Name { get; set; } = null!; public string Name { get; set; } = name;
/// <summary> /// <summary>
/// 文档描述 /// 文档描述
/// </summary> /// </summary>
public string Description { get; set; } = null!; public string Description { get; set; } = description!;
} }
/// <summary> /// <summary>
@@ -29,7 +29,7 @@ public class SimApiDocGroupOption
/// </summary> /// </summary>
public class SimApiAuthOption public class SimApiAuthOption
{ {
public string[] Type { get; set; } = new[] { "SimApiAuth" }; public string[] Type { get; set; } = ["SimApiAuth"];
public string Description { get; set; } = "认证服务器颁发的AccessToken"; public string Description { get; set; } = "认证服务器颁发的AccessToken";
@@ -48,20 +48,15 @@ public class SimApiDocOptions
/// <summary> /// <summary>
/// 文档组配置 /// 文档组配置
/// </summary> /// </summary>
public SimApiDocGroupOption[] ApiGroups { get; set; } = new[] public SimApiDocGroupOption[] ApiGroups { get; set; } =
{ [
new SimApiDocGroupOption new("api", "Api", "Api接口文档")
{ ];
Id = "api",
Name = "Api",
Description = "Api接口文档"
}
};
/// <summary> /// <summary>
/// 授权配置 /// 授权配置
/// </summary> /// </summary>
public SimApiAuthOption ApiAuth { get; set; } = new SimApiAuthOption(); public SimApiAuthOption ApiAuth { get; set; } = new();
/// <summary> /// <summary>
/// 文档页面标题 /// 文档页面标题
@@ -71,5 +66,5 @@ public class SimApiDocOptions
/// <summary> /// <summary>
/// 接口支持的调用方式 /// 接口支持的调用方式
/// </summary> /// </summary>
public SubmitMethod[] SupportedMethod { get; set; } = new[] { SubmitMethod.Post }; public SubmitMethod[] SupportedMethod { get; set; } = [SubmitMethod.Post];
} }
+49
View File
@@ -0,0 +1,49 @@
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using SimApi.Attributes;
using SimApi.Communications;
using SimApi.Helpers;
namespace SimApi.Controllers;
public class SimApiAuthController(SimApiAuth auth) : SimApiBaseController
{
/// <summary>
/// 检测用户登陆的控制器
/// </summary>
/// <returns></returns>
[HttpPost, SimApiDoc("认证", "检测登陆")]
public SimApiBaseResponse<string> CheckLogin()
{
ErrorWhenNull(LoginInfo, 401, "未登录");
return new SimApiBaseResponse<string>
{
Data = LoginInfo.Id
};
}
/// <summary>
/// 退出登陆
/// </summary>
/// <returns></returns>
[HttpPost, SimApiDoc("认证", "退出登陆")]
public SimApiBaseResponse Logout()
{
string? token = null;
if (Request.Headers.TryGetValue("Token", out var value))
{
token = value;
}
auth.Logout(token!);
return new SimApiBaseResponse();
}
[HttpPost, SimApiAuth]
public SimApiBaseResponse<SimApiLoginItem> UserInfo()
{
return new SimApiBaseResponse<SimApiLoginItem>(LoginInfo);
}
}
+2 -40
View File
@@ -1,12 +1,11 @@
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using SimApi.Attributes;
using SimApi.Communications; using SimApi.Communications;
using SimApi.Helpers; using SimApi.Helpers;
namespace SimApi.Controllers; namespace SimApi.Controllers;
public class SimApiCommonController(SimApiAuth auth) : SimApiBaseController public class SimApiCommonController : SimApiBaseController
{ {
/// <summary> /// <summary>
/// 错误回馈页面 /// 错误回馈页面
@@ -20,37 +19,6 @@ public class SimApiCommonController(SimApiAuth auth) : SimApiBaseController
return new SimApiBaseResponse(code); return new SimApiBaseResponse(code);
} }
/// <summary>
/// 检测用户登陆的控制器
/// </summary>
/// <returns></returns>
[HttpPost, SimApiDoc("认证", "检测登陆")]
public SimApiBaseResponse<string> CheckLogin()
{
ErrorWhenNull(LoginInfo, 401, "未登录");
return new SimApiBaseResponse<string>
{
Data = LoginInfo.Id
};
}
/// <summary>
/// 退出登陆
/// </summary>
/// <returns></returns>
[HttpPost, SimApiDoc("认证", "退出登陆")]
public SimApiBaseResponse Logout()
{
string? token = null;
if (Request.Headers.TryGetValue("Token", out var value))
{
token = value;
}
auth.Logout(token!);
return new SimApiBaseResponse();
}
[HttpPost, HttpGet] [HttpPost, HttpGet]
public SimApiBaseResponse<Dictionary<string, string>> Versions() public SimApiBaseResponse<Dictionary<string, string>> Versions()
@@ -64,10 +32,4 @@ public class SimApiCommonController(SimApiAuth auth) : SimApiBaseController
} }
}; };
} }
[HttpPost, SimApiAuth]
public SimApiBaseResponse<SimApiLoginItem> UserInfo()
{
return new SimApiBaseResponse<SimApiLoginItem>(LoginInfo);
}
} }
+20 -3
View File
@@ -12,6 +12,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Models;
using SimApi.Middlewares; using SimApi.Middlewares;
using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using SimApi.Attributes; using SimApi.Attributes;
@@ -128,6 +129,22 @@ public static class SimApiExtensions
x.OperationFilter<SimApiAuthOperationFilter>(); x.OperationFilter<SimApiAuthOperationFilter>();
} }
x.DocInclusionPredicate((docName, apiDesc) =>
{
// 获取接口标记的 GroupName(未标记则为 null
var actionGroupName = apiDesc.ActionDescriptor.EndpointMetadata
.OfType<ApiExplorerSettingsAttribute>()
.FirstOrDefault()?.GroupName;
// 情况1:接口未标记任何 GroupNameactionGroupName 为 null
if (actionGroupName == null)
{
return docName == "api"; // 未分组接口只属于默认分组v1
}
return docName == actionGroupName;
});
x.EnableAnnotations(); x.EnableAnnotations();
var haveOauth = false; var haveOauth = false;
var oauthFlows = new OpenApiOAuthFlows(); var oauthFlows = new OpenApiOAuthFlows();
@@ -308,19 +325,19 @@ public static class SimApiExtensions
builder.MapControllerRoute(name: "GetUserInfo", pattern: "/user/info", builder.MapControllerRoute(name: "GetUserInfo", pattern: "/user/info",
defaults: new defaults: new
{ {
controller = "SimApiCommon", controller = "SimApiAuth",
action = "UserInfo" action = "UserInfo"
}); });
builder.MapControllerRoute(name: "CheckLogin", pattern: "/auth/check", builder.MapControllerRoute(name: "CheckLogin", pattern: "/auth/check",
defaults: new defaults: new
{ {
controller = "SimApiCommon", controller = "SimApiAuth",
action = "CheckLogin" action = "CheckLogin"
}); });
builder.MapControllerRoute(name: "Logout", pattern: "/auth/logout", builder.MapControllerRoute(name: "Logout", pattern: "/auth/logout",
defaults: new defaults: new
{ {
controller = "SimApiCommon", controller = "SimApiAuth",
action = "Logout" action = "Logout"
}); });
if (options.EnableCoceSdk) if (options.EnableCoceSdk)