Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
be9092bf3c | ||
|
|
345aaa261e | ||
|
|
f98cc6ff5a | ||
|
|
a3fff03ff6 |
+19
-17
@@ -3,6 +3,7 @@ using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.Encodings.Web;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Text.Unicode;
|
||||
|
||||
@@ -13,18 +14,22 @@ namespace SimApi.Helpers
|
||||
/// <summary>
|
||||
/// 当前CST时间
|
||||
/// </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>
|
||||
public static double TimestampNow
|
||||
{
|
||||
get => (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds;
|
||||
}
|
||||
public static double TimestampNow => (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds;
|
||||
|
||||
/// <summary>
|
||||
/// 检测手机号是否正确
|
||||
@@ -45,13 +50,13 @@ namespace SimApi.Helpers
|
||||
/// <returns></returns>
|
||||
public static string Md5(string source, string mode = "x2")
|
||||
{
|
||||
byte[] sor = Encoding.UTF8.GetBytes(source);
|
||||
MD5 md5 = MD5.Create();
|
||||
byte[] result = md5.ComputeHash(sor);
|
||||
StringBuilder strbul = new StringBuilder(40);
|
||||
for (int i = 0; i < result.Length; i++)
|
||||
var sor = Encoding.UTF8.GetBytes(source);
|
||||
var md5 = MD5.Create();
|
||||
var result = md5.ComputeHash(sor);
|
||||
var strbul = new StringBuilder(40);
|
||||
foreach (var t in result)
|
||||
{
|
||||
strbul.Append(result[i].ToString(mode));
|
||||
strbul.Append(t.ToString(mode));
|
||||
}
|
||||
|
||||
return strbul.ToString();
|
||||
@@ -64,10 +69,7 @@ namespace SimApi.Helpers
|
||||
/// <returns></returns>
|
||||
public static string Json(object obj)
|
||||
{
|
||||
return JsonSerializer.Serialize(obj, new JsonSerializerOptions
|
||||
{
|
||||
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
|
||||
});
|
||||
return JsonSerializer.Serialize(obj, JsonOption);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
# YYApi 基于Asp.net Core 3/5的一个基础辅助包
|
||||
# SimApi 基于.Net的一个基础辅助包
|
||||
|
||||
[](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>();
|
||||
});
|
||||
```
|
||||
基于S3的存储,基于RabbitMq的事件RPC调用,自定义Logger格式,自定义响应格式
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Text.Encodings.Web;
|
||||
using System.Text.Json;
|
||||
using System.Text.Unicode;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SimApi.Helpers;
|
||||
|
||||
namespace SimApi;
|
||||
|
||||
@@ -16,11 +17,7 @@ public partial class Synapse
|
||||
|
||||
private void FireEvent(string eventName, object param)
|
||||
{
|
||||
var paramJson = JsonSerializer.Serialize(param, new JsonSerializerOptions
|
||||
{
|
||||
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
});
|
||||
var paramJson = JsonSerializer.Serialize(param, SimApiUtil.JsonOption);
|
||||
var router = $"event.{Options.AppName}.{eventName}";
|
||||
var props = EventClientChannel.CreateBasicProperties();
|
||||
props.AppId = Options.AppId;
|
||||
|
||||
@@ -5,6 +5,7 @@ using System.Text.Json;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using RabbitMQ.Client.Events;
|
||||
using SimApi.Helpers;
|
||||
|
||||
namespace SimApi;
|
||||
|
||||
@@ -35,18 +36,12 @@ public partial class Synapse
|
||||
{
|
||||
mt.Invoke(callClass, pt == typeof(string)
|
||||
? new object[] { reqBody }
|
||||
: new[]
|
||||
{
|
||||
JsonSerializer.Deserialize(reqBody, pt, new JsonSerializerOptions
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
||||
})
|
||||
});
|
||||
: new[] { JsonSerializer.Deserialize(reqBody, pt, SimApiUtil.JsonOption) });
|
||||
EventServerChannel.BasicAck(ea.DeliveryTag, false);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.LogError("Event Processor Error: {Err}", e.Message);
|
||||
Logger.LogError("Event Processor Error: {Err}", e.InnerException);
|
||||
EventServerChannel.BasicNack(ea.DeliveryTag, false, false);
|
||||
}
|
||||
};
|
||||
|
||||
+5
-14
@@ -36,15 +36,11 @@ public partial class Synapse
|
||||
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
|
||||
{
|
||||
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
});
|
||||
var paramJson = JsonSerializer.Serialize(param, SimApiUtil.JsonOption);
|
||||
var router = $"server.{app}";
|
||||
SimApiBaseResponse response;
|
||||
string response;
|
||||
var props = RpcClientChannel.CreateBasicProperties();
|
||||
props.AppId = Options.AppId;
|
||||
props.MessageId = Guid.NewGuid().ToString();
|
||||
@@ -59,17 +55,12 @@ public partial class Synapse
|
||||
{
|
||||
if (SimApiUtil.TimestampNow - ts > Options.RpcTimeout)
|
||||
{
|
||||
response = new SimApiBaseResponse(502, "timeout");
|
||||
response = JsonSerializer.Serialize(new SimApiBaseResponse(502, "timeout"), SimApiUtil.JsonOption);
|
||||
break;
|
||||
}
|
||||
if (ResponseCache.TryGetValue(props.MessageId, out var value))
|
||||
{
|
||||
response = JsonSerializer.Deserialize<SimApiBaseResponse<object>>(
|
||||
Encoding.UTF8.GetString(value), new JsonSerializerOptions
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
|
||||
});
|
||||
response = Encoding.UTF8.GetString(value);
|
||||
ResponseCache.Remove(props.MessageId);
|
||||
break;
|
||||
}
|
||||
|
||||
+19
-18
@@ -1,8 +1,10 @@
|
||||
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;
|
||||
@@ -37,28 +39,33 @@ public partial class Synapse
|
||||
{
|
||||
var callClass = Sp.CreateScope().ServiceProvider.GetRequiredService(method.Class);
|
||||
var mt = callClass.GetType().GetMethod(method.Method);
|
||||
var param = Array.Empty<object>();
|
||||
try
|
||||
{
|
||||
var pt = mt.GetParameters()[0].ParameterType;
|
||||
if (pt == typeof(string))
|
||||
{
|
||||
var ret = mt.Invoke(callClass, new[] { reqBody });
|
||||
res = new SimApiBaseResponse<object>(ret);
|
||||
param = new[] { reqBody };
|
||||
}
|
||||
else
|
||||
{
|
||||
var paramObj = JsonSerializer.Deserialize(reqBody, pt, new JsonSerializerOptions
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
||||
});
|
||||
var ret = mt.Invoke(callClass, new[] { paramObj });
|
||||
res = new SimApiBaseResponse<object>(ret);
|
||||
var paramObj = JsonSerializer.Deserialize(reqBody, pt, SimApiUtil.JsonOption);
|
||||
param = new[] { paramObj };
|
||||
}
|
||||
var ret = mt.Invoke(callClass, param);
|
||||
res = new SimApiBaseResponse<object>(ret);
|
||||
}
|
||||
catch (SimApiException e)
|
||||
catch (TargetInvocationException e)
|
||||
{
|
||||
Logger.LogDebug("RPC错误: {Err}", e.Message);
|
||||
res = new SimApiBaseResponse(e.Code, e.Message);
|
||||
if (e.InnerException is SimApiException ie)
|
||||
{
|
||||
Logger.LogDebug("RPC调用错误: {Err}", ie.Message);
|
||||
res = new SimApiBaseResponse(ie.Code, ie.Message);
|
||||
}
|
||||
else
|
||||
{
|
||||
res = new SimApiBaseResponse(500, e.Message);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -67,13 +74,7 @@ public partial class Synapse
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var returnJson = JsonSerializer.Serialize((object)res, new JsonSerializerOptions
|
||||
{
|
||||
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
});
|
||||
Console.WriteLine(returnJson);
|
||||
var returnJson = JsonSerializer.Serialize((object)res, SimApiUtil.JsonOption);
|
||||
var reply = $"client.{ea.BasicProperties.ReplyTo}.{ea.BasicProperties.AppId}";
|
||||
var props = RpcServerChannel.CreateBasicProperties();
|
||||
props.AppId = Options.AppId;
|
||||
|
||||
+2
-1
@@ -96,7 +96,8 @@ public partial class Synapse
|
||||
}
|
||||
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>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user