hongcheng-components.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // ==============================================================================
  2. // 洪城到家 · VOC 报告共享 UI 组件
  3. // ==============================================================================
  4. const BRAND = {
  5. HONGCHENG_RED: '#E63946',
  6. HONGCHENG_GOLD: '#F4A261',
  7. MATERNITY_PINK: '#FFB4C2',
  8. BABY_BLUE: '#89CFF0',
  9. TRUST_GREEN: '#2D9C5D',
  10. SERVICE_ORANGE: '#FF8C42',
  11. SOCIAL_PURPLE: '#8B5CF6',
  12. COMPETITOR_GRAY: '#6C757D',
  13. WARM_CREAM: '#FFF4E6',
  14. CORE_STRATEGY: '#1D3557',
  15. };
  16. const PLATFORM_LABELS = {
  17. xhs: { name: '小红书', color: '#FF2442', short: '红' },
  18. douyin: { name: '抖音', color: '#1A1A1A', short: '抖' },
  19. meituan: { name: '美团', color: '#FFD100', short: '美' },
  20. unknown: { name: '其他', color: '#888', short: '—' },
  21. };
  22. const SENTIMENT_LABELS = {
  23. positive: { label: '好评', color: '#2D9C5D', icon: '▲' },
  24. neutral: { label: '中性', color: '#888', icon: '●' },
  25. negative: { label: '差评', color: '#E63946', icon: '▼' },
  26. conflicted: { label: '矛盾', color: '#FF8C42', icon: '◆' },
  27. };
  28. function esc(s) {
  29. return String(s ?? '').replace(/[&<>"']/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' }[c]));
  30. }
  31. function fmtLikes(n) {
  32. if (n == null) return '0';
  33. if (n >= 10000) return (n / 10000).toFixed(1).replace(/\.0$/, '') + 'w';
  34. if (n >= 1000) return (n / 1000).toFixed(1).replace(/\.0$/, '') + 'k';
  35. return String(n);
  36. }
  37. function truncate(s, n = 160) {
  38. s = String(s ?? '');
  39. return s.length > n ? s.slice(0, n - 1) + '…' : s;
  40. }
  41. function renderVocCard(item, opts = {}) {
  42. const { accent = BRAND.HONGCHENG_RED, showBasis = false, compact = false, maxChars = 240 } = opts;
  43. const pf = PLATFORM_LABELS[item.platform] || PLATFORM_LABELS.unknown;
  44. const sen = SENTIMENT_LABELS[item.sentiment] || SENTIMENT_LABELS.neutral;
  45. const isSeed = (item.source || '').includes('pattern') || (item.source || '').includes('seed');
  46. const tags = Array.isArray(item.tags) ? item.tags.slice(0, 3) : [];
  47. return `<div style="background:var(--bg-1); border:1px solid var(--border); border-left:3px solid ${accent}; border-radius:var(--radius-sm); padding:${compact ? '12px 14px' : '16px 18px'}; display:flex; flex-direction:column; gap:${compact ? '8px' : '10px'};">
  48. <div style="display:flex; justify-content:space-between; align-items:center; gap:8px; flex-wrap:wrap;">
  49. <div style="display:flex; align-items:center; gap:6px;">
  50. <span style="display:inline-flex; align-items:center; justify-content:center; width:22px; height:22px; border-radius:50%; background:${pf.color}22; color:${pf.color}; font-size:0.62rem; font-weight:700;">${pf.short}</span>
  51. <span style="color:var(--text-1); font-size:0.78rem; font-weight:600;">${esc(item.nickname || '匿名用户')}</span>
  52. ${item.ip ? `<span style="color:var(--text-3); font-size:0.68rem;">· ${esc(item.ip)}</span>` : ''}
  53. ${item.keyword ? `<span style="color:var(--text-3); font-size:0.66rem; font-family:var(--font-mono);">· ${esc(item.keyword)}</span>` : ''}
  54. </div>
  55. <div style="display:flex; align-items:center; gap:6px;">
  56. <span style="color:${sen.color}; font-size:0.7rem; font-weight:600;">${sen.icon} ${sen.label}</span>
  57. <span style="color:var(--text-3); font-size:0.68rem; font-family:var(--font-mono);">♥${fmtLikes(item.likes)}</span>
  58. ${isSeed ? `<span style="padding:1px 6px; background:${BRAND.MATERNITY_PINK}18; color:${BRAND.MATERNITY_PINK}; border-radius:10px; font-size:0.6rem; font-family:var(--font-mono); font-weight:700;">◈模式</span>` : `<span style="padding:1px 6px; background:${BRAND.HONGCHENG_RED}18; color:${BRAND.HONGCHENG_RED}; border-radius:10px; font-size:0.6rem; font-family:var(--font-mono); font-weight:700;">✓真实</span>`}
  59. </div>
  60. </div>
  61. <div style="color:var(--text-1); font-size:${compact ? '0.86rem' : '0.92rem'}; line-height:1.62;">"${esc(truncate(item.content, maxChars))}"</div>
  62. ${tags.length ? `<div style="display:flex; flex-wrap:wrap; gap:4px;">
  63. ${tags.map((t) => `<span style="padding:2px 8px; background:${accent}12; color:${accent}; border-radius:3px; font-size:0.68rem; font-weight:600;">${esc(t)}</span>`).join('')}
  64. </div>` : ''}
  65. ${showBasis && item.basis ? `<div style="color:var(--text-3); font-size:0.66rem; font-family:var(--font-mono); letter-spacing:0.05em; padding-top:6px; border-top:1px dashed var(--border);">依据:${esc(item.basis)}</div>` : ''}
  66. </div>`;
  67. }
  68. function renderVocGrid(items, opts = {}) {
  69. const { columns = 2, accent = BRAND.HONGCHENG_RED, compact = false, maxChars } = opts;
  70. return `<div style="display:grid; grid-template-columns:repeat(${columns}, 1fr); gap:14px;">
  71. ${items.map((it) => renderVocCard(it, { accent, compact, maxChars })).join('')}
  72. </div>`;
  73. }
  74. function renderInsightHero({ eyebrow, title, subtitle, color = BRAND.HONGCHENG_RED }) {
  75. return `<div class="rise-in" style="padding:28px 32px; background:linear-gradient(135deg, ${color}12, ${color}04); border-left:4px solid ${color}; border-radius:var(--radius-md); margin-bottom:32px;">
  76. ${eyebrow ? `<div class="text-mono text-xs" style="color:${color}; letter-spacing:0.18em; margin-bottom:10px; font-weight:700;">${esc(eyebrow)}</div>` : ''}
  77. <div style="color:var(--text-1); font-size:1.18rem; line-height:1.72; font-weight:600;">${title}</div>
  78. ${subtitle ? `<div style="color:var(--text-2); font-size:0.92rem; line-height:1.7; margin-top:10px;">${subtitle}</div>` : ''}
  79. </div>`;
  80. }
  81. function renderDecisionList(items, opts = {}) {
  82. const { accent = BRAND.CORE_STRATEGY, title = 'DECISION · 落地决策项', columns = 2 } = opts;
  83. return `<div class="fade-up" style="margin-top:24px; padding:22px 26px; background:${accent}08; border:1px solid ${accent}33; border-radius:var(--radius-md);">
  84. <div class="text-mono text-xs" style="color:${accent}; letter-spacing:0.2em; margin-bottom:14px; font-weight:700;">${esc(title)}</div>
  85. <div style="display:grid; grid-template-columns:repeat(${columns}, 1fr); gap:14px;">
  86. ${items.map((it, i) => `
  87. <div style="display:flex; gap:10px; align-items:flex-start;">
  88. <div style="flex-shrink:0; width:22px; height:22px; border-radius:50%; background:${accent}; color:#fff; font-size:0.72rem; font-weight:700; display:flex; align-items:center; justify-content:center;">${i + 1}</div>
  89. <div style="color:var(--text-1); font-size:0.9rem; line-height:1.64;">${it}</div>
  90. </div>`).join('')}
  91. </div>
  92. </div>`;
  93. }
  94. function renderTheoryBox({ theory, mapping, accent = BRAND.SOCIAL_PURPLE }) {
  95. return `<div class="fade-up" style="margin-top:24px; padding:22px 26px; background:linear-gradient(135deg, ${accent}0E, transparent); border:1px solid ${accent}30; border-radius:var(--radius-md);">
  96. <div class="text-mono text-xs" style="color:${accent}; letter-spacing:0.2em; margin-bottom:12px; font-weight:700;">THEORY · ${esc(theory)}</div>
  97. <div style="color:var(--text-1); font-size:0.95rem; line-height:1.72;">${mapping}</div>
  98. </div>`;
  99. }
  100. function renderTagCloud(tagGroups, opts = {}) {
  101. const { accent = BRAND.SERVICE_ORANGE, top = 15, title = '主题分布' } = opts;
  102. const list = tagGroups.slice(0, top);
  103. if (!list.length) return '';
  104. const maxCount = Math.max(...list.map((g) => g.count));
  105. return `<div style="padding:20px 22px; background:var(--bg-1); border:1px solid var(--border); border-radius:var(--radius-md);">
  106. <div class="text-mono text-xs" style="color:${accent}; letter-spacing:0.15em; margin-bottom:14px; font-weight:700;">${esc(title)} · TOP ${list.length}</div>
  107. <div style="display:flex; flex-wrap:wrap; gap:8px; align-items:center;">
  108. ${list.map((g) => {
  109. const ratio = g.count / maxCount;
  110. const fs = (0.72 + ratio * 0.45).toFixed(2);
  111. const bg = Math.round(10 + ratio * 30);
  112. return `<span style="padding:6px 12px; background:${accent}${String(bg).padStart(2, '0')}; color:${accent}; border-radius:16px; font-size:${fs}rem; font-weight:${Math.round(500 + ratio * 300)}; letter-spacing:0.02em;">${esc(g.tag)} <span style="opacity:0.7; font-size:0.8em; font-family:var(--font-mono);">×${g.count}</span></span>`;
  113. }).join('')}
  114. </div>
  115. </div>`;
  116. }
  117. function renderStatStrip(stats, opts = {}) {
  118. const { accent = BRAND.HONGCHENG_RED } = opts;
  119. return `<div style="display:grid; grid-template-columns:repeat(${stats.length}, 1fr); gap:12px; padding:18px 22px; background:var(--bg-1); border:1px solid var(--border); border-radius:var(--radius-md);">
  120. ${stats.map((s) => `
  121. <div style="text-align:center;">
  122. <div style="font-family:var(--font-head); font-size:1.8rem; font-weight:800; color:${s.color || accent}; letter-spacing:-0.02em;">${s.num}</div>
  123. <div style="color:var(--text-3); font-size:0.72rem; font-family:var(--font-mono); letter-spacing:0.1em; margin-top:4px;">${esc(s.label)}</div>
  124. ${s.delta ? `<div style="color:${s.deltaColor || accent}; font-size:0.7rem; margin-top:2px;">${esc(s.delta)}</div>` : ''}
  125. </div>`).join('')}
  126. </div>`;
  127. }
  128. function renderBarChart(rows, opts = {}) {
  129. const { accent = BRAND.HONGCHENG_RED, height = 260, title = '' } = opts;
  130. const max = Math.max(...rows.map((r) => r.value), 1);
  131. const barH = 28;
  132. const gap = 10;
  133. const padL = 160;
  134. const padR = 80;
  135. const padV = 16;
  136. const totalH = rows.length * (barH + gap) + padV * 2;
  137. const W = 900;
  138. return `<div style="padding:18px 22px; background:var(--bg-1); border:1px solid var(--border); border-radius:var(--radius-md);">
  139. ${title ? `<div class="text-mono text-xs" style="color:${accent}; letter-spacing:0.15em; margin-bottom:12px; font-weight:700;">${esc(title)}</div>` : ''}
  140. <svg viewBox="0 0 ${W} ${totalH}" style="width:100%; height:auto; max-height:${height}px;" preserveAspectRatio="xMidYMid meet">
  141. ${rows.map((r, i) => {
  142. const y = padV + i * (barH + gap);
  143. const w = (r.value / max) * (W - padL - padR);
  144. const color = r.color || accent;
  145. return `
  146. <text x="${padL - 10}" y="${y + barH / 2 + 5}" text-anchor="end" font-size="13" font-weight="600" fill="var(--text-1, #333)">${esc(r.label)}</text>
  147. <rect x="${padL}" y="${y}" width="${w}" height="${barH}" rx="4" fill="${color}" opacity="0.85"/>
  148. <text x="${padL + w + 8}" y="${y + barH / 2 + 5}" font-size="13" font-weight="700" fill="${color}" font-family="monospace">${esc(r.valueLabel || r.value)}</text>
  149. `;
  150. }).join('')}
  151. </svg>
  152. </div>`;
  153. }
  154. function renderMatrix2x2({ xLabel, yLabel, xLow, xHigh, yLow, yHigh, quadrants, accent = BRAND.SERVICE_ORANGE }) {
  155. const q1 = quadrants.q1;
  156. const q2 = quadrants.q2;
  157. const q3 = quadrants.q3;
  158. const q4 = quadrants.q4;
  159. const renderQuad = (q) => `
  160. <div style="padding:16px; background:${q.color}08; border:1px solid ${q.color}40; border-radius:6px; height:100%; display:flex; flex-direction:column;">
  161. <div class="text-mono text-xs" style="color:${q.color}; letter-spacing:0.15em; margin-bottom:8px; font-weight:700;">${esc(q.title)}</div>
  162. <ul style="color:var(--text-1); font-size:0.82rem; line-height:1.55; margin:0; padding-left:18px; list-style:disc;">
  163. ${q.items.map((it) => `<li style="margin-bottom:4px;">${it}</li>`).join('')}
  164. </ul>
  165. ${q.note ? `<div style="margin-top:auto; padding-top:10px; color:${q.color}; font-size:0.74rem; font-style:italic;">${q.note}</div>` : ''}
  166. </div>`;
  167. return `<div style="padding:20px 26px; background:var(--bg-1); border:1px solid var(--border); border-radius:var(--radius-md);">
  168. <div style="display:grid; grid-template-columns:52px 1fr 1fr; grid-template-rows:auto 1fr 1fr 40px; gap:12px; height:520px;">
  169. <div style="grid-row:2 / span 2; grid-column:1; display:flex; align-items:center; justify-content:center; transform:rotate(-90deg); white-space:nowrap;">
  170. <span class="text-mono text-xs" style="color:${accent}; letter-spacing:0.2em; font-weight:700;">${esc(yLabel)}</span>
  171. </div>
  172. <div style="grid-row:1; grid-column:2; text-align:center;"><span class="text-mono text-xs" style="color:${q2.color}; font-weight:700;">${esc(yHigh)}</span></div>
  173. <div style="grid-row:1; grid-column:3; text-align:center;"><span class="text-mono text-xs" style="color:${q1.color}; font-weight:700;">${esc(yHigh)}</span></div>
  174. <div style="grid-row:2; grid-column:2;">${renderQuad(q2)}</div>
  175. <div style="grid-row:2; grid-column:3;">${renderQuad(q1)}</div>
  176. <div style="grid-row:3; grid-column:2;">${renderQuad(q3)}</div>
  177. <div style="grid-row:3; grid-column:3;">${renderQuad(q4)}</div>
  178. <div style="grid-row:4; grid-column:2; text-align:center;"><span class="text-mono text-xs" style="color:var(--text-3); letter-spacing:0.15em;">${esc(xLow)} ← ${esc(xLabel)} → ${esc(xHigh)}</span></div>
  179. <div style="grid-row:4; grid-column:3;"></div>
  180. </div>
  181. </div>`;
  182. }
  183. function renderSectionHead({ eyebrow, title, subtitle, color = BRAND.HONGCHENG_RED }) {
  184. return `<div class="section-head rise-in">
  185. <div class="section-eyebrow" style="color:${color};">${esc(eyebrow)}</div>
  186. <h2 class="text-h1"><span class="headline-anim">${title}</span></h2>
  187. ${subtitle ? `<p class="section-sub">${subtitle}</p>` : ''}
  188. </div>`;
  189. }
  190. function renderCompareTable({ title, headers, rows, accent = BRAND.HONGCHENG_RED }) {
  191. return `<div style="padding:18px 22px; background:var(--bg-1); border:1px solid var(--border); border-radius:var(--radius-md);">
  192. ${title ? `<div class="text-mono text-xs" style="color:${accent}; letter-spacing:0.15em; margin-bottom:12px; font-weight:700;">${esc(title)}</div>` : ''}
  193. <table style="width:100%; border-collapse:collapse; font-size:0.88rem;">
  194. <thead>
  195. <tr style="border-bottom:2px solid ${accent}33;">
  196. ${headers.map((h, i) => `<th style="text-align:${i === 0 ? 'left' : 'center'}; padding:10px 6px; color:${i === 0 ? 'var(--text-3)' : accent}; font-weight:700; font-size:0.8rem; letter-spacing:0.05em;">${esc(h)}</th>`).join('')}
  197. </tr>
  198. </thead>
  199. <tbody>
  200. ${rows.map((r, ri) => `<tr style="border-bottom:1px solid var(--border);">
  201. ${r.map((cell, ci) => {
  202. const isHeader = ci === 0;
  203. const content = typeof cell === 'string' ? cell : (cell.html || '');
  204. const cellColor = typeof cell === 'object' && cell.color ? cell.color : (isHeader ? 'var(--text-2)' : 'var(--text-1)');
  205. const weight = typeof cell === 'object' && cell.bold ? '700' : '400';
  206. return `<td style="padding:${ri === rows.length - 1 ? '12px' : '10px'} 6px; color:${cellColor}; text-align:${isHeader ? 'left' : 'center'}; font-weight:${weight};">${content}</td>`;
  207. }).join('')}
  208. </tr>`).join('')}
  209. </tbody>
  210. </table>
  211. </div>`;
  212. }
  213. function renderFindingCard({ icon = '💡', title, body, accent = BRAND.SERVICE_ORANGE }) {
  214. return `<div style="padding:20px 24px; background:${accent}08; border-left:3px solid ${accent}; border-radius:6px;">
  215. <div style="display:flex; align-items:center; gap:10px; margin-bottom:10px;">
  216. <span style="font-size:1.4rem;">${icon}</span>
  217. <div style="color:${accent}; font-weight:700; font-size:0.96rem;">${esc(title)}</div>
  218. </div>
  219. <div style="color:var(--text-1); font-size:0.88rem; line-height:1.7;">${body}</div>
  220. </div>`;
  221. }
  222. function renderHypothesisBadge(h, opts = {}) {
  223. const { accent = BRAND.SOCIAL_PURPLE, size = 'md' } = opts;
  224. const fs = size === 'sm' ? '0.64rem' : size === 'lg' ? '0.82rem' : '0.72rem';
  225. const px = size === 'sm' ? '3px 8px' : size === 'lg' ? '6px 14px' : '4px 10px';
  226. return `<span style="padding:${px}; background:${accent}15; color:${accent}; border:1px solid ${accent}44; border-radius:12px; font-size:${fs}; font-family:var(--font-mono); letter-spacing:0.05em; font-weight:700;">${esc(h)}</span>`;
  227. }
  228. function renderChannelCard({ icon = '🏥', title, desc, metrics, accent = BRAND.TRUST_GREEN }) {
  229. return `<div style="padding:20px 24px; background:${accent}08; border:1px solid ${accent}30; border-radius:var(--radius-md);">
  230. <div style="display:flex; align-items:center; gap:12px; margin-bottom:12px;">
  231. <span style="font-size:1.8rem;">${icon}</span>
  232. <div>
  233. <div style="color:${accent}; font-weight:700; font-size:1rem;">${esc(title)}</div>
  234. <div style="color:var(--text-2); font-size:0.82rem;">${esc(desc)}</div>
  235. </div>
  236. </div>
  237. <div style="display:flex; gap:16px; flex-wrap:wrap;">
  238. ${metrics.map((m) => `<div style="text-align:center;">
  239. <div style="font-size:1.4rem; font-weight:800; color:${accent};">${esc(m.value)}</div>
  240. <div style="color:var(--text-3); font-size:0.7rem;">${esc(m.label)}</div>
  241. </div>`).join('')}
  242. </div>
  243. </div>`;
  244. }
  245. module.exports = {
  246. BRAND,
  247. PLATFORM_LABELS,
  248. SENTIMENT_LABELS,
  249. esc,
  250. fmtLikes,
  251. truncate,
  252. renderVocCard,
  253. renderVocGrid,
  254. renderInsightHero,
  255. renderDecisionList,
  256. renderTheoryBox,
  257. renderTagCloud,
  258. renderStatStrip,
  259. renderBarChart,
  260. renderMatrix2x2,
  261. renderSectionHead,
  262. renderCompareTable,
  263. renderFindingCard,
  264. renderHypothesisBadge,
  265. renderChannelCard,
  266. };