Files
geo/apps/admin-web/src/lib/publish-platforms.ts
T

73 lines
2.8 KiB
TypeScript
Raw Normal View History

export interface PublishPlatformOption {
id: string;
name: string;
shortName: string;
accent: string;
accountLabel?: string;
}
const platformCatalog: Record<string, Omit<PublishPlatformOption, "accountLabel">> = {
zhihu: { id: "zhihu", name: "知乎", shortName: "知", accent: "#1677ff" },
baijiahao: { id: "baijiahao", name: "百家号", shortName: "百", accent: "#31445a" },
toutiao: { id: "toutiao", name: "头条号", shortName: "头", accent: "#f5222d" },
souhu: { id: "souhu", name: "搜狐号", shortName: "搜", accent: "#fa8c16" },
qq: { id: "qq", name: "企鹅号", shortName: "Q", accent: "#111827" },
wechat: { id: "wechat", name: "公众号", shortName: "微", accent: "#13c26b" },
bilibili: { id: "bilibili", name: "bilibili", shortName: "B", accent: "#eb2f96" },
jianshu: { id: "jianshu", name: "简书", shortName: "简", accent: "#f56a5e" },
juejin: { id: "juejin", name: "稀土掘金", shortName: "掘", accent: "#1677ff" },
smzdm: { id: "smzdm", name: "什么值得买", shortName: "值", accent: "#f5222d" },
zol: { id: "zol", name: "中关村在线", shortName: "Z", accent: "#ff4d4f" },
dongchedi: { id: "dongchedi", name: "懂车帝", shortName: "懂", accent: "#fadb14" },
};
const mockBoundPlatformIds = ["zhihu", "wechat", "juejin", "bilibili", "jianshu", "baijiahao"];
export function getBoundPublishPlatforms(): PublishPlatformOption[] {
return mockBoundPlatformIds
.map((id) => platformCatalog[id])
.filter((item): item is Omit<PublishPlatformOption, "accountLabel"> => Boolean(item));
}
export function getAllPublishPlatforms(): PublishPlatformOption[] {
return Object.values(platformCatalog);
}
export function isPlatformBound(platformId: string): boolean {
return mockBoundPlatformIds.includes(platformId);
}
export function serializeTargetPlatforms(platformIds: string[]): string | undefined {
const next = Array.from(new Set(platformIds.map((item) => item.trim()).filter(Boolean)));
return next.length ? next.join(",") : undefined;
}
export function parseTargetPlatforms(value?: string | null): string[] {
if (!value) {
return [];
}
return Array.from(
new Set(
value
.split(",")
.map((item) => item.trim())
.filter(Boolean),
),
);
}
export function getPublishPlatformLabel(platformId: string): string {
return platformCatalog[platformId]?.name ?? platformId;
}
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 {
const labels = (platformIds ?? []).map(getPublishPlatformLabel).filter(Boolean);
return labels.length ? labels.join("、") : "--";
}