Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9a8d14142a | ||
|
|
cb89a1a003 | ||
|
|
462bce50cc |
@@ -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];
|
||||||
}
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
+2
-2
@@ -3,20 +3,20 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Library</OutputType>
|
<OutputType>Library</OutputType>
|
||||||
<PackOnBuild>true</PackOnBuild>
|
<PackOnBuild>true</PackOnBuild>
|
||||||
|
<IsPackable>true</IsPackable>
|
||||||
<Version>0.0.0</Version>
|
<Version>0.0.0</Version>
|
||||||
<Authors>xRain@SimcuTeam</Authors>
|
<Authors>xRain@SimcuTeam</Authors>
|
||||||
<Description>AspNetCore一个方便的API文档,捕获异常,统一输入输出的API类库</Description>
|
<Description>AspNetCore一个方便的API文档,捕获异常,统一输入输出的API类库</Description>
|
||||||
<PackageId>Simcu.SimApi</PackageId>
|
<PackageId>Simcu.SimApi</PackageId>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
|
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
|
||||||
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="Communications\"/>
|
<Folder Include="Communications\"/>
|
||||||
<Folder Include="Exceptions\"/>
|
<Folder Include="Exceptions\"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
||||||
<PackageReference Include="Hangfire.AspNetCore" Version="1.8.21" />
|
<PackageReference Include="Hangfire.AspNetCore" Version="1.8.21" />
|
||||||
<PackageReference Include="Hangfire.Console" Version="1.4.3"/>
|
<PackageReference Include="Hangfire.Console" Version="1.4.3"/>
|
||||||
<PackageReference Include="Hangfire.Redis.StackExchange" Version="1.12.0"/>
|
<PackageReference Include="Hangfire.Redis.StackExchange" Version="1.12.0"/>
|
||||||
|
|||||||
+20
-3
@@ -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:接口未标记任何 GroupName(actionGroupName 为 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)
|
||||||
|
|||||||
Reference in New Issue
Block a user