content.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // 多平台内容 URL / 嵌入播放器 / 平台名:按 item.platform 分派,替代 V2 抖音写死。
  2. type Item = Record<string, unknown>;
  3. function str(v: unknown): string | null {
  4. if (typeof v === "string" && v.trim()) return v;
  5. return null;
  6. }
  7. // 平台名静态兜底(与 platform_profiles 的 platform_label 对齐)。
  8. // 动态可由 /config/platforms 覆盖;此表保证 catalog 未加载时各处(列表卡、顶栏 chip)仍显示中文。
  9. const PLATFORM_LABELS: Record<string, string> = {
  10. douyin: "抖音",
  11. shipinhao: "视频号",
  12. kuaishou: "快手",
  13. xiaohongshu: "小红书",
  14. bilibili: "B站",
  15. weixin: "公众号",
  16. toutiao: "头条",
  17. zhihu: "知乎",
  18. youtube: "YouTube",
  19. github: "GitHub"
  20. };
  21. export function platformLabel(platform: unknown, catalog?: Record<string, { label: string }>): string {
  22. const key = String(platform || "").toLowerCase();
  23. return catalog?.[key]?.label || PLATFORM_LABELS[key] || (key ? key : "未知平台");
  24. }
  25. // 统一互动指标键 → 中文标签(后端各平台 normalizer 已把原生字段映射到这 5 个键)。
  26. export const HEAT_FIELD_LABELS: Record<string, string> = {
  27. digg_count: "点赞",
  28. comment_count: "评论",
  29. share_count: "分享",
  30. collect_count: "收藏",
  31. play_count: "播放"
  32. };
  33. // 原帖链接:优先用记录里真实落库的 url;无则仅抖音可由 content_id 拼回,其余平台返回 null(不伪造)。
  34. export function contentUrl(item: Item): string | null {
  35. const media = (item.media_record as Item) || {};
  36. const direct =
  37. str(item.platform_content_url) ||
  38. str(item.share_url) ||
  39. str(item.url) ||
  40. str(item.aweme_url) ||
  41. str(media.share_url) ||
  42. str(item.play_url) || // 视频号无网页原帖,仅有视频流地址,兜底用它
  43. str(media.play_url);
  44. if (direct) return direct;
  45. const platform = String(item.platform || "").toLowerCase();
  46. const id = str(item.platform_content_id);
  47. if (platform === "douyin" && id) {
  48. return `https://www.douyin.com/video/${encodeURIComponent(id)}`;
  49. }
  50. return null;
  51. }
  52. // 可跨站 iframe 的官方嵌入播放器:仅抖音有(open.douyin.com 无 X-Frame-Options);
  53. // 视频号等无公开 embed → null,UI 降级为"打开原帖"。
  54. export function embedPlayerUrl(item: Item): string | null {
  55. const platform = String(item.platform || "").toLowerCase();
  56. const id = str(item.platform_content_id);
  57. if (platform === "douyin" && id) {
  58. return `https://open.douyin.com/player/video?vid=${encodeURIComponent(id)}&autoplay=1`;
  59. }
  60. return null;
  61. }