Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6ef7aca8ae | ||
|
|
4171756c54 | ||
|
|
c68cfa6192 | ||
|
|
8344567ac6 | ||
|
|
99b26d99f6 |
@@ -1,22 +1,37 @@
|
||||
namespace SimApi.Communications;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace SimApi.Communications;
|
||||
|
||||
/// <summary>
|
||||
/// 只有ID的请求
|
||||
/// </summary>
|
||||
public record SimApiIdOnlyRequest(int Id);
|
||||
public class SimApiIdOnlyRequest
|
||||
{
|
||||
[Required] public int Id { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 只有ID的请求(字符串)
|
||||
/// </summary>
|
||||
public record SimApiStringIdOnlyRequest(string Id);
|
||||
public class SimApiStringIdOnlyRequest
|
||||
{
|
||||
[Required] public string Id { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 动态类型单字段请求
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public record SimApiOneFieldRequest<T>(T Data);
|
||||
public class SimApiOneFieldRequest<T>
|
||||
{
|
||||
[Required] public T Data { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 基础分页请求
|
||||
/// </summary>
|
||||
public record SimApiBasePageRequest(int Page, int Count);
|
||||
public class SimApiBasePageRequest
|
||||
{
|
||||
[Required] public int Page { get; set; }
|
||||
[Required] public int Count { get; set; }
|
||||
}
|
||||
@@ -7,8 +7,11 @@ namespace SimApi.Communications;
|
||||
/// <summary>
|
||||
/// 基础相应
|
||||
/// </summary>
|
||||
public record SimApiBaseResponse(int Code = 200, string Message = "成功")
|
||||
public class SimApiBaseResponse(int code = 200, string message = "成功")
|
||||
{
|
||||
public int Code { get; set; } = code;
|
||||
public string Message { get; set; } = message;
|
||||
|
||||
/// <summary>
|
||||
/// 默认错误代码对应提示信息
|
||||
/// </summary>
|
||||
@@ -23,7 +26,7 @@ public record SimApiBaseResponse(int Code = 200, string Message = "成功")
|
||||
{ 500, "服务器错误" }
|
||||
};
|
||||
|
||||
public SimApiBaseResponse(int code) : this(code, MsgBox.ContainsKey(code) ? MsgBox[code] : "未知错误")
|
||||
public SimApiBaseResponse(int code) : this(code, MsgBox.GetValueOrDefault(code, "未知错误"))
|
||||
{
|
||||
}
|
||||
|
||||
@@ -49,17 +52,19 @@ public record SimApiBaseResponse(int Code = 200, string Message = "成功")
|
||||
/// 动态内容分页
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public record SimApiBasePageResponse<T>(
|
||||
T List,
|
||||
int Page = 1,
|
||||
int Count = 1,
|
||||
int Total = 1,
|
||||
int Code = 200,
|
||||
string Message = "成功") : SimApiBaseResponse(Code, Message);
|
||||
public class SimApiBasePageResponse<T> : SimApiBaseResponse
|
||||
{
|
||||
public T List { get; set; }
|
||||
public int Page { get; set; } = 1;
|
||||
public int Count { get; set; } = 20;
|
||||
public int Total { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 动态Data返回
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public record SimApiBaseResponse<T>(T Data, int Code = 200, string Message = "成功") : SimApiBaseResponse(Code,
|
||||
Message);
|
||||
public class SimApiBaseResponse<T> : SimApiBaseResponse
|
||||
{
|
||||
public T Data { get; set; }
|
||||
}
|
||||
@@ -50,7 +50,7 @@ public class SimApiBaseController : Controller
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检测条件,根据条件返回报错
|
||||
/// 检测条件,根据条件返回报错 如果condition是ture报错
|
||||
/// </summary>
|
||||
/// <param name="condition">检测条件</param>
|
||||
/// <param name="code">错误代码</param>
|
||||
@@ -63,6 +63,30 @@ public class SimApiBaseController : Controller
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 检测条件,根据条件返回报错 如果condition是ture报错
|
||||
/// </summary>
|
||||
/// <param name="condition">检测条件</param>
|
||||
/// <param name="code">错误代码</param>
|
||||
/// <param name="message">错误描述</param>
|
||||
protected static void ErrorWhenTrue(bool condition, int code = 500, string message = "")
|
||||
{
|
||||
ErrorWhen(condition, code, message);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 如果condition是false 报错
|
||||
/// </summary>
|
||||
/// <param name="condition"></param>
|
||||
/// <param name="code"></param>
|
||||
/// <param name="message"></param>
|
||||
protected static void ErrorWhenFalse(bool condition, int code = 500, string message = "")
|
||||
{
|
||||
ErrorWhen(!condition, code, message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检测给定的变量是否为NUll
|
||||
/// </summary>
|
||||
@@ -80,6 +104,6 @@ public class SimApiBaseController : Controller
|
||||
/// <returns></returns>
|
||||
protected SimApiBaseResponse<string> UploadFile()
|
||||
{
|
||||
return new SimApiBaseResponse<string>(null);
|
||||
return new SimApiBaseResponse<string>();
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,10 @@ public class SimApiCommonController(SimApiAuth auth) : SimApiBaseController
|
||||
public SimApiBaseResponse<string> CheckLogin()
|
||||
{
|
||||
ErrorWhenNull(LoginInfo, 401);
|
||||
return new SimApiBaseResponse<string>(LoginInfo.Id);
|
||||
return new SimApiBaseResponse<string>
|
||||
{
|
||||
Data = LoginInfo.Id
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -40,12 +43,12 @@ public class SimApiCommonController(SimApiAuth auth) : SimApiBaseController
|
||||
{
|
||||
string token = null;
|
||||
|
||||
if (Request.Headers.ContainsKey("Token"))
|
||||
if (Request.Headers.TryGetValue("Token", out var value))
|
||||
{
|
||||
token = Request.Headers["Token"];
|
||||
token = value;
|
||||
}
|
||||
|
||||
auth.Logout(token);
|
||||
auth.Logout(token!);
|
||||
return new SimApiBaseResponse();
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
@@ -14,6 +15,8 @@ public class SimApiAuth(IDistributedCache cache)
|
||||
/// 产生一个Token记录并返回Token
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
public string Login(string id, string type = "user", string token = null)
|
||||
{
|
||||
@@ -25,6 +28,7 @@ public class SimApiAuth(IDistributedCache cache)
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="uuid"></param>
|
||||
/// <returns></returns>
|
||||
public string Login(string id, string[] type, string uuid = null)
|
||||
{
|
||||
@@ -34,6 +38,17 @@ public class SimApiAuth(IDistributedCache cache)
|
||||
return uuid;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取登陆信息
|
||||
/// </summary>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
public SimApiLoginItem? GetLogin(string token)
|
||||
{
|
||||
var login = cache.GetString(token);
|
||||
return login != null ? JsonSerializer.Deserialize<SimApiLoginItem>(login) : default;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 退出登陆
|
||||
/// </summary>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.Encodings.Web;
|
||||
@@ -71,4 +72,21 @@ public static class SimApiUtil
|
||||
{
|
||||
return JsonSerializer.Serialize(obj, JsonOption);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 分页
|
||||
/// </summary>
|
||||
/// <param name="query"></param>
|
||||
/// <param name="page">页码</param>
|
||||
/// <param name="count">每页数量</param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static IQueryable<T> Paginate<T>(this IQueryable<T> query, int page, int count)
|
||||
{
|
||||
if (page < 1)
|
||||
page = 1;
|
||||
if (count <= 0)
|
||||
count = 10;
|
||||
return query.Skip((page - 1) * count).Take(count);
|
||||
}
|
||||
}
|
||||
@@ -23,11 +23,12 @@ public class SimApiLogger(string name) : ILogger
|
||||
_ => ConsoleColor.White
|
||||
};
|
||||
var message =
|
||||
$"[ {name} ][ {SimApiUtil.CstNow.ToString("yyyy-MM-dd HH:mm:ss:ffff")} ][ {logLevel.ToString()} ]\n{state}\n";
|
||||
$"[ {name} ][ {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff")} ][ {logLevel.ToString()} ]\n{state}\n";
|
||||
if (exception != null)
|
||||
{
|
||||
message += $"{exception}\n";
|
||||
}
|
||||
|
||||
Console.WriteLine(message);
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
using SimApi.Communications;
|
||||
using SimApi.Helpers;
|
||||
|
||||
namespace SimApi.Middlewares;
|
||||
|
||||
@@ -11,7 +12,7 @@ namespace SimApi.Middlewares;
|
||||
/// </summary>
|
||||
public class SimApiAuthMiddleware(RequestDelegate next)
|
||||
{
|
||||
public Task Invoke(HttpContext httpContext, IDistributedCache cache)
|
||||
public Task Invoke(HttpContext httpContext, IDistributedCache cache, SimApiAuth auth)
|
||||
{
|
||||
string token = null;
|
||||
if (httpContext.Request.Headers.TryGetValue("Token", out var header))
|
||||
@@ -21,10 +22,10 @@ public class SimApiAuthMiddleware(RequestDelegate next)
|
||||
|
||||
if (!string.IsNullOrEmpty(token))
|
||||
{
|
||||
var login = cache.GetString(token);
|
||||
var login = auth.GetLogin(token);
|
||||
if (login != null)
|
||||
{
|
||||
httpContext.Items.Add("LoginInfo", JsonSerializer.Deserialize<SimApiLoginItem>(login));
|
||||
httpContext.Items.Add("LoginInfo", login);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,14 +7,11 @@ namespace SimApi.Models;
|
||||
|
||||
public class SimApiBaseModel
|
||||
{
|
||||
[Column(Order = 1)]
|
||||
public string Id { get; set; } = Guid.NewGuid().ToString();
|
||||
[Column(Order = 1)] public string Id { get; set; } = Guid.NewGuid().ToString();
|
||||
|
||||
[Column(Order = 9998)]
|
||||
public DateTime UpdatedAt { get; set; } = SimApiUtil.CstNow;
|
||||
[Column(Order = 9998)] public DateTime UpdatedAt { get; set; } = DateTime.Now;
|
||||
|
||||
[Column(Order = 9999)]
|
||||
public DateTime CreatedAt { get; set; } = SimApiUtil.CstNow;
|
||||
[Column(Order = 9999)] public DateTime CreatedAt { get; set; } = DateTime.Now;
|
||||
|
||||
protected virtual string[] MapperIgnoreField { get; set; } = { "Id", "CreatedAt", "UpdatedAt" };
|
||||
|
||||
@@ -84,6 +81,6 @@ public class SimApiBaseModel
|
||||
/// <returns></returns>
|
||||
public void UpdateTime()
|
||||
{
|
||||
GetType().GetProperty(UpdatedTimeField)?.SetValue(this, SimApiUtil.CstNow);
|
||||
GetType().GetProperty(UpdatedTimeField)?.SetValue(this, DateTime.Now);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -33,7 +33,7 @@ public static class SimApiExtensions
|
||||
// 是否使用 AUTH
|
||||
if (simApiOptions.EnableSimApiAuth)
|
||||
{
|
||||
builder.AddScoped<SimApiAuth>();
|
||||
builder.AddSingleton<SimApiAuth>();
|
||||
}
|
||||
|
||||
if (simApiOptions.EnableCors)
|
||||
|
||||
@@ -2,10 +2,6 @@ using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Text.Encodings.Web;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.Unicode;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using RabbitMQ.Client.Events;
|
||||
@@ -52,8 +48,12 @@ public partial class Synapse
|
||||
var paramObj = JsonSerializer.Deserialize(reqBody, pt, SimApiUtil.JsonOption);
|
||||
param = new[] { paramObj };
|
||||
}
|
||||
|
||||
var ret = mt.Invoke(callClass, param);
|
||||
res = new SimApiBaseResponse<object>(ret);
|
||||
res = new SimApiBaseResponse<object>
|
||||
{
|
||||
Data = ret
|
||||
};
|
||||
}
|
||||
catch (TargetInvocationException e)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user