http客户端返回修改,版本升级
This commit is contained in:
@@ -434,3 +434,6 @@ MigrationBackup/
|
|||||||
# Ionide (cross platform F# VS Code tools) working folder
|
# Ionide (cross platform F# VS Code tools) working folder
|
||||||
.ionide/
|
.ionide/
|
||||||
|
|
||||||
|
|
||||||
|
# BitFun snapshot data - auto managed
|
||||||
|
.bitfun/
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ public class SimApiHttpClient(SimApiOptions? apiOptions = null, ILogger<SimApiHt
|
|||||||
/// <param name="queries"></param>
|
/// <param name="queries"></param>
|
||||||
/// <typeparam name="T"></typeparam>
|
/// <typeparam name="T"></typeparam>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public T? SignQuery<T>(string url, object? body = null, Dictionary<string, string>? queries = null)
|
public T SignQuery<T>(string url, object? body = null, Dictionary<string, string>? queries = null)
|
||||||
{
|
{
|
||||||
url = Server + url;
|
url = Server + url;
|
||||||
var queryUrl = SignFields.Aggregate(string.Empty,
|
var queryUrl = SignFields.Aggregate(string.Empty,
|
||||||
@@ -60,7 +60,7 @@ public class SimApiHttpClient(SimApiOptions? apiOptions = null, ILogger<SimApiHt
|
|||||||
/// <param name="body"></param>
|
/// <param name="body"></param>
|
||||||
/// <typeparam name="T"></typeparam>
|
/// <typeparam name="T"></typeparam>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public T? AesQuery<T>(string url, object body)
|
public T AesQuery<T>(string url, object body)
|
||||||
{
|
{
|
||||||
url = Server + url;
|
url = Server + url;
|
||||||
if (!string.IsNullOrEmpty(AppIdName))
|
if (!string.IsNullOrEmpty(AppIdName))
|
||||||
@@ -83,7 +83,7 @@ public class SimApiHttpClient(SimApiOptions? apiOptions = null, ILogger<SimApiHt
|
|||||||
/// <param name="queries"></param>
|
/// <param name="queries"></param>
|
||||||
/// <typeparam name="T"></typeparam>
|
/// <typeparam name="T"></typeparam>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public T? AesSignQuery<T>(string url, object body, Dictionary<string, string>? queries = null)
|
public T AesSignQuery<T>(string url, object body, Dictionary<string, string>? queries = null)
|
||||||
{
|
{
|
||||||
var req = new SimApiOneFieldRequest<string>
|
var req = new SimApiOneFieldRequest<string>
|
||||||
{
|
{
|
||||||
@@ -100,7 +100,7 @@ public class SimApiHttpClient(SimApiOptions? apiOptions = null, ILogger<SimApiHt
|
|||||||
/// <typeparam name="T"></typeparam>
|
/// <typeparam name="T"></typeparam>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
/// <exception cref="SimApiException"></exception>
|
/// <exception cref="SimApiException"></exception>
|
||||||
private T? Query<T>(string url, object? req)
|
private T Query<T>(string url, object? req)
|
||||||
{
|
{
|
||||||
var http = new HttpClient();
|
var http = new HttpClient();
|
||||||
logger?.LogDebug($"[HTTPCLIENT请求] {url}\n{SimApiUtil.Json(req)}\n");
|
logger?.LogDebug($"[HTTPCLIENT请求] {url}\n{SimApiUtil.Json(req)}\n");
|
||||||
@@ -110,6 +110,6 @@ public class SimApiHttpClient(SimApiOptions? apiOptions = null, ILogger<SimApiHt
|
|||||||
var res = resp.Content.ReadFromJsonAsync<SimApiBaseResponse<T>>().Result;
|
var res = resp.Content.ReadFromJsonAsync<SimApiBaseResponse<T>>().Result;
|
||||||
SimApiError.ErrorWhenNull(res, 500, "请求发生错误");
|
SimApiError.ErrorWhenNull(res, 500, "请求发生错误");
|
||||||
SimApiError.ErrorWhen(res.Code != 200, res.Code, res.Message);
|
SimApiError.ErrorWhen(res.Code != 200, res.Code, res.Message);
|
||||||
return res.Data;
|
return res.Data!;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -38,7 +38,7 @@ app.Run();
|
|||||||
所有接口输出 JSON,HTTP 状态码始终 `200`,错误信息在 `code` 字段:
|
所有接口输出 JSON,HTTP 状态码始终 `200`,错误信息在 `code` 字段:
|
||||||
|
|
||||||
| code | 含义 |
|
| code | 含义 |
|
||||||
|------|------|
|
| ---- | ---------- |
|
||||||
| 200 | 成功 |
|
| 200 | 成功 |
|
||||||
| 204 | 无数据 |
|
| 204 | 无数据 |
|
||||||
| 400 | 参数错误 |
|
| 400 | 参数错误 |
|
||||||
@@ -147,18 +147,18 @@ public class SimApiBaseController : Controller
|
|||||||
### 自动路由
|
### 自动路由
|
||||||
|
|
||||||
| 路由 | 方法 | 条件 | 说明 |
|
| 路由 | 方法 | 条件 | 说明 |
|
||||||
|------|------|------|------|
|
| ----------------------- | -------- | -------------------------------------------- | ------------------------ |
|
||||||
| `/versions` | GET/POST | `EnableVersionUrl`(默认) | 返回 SimApi/App 版本 |
|
| `/versions` | GET/POST | `VersionRoute != null`(默认) | 返回 SimApi/App 版本 |
|
||||||
| `/user/info` | POST | `EnableSimApiAuth` | 需登录,返回 LoginInfo |
|
| `/user/info` | POST | `EnableSimApiAuth` + `UserInfoRoute != null` | 需登录,返回 LoginInfo |
|
||||||
| `/logout` | POST | `EnableSimApiAuth` | 退出登录(可自定义路由) |
|
| `/auth/logout` | POST | `EnableSimApiAuth` + `LogoutRoute != null` | 退出登录(可自定义路由) |
|
||||||
| `/swagger` | GET | `EnableSimApiDoc` | Swagger UI |
|
| `/swagger` | GET | `EnableSimApiDoc` | Swagger UI |
|
||||||
| `/jobs` | GET | `EnableJob` + DashboardUrl | Hangfire 控制台 |
|
| `/jobs` | GET | `EnableJob` + `DashboardUrl != null` | Hangfire 控制台 |
|
||||||
| `/exception/{code:int}` | GET | 始终 | 错误反馈页面 |
|
| `/exception/{code:int}` | GET | 始终 | 错误反馈页面 |
|
||||||
|
|
||||||
### 返回值规范
|
### 返回值规范
|
||||||
|
|
||||||
| 场景 | 返回类型 |
|
| 场景 | 返回类型 |
|
||||||
|------|----------|
|
| ---------- | ------------------------- |
|
||||||
| 写操作 | `void` |
|
| 写操作 | `void` |
|
||||||
| 单条查询 | 直接 Entity |
|
| 单条查询 | 直接 Entity |
|
||||||
| 列表查询 | `Entity[]` |
|
| 列表查询 | `Entity[]` |
|
||||||
@@ -322,7 +322,8 @@ public class MySignProvider : SimApiSignProviderBase
|
|||||||
```csharp
|
```csharp
|
||||||
[SynapseEvent("order/created")] // 指定 eventName
|
[SynapseEvent("order/created")] // 指定 eventName
|
||||||
[SynapseEvent] // 不指定 = 方法名
|
[SynapseEvent] // 不指定 = 方法名
|
||||||
// 参数: 0个 / 1个(string eventName) / 2个(string eventName, T data)
|
// 参数: 1个(string eventName) / 2个(string eventName, T data)
|
||||||
|
// 注意: 至少需要1个参数
|
||||||
```
|
```
|
||||||
|
|
||||||
### [SynapseRpc] — MQTT RPC 方法
|
### [SynapseRpc] — MQTT RPC 方法
|
||||||
@@ -359,7 +360,7 @@ options.ConfigureSimApiDoc(doc =>
|
|||||||
### 自动过滤器
|
### 自动过滤器
|
||||||
|
|
||||||
| 过滤器 | 效果 |
|
| 过滤器 | 效果 |
|
||||||
|--------|------|
|
| --------------------------------- | ------------------------------------ |
|
||||||
| `SimApiResponseOperationFilter` | 返回值包装为 `SimApiBaseResponse<T>` |
|
| `SimApiResponseOperationFilter` | 返回值包装为 `SimApiBaseResponse<T>` |
|
||||||
| `SimApiAuthOperationFilter` | 鉴权接口 + Token Header |
|
| `SimApiAuthOperationFilter` | 鉴权接口 + Token Header |
|
||||||
| `SimApiSignOperationFilter` | 签名接口注入签名参数 |
|
| `SimApiSignOperationFilter` | 签名接口注入签名参数 |
|
||||||
@@ -439,9 +440,9 @@ virtual string[] SignFields { get; init; } = [];
|
|||||||
### 调用方法
|
### 调用方法
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
T? SignQuery<T>(string url, object? body = null, Dictionary<string, string>? queries = null);
|
T SignQuery<T>(string url, object? body = null, Dictionary<string, string>? queries = null);
|
||||||
T? AesQuery<T>(string url, object body);
|
T AesQuery<T>(string url, object body);
|
||||||
T? AesSignQuery<T>(string url, object body, Dictionary<string, string>? queries = null);
|
T AesSignQuery<T>(string url, object body, Dictionary<string, string>? queries = null);
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
@@ -499,7 +500,7 @@ options.ConfigureSimApiSynapse(s =>
|
|||||||
### Topic 规则
|
### Topic 规则
|
||||||
|
|
||||||
| 用途 | Topic 格式 |
|
| 用途 | Topic 格式 |
|
||||||
|------|-----------|
|
| -------- | ------------------------------------------------------ |
|
||||||
| 事件发布 | `{SysName}/event/{AppName}/{eventName}` |
|
| 事件发布 | `{SysName}/event/{AppName}/{eventName}` |
|
||||||
| 事件订阅 | `{SysName}/event/{eventName}` (或 `$queue/` 前缀) |
|
| 事件订阅 | `{SysName}/event/{eventName}` (或 `$queue/` 前缀) |
|
||||||
| RPC 请求 | `{SysName}/{targetApp}/rpc/server/{method}` |
|
| RPC 请求 | `{SysName}/{targetApp}/rpc/server/{method}` |
|
||||||
@@ -589,7 +590,7 @@ void UpdateTime();
|
|||||||
## 15. DTO 规范
|
## 15. DTO 规范
|
||||||
|
|
||||||
| 类型 | 命名 | 示例 |
|
| 类型 | 命名 | 示例 |
|
||||||
|------|------|------|
|
| ---- | ----------------- | ----------------- |
|
||||||
| 请求 | `[动作]Request` | `UserEditRequest` |
|
| 请求 | `[动作]Request` | `UserEditRequest` |
|
||||||
| 响应 | `[动作]Response` | `TokenResponse` |
|
| 响应 | `[动作]Response` | `TokenResponse` |
|
||||||
| 载体 | `[含义]Data/Item` | `GenerateData` |
|
| 载体 | `[含义]Data/Item` | `GenerateData` |
|
||||||
@@ -628,7 +629,6 @@ builder.Services.AddSimApi(options =>
|
|||||||
options.EnableSimApiResponseFilter = true; // 响应统一封装
|
options.EnableSimApiResponseFilter = true; // 响应统一封装
|
||||||
options.EnableForwardHeaders = true; // 反向代理 Header
|
options.EnableForwardHeaders = true; // 反向代理 Header
|
||||||
options.EnableLowerUrl = true; // URL 小写
|
options.EnableLowerUrl = true; // URL 小写
|
||||||
options.EnableVersionUrl = true; // /versions 接口
|
|
||||||
|
|
||||||
// 子模块配置
|
// 子模块配置
|
||||||
options.ConfigureSimApiDoc(doc => { ... });
|
options.ConfigureSimApiDoc(doc => { ... });
|
||||||
@@ -647,7 +647,7 @@ builder.Services.AddSimApi(options =>
|
|||||||
## 17. GOTCHAS — 常见错误
|
## 17. GOTCHAS — 常见错误
|
||||||
|
|
||||||
| ❌ 错误 | ✅ 正确 |
|
| ❌ 错误 | ✅ 正确 |
|
||||||
|---------|---------|
|
| ------------------------------------------------------ | ------------------------------------------- |
|
||||||
| 存储路径 `avatars/file.jpg`(无前导 `/`) | 必须以 **`/`** 开头 |
|
| 存储路径 `avatars/file.jpg`(无前导 `/`) | 必须以 **`/`** 开头 |
|
||||||
| `s.Endpoint = "http://x:9000/"` | **不能以 `/` 结尾** |
|
| `s.Endpoint = "http://x:9000/"` | **不能以 `/` 结尾** |
|
||||||
| `synapse.PublishEvent(...)` | 方法名是 **`synapse.Event(...)`** |
|
| `synapse.PublishEvent(...)` | 方法名是 **`synapse.Event(...)`** |
|
||||||
@@ -662,7 +662,7 @@ builder.Services.AddSimApi(options =>
|
|||||||
## 18. 禁止事项
|
## 18. 禁止事项
|
||||||
|
|
||||||
| ❌ 禁止 | ✅ 正确 |
|
| ❌ 禁止 | ✅ 正确 |
|
||||||
|---------|------------------------------------------------------|
|
| --------------------------------------- | ----------------------------------------------------- |
|
||||||
| HTTP 4xx/5xx 表达业务错误 | HTTP 200 + JSON `code` |
|
| HTTP 4xx/5xx 表达业务错误 | HTTP 200 + JSON `code` |
|
||||||
| `throw new Exception(msg)` | `ErrorWhen` 或 `throw new SimApiException(code, msg)` |
|
| `throw new Exception(msg)` | `ErrorWhen` 或 `throw new SimApiException(code, msg)` |
|
||||||
| 鉴权 Attribute 只放方法 | 可以放 Controller **类**上 |
|
| 鉴权 Attribute 只放方法 | 可以放 Controller **类**上 |
|
||||||
|
|||||||
+5
-5
@@ -12,13 +12,13 @@
|
|||||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Hangfire.AspNetCore" Version="1.8.22" />
|
<PackageReference Include="Hangfire.AspNetCore" Version="1.8.23" />
|
||||||
<PackageReference Include="Hangfire.Console" Version="1.4.3"/>
|
<PackageReference Include="Hangfire.Console" Version="1.4.3"/>
|
||||||
<PackageReference Include="Hangfire.Redis.StackExchange" Version="1.12.0"/>
|
<PackageReference Include="Hangfire.Redis.StackExchange" Version="1.12.0"/>
|
||||||
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="10.0.1" />
|
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="10.0.9" />
|
||||||
<PackageReference Include="Minio" Version="7.0.0" />
|
<PackageReference Include="Minio" Version="7.0.0" />
|
||||||
<PackageReference Include="MQTTnet" Version="5.0.1.1416"/>
|
<PackageReference Include="MQTTnet" Version="5.1.0.1559" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="10.1.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="10.2.3" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="10.1.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="10.2.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
Reference in New Issue
Block a user