feat: 能力插件元数据与运行时消费闭环
This commit is contained in:
1 parent
076c6124c8
commit
bb0709b8cc
26 files changed
+2188
-267
No files matched your search
@@ -0,0 +1,43 @@
|
||||
import { Button, Text, View } from "@tarojs/components";
|
||||
import type { ReactNode } from "react";
|
||||
import type { RuntimeWorkspace } from "@personal-brand/types";
|
||||
import { pluginByKey, pluginConfigText } from "../runtime/plugins";
|
||||
import { navigate } from "../runtime/navigation";
|
||||
|
||||
export function CapabilityAction({
|
||||
workspace,
|
||||
pluginKey,
|
||||
params,
|
||||
presentationLabel,
|
||||
fallbackLabel,
|
||||
className,
|
||||
editorTarget,
|
||||
children,
|
||||
}: {
|
||||
workspace: RuntimeWorkspace;
|
||||
pluginKey: string;
|
||||
params?: Record<string, string>;
|
||||
presentationLabel?: string;
|
||||
fallbackLabel: string;
|
||||
className?: string;
|
||||
editorTarget?: string;
|
||||
children?: ReactNode;
|
||||
}) {
|
||||
const plugin = pluginByKey(workspace, pluginKey);
|
||||
const label = pluginConfigText(plugin, "entryLabel") || presentationLabel || fallbackLabel;
|
||||
if (plugin?.status !== "enabled")
|
||||
return (
|
||||
<View className={className} data-editor-target={editorTarget}>
|
||||
<Text>{pluginConfigText(plugin, "disabledHint") || `${fallbackLabel}暂不可用`}</Text>
|
||||
</View>
|
||||
);
|
||||
return (
|
||||
<Button
|
||||
className={className}
|
||||
data-editor-target={editorTarget}
|
||||
onClick={() => navigate("inquiry", params)}
|
||||
>
|
||||
{children ?? label}
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
@@ -16,6 +16,7 @@ import { missingField, visibleFields, presentation } from "../runtime/content";
|
||||
import type { Values } from "../runtime/content";
|
||||
import { ensureSession, request, brandSlug } from "../runtime/api";
|
||||
import { takeQuery, navigate } from "../runtime/navigation";
|
||||
import { inquiryPluginState, pluginConfigText, usableInquiryForms } from "../runtime/plugins";
|
||||
import "./forms.css";
|
||||
export function Inquiry({
|
||||
workspace,
|
||||
@@ -26,14 +27,16 @@ export function Inquiry({
|
||||
}) {
|
||||
const router = useRouter();
|
||||
const [offeringId, setOfferingId] = useState(router.params.offeringId || "");
|
||||
const forms = usableInquiryForms(workspace);
|
||||
const form: RuntimeInquiryForm | undefined =
|
||||
workspace.inquiryForms.find((form) => offeringId && form.targetId === offeringId) ||
|
||||
workspace.inquiryForms.find((form) => form.target === "brand");
|
||||
forms.find((form) => offeringId && form.targetId === offeringId) ||
|
||||
forms.find((form) => form.target === "brand");
|
||||
const [values, setValues] = useState<Values>({});
|
||||
const [error, setError] = useState("");
|
||||
const [sending, setSending] = useState(false);
|
||||
const busy = useRef(false);
|
||||
const p = presentation(workspace, "inquiry");
|
||||
const inquiryPlugin = inquiryPluginState(workspace);
|
||||
const compactFieldKeys = new Set(["people", "date", "budget", "delivery"]);
|
||||
const compactForm = Boolean(form?.fields.some((field) => compactFieldKeys.has(field.key)));
|
||||
useDidShow(() => {
|
||||
@@ -89,7 +92,12 @@ export function Inquiry({
|
||||
<Text className="form-title">{p.title || "提交咨询"}</Text>
|
||||
<Text className="form-subtitle">{p.subtitle}</Text>
|
||||
</View>
|
||||
{!form ? (
|
||||
{!inquiryPlugin.enabled && form?.purpose !== "quote-request" ? (
|
||||
<View className="records-empty">
|
||||
<Text>{inquiryPlugin.disabledHint}</Text>
|
||||
<Button onClick={() => navigate("records")}>查看咨询记录</Button>
|
||||
</View>
|
||||
) : !form ? (
|
||||
<View className="form-error">尚未配置可用的咨询表单</View>
|
||||
) : (
|
||||
<View className="inquiry-form">
|
||||
@@ -197,6 +205,11 @@ export function Inquiry({
|
||||
{error}
|
||||
</View>
|
||||
)}
|
||||
{pluginConfigText(inquiryPlugin.plugin, "submitHint") && (
|
||||
<Text className="form-subtitle">
|
||||
{pluginConfigText(inquiryPlugin.plugin, "submitHint")}
|
||||
</Text>
|
||||
)}
|
||||
<Button
|
||||
className="submit-button"
|
||||
disabled={sending}
|
||||
|
||||
@@ -5,6 +5,7 @@ import type { RuntimeWorkspace } from "@personal-brand/types";
|
||||
import { ensureSession, hasSession, request } from "../runtime/api";
|
||||
import { navigate } from "../runtime/navigation";
|
||||
import { presentation } from "../runtime/content";
|
||||
import { inquiryPluginState } from "../runtime/plugins";
|
||||
import "./forms.css";
|
||||
type RecordItem = {
|
||||
id: string;
|
||||
@@ -37,6 +38,7 @@ export function Records({
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [authenticated, setAuthenticated] = useState(preview ? false : hasSession());
|
||||
const p = presentation(workspace, "records");
|
||||
const inquiryPlugin = inquiryPluginState(workspace);
|
||||
const load = useCallback(() => {
|
||||
if (preview) return;
|
||||
if (!hasSession()) {
|
||||
@@ -58,7 +60,14 @@ export function Records({
|
||||
return (
|
||||
<View className="records-page">
|
||||
<Text className="form-title">{p.title || "我的咨询记录"}</Text>
|
||||
<Button onClick={() => navigate("inquiry")}>{p.createAction || "新增咨询"}</Button>
|
||||
{inquiryPlugin.enabled ? (
|
||||
<Button onClick={() => navigate("inquiry")}>{p.createAction || "新增咨询"}</Button>
|
||||
) : (
|
||||
<View className="records-empty">
|
||||
<Text>{inquiryPlugin.disabledHint}</Text>
|
||||
<Text className="form-subtitle">暂无可发起的咨询入口</Text>
|
||||
</View>
|
||||
)}
|
||||
{loading ? (
|
||||
<View className="runtime-state">正在加载…</View>
|
||||
) : error ? (
|
||||
@@ -94,7 +103,9 @@ export function Records({
|
||||
<View className="records-empty">
|
||||
<Text>{p.emptyTitle || "还没有咨询记录"}</Text>
|
||||
<Text className="form-subtitle">{p.emptyDescription}</Text>
|
||||
<Button onClick={() => navigate("inquiry")}>{p.emptyAction || "开始咨询"}</Button>
|
||||
{inquiryPlugin.enabled && (
|
||||
<Button onClick={() => navigate("inquiry")}>{p.emptyAction || "开始咨询"}</Button>
|
||||
)}
|
||||
</View>
|
||||
) : (
|
||||
records.map((item) => (
|
||||
|
||||
@@ -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