feat: 能力插件元数据与运行时消费闭环
This commit is contained in:
1 parent
076c6124c8
commit
bb0709b8cc
26 files changed
+2188
-267
No files matched your search
@@ -47,8 +47,6 @@ export async function loadWorkspace() {
|
||||
const data = await request<RuntimeWorkspace>("/public/workspace");
|
||||
const editorPreview = isEditorPreview();
|
||||
if (!editorPreview && data.publication?.status !== "published") throw new Error("品牌尚未发布");
|
||||
if (!editorPreview && data.brand.visualTemplateKey !== TEMPLATE_KEY)
|
||||
throw new Error("该品牌使用其他模板,请选择匹配的模板预览地址");
|
||||
return {
|
||||
...data,
|
||||
brand: data.brand ?? {},
|
||||
@@ -59,6 +57,7 @@ export async function loadWorkspace() {
|
||||
trustPresentation: data.trustPresentation ?? {},
|
||||
capabilities: data.capabilities ?? [],
|
||||
theme: data.theme ?? { colors: {} },
|
||||
plugins: data.plugins ?? [],
|
||||
};
|
||||
}
|
||||
export async function ensureSession() {
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
import type { RuntimeInquiryForm, RuntimeWorkspace } from "@personal-brand/types";
|
||||
|
||||
export type RuntimePluginStatus = "enabled" | "disabled" | "degraded";
|
||||
|
||||
type RuntimePlugin = {
|
||||
key: string;
|
||||
displayName?: string;
|
||||
status: RuntimePluginStatus;
|
||||
config: Record<string, unknown>;
|
||||
purpose?: string;
|
||||
purposeBindings?: readonly string[];
|
||||
configFields?: Array<{
|
||||
key: string;
|
||||
label: string;
|
||||
input: string;
|
||||
required: boolean;
|
||||
}>;
|
||||
};
|
||||
|
||||
function pluginList(workspace: RuntimeWorkspace): RuntimePlugin[] {
|
||||
const plugins = (workspace as { plugins?: unknown }).plugins;
|
||||
if (!Array.isArray(plugins)) return [];
|
||||
return plugins.filter((plugin): plugin is RuntimePlugin => {
|
||||
if (!plugin || typeof plugin !== "object" || Array.isArray(plugin)) return false;
|
||||
const candidate = plugin as Partial<RuntimePlugin>;
|
||||
return (
|
||||
typeof candidate.key === "string" &&
|
||||
(candidate.status === "enabled" ||
|
||||
candidate.status === "disabled" ||
|
||||
candidate.status === "degraded") &&
|
||||
Boolean(candidate.config) &&
|
||||
typeof candidate.config === "object" &&
|
||||
!Array.isArray(candidate.config)
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
export function pluginByKey(workspace: RuntimeWorkspace, key: string) {
|
||||
return pluginList(workspace).find((plugin) => plugin.key === key);
|
||||
}
|
||||
|
||||
export function pluginByPurpose(workspace: RuntimeWorkspace, purpose: string) {
|
||||
return pluginList(workspace).find(
|
||||
(plugin) => plugin.purpose === purpose || plugin.purposeBindings?.includes(purpose),
|
||||
);
|
||||
}
|
||||
|
||||
export function enabledPluginByKey(workspace: RuntimeWorkspace, key: string) {
|
||||
const plugin = pluginByKey(workspace, key);
|
||||
return plugin?.status === "enabled" ? plugin : undefined;
|
||||
}
|
||||
|
||||
export function enabledPluginByPurpose(workspace: RuntimeWorkspace, purpose: string) {
|
||||
const plugin = pluginByPurpose(workspace, purpose);
|
||||
return plugin?.status === "enabled" ? plugin : undefined;
|
||||
}
|
||||
|
||||
export function pluginConfigText(plugin: RuntimePlugin | undefined, key: string) {
|
||||
const value = plugin?.config[key];
|
||||
return typeof value === "string" ? value : "";
|
||||
}
|
||||
|
||||
export function pluginStateText(workspace: RuntimeWorkspace, key: string, configKey: string) {
|
||||
const plugin = pluginByKey(workspace, key);
|
||||
const value = plugin?.config[configKey];
|
||||
return typeof value === "string" ? value : "";
|
||||
}
|
||||
|
||||
export function inquiryFormAvailability(
|
||||
workspace: RuntimeWorkspace,
|
||||
form: RuntimeInquiryForm | undefined,
|
||||
) {
|
||||
const plugin = pluginByKey(
|
||||
workspace,
|
||||
form?.purpose === "quote-request" ? "quote-request" : "inquiry",
|
||||
);
|
||||
return {
|
||||
status: plugin?.status,
|
||||
disabledHint: pluginConfigText(plugin, "disabledHint"),
|
||||
};
|
||||
}
|
||||
|
||||
export function isPluginUsable(status: RuntimePluginStatus | undefined) {
|
||||
return status === "enabled";
|
||||
}
|
||||
|
||||
export function usableInquiryForms(workspace: RuntimeWorkspace) {
|
||||
return workspace.inquiryForms.filter((form) => {
|
||||
const { status } = inquiryFormAvailability(workspace, form);
|
||||
return isPluginUsable(status);
|
||||
});
|
||||
}
|
||||
|
||||
export function inquiryPluginState(workspace: RuntimeWorkspace) {
|
||||
const plugin = pluginByKey(workspace, "inquiry");
|
||||
return {
|
||||
plugin,
|
||||
enabled: plugin?.status === "enabled",
|
||||
disabledHint: pluginConfigText(plugin, "disabledHint") || "咨询功能暂不可用",
|
||||
};
|
||||
}
|
||||
@@ -8,13 +8,7 @@ const renderers = import.meta.glob<{
|
||||
}>("../../../../packages/template-*/taro/index.tsx", { eager: true });
|
||||
|
||||
export function TemplatePage(props: TemplateProps) {
|
||||
const config = props.workspace.brand.config;
|
||||
const visualTemplateKey =
|
||||
config && typeof config === "object" && !Array.isArray(config)
|
||||
? (config as Record<string, unknown>).visualTemplateKey
|
||||
: undefined;
|
||||
const templateKey = typeof visualTemplateKey === "string" ? visualTemplateKey : TEMPLATE_KEY;
|
||||
const suffix = `/template-${templateKey}/taro/index.tsx`;
|
||||
const suffix = `/template-${TEMPLATE_KEY}/taro/index.tsx`;
|
||||
const rendererModule = Object.entries(renderers).find(([path]) => path.endsWith(suffix))?.[1];
|
||||
if (!rendererModule)
|
||||
return <div className="runtime-state">当前模板没有可用的 H5 预览 renderer</div>;
|
||||
|
||||
Reference in new issue
Block a user