2026-04-20 09:52:48 +08:00
|
|
|
import type { DesktopAccountInfo, MediaPlatform, PlatformAccount } from "@geo/shared-types";
|
2026-04-06 15:41:49 +08:00
|
|
|
|
2026-04-02 00:31:28 +08:00
|
|
|
export interface PublishPlatformOption {
|
|
|
|
|
id: string;
|
|
|
|
|
name: string;
|
|
|
|
|
shortName: string;
|
|
|
|
|
accent: string;
|
|
|
|
|
accountLabel?: string;
|
2026-04-06 15:41:49 +08:00
|
|
|
bound?: boolean;
|
2026-04-02 00:31:28 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-06 15:41:49 +08:00
|
|
|
const platformCatalog: Record<string, Omit<PublishPlatformOption, "accountLabel" | "bound">> = {
|
|
|
|
|
toutiaohao: { id: "toutiaohao", name: "头条号", shortName: "头", accent: "#f5222d" },
|
2026-04-02 00:31:28 +08:00
|
|
|
baijiahao: { id: "baijiahao", name: "百家号", shortName: "百", accent: "#31445a" },
|
2026-04-06 15:41:49 +08:00
|
|
|
sohuhao: { id: "sohuhao", name: "搜狐号", shortName: "搜", accent: "#fa8c16" },
|
|
|
|
|
qiehao: { id: "qiehao", name: "企鹅号", shortName: "Q", accent: "#111827" },
|
|
|
|
|
zhihu: { id: "zhihu", name: "知乎", shortName: "知", accent: "#1677ff" },
|
|
|
|
|
wangyihao: { id: "wangyihao", name: "网易号", shortName: "网", accent: "#ff4d4f" },
|
2026-04-02 00:31:28 +08:00
|
|
|
jianshu: { id: "jianshu", name: "简书", shortName: "简", accent: "#f56a5e" },
|
2026-04-06 15:41:49 +08:00
|
|
|
bilibili: { id: "bilibili", name: "bilibili", shortName: "B", accent: "#eb2f96" },
|
2026-04-02 00:31:28 +08:00
|
|
|
juejin: { id: "juejin", name: "稀土掘金", shortName: "掘", accent: "#1677ff" },
|
|
|
|
|
smzdm: { id: "smzdm", name: "什么值得买", shortName: "值", accent: "#f5222d" },
|
2026-04-06 15:41:49 +08:00
|
|
|
weixin_gzh: { id: "weixin_gzh", name: "微信公众号", shortName: "微", accent: "#13c26b" },
|
2026-04-02 00:31:28 +08:00
|
|
|
zol: { id: "zol", name: "中关村在线", shortName: "Z", accent: "#ff4d4f" },
|
|
|
|
|
dongchedi: { id: "dongchedi", name: "懂车帝", shortName: "懂", accent: "#fadb14" },
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-07 16:07:21 +08:00
|
|
|
export function getPublishPlatformMeta(platformId: string): Omit<PublishPlatformOption, "accountLabel" | "bound"> {
|
|
|
|
|
const normalized = normalizePublishPlatformId(platformId);
|
|
|
|
|
const fallbackShortName = normalized.slice(0, 1).toUpperCase() || "?";
|
|
|
|
|
|
|
|
|
|
return platformCatalog[normalized] ?? {
|
|
|
|
|
id: normalized,
|
|
|
|
|
name: normalized || "--",
|
|
|
|
|
shortName: fallbackShortName,
|
|
|
|
|
accent: "#667085",
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-06 15:41:49 +08:00
|
|
|
export function normalizePublishPlatformId(platformId?: string | null): string {
|
|
|
|
|
return String(platformId ?? "").trim();
|
2026-04-02 00:31:28 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-06 15:41:49 +08:00
|
|
|
export function normalizePublishPlatformIds(platformIds?: Array<string | null | undefined>): string[] {
|
|
|
|
|
return Array.from(
|
|
|
|
|
new Set(
|
|
|
|
|
(platformIds ?? [])
|
|
|
|
|
.map((item) => normalizePublishPlatformId(item))
|
|
|
|
|
.filter(Boolean),
|
|
|
|
|
),
|
|
|
|
|
);
|
2026-04-02 00:31:28 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-06 15:41:49 +08:00
|
|
|
export function buildPublishPlatformOptions(
|
|
|
|
|
platforms: MediaPlatform[],
|
2026-04-20 09:52:48 +08:00
|
|
|
accounts?: Array<PlatformAccount | DesktopAccountInfo> | null,
|
2026-04-06 15:41:49 +08:00
|
|
|
): PublishPlatformOption[] {
|
|
|
|
|
const boundPlatformIds = new Set(
|
2026-04-20 09:52:48 +08:00
|
|
|
(accounts ?? [])
|
|
|
|
|
.map((account) =>
|
|
|
|
|
normalizePublishPlatformId("platform_id" in account ? account.platform_id : account.platform),
|
|
|
|
|
)
|
|
|
|
|
.filter(Boolean),
|
2026-04-06 15:41:49 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return platforms.map((platform) => {
|
|
|
|
|
const id = normalizePublishPlatformId(platform.platform_id);
|
|
|
|
|
return {
|
|
|
|
|
id,
|
|
|
|
|
name: platform.name,
|
|
|
|
|
shortName: platform.short_name,
|
|
|
|
|
accent: platform.accent_color,
|
|
|
|
|
bound: boundPlatformIds.has(id),
|
|
|
|
|
};
|
|
|
|
|
});
|
2026-04-02 00:31:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function serializeTargetPlatforms(platformIds: string[]): string | undefined {
|
2026-04-06 15:41:49 +08:00
|
|
|
const next = normalizePublishPlatformIds(platformIds);
|
2026-04-02 00:31:28 +08:00
|
|
|
return next.length ? next.join(",") : undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function parseTargetPlatforms(value?: string | null): string[] {
|
|
|
|
|
if (!value) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-06 15:41:49 +08:00
|
|
|
return normalizePublishPlatformIds(value.split(","));
|
2026-04-02 00:31:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getPublishPlatformLabel(platformId: string): string {
|
2026-04-07 16:07:21 +08:00
|
|
|
return getPublishPlatformMeta(platformId).name;
|
2026-04-02 00:31:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function formatPublishPlatformSummary(value?: string | null): string {
|
|
|
|
|
const labels = parseTargetPlatforms(value).map(getPublishPlatformLabel);
|
|
|
|
|
return labels.length ? labels.join("、") : "--";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function formatPublishPlatformList(platformIds?: string[] | null): string {
|
2026-04-06 15:41:49 +08:00
|
|
|
const labels = normalizePublishPlatformIds(platformIds ?? []).map(getPublishPlatformLabel);
|
2026-04-02 00:31:28 +08:00
|
|
|
return labels.length ? labels.join("、") : "--";
|
|
|
|
|
}
|