diff --git a/Communications/Base.cs b/Communications/Base.cs index daf191f..d891bdb 100644 --- a/Communications/Base.cs +++ b/Communications/Base.cs @@ -4,6 +4,9 @@ using System.Text.Json.Serialization; namespace YYApi.Communications { + /// + /// 基础相应 + /// public class BaseResponse { /// @@ -37,6 +40,7 @@ namespace YYApi.Communications SetCode(200); } + /// /// 返回指定代码的描述 /// @@ -90,8 +94,20 @@ namespace YYApi.Communications { IgnoreReadOnlyProperties = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase, - Converters = { new JsonStringEnumConverter() } + Converters = {new JsonStringEnumConverter()} }); } } + + /// + /// 动态内容 + /// + /// + public class BaseResponse : BaseResponse + { + /// + /// 动态内容 + /// + public T Data { get; set; } + } } \ No newline at end of file diff --git a/Controllers/BaseController.cs b/Controllers/BaseController.cs index f972104..de0467f 100644 --- a/Controllers/BaseController.cs +++ b/Controllers/BaseController.cs @@ -18,7 +18,7 @@ namespace YYApi.Controllers /// /// 当前登录用户的ID /// - protected int LoginId => HttpContext.Items["LoginId"] == null ? 0 : int.Parse(HttpContext.Items["LoginId"].ToString()); + protected LoginInfoItem LoginInfo => (LoginInfoItem)HttpContext.Items["LoginInfo"]; /// /// 验证请求参数 diff --git a/Helpers/AuthMiddleware.cs b/Helpers/AuthMiddleware.cs index 0153453..604d60c 100644 --- a/Helpers/AuthMiddleware.cs +++ b/Helpers/AuthMiddleware.cs @@ -1,4 +1,5 @@ using System; +using System.Text.Json; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.Filters; @@ -28,10 +29,10 @@ namespace YYApi.Helpers if (!string.IsNullOrEmpty(token)) { - var id = cache.GetString(token); - if (id != null) + var login = cache.GetString(token); + if (login != null) { - httpContext.Items.Add("LoginId", id); + httpContext.Items.Add("LoginInfo", JsonSerializer.Deserialize(login)); } } @@ -39,6 +40,17 @@ namespace YYApi.Helpers } } + /// + /// 登录信息中间件 + /// + public class LoginInfoItem + { + //登录用户的ID + public int Id { get; set; } + //登录用户来源 + public string Type { get; set; } + } + /// /// 检测登录中间件 /// @@ -70,10 +82,15 @@ namespace YYApi.Helpers /// /// /// - public string SetId(int id) + public string SetId(int id, string type = "user") { var uuid = GetUUID(); - Cache.SetString(uuid, id.ToString()); + var loginItem = new LoginInfoItem + { + Id = id, + Type = type + }; + Cache.SetString(uuid, JsonSerializer.Serialize(loginItem)); return uuid; } diff --git a/YYApi.csproj b/YYApi.csproj index 41d7e5e..bb27615 100644 --- a/YYApi.csproj +++ b/YYApi.csproj @@ -4,14 +4,13 @@ netcoreapp3.1 Library true - 0.1.0 + 0.1.3 xRain@YYTech AspNetCore一个方便的API文档,捕获异常,统一输入输出的API类库 YY-Tech.YYApi true snupkg true - 0.1.2