@macrowing/inbiz-codegen 0.0.0-stage → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +592 -2
- package/bin/inbiz-codegen.mjs +7 -0
- package/package.json +32 -4
- package/scripts/prepare-templates.mjs +35 -0
- package/src/backend/init.mjs +170 -0
- package/src/config.mjs +150 -0
- package/src/favicon.mjs +100 -0
- package/src/generate.mjs +935 -0
- package/src/generated-output.mjs +107 -0
- package/src/index.mjs +346 -0
- package/src/menu/build.mjs +252 -0
- package/src/menu/check.mjs +163 -0
- package/src/menu/command.mjs +179 -0
- package/src/migrate-runtime.mjs +106 -0
- package/src/platform/client.mjs +167 -0
- package/src/platform/command.mjs +386 -0
- package/src/platform/pack.mjs +591 -0
- package/src/platform/recover.mjs +236 -0
- package/src/platform/startup.mjs +343 -0
- package/src/platform/state.mjs +116 -0
- package/src/platform/update.mjs +157 -0
- package/src/platform/zip.mjs +134 -0
- package/src/pull.mjs +284 -0
- package/src/scaffold/commands.mjs +349 -0
- package/src/scaffold/init.mjs +392 -0
- package/src/scaffold/template-files.mjs +40 -0
- package/src/scaffold/template-source.mjs +235 -0
- package/src/scaffold/template-sync.mjs +335 -0
- package/src/transform/heaviness.mjs +380 -0
- package/src/transform/preview-props.mjs +108 -0
- package/src/transform/published-page-links.mjs +61 -0
- package/src/transform/published-script.mjs +110 -0
- package/src/transform/registry.mjs +781 -0
- package/src/transform/schema-to-jsx.mjs +1708 -0
- package/src/util/des.mjs +10 -0
- package/templates/README.md +34 -0
- package/templates/backend/Directory.Build.props +19 -0
- package/templates/backend/Directory.Packages.props +49 -0
- package/templates/backend/Macrowing.Scaffold.slnx +13 -0
- package/templates/backend/host/Macrowing.Start/Init/app.json +8 -0
- package/templates/backend/host/Macrowing.Start/Init/menus.json +4 -0
- package/templates/backend/host/Macrowing.Start/Initialization/InitializationDefinition.cs +245 -0
- package/templates/backend/host/Macrowing.Start/Initialization/InitializationEndpoints.cs +57 -0
- package/templates/backend/host/Macrowing.Start/Initialization/InitializationHostedService.cs +63 -0
- package/templates/backend/host/Macrowing.Start/Initialization/InitializationOptions.cs +44 -0
- package/templates/backend/host/Macrowing.Start/Initialization/InitializationRunner.cs +294 -0
- package/templates/backend/host/Macrowing.Start/Initialization/InitializationState.cs +72 -0
- package/templates/backend/host/Macrowing.Start/Initialization/MenuFactory.cs +118 -0
- package/templates/backend/host/Macrowing.Start/Initialization/PackageBuilder.cs +317 -0
- package/templates/backend/host/Macrowing.Start/Initialization/PlatformAppImporter.cs +371 -0
- package/templates/backend/host/Macrowing.Start/Macrowing.Start.csproj +26 -0
- package/templates/backend/host/Macrowing.Start/Program.cs +33 -0
- package/templates/backend/host/Macrowing.Start/StartModule.cs +67 -0
- package/templates/backend/host/Macrowing.Start/appsettings.json +38 -0
- package/templates/backend/modules/Macrowing.Generated/Application/GeneratedApplicationAutoMapperProfile.cs +42 -0
- package/templates/backend/modules/Macrowing.Generated/Domain/Entities/GeneratedEntityBase.cs +72 -0
- package/templates/backend/modules/Macrowing.Generated/Domain/GeneratedConsts.cs +50 -0
- package/templates/backend/modules/Macrowing.Generated/EntityFrameworkCore/GeneratedDbContext.cs +55 -0
- package/templates/backend/modules/Macrowing.Generated/EntityFrameworkCore/GeneratedDbContextModelBuilderExtensions.cs +61 -0
- package/templates/backend/modules/Macrowing.Generated/EntityFrameworkCore/IGeneratedDbContext.cs +32 -0
- package/templates/backend/modules/Macrowing.Generated/GeneratedModule.cs +89 -0
- package/templates/backend/modules/Macrowing.Generated/Macrowing.Generated.csproj +22 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Cms/CmsClient.cs +95 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Cms/CmsModels.cs +81 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Dictionary/DictionaryClient.cs +44 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/InBizSdkModule.cs +16 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/InterfaceEngine/InterfaceEngineClient.cs +50 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Localization/LocalizationClient.cs +28 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Macrowing.InBiz.SDK.csproj +16 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Message/MessageClient.cs +99 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Message/MessageModels.cs +63 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Model/ModelClient.cs +131 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Model/ModelQuery.cs +108 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Model/ModelWrite.cs +44 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Permission/PermissionClient.cs +86 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Platform/InBizPlatformException.cs +42 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Platform/InBizPlatformRegistration.cs +60 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Platform/InBizPlatformTransport.cs +96 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Platform/InBizRequestContext.cs +27 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/README.md +117 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Details/WorkFlowDetailsClient.cs +35 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Details/WorkFlowDetailsQuery.cs +20 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Operations/WorkFlowOperationRequests.cs +105 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Operations/WorkFlowOperationsClient.cs +53 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/README.md +132 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Tracking/WorkFlowTrackingClient.cs +50 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Upgrade/WorkFlowUpgradeClient.cs +78 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Upgrade/WorkFlowUpgradeModels.cs +34 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/WorkFlowContracts.cs +88 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/WorkFlowRegistration.cs +59 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/WorkFlowTransport.cs +163 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Workbench/WorkFlowWorkbenchClient.cs +59 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Workbench/WorkFlowWorkbenchModels.cs +76 -0
- package/templates/backend/modules/Macrowing.OC.SDK/Macrowing.OC.SDK.csproj +20 -0
- package/templates/backend/modules/Macrowing.OC.SDK/OcSdkModule.cs +18 -0
- package/templates/backend/modules/Macrowing.OC.SDK/Protos/README.md +22 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/Macrowing.InBiz.SDK.Tests.csproj +14 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/PlatformContractTests.cs +288 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/PlatformLiveTests.cs +173 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/WorkFlowHttpServer.cs +113 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/WorkFlowLiveTests.cs +269 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/WorkFlowOperationsTests.cs +80 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/WorkFlowQueryTests.cs +163 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/WorkFlowTransportTests.cs +111 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/WorkFlowUpgradeTests.cs +87 -0
- package/templates/frontend/components/README.md +39 -0
- package/templates/frontend/components/app-components/README.md +33 -0
- package/templates/frontend/components/app-components/common/.gitkeep +0 -0
- package/templates/frontend/components/app-components/components/.gitkeep +0 -0
- package/templates/frontend/components/app-components/package.json +20 -0
- package/templates/frontend/mgt/.umirc.ts +574 -0
- package/templates/frontend/mgt/package.json +96 -0
- package/templates/frontend/mgt/src/app-extends.ts +9 -0
- package/templates/frontend/mgt/src/app.tsx +20 -0
- package/templates/frontend/mgt/src/config/app.ts +31 -0
- package/templates/frontend/mgt/src/layout/header.tsx +14 -0
- package/templates/frontend/mgt/src/layout/index.tsx +11 -0
- package/templates/frontend/mgt/src/layout/sidebar.tsx +14 -0
- package/templates/frontend/mgt/src/locales.ts +8 -0
- package/templates/frontend/mgt/src/pages/.gitkeep +0 -0
- package/templates/frontend/mgt/src/root.tsx +12 -0
- package/templates/frontend/mgt/src/route-view.tsx +3 -0
- package/templates/frontend/mgt/src/runtime.ts +21 -0
- package/templates/frontend/mgt/src/styles/app.css +1 -0
- package/templates/frontend/mgt/src/styles/layout.less +79 -0
- package/templates/frontend/mgt/tsconfig.json +65 -0
- package/templates/frontend/mgt/typings.d.ts +57 -0
- package/templates/frontend/wap/.umirc.ts +574 -0
- package/templates/frontend/wap/package.json +73 -0
- package/templates/frontend/wap/src/app-extends.ts +9 -0
- package/templates/frontend/wap/src/app.tsx +28 -0
- package/templates/frontend/wap/src/config/app.ts +31 -0
- package/templates/frontend/wap/src/layout/header.tsx +14 -0
- package/templates/frontend/wap/src/layout/index.tsx +18 -0
- package/templates/frontend/wap/src/layout/sidebar.tsx +14 -0
- package/templates/frontend/wap/src/locales.ts +8 -0
- package/templates/frontend/wap/src/pages/.gitkeep +0 -0
- package/templates/frontend/wap/src/root.tsx +12 -0
- package/templates/frontend/wap/src/route-view.tsx +3 -0
- package/templates/frontend/wap/src/runtime.ts +21 -0
- package/templates/frontend/wap/src/styles/adaptation.less +190 -0
- package/templates/frontend/wap/src/styles/app.css +7 -0
- package/templates/frontend/wap/src/styles/global.less +81 -0
- package/templates/frontend/wap/src/styles/layout.less +92 -0
- package/templates/frontend/wap/tsconfig.json +66 -0
- package/templates/frontend/wap/typings.d.ts +24 -0
- package/templates/frontend/web/.umirc.ts +574 -0
- package/templates/frontend/web/package.json +26 -0
- package/templates/frontend/web/src/app-extends.ts +9 -0
- package/templates/frontend/web/src/app.tsx +25 -0
- package/templates/frontend/web/src/config/app.ts +31 -0
- package/templates/frontend/web/src/layout/header.tsx +14 -0
- package/templates/frontend/web/src/layout/index.tsx +11 -0
- package/templates/frontend/web/src/layout/sidebar.tsx +14 -0
- package/templates/frontend/web/src/locales.ts +8 -0
- package/templates/frontend/web/src/root.tsx +12 -0
- package/templates/frontend/web/src/route-view.tsx +3 -0
- package/templates/frontend/web/src/runtime.ts +21 -0
- package/templates/frontend/web/src/styles/app.css +104 -0
- package/templates/frontend/web/src/styles/layout.less +124 -0
- package/templates/frontend/web/tsconfig.json +91 -0
- package/templates/frontend/web/typings.d.ts +46 -0
- package/templates/package.json +14 -0
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
using System.Net.Http.Headers;
|
|
2
|
+
using System.Net.Http.Json;
|
|
3
|
+
using System.Text.Json;
|
|
4
|
+
using System.Text.Json.Nodes;
|
|
5
|
+
using Microsoft.Extensions.Options;
|
|
6
|
+
|
|
7
|
+
namespace Macrowing.Start.Initialization;
|
|
8
|
+
|
|
9
|
+
/// <summary>应用导入/发布能力。抽成接口是为了让初始化流程能在测试里替换掉平台。</summary>
|
|
10
|
+
public interface IPlatformAppImporter
|
|
11
|
+
{
|
|
12
|
+
/// <summary>
|
|
13
|
+
/// 按域名查应用;不存在返回 null。其它错误(网络/鉴权/500)必须抛出。
|
|
14
|
+
/// <paramref name="workingVersion"/> 为 true 时返回**工作版本**(GetVersionInfo=true),
|
|
15
|
+
/// 否则返回已发布版本 —— 发布接口要的是工作版本的 id。
|
|
16
|
+
/// </summary>
|
|
17
|
+
Task<JsonNode?> GetByDomainAsync(string domain, bool workingVersion = false, CancellationToken ct = default);
|
|
18
|
+
|
|
19
|
+
/// <summary>导入应用包。<paramref name="overwrite"/> 为 true 时更新已有应用。</summary>
|
|
20
|
+
Task<ImportResult> ImportAsync(Stream package, string domain, string appName,
|
|
21
|
+
string tablePrefix, int contains, bool overwrite, string? appId = null,
|
|
22
|
+
CancellationToken ct = default);
|
|
23
|
+
|
|
24
|
+
/// <summary>
|
|
25
|
+
/// 发布一个工作版本,让菜单接口返回新的菜单。
|
|
26
|
+
/// 返回发布后**新的工作版本 id**(平台会把已发布版本冻结,另建一个工作版本)。
|
|
27
|
+
/// </summary>
|
|
28
|
+
Task<string?> PublishAsync(string appId, string siteOutId, string dataVersionId,
|
|
29
|
+
string? remarks = null, CancellationToken ct = default);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public sealed record ImportResult(string AppId, string? SecretKey, string? Key);
|
|
33
|
+
|
|
34
|
+
/// <summary>
|
|
35
|
+
/// 平台实现。接口路径来自 InBiz VS Code 扩展的恢复契约
|
|
36
|
+
/// (<c>references/platform/APPLICATION_RECOVERY_APIS.md</c>)。
|
|
37
|
+
/// </summary>
|
|
38
|
+
public sealed class PlatformAppImporter : IPlatformAppImporter
|
|
39
|
+
{
|
|
40
|
+
private readonly HttpClient _http;
|
|
41
|
+
private readonly InitializationOptions _options;
|
|
42
|
+
private readonly ILogger<PlatformAppImporter> _logger;
|
|
43
|
+
|
|
44
|
+
private string? _token;
|
|
45
|
+
private readonly SemaphoreSlim _tokenLock = new(1, 1);
|
|
46
|
+
|
|
47
|
+
public PlatformAppImporter(HttpClient http, IOptions<InitializationOptions> options, ILogger<PlatformAppImporter> logger)
|
|
48
|
+
{
|
|
49
|
+
_http = http;
|
|
50
|
+
_options = options.Value;
|
|
51
|
+
_logger = logger;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private string BaseUrl => (_options.ServerUrl ?? _http.BaseAddress?.ToString() ?? string.Empty).TrimEnd('/');
|
|
55
|
+
|
|
56
|
+
/// <summary>
|
|
57
|
+
/// 取 Token(缓存)。
|
|
58
|
+
///
|
|
59
|
+
/// 部署环境没有 <c>~/.inbiz/credentials</c>,所以用集成账号现场换一个;
|
|
60
|
+
/// 换不到就直接失败 —— 没有凭据的导入请求只会得到 401,重试没有意义。
|
|
61
|
+
/// </summary>
|
|
62
|
+
private async Task<string> EnsureTokenAsync(CancellationToken ct)
|
|
63
|
+
{
|
|
64
|
+
if (!string.IsNullOrEmpty(_token)) return _token;
|
|
65
|
+
// 预签发 token 优先,直接固定下来,不再走集成登录
|
|
66
|
+
if (!string.IsNullOrWhiteSpace(_options.Token))
|
|
67
|
+
{
|
|
68
|
+
_token = _options.Token!;
|
|
69
|
+
return _token;
|
|
70
|
+
}
|
|
71
|
+
if (string.IsNullOrWhiteSpace(_options.UserName) || string.IsNullOrWhiteSpace(_options.IntegrationKey))
|
|
72
|
+
throw new UnauthorizedAccessException(
|
|
73
|
+
"缺少 Initialization:UserName / IntegrationKey,无法调用平台导入接口");
|
|
74
|
+
|
|
75
|
+
await _tokenLock.WaitAsync(ct);
|
|
76
|
+
try
|
|
77
|
+
{
|
|
78
|
+
if (!string.IsNullOrEmpty(_token)) return _token;
|
|
79
|
+
|
|
80
|
+
using var request = new HttpRequestMessage(HttpMethod.Post,
|
|
81
|
+
BaseUrl + "/inbiz/auth/api/AuthCore/UserLoginIntegrationByUserLoginName")
|
|
82
|
+
{
|
|
83
|
+
Content = JsonContent.Create(new
|
|
84
|
+
{
|
|
85
|
+
LoginName = _options.UserName,
|
|
86
|
+
IPAddress = "127.0.0.1",
|
|
87
|
+
IntegrationKey = _options.IntegrationKey,
|
|
88
|
+
}),
|
|
89
|
+
};
|
|
90
|
+
using var response = await _http.SendAsync(request, ct);
|
|
91
|
+
var text = await response.Content.ReadAsStringAsync(ct);
|
|
92
|
+
if (!response.IsSuccessStatusCode)
|
|
93
|
+
throw new UnauthorizedAccessException($"集成登录失败:HTTP {(int)response.StatusCode} {Truncate(text)}");
|
|
94
|
+
|
|
95
|
+
_token = JsonNode.Parse(text)?["Token"]?.GetValue<string>()
|
|
96
|
+
?? throw new UnauthorizedAccessException("集成登录未返回 Token");
|
|
97
|
+
return _token;
|
|
98
|
+
}
|
|
99
|
+
finally
|
|
100
|
+
{
|
|
101
|
+
_tokenLock.Release();
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
private async Task<HttpRequestMessage> RequestAsync(HttpMethod method, string url, CancellationToken ct)
|
|
106
|
+
{
|
|
107
|
+
var request = new HttpRequestMessage(method, BaseUrl + url);
|
|
108
|
+
// 导入/校验/轮询必须不带旧 AppId,否则新应用会挂到旧应用下;
|
|
109
|
+
// 应用维度只通过 token 的租户上下文体现。
|
|
110
|
+
request.Headers.Add("token", await EnsureTokenAsync(ct));
|
|
111
|
+
return request;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
private HttpRequestMessage Request(HttpMethod method, string url) =>
|
|
115
|
+
new(method, BaseUrl + url);
|
|
116
|
+
|
|
117
|
+
public async Task<JsonNode?> GetByDomainAsync(string domain, bool workingVersion = false, CancellationToken ct = default)
|
|
118
|
+
{
|
|
119
|
+
// getVersionInfo=true → 站点返回的是**工作版本**(AppSiteData)的 id,
|
|
120
|
+
// 发布接口要的正是这个;false → 已发布版本 id(前端读菜单用的那个)。
|
|
121
|
+
var url = $"/inbiz/api/services/front/engines/v3/app/info"
|
|
122
|
+
+ $"?domain={Uri.EscapeDataString(domain)}&getVersionInfo={(workingVersion ? "true" : "false")}";
|
|
123
|
+
using var response = await _http.SendAsync(await RequestAsync(HttpMethod.Get, url, ct), ct);
|
|
124
|
+
if (response.StatusCode == System.Net.HttpStatusCode.NotFound) return null;
|
|
125
|
+
var text = await response.Content.ReadAsStringAsync(ct);
|
|
126
|
+
|
|
127
|
+
// 平台对「不存在」也会回 500 + 结构化业务错误;只有三段都匹配才算不存在
|
|
128
|
+
if (!response.IsSuccessStatusCode)
|
|
129
|
+
{
|
|
130
|
+
if (IsAppMissing(text)) return null;
|
|
131
|
+
throw new InvalidOperationException($"查询应用失败:HTTP {(int)response.StatusCode} {Truncate(text)}");
|
|
132
|
+
}
|
|
133
|
+
return JsonNode.Parse(text);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
private static bool IsAppMissing(string text)
|
|
137
|
+
{
|
|
138
|
+
try
|
|
139
|
+
{
|
|
140
|
+
var json = JsonNode.Parse(text);
|
|
141
|
+
var error = json?["Error"];
|
|
142
|
+
if (error is null) return false;
|
|
143
|
+
var codeOk = error["Code"]?.GetValue<string>() == "coreErr:000002";
|
|
144
|
+
var msgOk = error["Data"]?["errorMsg"]?.GetValue<string>() == "应用";
|
|
145
|
+
var textOk = error["Message"]?.GetValue<string>()?.Contains("不存在") == true;
|
|
146
|
+
return codeOk && msgOk && textOk;
|
|
147
|
+
}
|
|
148
|
+
catch (JsonException)
|
|
149
|
+
{
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
public async Task<ImportResult> ImportAsync(Stream package, string domain, string appName,
|
|
155
|
+
string tablePrefix, int contains, bool overwrite, string? appId = null,
|
|
156
|
+
CancellationToken ct = default)
|
|
157
|
+
{
|
|
158
|
+
/**
|
|
159
|
+
* 创建与更新走**两套**接口,不能混用:
|
|
160
|
+
*
|
|
161
|
+
* 创建:importZip → validateImportRule → import
|
|
162
|
+
* 更新:importZipUpdated → validateUpdateRule → updateImport
|
|
163
|
+
*
|
|
164
|
+
* 更新路径才是「改名字/改图标」能生效的那条:它在工作版本里按 OutId
|
|
165
|
+
* upsert(新增 + 覆盖),不删除包里没有的菜单。`isCover` 只影响流程引擎,
|
|
166
|
+
* 对菜单没有任何作用,所以这里不能靠它来区分创建/更新。
|
|
167
|
+
*/
|
|
168
|
+
if (overwrite)
|
|
169
|
+
{
|
|
170
|
+
if (string.IsNullOrWhiteSpace(appId))
|
|
171
|
+
throw new InvalidOperationException("更新已有应用必须提供 appId");
|
|
172
|
+
return await UpdateAsync(package, contains, appId!, ct);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// 1) 上传
|
|
176
|
+
using var upload = await RequestAsync(HttpMethod.Post, "/inbiz/api/services/init/v3/app/importZip", ct);
|
|
177
|
+
var form = new MultipartFormDataContent();
|
|
178
|
+
var file = new StreamContent(package);
|
|
179
|
+
file.Headers.ContentType = new MediaTypeHeaderValue("application/zip");
|
|
180
|
+
form.Add(file, "file", "app.zip");
|
|
181
|
+
form.Add(new StringContent("true"), "isVsCode");
|
|
182
|
+
upload.Content = form;
|
|
183
|
+
|
|
184
|
+
using var uploadResponse = await _http.SendAsync(upload, ct);
|
|
185
|
+
var uploadText = await uploadResponse.Content.ReadAsStringAsync(ct);
|
|
186
|
+
EnsureSuccess(uploadResponse, uploadText, "上传应用包");
|
|
187
|
+
var uploadJson = JsonNode.Parse(uploadText)!;
|
|
188
|
+
var tempFolder = uploadJson["TempFolderName"]?.GetValue<string>()
|
|
189
|
+
?? throw new InvalidOperationException("上传成功但未返回 TempFolderName");
|
|
190
|
+
|
|
191
|
+
// 2) 规则校验 + 轮询
|
|
192
|
+
using var validate = await RequestAsync(HttpMethod.Post, "/inbiz/api/services/init/v3/app/validateImportRule", ct);
|
|
193
|
+
validate.Content = JsonContent.Create(new { contains, tempFolderName = tempFolder });
|
|
194
|
+
using var validateResponse = await _http.SendAsync(validate, ct);
|
|
195
|
+
var validateText = await validateResponse.Content.ReadAsStringAsync(ct);
|
|
196
|
+
EnsureSuccess(validateResponse, validateText, "规则校验");
|
|
197
|
+
var validateKey = JsonNode.Parse(validateText)?["ValidateKey"]?.GetValue<string>()
|
|
198
|
+
?? throw new InvalidOperationException("规则校验未返回 ValidateKey");
|
|
199
|
+
await WaitForTaskAsync(validateKey, "规则校验", ct);
|
|
200
|
+
|
|
201
|
+
// 3) 正式导入
|
|
202
|
+
using var import = await RequestAsync(HttpMethod.Post, "/inbiz/api/services/init/v3/app/import", ct);
|
|
203
|
+
import.Content = JsonContent.Create(new
|
|
204
|
+
{
|
|
205
|
+
tempFolderName = tempFolder,
|
|
206
|
+
appName,
|
|
207
|
+
domain,
|
|
208
|
+
tableNamePrefix = tablePrefix,
|
|
209
|
+
isCover = overwrite,
|
|
210
|
+
});
|
|
211
|
+
using var importResponse = await _http.SendAsync(import, ct);
|
|
212
|
+
var importText = await importResponse.Content.ReadAsStringAsync(ct);
|
|
213
|
+
EnsureSuccess(importResponse, importText, "导入应用");
|
|
214
|
+
var importJson = JsonNode.Parse(importText)!;
|
|
215
|
+
var createdAppId = importJson["AppId"]?.GetValue<string>()
|
|
216
|
+
?? throw new InvalidOperationException("导入未返回 AppId");
|
|
217
|
+
var key = importJson["Key"]?.GetValue<string>();
|
|
218
|
+
if (!string.IsNullOrEmpty(key)) await WaitForTaskAsync(key, "应用导入", ct);
|
|
219
|
+
|
|
220
|
+
_logger.LogInformation("应用包已导入,appId={AppId} overwrite={Overwrite}", createdAppId, overwrite);
|
|
221
|
+
return new ImportResult(createdAppId, null, key);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/// <summary>
|
|
225
|
+
/// 更新已有应用的包(`importZipUpdated` → `validateUpdateRule` → `updateImport`)。
|
|
226
|
+
///
|
|
227
|
+
/// 这条链在**工作版本**里按 OutId 做 upsert:包里有的新增/覆盖,
|
|
228
|
+
/// 包里没有的不删除。所以「改名字、改图标、调顺序」能生效,
|
|
229
|
+
/// 而「删掉一个菜单」不会——删除只能用菜单接口逐条做。
|
|
230
|
+
/// </summary>
|
|
231
|
+
private async Task<ImportResult> UpdateAsync(Stream package, int contains, string appId, CancellationToken ct)
|
|
232
|
+
{
|
|
233
|
+
// 1) 上传(appId 走 header,这条接口不靠 body 传应用维度)
|
|
234
|
+
using var upload = await RequestAsync(HttpMethod.Post, "/inbiz/api/services/init/v3/app/importZipUpdated", ct);
|
|
235
|
+
upload.Headers.Add("appId", appId);
|
|
236
|
+
var form = new MultipartFormDataContent();
|
|
237
|
+
var file = new StreamContent(package);
|
|
238
|
+
file.Headers.ContentType = new MediaTypeHeaderValue("application/zip");
|
|
239
|
+
form.Add(file, "file", "app.zip");
|
|
240
|
+
form.Add(new StringContent("true"), "isVsCode");
|
|
241
|
+
upload.Content = form;
|
|
242
|
+
|
|
243
|
+
using var uploadResponse = await _http.SendAsync(upload, ct);
|
|
244
|
+
var uploadText = await uploadResponse.Content.ReadAsStringAsync(ct);
|
|
245
|
+
EnsureSuccess(uploadResponse, uploadText, "上传更新包");
|
|
246
|
+
var tempFolder = JsonNode.Parse(uploadText)?["TempFolderName"]?.GetValue<string>()
|
|
247
|
+
?? throw new InvalidOperationException("上传成功但未返回 TempFolderName");
|
|
248
|
+
|
|
249
|
+
// 2) 更新规则校验
|
|
250
|
+
using var validate = await RequestAsync(HttpMethod.Post,
|
|
251
|
+
$"/inbiz/api/services/init/v3/app/{Uri.EscapeDataString(appId)}/validateUpdateRule", ct);
|
|
252
|
+
validate.Headers.Add("appId", appId);
|
|
253
|
+
validate.Content = JsonContent.Create(new { tempFolderName = tempFolder, contains });
|
|
254
|
+
using var validateResponse = await _http.SendAsync(validate, ct);
|
|
255
|
+
var validateText = await validateResponse.Content.ReadAsStringAsync(ct);
|
|
256
|
+
EnsureSuccess(validateResponse, validateText, "更新规则校验");
|
|
257
|
+
var validateKey = JsonNode.Parse(validateText)?["ValidateKey"]?.GetValue<string>();
|
|
258
|
+
if (!string.IsNullOrEmpty(validateKey)) await WaitForTaskAsync(validateKey, "更新规则校验", ct);
|
|
259
|
+
|
|
260
|
+
// 3) 正式更新
|
|
261
|
+
using var update = await RequestAsync(HttpMethod.Post,
|
|
262
|
+
$"/inbiz/api/services/init/v3/app/{Uri.EscapeDataString(appId)}/updateImport", ct);
|
|
263
|
+
update.Headers.Add("appId", appId);
|
|
264
|
+
update.Content = JsonContent.Create(new
|
|
265
|
+
{
|
|
266
|
+
tempFolderName = tempFolder,
|
|
267
|
+
// 业务属性覆盖:让改名/改图标真正写进去
|
|
268
|
+
overwrite = true,
|
|
269
|
+
isCover = false,
|
|
270
|
+
});
|
|
271
|
+
using var updateResponse = await _http.SendAsync(update, ct);
|
|
272
|
+
var updateText = await updateResponse.Content.ReadAsStringAsync(ct);
|
|
273
|
+
EnsureSuccess(updateResponse, updateText, "更新导入");
|
|
274
|
+
var key = JsonNode.Parse(updateText)?["Key"]?.GetValue<string>();
|
|
275
|
+
if (!string.IsNullOrEmpty(key)) await WaitForTaskAsync(key, "应用更新", ct);
|
|
276
|
+
|
|
277
|
+
_logger.LogInformation("应用包已更新,appId={AppId}", appId);
|
|
278
|
+
return new ImportResult(appId, null, key);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/// <summary>
|
|
282
|
+
/// 发布版本。契约来自平台源码
|
|
283
|
+
/// (`EDoc2.InBiz.Engine.Application/Service/AppSiteAppService.cs:68-116`)。
|
|
284
|
+
///
|
|
285
|
+
/// 注意三点:
|
|
286
|
+
/// - 这是**同步**调用,没有任务号;平台内部会复制版本、清缓存、导出站点包,
|
|
287
|
+
/// 耗时较长,所以要给足超时;
|
|
288
|
+
/// - 返回的是**发布后新建的工作版本**(state=0),不是刚发布的那个;
|
|
289
|
+
/// - 同一个 dataVersionId 不能发布两次(已发布版本会被拒绝,coreErr:000037)。
|
|
290
|
+
/// </summary>
|
|
291
|
+
public async Task<string?> PublishAsync(string appId, string siteOutId, string dataVersionId,
|
|
292
|
+
string? remarks = null, CancellationToken ct = default)
|
|
293
|
+
{
|
|
294
|
+
if (string.IsNullOrWhiteSpace(siteOutId) || string.IsNullOrWhiteSpace(dataVersionId))
|
|
295
|
+
throw new InvalidOperationException("发布需要 siteOutId 与 dataVersionId(工作版本)");
|
|
296
|
+
|
|
297
|
+
using var request = await RequestAsync(HttpMethod.Post, "/inbiz/api/services/engines/v3/appSite/publish", ct);
|
|
298
|
+
request.Headers.Add("appId", appId);
|
|
299
|
+
request.Content = JsonContent.Create(new
|
|
300
|
+
{
|
|
301
|
+
siteOutId,
|
|
302
|
+
dataVersionId,
|
|
303
|
+
remarks = remarks ?? string.Empty,
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
using var response = await _http.SendAsync(request, ct);
|
|
307
|
+
var text = await response.Content.ReadAsStringAsync(ct);
|
|
308
|
+
EnsureSuccess(response, text, "发布版本");
|
|
309
|
+
|
|
310
|
+
var json = JsonNode.Parse(text);
|
|
311
|
+
var newVersionId = json?["id"]?.GetValue<string>() ?? json?["Id"]?.GetValue<string>();
|
|
312
|
+
_logger.LogInformation("已发布站点 {SiteOutId},新工作版本 {VersionId}", siteOutId, newVersionId);
|
|
313
|
+
return newVersionId;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
private async Task WaitForTaskAsync(string key, string what, CancellationToken ct)
|
|
317
|
+
{
|
|
318
|
+
var startedAt = DateTimeOffset.UtcNow;
|
|
319
|
+
var failures = 0;
|
|
320
|
+
|
|
321
|
+
while (true)
|
|
322
|
+
{
|
|
323
|
+
if (DateTimeOffset.UtcNow - startedAt > TimeSpan.FromMinutes(5))
|
|
324
|
+
throw new TimeoutException($"{what}超时(key={key})");
|
|
325
|
+
|
|
326
|
+
JsonNode? progress;
|
|
327
|
+
try
|
|
328
|
+
{
|
|
329
|
+
using var request = await RequestAsync(HttpMethod.Get,
|
|
330
|
+
$"/inbiz/api/services/init/v3/app/processInfo?key={Uri.EscapeDataString(key)}", ct);
|
|
331
|
+
using var response = await _http.SendAsync(request, ct);
|
|
332
|
+
var text = await response.Content.ReadAsStringAsync(ct);
|
|
333
|
+
EnsureSuccess(response, text, $"{what}进度");
|
|
334
|
+
progress = JsonNode.Parse(text);
|
|
335
|
+
failures = 0;
|
|
336
|
+
}
|
|
337
|
+
catch (Exception) when (failures < 20)
|
|
338
|
+
{
|
|
339
|
+
failures++;
|
|
340
|
+
await Task.Delay(TimeSpan.FromSeconds(1), ct);
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
var status = progress?["Process"]?["Status"]?.GetValue<int>() ?? 0;
|
|
345
|
+
if (status is 2 or 3)
|
|
346
|
+
{
|
|
347
|
+
var message = progress?["Process"]?["Message"]?.GetValue<string>() ?? string.Empty;
|
|
348
|
+
var blockers = progress?["AppRule"]?["ModuleRuleOutDto"]?.AsArray()
|
|
349
|
+
.SelectMany(m => m?["RuleOutDto"]?.AsArray() ?? new JsonArray())
|
|
350
|
+
.Select(r => r?["Msg"]?.GetValue<string>())
|
|
351
|
+
.Where(m => !string.IsNullOrWhiteSpace(m))
|
|
352
|
+
.ToList() ?? new List<string?>();
|
|
353
|
+
throw new InvalidOperationException(
|
|
354
|
+
$"{what}失败(status={status}):{string.Join(";", blockers.DefaultIfEmpty(message))}");
|
|
355
|
+
}
|
|
356
|
+
if (status == 4) return;
|
|
357
|
+
|
|
358
|
+
await Task.Delay(TimeSpan.FromSeconds(1), ct);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
private static void EnsureSuccess(HttpResponseMessage response, string text, string what)
|
|
363
|
+
{
|
|
364
|
+
if (response.IsSuccessStatusCode) return;
|
|
365
|
+
if (response.StatusCode is System.Net.HttpStatusCode.Unauthorized or System.Net.HttpStatusCode.Forbidden)
|
|
366
|
+
throw new UnauthorizedAccessException($"{what}:Token 失效或无权访问(HTTP {(int)response.StatusCode})");
|
|
367
|
+
throw new InvalidOperationException($"{what}失败:HTTP {(int)response.StatusCode} {Truncate(text)}");
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
private static string Truncate(string text) => text.Length <= 300 ? text : text[..300];
|
|
371
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
<Project Sdk="Microsoft.NET.Sdk.Web">
|
|
2
|
+
|
|
3
|
+
<PropertyGroup>
|
|
4
|
+
<RootNamespace>Macrowing.Start</RootNamespace>
|
|
5
|
+
<AssemblyName>Macrowing.Start</AssemblyName>
|
|
6
|
+
<UserSecretsId>macrowing-start-codegen</UserSecretsId>
|
|
7
|
+
</PropertyGroup>
|
|
8
|
+
|
|
9
|
+
<ItemGroup>
|
|
10
|
+
<PackageReference Include="Volo.Abp.AspNetCore.Mvc" />
|
|
11
|
+
<PackageReference Include="Volo.Abp.Autofac" />
|
|
12
|
+
<PackageReference Include="Volo.Abp.AspNetCore.Serilog" />
|
|
13
|
+
<PackageReference Include="Volo.Abp.Swashbuckle" />
|
|
14
|
+
<PackageReference Include="Serilog.AspNetCore" />
|
|
15
|
+
<PackageReference Include="Serilog.Sinks.Async" />
|
|
16
|
+
<PackageReference Include="Swashbuckle.AspNetCore" />
|
|
17
|
+
</ItemGroup>
|
|
18
|
+
|
|
19
|
+
<ItemGroup>
|
|
20
|
+
<ProjectReference Include="..\..\modules\Macrowing.InBiz.SDK\Macrowing.InBiz.SDK.csproj" />
|
|
21
|
+
<ProjectReference Include="..\..\modules\Macrowing.OC.SDK\Macrowing.OC.SDK.csproj" />
|
|
22
|
+
<!-- 出码产物:生成的业务模块挂在这里。首次无产物时为占位模块。 -->
|
|
23
|
+
<ProjectReference Include="..\..\modules\Macrowing.Generated\Macrowing.Generated.csproj" />
|
|
24
|
+
</ItemGroup>
|
|
25
|
+
|
|
26
|
+
</Project>
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
using Macrowing.Start;
|
|
2
|
+
using Serilog;
|
|
3
|
+
using Serilog.Events;
|
|
4
|
+
|
|
5
|
+
Log.Logger = new LoggerConfiguration()
|
|
6
|
+
.MinimumLevel.Information()
|
|
7
|
+
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
|
|
8
|
+
.Enrich.FromLogContext()
|
|
9
|
+
.WriteTo.Async(c => c.Console())
|
|
10
|
+
.CreateLogger();
|
|
11
|
+
|
|
12
|
+
try
|
|
13
|
+
{
|
|
14
|
+
Log.Information("Starting Macrowing.Start");
|
|
15
|
+
var builder = WebApplication.CreateBuilder(args);
|
|
16
|
+
builder.Host
|
|
17
|
+
.UseSerilog()
|
|
18
|
+
.UseAutofac();
|
|
19
|
+
await builder.AddApplicationAsync<StartModule>();
|
|
20
|
+
var app = builder.Build();
|
|
21
|
+
await app.InitializeApplicationAsync();
|
|
22
|
+
await app.RunAsync();
|
|
23
|
+
return 0;
|
|
24
|
+
}
|
|
25
|
+
catch (Exception ex)
|
|
26
|
+
{
|
|
27
|
+
Log.Fatal(ex, "Host terminated unexpectedly");
|
|
28
|
+
return 1;
|
|
29
|
+
}
|
|
30
|
+
finally
|
|
31
|
+
{
|
|
32
|
+
await Log.CloseAndFlushAsync();
|
|
33
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
using Macrowing.Generated;
|
|
2
|
+
using Macrowing.InBiz.SDK;
|
|
3
|
+
using Macrowing.OC.SDK;
|
|
4
|
+
using Macrowing.Start.Initialization;
|
|
5
|
+
using Volo.Abp.AspNetCore.Mvc;
|
|
6
|
+
using Volo.Abp.Autofac;
|
|
7
|
+
using Volo.Abp;
|
|
8
|
+
using Volo.Abp.Modularity;
|
|
9
|
+
|
|
10
|
+
namespace Macrowing.Start;
|
|
11
|
+
|
|
12
|
+
[DependsOn(
|
|
13
|
+
typeof(AbpAutofacModule),
|
|
14
|
+
typeof(AbpAspNetCoreMvcModule),
|
|
15
|
+
typeof(InBizSdkModule),
|
|
16
|
+
typeof(OcSdkModule),
|
|
17
|
+
typeof(GeneratedModule)
|
|
18
|
+
)]
|
|
19
|
+
public class StartModule : AbpModule
|
|
20
|
+
{
|
|
21
|
+
public override void ConfigureServices(ServiceConfigurationContext context)
|
|
22
|
+
{
|
|
23
|
+
var configuration = context.Services.GetConfiguration();
|
|
24
|
+
|
|
25
|
+
Configure<AbpAspNetCoreMvcOptions>(options =>
|
|
26
|
+
{
|
|
27
|
+
// 出码产物的控制器统一挂在 /api 前缀下
|
|
28
|
+
options.ConventionalControllers.Create(typeof(GeneratedModule).Assembly);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
context.Services.AddEndpointsApiExplorer();
|
|
32
|
+
context.Services.AddSwaggerGen();
|
|
33
|
+
|
|
34
|
+
// ── 应用初始化(部署期自动执行,开发期可用 /api/init/apply 触发)────────
|
|
35
|
+
//
|
|
36
|
+
// 初始化文件(Init/app.json、Init/menus.json)是期望态;
|
|
37
|
+
// 宿主启动时把它打包成应用包导入平台,菜单变了就重新导入并发布。
|
|
38
|
+
Configure<InitializationOptions>(configuration.GetSection(InitializationOptions.SectionName));
|
|
39
|
+
|
|
40
|
+
context.Services.AddHttpClient<IPlatformAppImporter, PlatformAppImporter>();
|
|
41
|
+
context.Services.AddTransient<InitializationRunner>();
|
|
42
|
+
context.Services.AddHostedService<InitializationHostedService>();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
public override void OnApplicationInitialization(ApplicationInitializationContext context)
|
|
46
|
+
{
|
|
47
|
+
var app = context.GetApplicationBuilder();
|
|
48
|
+
var env = context.GetEnvironment();
|
|
49
|
+
|
|
50
|
+
if (env.IsDevelopment())
|
|
51
|
+
{
|
|
52
|
+
app.UseSwagger();
|
|
53
|
+
app.UseSwaggerUI();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
app.UseRouting();
|
|
57
|
+
app.UseCors(b => b.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
|
|
58
|
+
app.UseConfiguredEndpoints();
|
|
59
|
+
|
|
60
|
+
// 初始化运维入口:状态随时可查;写入口只在非生产开放
|
|
61
|
+
// (生产上的初始化由启动期自动完成,开放重放导入的入口风险不对等)
|
|
62
|
+
// ABP 这里给的是 IApplicationBuilder,Map* 扩展要求 WebApplication,
|
|
63
|
+
// 所以先转一下;Program.cs 里建的本来就是 WebApplication。
|
|
64
|
+
if (app is WebApplication webApp)
|
|
65
|
+
webApp.MapInitializationEndpoints(exposeWrites: env.IsDevelopment());
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"Logging": {
|
|
3
|
+
"LogLevel": {
|
|
4
|
+
"Default": "Information",
|
|
5
|
+
"Microsoft.AspNetCore": "Warning"
|
|
6
|
+
}
|
|
7
|
+
},
|
|
8
|
+
"AllowedHosts": "*",
|
|
9
|
+
|
|
10
|
+
"InBiz": {
|
|
11
|
+
"Platform": {
|
|
12
|
+
"BaseUrl": "",
|
|
13
|
+
"Timeout": "00:00:30"
|
|
14
|
+
},
|
|
15
|
+
"WorkFlow": {
|
|
16
|
+
"BaseUrl": "",
|
|
17
|
+
"Timeout": "00:00:30"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
|
|
21
|
+
"//Initialization": [
|
|
22
|
+
"部署期自动初始化:启动时把 Init/ 下的定义打成应用包导入平台。",
|
|
23
|
+
"Enabled=false 可整体关闭(例如平台不可达的离线环境)。",
|
|
24
|
+
"FailOnError=false:已初始化过的环境不应因为平台暂时不可达而起不来。",
|
|
25
|
+
"ServerUrl / UserName / IntegrationKey 建议用环境变量注入:",
|
|
26
|
+
" Initialization__UserName / Initialization__IntegrationKey",
|
|
27
|
+
"不要把集成密钥写进这个文件。"
|
|
28
|
+
],
|
|
29
|
+
"Initialization": {
|
|
30
|
+
"Enabled": true,
|
|
31
|
+
"Directory": "Init",
|
|
32
|
+
"FailOnError": false,
|
|
33
|
+
"AllowMenuUpdate": true,
|
|
34
|
+
"ServerUrl": "",
|
|
35
|
+
"UserName": "",
|
|
36
|
+
"IntegrationKey": ""
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
using AutoMapper;
|
|
2
|
+
|
|
3
|
+
namespace Macrowing.Generated.Application;
|
|
4
|
+
|
|
5
|
+
/// <summary>
|
|
6
|
+
/// 出码模块的 AutoMapper 映射配置。
|
|
7
|
+
///
|
|
8
|
+
/// 【出码器会往这里写什么】
|
|
9
|
+
/// 每个出码实体追加两行映射,不生成多个 Profile 文件:
|
|
10
|
+
/// <code>
|
|
11
|
+
/// CreateMap<{实体名}, {实体名}Dto>();
|
|
12
|
+
/// CreateMap<CreateUpdate{实体名}Dto, {实体名}>();
|
|
13
|
+
/// </code>
|
|
14
|
+
///
|
|
15
|
+
/// 为什么只用一个 Profile:
|
|
16
|
+
/// Profile 是按程序集扫描注册的(见 GeneratedModule 的 AddMaps),
|
|
17
|
+
/// 一个实体一个 Profile 会让 <c>GetAllTypes()</c> 的扫描成本与文件数量线性增长,
|
|
18
|
+
/// 而映射规则本身很简单,合并到一个文件不会降低可读性,反而让「哪些实体有映射」一眼可见。
|
|
19
|
+
///
|
|
20
|
+
/// 关于两个方向的映射:
|
|
21
|
+
/// <list type="bullet">
|
|
22
|
+
/// <item>实体 → DTO:带上 Id / OutId / DataVersion / 审计字段,前端直接平铺取值。</item>
|
|
23
|
+
/// <item>CreateUpdateDto → 实体:只覆盖 DTO 里有的属性。
|
|
24
|
+
/// DTO 刻意不含 OutId / DataVersion,因此更新操作不会改写这两个服务端字段 ——
|
|
25
|
+
/// 这是「用 DTO 结构表达不可变性」的做法,比在服务里逐字段排斥更不容易被后续生成覆盖掉。</item>
|
|
26
|
+
/// </list>
|
|
27
|
+
///
|
|
28
|
+
/// 注意:本模块的 <c>AddMaps</c> 使用 <c>validate: false</c>,
|
|
29
|
+
/// 映射写错不会在启动时报错,而是运行到这行才失败。
|
|
30
|
+
/// 出码器应在生成后自检:实体新增字段时同步更新 Dto,否则该字段会被静默丢弃(映射为 null/0)。
|
|
31
|
+
/// </summary>
|
|
32
|
+
public class GeneratedApplicationAutoMapperProfile : Profile
|
|
33
|
+
{
|
|
34
|
+
public GeneratedApplicationAutoMapperProfile()
|
|
35
|
+
{
|
|
36
|
+
// 初始工程没有实体。出码器每生成一个实体,就在本构造函数末尾追加两行:
|
|
37
|
+
// CreateMap<{实体名}, {实体名}Dto>();
|
|
38
|
+
// CreateMap<CreateUpdate{实体名}Dto, {实体名}>();
|
|
39
|
+
//
|
|
40
|
+
// 反向映射只覆盖 DTO 声明的属性;实体上多出来的审计/身份字段保持原值。
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
using System;
|
|
2
|
+
using Volo.Abp.Domain.Entities.Auditing;
|
|
3
|
+
|
|
4
|
+
namespace Macrowing.Generated.Domain.Entities;
|
|
5
|
+
|
|
6
|
+
/// <summary>
|
|
7
|
+
/// 所有出码实体的基类。
|
|
8
|
+
///
|
|
9
|
+
/// 【出码器会往这里写什么】
|
|
10
|
+
/// 出码器**不写**这个文件 —— 它是脚手架提供的固定基类,任何模型实体都继承它。
|
|
11
|
+
/// 出码器写的是同目录下的 <c>*.generated.cs</c> 实体文件。
|
|
12
|
+
///
|
|
13
|
+
/// 字段设计说明(为什么是这四个身份/审计字段):
|
|
14
|
+
/// <list type="bullet">
|
|
15
|
+
/// <item>
|
|
16
|
+
/// <b>Id(Guid)</b>:本地库主键。由 ABP 的 <c>FullAuditedAggregateRoot<Guid></c> 提供。
|
|
17
|
+
/// 之所以不复用 InBiz 的 outId 当主键:平台侧主键是字符串,
|
|
18
|
+
/// 且「同一业务记录在不同数据版本下可以各有一份」,用 Guid 主键可以容纳多版本副本。
|
|
19
|
+
/// </item>
|
|
20
|
+
/// <item>
|
|
21
|
+
/// <b>OutId(string)</b>:InBiz 侧对象 id。出码产物与平台对齐、二次拉取做增量同步时靠它,
|
|
22
|
+
/// 不能为空,且与 <see cref="DataVersion"/> 组成唯一索引(见 GeneratedDbContextModelBuilderExtensions)。
|
|
23
|
+
/// </item>
|
|
24
|
+
/// <item>
|
|
25
|
+
/// <b>创建时间</b>:来自 <c>FullAuditedAggregateRoot</c> 的 CreationTime / CreatorId /
|
|
26
|
+
/// LastModificationTime / DeleterId 等审计列。InBiz 的「回收站」语义对应这里的软删除
|
|
27
|
+
/// (IsDeleted),不要生成硬删除逻辑。
|
|
28
|
+
/// </item>
|
|
29
|
+
/// <item>
|
|
30
|
+
/// <b>DataVersion(string)</b>:数据版本号,对应平台加载详情接口里的 <c>dataVersionId</c>。
|
|
31
|
+
/// 平台存在「版本模型 + 定制数据 copy-on-write」机制(见 02-codegen-framework.md §7.1 第 3 条),
|
|
32
|
+
/// 同一条业务数据可能同时存在「基线版本」与「某个定制版本」两份记录。
|
|
33
|
+
/// 因此每个实体都带版本号,查询默认按当前版本过滤,而不是靠表名区分。
|
|
34
|
+
/// </item>
|
|
35
|
+
/// </list>
|
|
36
|
+
///
|
|
37
|
+
/// 关于动态字段:
|
|
38
|
+
/// <c>FullAuditedAggregateRoot</c> 已经实现了 <c>IHasExtraProperties</c>,提供
|
|
39
|
+
/// <c>ExtraProperties</c> 字典(落库为 JSON 列)。出码器把 schema 里**没有**对应到真实列的
|
|
40
|
+
/// 动态字段写进 ExtraProperties,这样平台加字段不会导致数据库迁移失败。
|
|
41
|
+
/// 判断标准:字段是否参与查询/排序/索引 —— 参与则建列,否则进 ExtraProperties。
|
|
42
|
+
///
|
|
43
|
+
/// 关于属性可写性:
|
|
44
|
+
/// 出码产物的字段集合来自平台 schema,会随平台变化;若字段全部用私有 setter + 构造函数封装,
|
|
45
|
+
/// 每次生成都要重写一遍构造函数,模板维护成本高且 diff 噪音大。
|
|
46
|
+
/// 因此基类和实体统一使用 public setter,业务不变量(如 outId 不可变)放在应用服务里强制。
|
|
47
|
+
/// </summary>
|
|
48
|
+
public abstract class GeneratedEntityBase : FullAuditedAggregateRoot<Guid>
|
|
49
|
+
{
|
|
50
|
+
/// <summary>
|
|
51
|
+
/// InBiz 侧对象 id。出码器从平台数据里原样搬运,不做重新生成。
|
|
52
|
+
/// </summary>
|
|
53
|
+
public virtual string OutId { get; set; } = string.Empty;
|
|
54
|
+
|
|
55
|
+
/// <summary>
|
|
56
|
+
/// 数据版本号。空值在写入前会被归一为 <see cref="GeneratedConsts.DefaultDataVersion"/>,
|
|
57
|
+
/// 避免唯一索引在多 NULL 值上的语义分歧(不同数据库对 NULL 唯一性的处理并不一致)。
|
|
58
|
+
/// </summary>
|
|
59
|
+
public virtual string DataVersion { get; set; } = string.Empty;
|
|
60
|
+
|
|
61
|
+
protected GeneratedEntityBase()
|
|
62
|
+
{
|
|
63
|
+
// EF Core 需要无参构造。字段默认值在属性初始化器里给出。
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
protected GeneratedEntityBase(Guid id, string outId, string dataVersion = GeneratedConsts.DefaultDataVersion)
|
|
67
|
+
: base(id)
|
|
68
|
+
{
|
|
69
|
+
OutId = outId;
|
|
70
|
+
DataVersion = string.IsNullOrWhiteSpace(dataVersion) ? GeneratedConsts.DefaultDataVersion : dataVersion;
|
|
71
|
+
}
|
|
72
|
+
}
|