Compare commits

...
4 Commits
Author SHA1 Message Date
xrain 106d3eec19 fix cache 2026-04-27 00:04:29 +08:00
xrain a8c7a20a64 fix 2026-04-26 18:43:34 +08:00
xrain b6dbcd6cee 简化了内置控制器返回写法 2026-04-26 12:27:56 +08:00
xrain b7775cbe6c fix logout bug 2026-04-26 11:33:39 +08:00
5 changed files with 29 additions and 45 deletions
+4 -4
View File
@@ -20,7 +20,7 @@ public class CoceController(CoceApp coce, SimApiAuth auth, IServiceProvider sp)
}
[HttpPost]
public SimApiBaseResponse<string> Login([FromBody] SimApiOneFieldRequest<string> request)
public string Login([FromBody] SimApiOneFieldRequest<string> request)
{
var data = coce.GetLevelToken(request.Data!);
ErrorWhenNull(data, 400);
@@ -39,14 +39,14 @@ public class CoceController(CoceApp coce, SimApiAuth auth, IServiceProvider sp)
};
var processor = sp.GetService<ICoceLoginProcessor>();
processor?.Process(loginItem, groups.ToArray());
return new SimApiBaseResponse<string>(auth.Login(loginItem));
return auth.Login(loginItem);
}
[HttpPost, SimApiAuth]
public SimApiBaseResponse<GroupInfo[]> ListGroups()
public GroupInfo[] ListGroups()
{
var levelToken = coce.GetToken(LoginInfo.Id!);
var groups = coce.GetUserGroups(levelToken!)!;
return new SimApiBaseResponse<GroupInfo[]>(groups.ToArray());
return groups.ToArray();
}
}
+6 -18
View File
@@ -14,13 +14,10 @@ public class SimApiAuthController(SimApiAuth auth) : SimApiBaseController
/// </summary>
/// <returns></returns>
[HttpPost, SimApiDoc("认证", "检测登陆")]
public SimApiBaseResponse<string> CheckLogin()
public string CheckLogin()
{
ErrorWhenNull(LoginInfo, 401, "未登录");
return new SimApiBaseResponse<string>
{
Data = LoginInfo.Id
};
return LoginInfo.Id;
}
/// <summary>
@@ -28,24 +25,15 @@ public class SimApiAuthController(SimApiAuth auth) : SimApiBaseController
/// </summary>
/// <returns></returns>
[HttpPost, SimApiDoc("认证", "退出登陆")]
public SimApiBaseResponse Logout()
public void Logout()
{
ErrorWhenNull(null);
string? token = null;
if (Request.Headers.TryGetValue("Token", out var value))
{
token = value;
auth.Logout(value!);
}
auth.Logout(token!);
return new SimApiBaseResponse();
}
[HttpPost, SimApiAuth]
public SimApiBaseResponse<SimApiLoginItem> UserInfo()
{
return new SimApiBaseResponse<SimApiLoginItem>(LoginInfo);
}
[HttpPost, SimApiAuth, SimApiDoc("认证", "获取已登录用户信息")]
public SimApiLoginItem UserInfo() => LoginInfo;
}
+6 -10
View File
@@ -1,6 +1,5 @@
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using SimApi.Communications;
using SimApi.Helpers;
namespace SimApi.Controllers;
@@ -14,22 +13,19 @@ public class SimApiCommonController : SimApiBaseController
/// <returns></returns>
[HttpGet("exception/{code:int}")]
[ApiExplorerSettings(IgnoreApi = true)]
public SimApiBaseResponse ExceptionHandler(int code)
public void ExceptionHandler(int code)
{
return new SimApiBaseResponse(code);
SimApiError.Error(code);
}
[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 -1
View File
@@ -15,6 +15,7 @@ public class SimApiCache(IDistributedCache cache)
/// <param name="options"></param>
public void Set(string key, object value, DistributedCacheEntryOptions? options = null)
{
SimApiError.ErrorWhenNull(value, 400, "缓存值不能为null");
if (options is not null)
{
cache.SetString(Prefix + key, SimApiUtil.Json(value), options);
@@ -34,6 +35,16 @@ public class SimApiCache(IDistributedCache cache)
cache.Remove(Prefix + key);
}
/// <summary>
/// 缓存Key是否存在
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool HasKey(string key)
{
return Get<string>(key) != null;
}
/// <summary>
/// 获取string类型缓存
/// </summary>
@@ -41,7 +52,7 @@ public class SimApiCache(IDistributedCache cache)
/// <returns></returns>
public string? Get(string key)
{
return cache.GetString(Prefix + key);
return Get<string>(Prefix + key);
}
/// <summary>
+1 -12
View File
@@ -11,6 +11,7 @@ public static class SimApiError
/// <param name="code">错误代码</param>
/// <param name="message">错误描述(若是常规错误,代码可自动带取描述)</param>
/// <returns></returns>
[DoesNotReturn]
public static void Error(int code = 500, string message = "")
{
throw new SimApiException(code, message);
@@ -68,16 +69,4 @@ public static class SimApiError
{
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);
}
}