Compare commits

...
2 Commits
Author SHA1 Message Date
xrain 2257ba5b2a new feature 2024-06-27 04:38:04 +08:00
xrain c40f8d664d default exception edit 2024-04-23 12:27:36 +08:00
7 changed files with 59 additions and 30 deletions
+4 -2
View File
@@ -1,6 +1,8 @@
namespace SimApi.Communications; using System.Collections.Generic;
namespace SimApi.Communications;
/// <summary> /// <summary>
/// 登录信息中间件 /// 登录信息中间件
/// </summary> /// </summary>
public record SimApiLoginItem(string Id, string[] Type); public record SimApiLoginItem(string Id, string[] Type,Dictionary<string,string> Meta = null);
+4 -4
View File
@@ -55,7 +55,7 @@ public class SimApiBaseController : Controller
/// <param name="condition">检测条件</param> /// <param name="condition">检测条件</param>
/// <param name="code">错误代码</param> /// <param name="code">错误代码</param>
/// <param name="message">错误描述</param> /// <param name="message">错误描述</param>
protected static void ErrorWhen(bool condition, int code = 500, string message = "") protected static void ErrorWhen(bool condition, int code = 400, string message = "")
{ {
if (condition) if (condition)
{ {
@@ -70,7 +70,7 @@ public class SimApiBaseController : Controller
/// <param name="condition">检测条件</param> /// <param name="condition">检测条件</param>
/// <param name="code">错误代码</param> /// <param name="code">错误代码</param>
/// <param name="message">错误描述</param> /// <param name="message">错误描述</param>
protected static void ErrorWhenTrue(bool condition, int code = 500, string message = "") protected static void ErrorWhenTrue(bool condition, int code = 400, string message = "")
{ {
ErrorWhen(condition, code, message); ErrorWhen(condition, code, message);
} }
@@ -82,7 +82,7 @@ public class SimApiBaseController : Controller
/// <param name="condition"></param> /// <param name="condition"></param>
/// <param name="code"></param> /// <param name="code"></param>
/// <param name="message"></param> /// <param name="message"></param>
protected static void ErrorWhenFalse(bool condition, int code = 500, string message = "") protected static void ErrorWhenFalse(bool condition, int code = 400, string message = "")
{ {
ErrorWhen(!condition, code, message); ErrorWhen(!condition, code, message);
} }
@@ -93,7 +93,7 @@ public class SimApiBaseController : Controller
/// <param name="condition">检测条件</param> /// <param name="condition">检测条件</param>
/// <param name="code">错误代码</param> /// <param name="code">错误代码</param>
/// <param name="message">错误描述</param> /// <param name="message">错误描述</param>
protected static void ErrorWhenNull(object condition, int code = 404, string message = "") protected static void ErrorWhenNull(object condition, int code = 404, string message = "请求的资源不存在")
{ {
ErrorWhen(condition == null, code, message); ErrorWhen(condition == null, code, message);
} }
+6
View File
@@ -51,4 +51,10 @@ public class SimApiCommonController(SimApiAuth auth) : SimApiBaseController
auth.Logout(token!); auth.Logout(token!);
return new SimApiBaseResponse(); return new SimApiBaseResponse();
} }
[HttpPost("/logined"),SimApiAuth]
public SimApiBaseResponse<SimApiLoginItem> UserInfo()
{
return new SimApiBaseResponse<SimApiLoginItem>(LoginInfo);
}
} }
+23 -4
View File
@@ -1,5 +1,6 @@
#nullable enable #nullable enable
using System; using System;
using System.Collections.Generic;
using System.Text.Json; using System.Text.Json;
using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Caching.Distributed;
using SimApi.Communications; using SimApi.Communications;
@@ -16,11 +17,12 @@ public class SimApiAuth(IDistributedCache cache)
/// </summary> /// </summary>
/// <param name="id"></param> /// <param name="id"></param>
/// <param name="type"></param> /// <param name="type"></param>
/// <param name="meta"></param>
/// <param name="token"></param> /// <param name="token"></param>
/// <returns></returns> /// <returns></returns>
public string Login(string id, string type = "user", string token = null) public string Login(string id, Dictionary<string, string>? meta = null, string type = "user", string? token = null)
{ {
return Login(id, new[] { type }, token); return Login(id, meta, new[] { type }, token);
} }
/// <summary> /// <summary>
@@ -28,16 +30,33 @@ public class SimApiAuth(IDistributedCache cache)
/// </summary> /// </summary>
/// <param name="id"></param> /// <param name="id"></param>
/// <param name="type"></param> /// <param name="type"></param>
/// <param name="meta"></param>
/// <param name="uuid"></param> /// <param name="uuid"></param>
/// <returns></returns> /// <returns></returns>
public string Login(string id, string[] type, string uuid = null) // ReSharper disable once MemberCanBePrivate.Global
public string Login(string id, Dictionary<string, string>? meta, string[] type, string? uuid = null)
{ {
uuid ??= Guid.NewGuid().ToString(); uuid ??= Guid.NewGuid().ToString();
var loginItem = new SimApiLoginItem(id, type); var loginItem = new SimApiLoginItem(id, type, meta);
cache.SetString(uuid, JsonSerializer.Serialize(loginItem)); cache.SetString(uuid, JsonSerializer.Serialize(loginItem));
return uuid; return uuid;
} }
/// <summary>
/// 设置登录的Meta信息
/// </summary>
/// <param name="token"></param>
/// <param name="meta"></param>
/// <returns></returns>
public bool SetMeta(string token, Dictionary<string, string> meta)
{
var login = GetLogin(token);
if (login == null) { return false; }
var newLogin = new SimApiLoginItem(login.Id, login.Type, login.Meta);
cache.SetString(token,JsonSerializer.Serialize(newLogin));
return true;
}
/// <summary> /// <summary>
/// 获取登陆信息 /// 获取登陆信息
/// </summary> /// </summary>
+1 -1
View File
@@ -14,7 +14,7 @@ public class SimApiStorage
private string ServeUrl { get; } private string ServeUrl { get; }
private string Bucket { get; } public string Bucket { get; }
private IHttpContextAccessor HttpContextAccessor { get; } private IHttpContextAccessor HttpContextAccessor { get; }
+5 -6
View File
@@ -51,16 +51,15 @@ public static class SimApiUtil
/// <returns></returns> /// <returns></returns>
public static string Md5(string source, string mode = "x2") public static string Md5(string source, string mode = "x2")
{ {
var sor = Encoding.UTF8.GetBytes(source); var sourceBytes = Encoding.UTF8.GetBytes(source);
var md5 = MD5.Create(); var result = MD5.HashData(sourceBytes);
var result = md5.ComputeHash(sor); var stringBuilder = new StringBuilder(40);
var strbul = new StringBuilder(40);
foreach (var t in result) foreach (var t in result)
{ {
strbul.Append(t.ToString(mode)); stringBuilder.Append(t.ToString(mode));
} }
return strbul.ToString(); return stringBuilder.ToString();
} }
/// <summary> /// <summary>
+16 -13
View File
@@ -24,12 +24,17 @@ public class SimApiExceptionMiddleware(RequestDelegate next, ILogger<SimApiExcep
try try
{ {
await next(context); await next(context);
if (context.Response.StatusCode != 200) switch (context.Response.StatusCode)
{ {
if (!new[] { 301, 302 }.Contains(context.Response.StatusCode)) case 200:
{ break;
case 404:
throw new SimApiException(context.Response.StatusCode, "请求的接口不存在");
case 301:
case 302:
break;
default:
throw new SimApiException(context.Response.StatusCode); throw new SimApiException(context.Response.StatusCode);
}
} }
} }
catch (SimApiException ex) catch (SimApiException ex)
@@ -42,8 +47,8 @@ public class SimApiExceptionMiddleware(RequestDelegate next, ILogger<SimApiExcep
} }
catch (Exception ex) catch (Exception ex)
{ {
log.LogError(ex.Message); log.LogError("{Msg}", ex.Message);
log.LogError(ex.StackTrace); log.LogError("{Msg}", ex.StackTrace);
response = new SimApiBaseResponse(500, ex.Message); response = new SimApiBaseResponse(500, ex.Message);
ErrorResponse(context, response); ErrorResponse(context, response);
} }
@@ -54,13 +59,11 @@ public class SimApiExceptionMiddleware(RequestDelegate next, ILogger<SimApiExcep
/// </summary> /// </summary>
/// <param name="context"></param> /// <param name="context"></param>
/// <param name="response"></param> /// <param name="response"></param>
private void ErrorResponse(HttpContext context, SimApiBaseResponse response) private static void ErrorResponse(HttpContext context, SimApiBaseResponse response)
{ {
if (!context.Response.HasStarted) if (context.Response.HasStarted) return;
{ context.Response.StatusCode = 200;
context.Response.StatusCode = 200; context.Response.Headers.Append("Content-Type", "application/json");
context.Response.Headers.Append("Content-Type", "application/json"); context.Response.WriteAsync(response.ToString());
context.Response.WriteAsync(response.ToString());
}
} }
} }