533 lines
20 KiB
TypeScript
533 lines
20 KiB
TypeScript
import { useState } from "react";
|
|
import { useNavigate } from "react-router-dom";
|
|
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import {
|
|
Alert,
|
|
Button,
|
|
Card,
|
|
Divider,
|
|
Empty,
|
|
Input,
|
|
Modal,
|
|
Select,
|
|
Space,
|
|
Spin,
|
|
Tag,
|
|
Typography,
|
|
message,
|
|
} from "antd";
|
|
import type { BrandTemplateKey } from "@personal-brand/api-client";
|
|
import { createApiClient } from "../../lib/api.js";
|
|
import { AdminPageHeader } from "../../components/AdminPageHeader.js";
|
|
import { MutationError, QueryState } from "../../components/QueryState.js";
|
|
|
|
const client = createApiClient();
|
|
|
|
const destinations: Record<string, string> = {
|
|
brand: "/admin/brand",
|
|
positioning: "/admin/brand",
|
|
template: "/admin/publication",
|
|
visual: "/admin/publication",
|
|
offerings: "/admin/services",
|
|
forms: "/admin/workspace",
|
|
trust: "/admin/trust",
|
|
};
|
|
|
|
export function PublicationPage({ canManageTemplate }: { canManageTemplate: boolean }) {
|
|
const navigate = useNavigate();
|
|
const queryClient = useQueryClient();
|
|
const [industryTarget, setIndustryTarget] = useState<BrandTemplateKey | null>(null);
|
|
const [confirmAction, setConfirmAction] = useState<"publish" | "revoke" | null>(null);
|
|
const [rollbackVersion, setRollbackVersion] = useState<number | null>(null);
|
|
const [note, setNote] = useState("");
|
|
const brand = useQuery({
|
|
queryKey: ["publication-brand"],
|
|
queryFn: () => client.getAdminBrand(),
|
|
});
|
|
const workspace = useQuery({
|
|
queryKey: ["publication-workspace"],
|
|
queryFn: () => client.getAdminWorkspace(),
|
|
});
|
|
const catalog = useQuery({
|
|
queryKey: ["publication-catalog"],
|
|
queryFn: () => client.listPublicTemplates(),
|
|
});
|
|
const sample = useQuery({
|
|
queryKey: ["publication-sample"],
|
|
queryFn: () => client.previewAdminSampleContent(),
|
|
enabled: Boolean(workspace.data),
|
|
});
|
|
const readiness = useQuery({
|
|
queryKey: ["publication-readiness"],
|
|
queryFn: () => client.getAdminReleaseReadiness(),
|
|
});
|
|
const releases = useQuery({
|
|
queryKey: ["publication-releases"],
|
|
queryFn: () => client.listAdminReleases(),
|
|
});
|
|
const miniAppReleases = useQuery({
|
|
queryKey: ["publication-miniapp-releases"],
|
|
queryFn: () => client.listAdminMiniAppReleases(),
|
|
});
|
|
const refresh = () => {
|
|
for (const key of [
|
|
"publication-brand",
|
|
"publication-workspace",
|
|
"publication-sample",
|
|
"publication-readiness",
|
|
"publication-releases",
|
|
"publication-miniapp-releases",
|
|
"admin-shell-brand",
|
|
"admin-workspace",
|
|
"public-runtime-workspace",
|
|
]) {
|
|
void queryClient.invalidateQueries({ queryKey: [key] });
|
|
}
|
|
};
|
|
const install = useMutation({
|
|
mutationFn: (key: BrandTemplateKey) => client.installAdminTemplate(key),
|
|
onSuccess: () => {
|
|
setIndustryTarget(null);
|
|
message.success("行业模板已安装");
|
|
refresh();
|
|
},
|
|
});
|
|
const selectVisual = useMutation({
|
|
mutationFn: (key: string) =>
|
|
client.updateAdminBrand({
|
|
config: { ...(brand.data?.config ?? {}), visualTemplateKey: key },
|
|
}),
|
|
onSuccess: () => {
|
|
message.success("视觉模板已保存到草稿");
|
|
refresh();
|
|
},
|
|
});
|
|
const importSample = useMutation({
|
|
mutationFn: () => client.importAdminSampleContent(),
|
|
onSuccess: ({ counts }) => {
|
|
message.success(
|
|
counts.inquiryForms
|
|
? "示例内容和客户咨询表单已导入草稿"
|
|
: "示例内容已导入草稿,现有客户咨询表单已保留",
|
|
);
|
|
refresh();
|
|
},
|
|
});
|
|
const publish = useMutation({
|
|
mutationFn: () => client.publishAdminRelease(note.trim() || undefined),
|
|
onSuccess: () => {
|
|
setConfirmAction(null);
|
|
setNote("");
|
|
message.success("品牌已发布");
|
|
refresh();
|
|
},
|
|
});
|
|
const revoke = useMutation({
|
|
mutationFn: () => client.revokeAdminRelease(),
|
|
onSuccess: () => {
|
|
setConfirmAction(null);
|
|
message.success("品牌已撤回,小程序将显示未发布状态");
|
|
refresh();
|
|
},
|
|
});
|
|
const rollback = useMutation({
|
|
mutationFn: (version: number) => client.rollbackAdminRelease(version),
|
|
onSuccess: () => {
|
|
setRollbackVersion(null);
|
|
message.success("历史版本已重新发布");
|
|
refresh();
|
|
},
|
|
});
|
|
const createMiniAppRelease = useMutation({
|
|
mutationFn: () => client.createAdminMiniAppRelease(),
|
|
onSuccess: () => {
|
|
message.success("小程序发布任务已创建");
|
|
void miniAppReleases.refetch();
|
|
},
|
|
});
|
|
const uploadMiniAppRelease = useMutation({
|
|
mutationFn: (id: string) => client.uploadAdminMiniAppRelease(id),
|
|
onSuccess: () => {
|
|
message.success("小程序代码已上传");
|
|
void miniAppReleases.refetch();
|
|
},
|
|
});
|
|
const updateMiniAppStatus = useMutation({
|
|
mutationFn: ({
|
|
id,
|
|
status,
|
|
}: {
|
|
id: string;
|
|
status: Parameters<typeof client.updateAdminMiniAppReleaseStatus>[1]["status"];
|
|
}) => client.updateAdminMiniAppReleaseStatus(id, { status }),
|
|
onSuccess: () => {
|
|
message.success("小程序发布状态已更新");
|
|
void miniAppReleases.refetch();
|
|
},
|
|
});
|
|
const loading = [brand, workspace, catalog, readiness, releases, miniAppReleases].some(
|
|
(query) => query.isLoading,
|
|
);
|
|
const error = [brand, workspace, catalog, readiness, releases, miniAppReleases].find(
|
|
(query) => query.isError,
|
|
)?.error;
|
|
const retry = () => {
|
|
void brand.refetch();
|
|
void workspace.refetch();
|
|
void catalog.refetch();
|
|
void readiness.refetch();
|
|
void releases.refetch();
|
|
void miniAppReleases.refetch();
|
|
};
|
|
const industryKey =
|
|
workspace.data?.installations[0]?.templateKey ??
|
|
workspace.data?.brand?.templateKey ??
|
|
brand.data?.templateKey ??
|
|
catalog.data?.industries[0]?.key ??
|
|
null;
|
|
const visuals = (catalog.data?.visuals ?? []).filter(
|
|
(item) =>
|
|
typeof industryKey === "string" && item.supportedIndustryTemplates.includes(industryKey),
|
|
);
|
|
const visualKey =
|
|
typeof brand.data?.config?.visualTemplateKey === "string"
|
|
? brand.data.config.visualTemplateKey
|
|
: (visuals[0]?.key ?? null);
|
|
const currentRelease = releases.data?.find((item) => item.status === "published");
|
|
const publishedHistory = (releases.data ?? []).filter(
|
|
(item) => item.status === "published" || item.status === "superseded",
|
|
);
|
|
const draftOfferings = workspace.data?.offerings.filter((item) => item.isEnabled) ?? [];
|
|
const draftForms = (workspace.data?.forms ?? []) as Array<{
|
|
id: string;
|
|
name: string;
|
|
isEnabled: boolean;
|
|
}>;
|
|
|
|
return (
|
|
<QueryState loading={loading} error={error} onRetry={retry}>
|
|
<AdminPageHeader
|
|
title="配置与发布"
|
|
description="编辑内容不会立即影响小程序;确认后统一发布当前品牌。"
|
|
/>
|
|
<Space direction="vertical" size={24} style={{ width: "100%" }}>
|
|
<section>
|
|
<Typography.Title level={4}>1. 选择表达方式</Typography.Title>
|
|
<Space wrap>
|
|
<Select
|
|
aria-label="行业模板"
|
|
style={{ width: 220 }}
|
|
value={industryKey}
|
|
disabled={!canManageTemplate || Boolean(workspace.data?.installations.length)}
|
|
options={(catalog.data?.industries ?? []).map((item) => ({
|
|
value: item.key,
|
|
label: item.name,
|
|
}))}
|
|
onChange={(key: BrandTemplateKey) => setIndustryTarget(key)}
|
|
/>
|
|
<Select
|
|
aria-label="视觉模板"
|
|
style={{ width: 220 }}
|
|
value={visualKey}
|
|
disabled={!canManageTemplate}
|
|
loading={selectVisual.isPending}
|
|
options={visuals.map((item) => ({ value: item.key, label: item.name }))}
|
|
onChange={(key) => selectVisual.mutate(key)}
|
|
/>
|
|
{canManageTemplate && (
|
|
<Button onClick={() => navigate("/admin/workspace")}>高级模板设置</Button>
|
|
)}
|
|
</Space>
|
|
{workspace.data?.installations.length ? (
|
|
<Typography.Paragraph type="secondary" style={{ marginTop: 8 }}>
|
|
当前品牌已安装主模板。模板版本与升级在高级设置中管理。
|
|
</Typography.Paragraph>
|
|
) : null}
|
|
<MutationError error={install.error || selectVisual.error} />
|
|
<Divider />
|
|
<Space wrap>
|
|
<Typography.Text>示例内容</Typography.Text>
|
|
{sample.isLoading && <Spin size="small" />}
|
|
{sample.data && (
|
|
<Tag color={sample.data.alreadyImported ? "green" : "blue"}>
|
|
{sample.data.alreadyImported ? "已导入" : "可导入"}
|
|
</Tag>
|
|
)}
|
|
<Button
|
|
disabled={!canManageTemplate || !sample.data || sample.data.alreadyImported}
|
|
loading={importSample.isPending}
|
|
onClick={() => importSample.mutate()}
|
|
>
|
|
导入示例内容
|
|
</Button>
|
|
{sample.isError && <Button onClick={() => void sample.refetch()}>重试</Button>}
|
|
</Space>
|
|
{sample.isError && (
|
|
<Alert
|
|
type="error"
|
|
message={sample.error instanceof Error ? sample.error.message : "示例内容加载失败"}
|
|
showIcon
|
|
style={{ marginTop: 8 }}
|
|
/>
|
|
)}
|
|
{sample.data && !sample.data.alreadyImported && (
|
|
<Typography.Paragraph type="secondary" style={{ marginTop: 8 }}>
|
|
将向草稿添加{" "}
|
|
{Object.entries(sample.data.counts)
|
|
.map(([key, count]) => `${key} ${count} 项`)
|
|
.join("、")}
|
|
;发布前请替换示例文案和图片。
|
|
</Typography.Paragraph>
|
|
)}
|
|
<MutationError error={importSample.error} />
|
|
</section>
|
|
|
|
<section>
|
|
<Typography.Title level={4}>2. 核对草稿内容</Typography.Title>
|
|
<Typography.Paragraph type="secondary">
|
|
此处核对的是待发布内容,不是小程序视觉预览。
|
|
</Typography.Paragraph>
|
|
<Space direction="vertical" style={{ width: "100%" }}>
|
|
<Typography.Text strong>{brand.data?.name || "未命名品牌"}</Typography.Text>
|
|
<Typography.Text>{brand.data?.tagline || "尚未填写品牌定位"}</Typography.Text>
|
|
<Typography.Text>
|
|
已启用业务项:{draftOfferings.map((item) => item.name).join("、") || "无"}
|
|
</Typography.Text>
|
|
<Typography.Text>
|
|
已启用咨询表单:
|
|
{draftForms
|
|
.filter((item) => item.isEnabled)
|
|
.map((item) => item.name)
|
|
.join("、") || "无"}
|
|
</Typography.Text>
|
|
<Space wrap>
|
|
<Button onClick={() => navigate("/admin/brand")}>编辑小程序页面</Button>
|
|
<Button onClick={() => navigate("/admin/services")}>编辑业务展示和详情</Button>
|
|
<Button onClick={() => navigate("/admin/trust")}>编辑信任证明</Button>
|
|
</Space>
|
|
</Space>
|
|
</section>
|
|
|
|
<section>
|
|
<Typography.Title level={4}>3. 检查并发布</Typography.Title>
|
|
<Space wrap style={{ marginBottom: 12 }}>
|
|
<Tag color={currentRelease ? "green" : "default"}>
|
|
{currentRelease ? `已发布 v${currentRelease.version}` : "尚未发布"}
|
|
</Tag>
|
|
<Button onClick={() => void readiness.refetch()}>重新检查</Button>
|
|
</Space>
|
|
<Space direction="vertical" style={{ width: "100%" }}>
|
|
{(readiness.data?.checks ?? []).map((check) => (
|
|
<Space key={check.key}>
|
|
<Tag color={check.passed ? "success" : "warning"}>
|
|
{check.passed ? "已完成" : "待完善"}
|
|
</Tag>
|
|
<Typography.Text>{check.label}</Typography.Text>
|
|
{check.help ? (
|
|
<Typography.Text type="secondary">{check.help}</Typography.Text>
|
|
) : null}
|
|
{!check.passed && (
|
|
<Button
|
|
type="link"
|
|
onClick={() =>
|
|
navigate(check.destination ?? destinations[check.key] ?? "/admin/overview")
|
|
}
|
|
>
|
|
去处理
|
|
</Button>
|
|
)}
|
|
</Space>
|
|
))}
|
|
</Space>
|
|
<Space wrap style={{ marginTop: 16 }}>
|
|
<Button
|
|
type="primary"
|
|
disabled={!readiness.data?.ready}
|
|
onClick={() => setConfirmAction("publish")}
|
|
>
|
|
发布当前草稿
|
|
</Button>
|
|
{currentRelease && (
|
|
<Button danger onClick={() => setConfirmAction("revoke")}>
|
|
撤回当前版本
|
|
</Button>
|
|
)}
|
|
</Space>
|
|
<MutationError error={publish.error || revoke.error} />
|
|
<Divider />
|
|
<Typography.Title level={5}>发布记录</Typography.Title>
|
|
{releases.data?.length ? (
|
|
<Space direction="vertical" style={{ width: "100%" }}>
|
|
{releases.data.map((item) => (
|
|
<Space key={item.id} wrap>
|
|
<Typography.Text>v{item.version}</Typography.Text>
|
|
<Tag color={item.status === "published" ? "green" : "default"}>{item.status}</Tag>
|
|
<Typography.Text type="secondary">
|
|
{new Date(item.createdAt).toLocaleString("zh-CN")}
|
|
</Typography.Text>
|
|
{item.note && <Typography.Text>{item.note}</Typography.Text>}
|
|
{item.status === "superseded" && publishedHistory.length > 1 && (
|
|
<Button size="small" onClick={() => setRollbackVersion(item.version)}>
|
|
恢复此版本
|
|
</Button>
|
|
)}
|
|
</Space>
|
|
))}
|
|
</Space>
|
|
) : (
|
|
<Empty description="暂无发布记录" />
|
|
)}
|
|
<MutationError error={rollback.error} />
|
|
</section>
|
|
<section>
|
|
<Typography.Title level={4}>小程序代码发布</Typography.Title>
|
|
<Typography.Paragraph type="secondary">
|
|
使用微信官方 miniprogram-ci 上传代码;审核与发布状态需按微信平台结果人工确认。
|
|
</Typography.Paragraph>
|
|
<Button
|
|
disabled={!currentRelease}
|
|
loading={createMiniAppRelease.isPending}
|
|
onClick={() => createMiniAppRelease.mutate()}
|
|
>
|
|
基于当前品牌版本创建任务
|
|
</Button>
|
|
<Divider />
|
|
{miniAppReleases.data?.length ? (
|
|
<Space direction="vertical" style={{ width: "100%" }}>
|
|
{miniAppReleases.data.map((task) => (
|
|
<Card key={task.id} size="small">
|
|
<Space wrap>
|
|
<Typography.Text>品牌 v{task.version}</Typography.Text>
|
|
<Tag
|
|
color={
|
|
task.status === "published"
|
|
? "green"
|
|
: task.status === "failed"
|
|
? "red"
|
|
: "blue"
|
|
}
|
|
>
|
|
{task.status}
|
|
</Tag>
|
|
{task.weappUploadId && (
|
|
<Typography.Text type="secondary">
|
|
微信版本 {task.weappUploadId}
|
|
</Typography.Text>
|
|
)}
|
|
{task.error && <Typography.Text type="danger">{task.error}</Typography.Text>}
|
|
{task.status === "built" && (
|
|
<Button
|
|
size="small"
|
|
loading={uploadMiniAppRelease.isPending}
|
|
onClick={() => uploadMiniAppRelease.mutate(task.id)}
|
|
>
|
|
上传微信
|
|
</Button>
|
|
)}
|
|
{task.status === "uploaded" && (
|
|
<Button
|
|
size="small"
|
|
onClick={() =>
|
|
updateMiniAppStatus.mutate({ id: task.id, status: "under_review" })
|
|
}
|
|
>
|
|
标记提交审核
|
|
</Button>
|
|
)}
|
|
{task.status === "under_review" && (
|
|
<>
|
|
<Button
|
|
size="small"
|
|
onClick={() =>
|
|
updateMiniAppStatus.mutate({ id: task.id, status: "approved" })
|
|
}
|
|
>
|
|
审核通过
|
|
</Button>
|
|
<Button
|
|
size="small"
|
|
danger
|
|
onClick={() =>
|
|
updateMiniAppStatus.mutate({ id: task.id, status: "rejected" })
|
|
}
|
|
>
|
|
审核驳回
|
|
</Button>
|
|
</>
|
|
)}
|
|
{task.status === "approved" && (
|
|
<Button
|
|
size="small"
|
|
type="primary"
|
|
onClick={() =>
|
|
updateMiniAppStatus.mutate({ id: task.id, status: "published" })
|
|
}
|
|
>
|
|
标记已发布
|
|
</Button>
|
|
)}
|
|
{task.status === "failed" && (
|
|
<Button
|
|
size="small"
|
|
onClick={() => updateMiniAppStatus.mutate({ id: task.id, status: "built" })}
|
|
>
|
|
重置可上传
|
|
</Button>
|
|
)}
|
|
</Space>
|
|
</Card>
|
|
))}
|
|
</Space>
|
|
) : (
|
|
<Empty description="暂无小程序发布任务" />
|
|
)}
|
|
</section>
|
|
</Space>
|
|
<Modal
|
|
title={confirmAction === "revoke" ? "确认撤回品牌" : "确认发布品牌"}
|
|
open={confirmAction !== null}
|
|
onCancel={() => setConfirmAction(null)}
|
|
onOk={() => (confirmAction === "revoke" ? revoke.mutate() : publish.mutate())}
|
|
confirmLoading={publish.isPending || revoke.isPending}
|
|
okText={confirmAction === "revoke" ? "确认撤回" : "确认发布"}
|
|
okButtonProps={{ danger: confirmAction === "revoke" }}
|
|
>
|
|
{confirmAction === "revoke" ? (
|
|
<Alert
|
|
type="warning"
|
|
message="撤回后,小程序将显示品牌尚未发布,新的咨询和预约会停止受理。"
|
|
/>
|
|
) : (
|
|
<Space direction="vertical" style={{ width: "100%" }}>
|
|
<Typography.Text>当前草稿将成为品牌新的公开版本。</Typography.Text>
|
|
<Input
|
|
value={note}
|
|
onChange={(event) => setNote(event.target.value)}
|
|
placeholder="发布备注(可选)"
|
|
maxLength={200}
|
|
/>
|
|
</Space>
|
|
)}
|
|
</Modal>
|
|
<Modal
|
|
title="确认恢复历史版本"
|
|
open={rollbackVersion !== null}
|
|
onCancel={() => setRollbackVersion(null)}
|
|
onOk={() => rollbackVersion !== null && rollback.mutate(rollbackVersion)}
|
|
confirmLoading={rollback.isPending}
|
|
>
|
|
将 v{rollbackVersion} 重新发布为新版本;当前草稿不会被覆盖。
|
|
</Modal>
|
|
<Modal
|
|
title="确认安装行业模板"
|
|
open={industryTarget !== null}
|
|
onCancel={() => setIndustryTarget(null)}
|
|
onOk={() => industryTarget && install.mutate(industryTarget)}
|
|
confirmLoading={install.isPending}
|
|
>
|
|
模板会更新当前品牌的草稿结构,不会立即改变已发布的小程序。请安装后核对业务内容与视觉模板。
|
|
</Modal>
|
|
</QueryState>
|
|
);
|
|
}
|