Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
be9092bf3c | ||
|
|
345aaa261e | ||
|
|
f98cc6ff5a |
+19
-17
@@ -3,6 +3,7 @@ using System.Security.Cryptography;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.Encodings.Web;
|
using System.Text.Encodings.Web;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using System.Text.Unicode;
|
using System.Text.Unicode;
|
||||||
|
|
||||||
@@ -13,18 +14,22 @@ namespace SimApi.Helpers
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 当前CST时间
|
/// 当前CST时间
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static DateTime CstNow
|
public static DateTime CstNow => DateTime.UtcNow.AddHours(8);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// JSON序列化常规选项
|
||||||
|
/// </summary>
|
||||||
|
public static JsonSerializerOptions JsonOption => new()
|
||||||
{
|
{
|
||||||
get => DateTime.UtcNow.AddHours(8);
|
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
|
||||||
}
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||||
|
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
|
||||||
|
};
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 当前秒级时间戳
|
/// 当前秒级时间戳
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static double TimestampNow
|
public static double TimestampNow => (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds;
|
||||||
{
|
|
||||||
get => (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 检测手机号是否正确
|
/// 检测手机号是否正确
|
||||||
@@ -45,13 +50,13 @@ namespace SimApi.Helpers
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string Md5(string source, string mode = "x2")
|
public static string Md5(string source, string mode = "x2")
|
||||||
{
|
{
|
||||||
byte[] sor = Encoding.UTF8.GetBytes(source);
|
var sor = Encoding.UTF8.GetBytes(source);
|
||||||
MD5 md5 = MD5.Create();
|
var md5 = MD5.Create();
|
||||||
byte[] result = md5.ComputeHash(sor);
|
var result = md5.ComputeHash(sor);
|
||||||
StringBuilder strbul = new StringBuilder(40);
|
var strbul = new StringBuilder(40);
|
||||||
for (int i = 0; i < result.Length; i++)
|
foreach (var t in result)
|
||||||
{
|
{
|
||||||
strbul.Append(result[i].ToString(mode));
|
strbul.Append(t.ToString(mode));
|
||||||
}
|
}
|
||||||
|
|
||||||
return strbul.ToString();
|
return strbul.ToString();
|
||||||
@@ -64,10 +69,7 @@ namespace SimApi.Helpers
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string Json(object obj)
|
public static string Json(object obj)
|
||||||
{
|
{
|
||||||
return JsonSerializer.Serialize(obj, new JsonSerializerOptions
|
return JsonSerializer.Serialize(obj, JsonOption);
|
||||||
{
|
|
||||||
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
# YYApi 基于Asp.net Core 3/5的一个基础辅助包
|
# SimApi 基于.Net的一个基础辅助包
|
||||||
|
|
||||||
[](https://github.com/simcu/simapi-net/actions/workflows/nuget-publish.yml)
|
[](https://github.com/simcu/simapi-net/actions/workflows/nuget-publish.yml)
|
||||||
### 包含了以下组件:
|
### 包含了以下组件:
|
||||||
@@ -52,3 +52,4 @@ services.AddSimApi(options =>
|
|||||||
options.SimApiStorageOptions = Configuration.GetSection("S3").Get<SimApiStorageOptions>();
|
options.SimApiStorageOptions = Configuration.GetSection("S3").Get<SimApiStorageOptions>();
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
基于S3的存储,基于RabbitMq的事件RPC调用,自定义Logger格式,自定义响应格式
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ using System.Text.Encodings.Web;
|
|||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Text.Unicode;
|
using System.Text.Unicode;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using SimApi.Helpers;
|
||||||
|
|
||||||
namespace SimApi;
|
namespace SimApi;
|
||||||
|
|
||||||
@@ -16,11 +17,7 @@ public partial class Synapse
|
|||||||
|
|
||||||
private void FireEvent(string eventName, object param)
|
private void FireEvent(string eventName, object param)
|
||||||
{
|
{
|
||||||
var paramJson = JsonSerializer.Serialize(param, new JsonSerializerOptions
|
var paramJson = JsonSerializer.Serialize(param, SimApiUtil.JsonOption);
|
||||||
{
|
|
||||||
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
|
|
||||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
||||||
});
|
|
||||||
var router = $"event.{Options.AppName}.{eventName}";
|
var router = $"event.{Options.AppName}.{eventName}";
|
||||||
var props = EventClientChannel.CreateBasicProperties();
|
var props = EventClientChannel.CreateBasicProperties();
|
||||||
props.AppId = Options.AppId;
|
props.AppId = Options.AppId;
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ using System.Text.Json;
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using RabbitMQ.Client.Events;
|
using RabbitMQ.Client.Events;
|
||||||
|
using SimApi.Helpers;
|
||||||
|
|
||||||
namespace SimApi;
|
namespace SimApi;
|
||||||
|
|
||||||
@@ -35,13 +36,7 @@ public partial class Synapse
|
|||||||
{
|
{
|
||||||
mt.Invoke(callClass, pt == typeof(string)
|
mt.Invoke(callClass, pt == typeof(string)
|
||||||
? new object[] { reqBody }
|
? new object[] { reqBody }
|
||||||
: new[]
|
: new[] { JsonSerializer.Deserialize(reqBody, pt, SimApiUtil.JsonOption) });
|
||||||
{
|
|
||||||
JsonSerializer.Deserialize(reqBody, pt, new JsonSerializerOptions
|
|
||||||
{
|
|
||||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
|
||||||
})
|
|
||||||
});
|
|
||||||
EventServerChannel.BasicAck(ea.DeliveryTag, false);
|
EventServerChannel.BasicAck(ea.DeliveryTag, false);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
|
|||||||
+5
-14
@@ -36,15 +36,11 @@ public partial class Synapse
|
|||||||
RpcClientChannel.BasicConsume(queue, false, "", false, false, null, consumer);
|
RpcClientChannel.BasicConsume(queue, false, "", false, false, null, consumer);
|
||||||
}
|
}
|
||||||
|
|
||||||
private object FireRpc(string app, string action, object param)
|
private string FireRpc(string app, string action, object param)
|
||||||
{
|
{
|
||||||
var paramJson = JsonSerializer.Serialize(param, new JsonSerializerOptions
|
var paramJson = JsonSerializer.Serialize(param, SimApiUtil.JsonOption);
|
||||||
{
|
|
||||||
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
|
|
||||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
||||||
});
|
|
||||||
var router = $"server.{app}";
|
var router = $"server.{app}";
|
||||||
SimApiBaseResponse response;
|
string response;
|
||||||
var props = RpcClientChannel.CreateBasicProperties();
|
var props = RpcClientChannel.CreateBasicProperties();
|
||||||
props.AppId = Options.AppId;
|
props.AppId = Options.AppId;
|
||||||
props.MessageId = Guid.NewGuid().ToString();
|
props.MessageId = Guid.NewGuid().ToString();
|
||||||
@@ -59,17 +55,12 @@ public partial class Synapse
|
|||||||
{
|
{
|
||||||
if (SimApiUtil.TimestampNow - ts > Options.RpcTimeout)
|
if (SimApiUtil.TimestampNow - ts > Options.RpcTimeout)
|
||||||
{
|
{
|
||||||
response = new SimApiBaseResponse(502, "timeout");
|
response = JsonSerializer.Serialize(new SimApiBaseResponse(502, "timeout"), SimApiUtil.JsonOption);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (ResponseCache.TryGetValue(props.MessageId, out var value))
|
if (ResponseCache.TryGetValue(props.MessageId, out var value))
|
||||||
{
|
{
|
||||||
response = JsonSerializer.Deserialize<SimApiBaseResponse<object>>(
|
response = Encoding.UTF8.GetString(value);
|
||||||
Encoding.UTF8.GetString(value), new JsonSerializerOptions
|
|
||||||
{
|
|
||||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
||||||
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
|
|
||||||
});
|
|
||||||
ResponseCache.Remove(props.MessageId);
|
ResponseCache.Remove(props.MessageId);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-11
@@ -4,6 +4,7 @@ using System.Reflection;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.Encodings.Web;
|
using System.Text.Encodings.Web;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
using System.Text.Unicode;
|
using System.Text.Unicode;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
@@ -48,10 +49,7 @@ public partial class Synapse
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var paramObj = JsonSerializer.Deserialize(reqBody, pt, new JsonSerializerOptions
|
var paramObj = JsonSerializer.Deserialize(reqBody, pt, SimApiUtil.JsonOption);
|
||||||
{
|
|
||||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
|
||||||
});
|
|
||||||
param = new[] { paramObj };
|
param = new[] { paramObj };
|
||||||
}
|
}
|
||||||
var ret = mt.Invoke(callClass, param);
|
var ret = mt.Invoke(callClass, param);
|
||||||
@@ -59,9 +57,8 @@ public partial class Synapse
|
|||||||
}
|
}
|
||||||
catch (TargetInvocationException e)
|
catch (TargetInvocationException e)
|
||||||
{
|
{
|
||||||
if (e.InnerException is SimApiException)
|
if (e.InnerException is SimApiException ie)
|
||||||
{
|
{
|
||||||
var ie = e.InnerException as SimApiException;
|
|
||||||
Logger.LogDebug("RPC调用错误: {Err}", ie.Message);
|
Logger.LogDebug("RPC调用错误: {Err}", ie.Message);
|
||||||
res = new SimApiBaseResponse(ie.Code, ie.Message);
|
res = new SimApiBaseResponse(ie.Code, ie.Message);
|
||||||
}
|
}
|
||||||
@@ -77,11 +74,7 @@ public partial class Synapse
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var returnJson = JsonSerializer.Serialize((object)res, new JsonSerializerOptions
|
var returnJson = JsonSerializer.Serialize((object)res, SimApiUtil.JsonOption);
|
||||||
{
|
|
||||||
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
|
|
||||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
||||||
});
|
|
||||||
var reply = $"client.{ea.BasicProperties.ReplyTo}.{ea.BasicProperties.AppId}";
|
var reply = $"client.{ea.BasicProperties.ReplyTo}.{ea.BasicProperties.AppId}";
|
||||||
var props = RpcServerChannel.CreateBasicProperties();
|
var props = RpcServerChannel.CreateBasicProperties();
|
||||||
props.AppId = Options.AppId;
|
props.AppId = Options.AppId;
|
||||||
|
|||||||
+2
-1
@@ -96,7 +96,8 @@ public partial class Synapse
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
res = FireRpc(appName, method, param);
|
var data = FireRpc(appName, method, param);
|
||||||
|
res = JsonSerializer.Deserialize<SimApiBaseResponse<T>>(data, SimApiUtil.JsonOption);
|
||||||
}
|
}
|
||||||
return res as SimApiBaseResponse<T>;
|
return res as SimApiBaseResponse<T>;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user