简化了内置控制器返回写法

This commit is contained in:
2026-04-26 12:27:56 +08:00
parent b7775cbe6c
commit b6dbcd6cee
4 changed files with 16 additions and 43 deletions
+4 -4
View File
@@ -20,7 +20,7 @@ public class CoceController(CoceApp coce, SimApiAuth auth, IServiceProvider sp)
} }
[HttpPost] [HttpPost]
public SimApiBaseResponse<string> Login([FromBody] SimApiOneFieldRequest<string> request) public string Login([FromBody] SimApiOneFieldRequest<string> request)
{ {
var data = coce.GetLevelToken(request.Data!); var data = coce.GetLevelToken(request.Data!);
ErrorWhenNull(data, 400); ErrorWhenNull(data, 400);
@@ -39,14 +39,14 @@ public class CoceController(CoceApp coce, SimApiAuth auth, IServiceProvider sp)
}; };
var processor = sp.GetService<ICoceLoginProcessor>(); var processor = sp.GetService<ICoceLoginProcessor>();
processor?.Process(loginItem, groups.ToArray()); processor?.Process(loginItem, groups.ToArray());
return new SimApiBaseResponse<string>(auth.Login(loginItem)); return auth.Login(loginItem);
} }
[HttpPost, SimApiAuth] [HttpPost, SimApiAuth]
public SimApiBaseResponse<GroupInfo[]> ListGroups() public GroupInfo[] ListGroups()
{ {
var levelToken = coce.GetToken(LoginInfo.Id!); var levelToken = coce.GetToken(LoginInfo.Id!);
var groups = coce.GetUserGroups(levelToken!)!; var groups = coce.GetUserGroups(levelToken!)!;
return new SimApiBaseResponse<GroupInfo[]>(groups.ToArray()); return groups.ToArray();
} }
} }
+6 -17
View File
@@ -14,13 +14,10 @@ public class SimApiAuthController(SimApiAuth auth) : SimApiBaseController
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
[HttpPost, SimApiDoc("认证", "检测登陆")] [HttpPost, SimApiDoc("认证", "检测登陆")]
public SimApiBaseResponse<string> CheckLogin() public string CheckLogin()
{ {
ErrorWhenNull(LoginInfo, 401, "未登录"); ErrorWhenNull(LoginInfo, 401, "未登录");
return new SimApiBaseResponse<string> return LoginInfo.Id;
{
Data = LoginInfo.Id
};
} }
/// <summary> /// <summary>
@@ -28,23 +25,15 @@ public class SimApiAuthController(SimApiAuth auth) : SimApiBaseController
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
[HttpPost, SimApiDoc("认证", "退出登陆")] [HttpPost, SimApiDoc("认证", "退出登陆")]
public SimApiBaseResponse Logout() public void Logout()
{ {
string? token = null;
if (Request.Headers.TryGetValue("Token", out var value)) if (Request.Headers.TryGetValue("Token", out var value))
{ {
token = value; auth.Logout(value!);
} }
auth.Logout(token!);
return new SimApiBaseResponse();
} }
[HttpPost, SimApiAuth] [HttpPost, SimApiAuth, SimApiDoc("认证", "获取已登录用户信息")]
public SimApiBaseResponse<SimApiLoginItem> UserInfo() public SimApiLoginItem UserInfo() => LoginInfo;
{
return new SimApiBaseResponse<SimApiLoginItem>(LoginInfo);
}
} }
+6 -10
View File
@@ -1,6 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using SimApi.Communications;
using SimApi.Helpers; using SimApi.Helpers;
namespace SimApi.Controllers; namespace SimApi.Controllers;
@@ -14,22 +13,19 @@ public class SimApiCommonController : SimApiBaseController
/// <returns></returns> /// <returns></returns>
[HttpGet("exception/{code:int}")] [HttpGet("exception/{code:int}")]
[ApiExplorerSettings(IgnoreApi = true)] [ApiExplorerSettings(IgnoreApi = true)]
public SimApiBaseResponse ExceptionHandler(int code) public void ExceptionHandler(int code)
{ {
return new SimApiBaseResponse(code); SimApiError.Error(code);
} }
[HttpPost, HttpGet] [HttpPost, HttpGet]
public SimApiBaseResponse<Dictionary<string, string>> Versions() public Dictionary<string, string> Versions()
{ {
return new SimApiBaseResponse<Dictionary<string, string>>() return new Dictionary<string, string>
{ {
Data = new Dictionary<string, string> { "SimApi", SimApiUtil.SimApiVersion },
{ { "App", SimApiUtil.AppVersion }
{ "SimApi", SimApiUtil.SimApiVersion },
{ "App", SimApiUtil.AppVersion }
}
}; };
} }
} }
-12
View File
@@ -68,16 +68,4 @@ public static class SimApiError
{ {
ErrorWhen(condition == null, code, message); ErrorWhen(condition == null, code, message);
} }
/// <summary>
/// 检测给定的泛型变量是否为Null(提供更精确的编译器 null 流分析)
/// </summary>
/// <param name="condition">检测条件</param>
/// <param name="code">错误代码</param>
/// <param name="message">错误描述</param>
public static void ErrorWhenNull<T>([NotNull] T? condition, int code = 404,
string message = "请求的资源不存在") where T : class
{
ErrorWhen(condition == null, code, message);
}
} }