GrowthHeatMapView.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <script setup lang="ts">
  2. import { computed, onMounted, ref } from 'vue'
  3. import IcicleHeatTree from '../components/IcicleHeatTree.vue'
  4. import {
  5. fetchGrowthCategoryTopContents,
  6. fetchGrowthCategoryTree,
  7. type GrowthChannelContentRankItem,
  8. } from '../api/growthCategory'
  9. import type { CategoryNode, WeightDimKey, WeightDimMeta } from '../types/category'
  10. import type { HeatTabKey } from '../types/heatTree'
  11. const GROWTH_HEAT_TABS: WeightDimMeta[] = [
  12. { key: 'read_rate', label: '阅读率' },
  13. { key: 'avg_read_rate', label: '平均阅读率' },
  14. { key: 'like_rate', label: '点赞率' },
  15. ]
  16. const nodes = ref<CategoryNode[]>([])
  17. const bizDt = ref<string | null>(null)
  18. const loading = ref(true)
  19. const error = ref<string | null>(null)
  20. const selectedNode = ref<{ id: number; name: string } | null>(null)
  21. const activeMetric = ref<WeightDimKey>('read_rate')
  22. const rankItems = ref<GrowthChannelContentRankItem[]>([])
  23. const rankLoading = ref(false)
  24. const rankError = ref<string | null>(null)
  25. const rankPanelCollapsed = ref(false)
  26. let rankRequestId = 0
  27. const activeMetricLabel = computed(
  28. () =>
  29. GROWTH_HEAT_TABS.find((tab) => tab.key === activeMetric.value)?.label ??
  30. activeMetric.value,
  31. )
  32. onMounted(async () => {
  33. try {
  34. const tree = await fetchGrowthCategoryTree()
  35. nodes.value = tree.nodes ?? []
  36. bizDt.value = tree.biz_dt ?? null
  37. } catch (cause) {
  38. error.value = cause instanceof Error ? cause.message : String(cause)
  39. } finally {
  40. loading.value = false
  41. }
  42. })
  43. async function loadRankings() {
  44. if (!selectedNode.value || !bizDt.value) return
  45. const requestId = ++rankRequestId
  46. rankLoading.value = true
  47. rankError.value = null
  48. try {
  49. const response = await fetchGrowthCategoryTopContents(
  50. selectedNode.value.id,
  51. bizDt.value,
  52. activeMetric.value,
  53. )
  54. if (requestId === rankRequestId) rankItems.value = response.items ?? []
  55. } catch (cause) {
  56. if (requestId === rankRequestId) {
  57. rankItems.value = []
  58. rankError.value = cause instanceof Error ? cause.message : String(cause)
  59. }
  60. } finally {
  61. if (requestId === rankRequestId) rankLoading.value = false
  62. }
  63. }
  64. function onNodeSelected(node: { id: number; name: string }) {
  65. selectedNode.value = node
  66. void loadRankings()
  67. }
  68. function onTabChange(tab: HeatTabKey) {
  69. if (tab === 'full') return
  70. activeMetric.value = tab
  71. if (selectedNode.value) void loadRankings()
  72. }
  73. function formatScore(value: number): string {
  74. if (!Number.isFinite(value)) return '—'
  75. if (Math.abs(value) >= 100) return value.toFixed(2)
  76. if (Math.abs(value) >= 1) return value.toFixed(4)
  77. return value.toFixed(6)
  78. }
  79. function formatContribution(value: number): string {
  80. return Number.isFinite(value) ? value.toFixed(2) : '—'
  81. }
  82. function openArticle(channelContentId: string) {
  83. const target = `/api/growth-channel-content/${encodeURIComponent(channelContentId)}/view`
  84. window.open(target, '_blank', 'noopener,noreferrer')
  85. }
  86. </script>
  87. <template>
  88. <div class="view">
  89. <div v-if="loading" class="state">正在加载增长分类树与权重…</div>
  90. <div v-else-if="error" class="state error">{{ error }}</div>
  91. <div
  92. v-else
  93. class="content-layout"
  94. :class="{ 'rank-panel-collapsed': rankPanelCollapsed }"
  95. >
  96. <IcicleHeatTree
  97. class="heat-map"
  98. :nodes="nodes"
  99. :biz-dt="bizDt"
  100. title="增长全局热度地图"
  101. subtitle="global_category_v2 分类树 · 横向为层级,纵向为分支规模"
  102. :heat-tabs="GROWTH_HEAT_TABS"
  103. initial-tab="read_rate"
  104. heat-only
  105. @node-selected="onNodeSelected"
  106. @tab-change="onTabChange"
  107. />
  108. <aside class="rank-panel" :class="{ collapsed: rankPanelCollapsed }">
  109. <header class="rank-header" :class="{ collapsed: rankPanelCollapsed }">
  110. <button
  111. type="button"
  112. class="rank-collapse-button"
  113. :title="rankPanelCollapsed ? '展开右侧详情' : '收起右侧详情'"
  114. :aria-label="rankPanelCollapsed ? '展开右侧详情' : '收起右侧详情'"
  115. :aria-expanded="!rankPanelCollapsed"
  116. @click="rankPanelCollapsed = !rankPanelCollapsed"
  117. >
  118. {{ rankPanelCollapsed ? '‹' : '›' }}
  119. </button>
  120. <div v-if="!rankPanelCollapsed" class="rank-header-copy">
  121. <span class="rank-eyebrow">CHANNEL CONTENT TOP 10</span>
  122. <h2>{{ selectedNode?.name || '请选择分类节点' }}</h2>
  123. <p v-if="selectedNode">
  124. {{ activeMetricLabel }}贡献权重 · stable_id {{ selectedNode.id }}
  125. </p>
  126. <p v-else>点击左侧热力图节点查看当前维度的文章榜单</p>
  127. </div>
  128. </header>
  129. <template v-if="!rankPanelCollapsed">
  130. <div v-if="rankLoading" class="rank-state">正在加载 Top 10…</div>
  131. <div v-else-if="rankError" class="rank-state error">{{ rankError }}</div>
  132. <div v-else-if="!selectedNode" class="rank-state">尚未选择节点</div>
  133. <div v-else-if="!rankItems.length" class="rank-state">该节点当前维度暂无文章</div>
  134. <ol v-else class="rank-list">
  135. <li v-for="item in rankItems" :key="item.channel_content_id">
  136. <button type="button" @click="openArticle(item.channel_content_id)">
  137. <span class="rank-no">{{ item.rank_no }}</span>
  138. <span class="rank-main">
  139. <code>{{ item.channel_content_id }}</code>
  140. <small>
  141. source_element {{ item.source_element_id }}
  142. · contribution {{ formatContribution(item.contribution) }}
  143. </small>
  144. </span>
  145. <span class="rank-score">
  146. <strong>{{ formatScore(item.weighted_score) }}</strong>
  147. <small>{{ activeMetricLabel }}贡献权重分</small>
  148. </span>
  149. </button>
  150. </li>
  151. </ol>
  152. </template>
  153. </aside>
  154. </div>
  155. </div>
  156. </template>
  157. <style scoped>
  158. .view {
  159. height: max(760px, calc(100vh - 106px));
  160. min-height: 760px;
  161. padding-bottom: 8px;
  162. }
  163. .content-layout {
  164. display: grid;
  165. grid-template-columns: minmax(0, 1fr) 360px;
  166. gap: 12px;
  167. height: 100%;
  168. min-height: 0;
  169. }
  170. .content-layout.rank-panel-collapsed {
  171. grid-template-columns: minmax(0, 1fr) 44px;
  172. }
  173. .heat-map {
  174. min-width: 0;
  175. }
  176. .rank-panel {
  177. display: flex;
  178. min-width: 0;
  179. min-height: 0;
  180. flex-direction: column;
  181. overflow: hidden;
  182. border: 1px solid #e2e8f0;
  183. border-radius: 10px;
  184. background: #fff;
  185. }
  186. .rank-panel.collapsed {
  187. background: #f8fafc;
  188. }
  189. .rank-header {
  190. flex-shrink: 0;
  191. padding: 16px;
  192. border-bottom: 1px solid #e8edf2;
  193. background: #f8fafc;
  194. }
  195. .rank-header.collapsed {
  196. display: flex;
  197. height: 100%;
  198. justify-content: center;
  199. padding: 8px 5px;
  200. border-bottom: 0;
  201. }
  202. .rank-header-copy {
  203. min-width: 0;
  204. }
  205. .rank-collapse-button {
  206. float: right;
  207. width: 28px;
  208. height: 28px;
  209. margin: 0 0 6px 8px;
  210. border: 1px solid #cbd5e1;
  211. border-radius: 7px;
  212. background: #fff;
  213. color: #475569;
  214. font-size: 18px;
  215. line-height: 1;
  216. cursor: pointer;
  217. }
  218. .rank-collapse-button:hover {
  219. border-color: #818cf8;
  220. color: #4338ca;
  221. }
  222. .rank-header.collapsed .rank-collapse-button {
  223. float: none;
  224. margin: 0;
  225. }
  226. .rank-eyebrow {
  227. color: #6366f1;
  228. font-size: 10px;
  229. font-weight: 700;
  230. letter-spacing: 0.1em;
  231. }
  232. .rank-header h2 {
  233. margin: 7px 0 4px;
  234. color: #0f172a;
  235. font-size: 17px;
  236. }
  237. .rank-header p {
  238. margin: 0;
  239. color: #64748b;
  240. font-size: 12px;
  241. }
  242. .rank-state {
  243. display: grid;
  244. flex: 1;
  245. place-items: center;
  246. padding: 24px;
  247. color: #94a3b8;
  248. font-size: 13px;
  249. text-align: center;
  250. }
  251. .rank-state.error {
  252. color: #b91c1c;
  253. }
  254. .rank-list {
  255. margin: 0;
  256. padding: 8px;
  257. overflow-y: auto;
  258. list-style: none;
  259. }
  260. .rank-list li + li {
  261. border-top: 1px solid #eef2f7;
  262. }
  263. .rank-list button {
  264. display: grid;
  265. width: 100%;
  266. padding: 12px 8px;
  267. grid-template-columns: 28px minmax(0, 1fr) auto;
  268. gap: 8px;
  269. align-items: center;
  270. border: 0;
  271. border-radius: 7px;
  272. background: transparent;
  273. color: inherit;
  274. cursor: pointer;
  275. text-align: left;
  276. }
  277. .rank-list button:hover {
  278. background: #f1f5f9;
  279. }
  280. .rank-no {
  281. display: grid;
  282. width: 24px;
  283. height: 24px;
  284. place-items: center;
  285. border-radius: 7px;
  286. background: #e0e7ff;
  287. color: #4338ca;
  288. font-size: 12px;
  289. font-weight: 700;
  290. }
  291. .rank-main,
  292. .rank-score {
  293. display: flex;
  294. min-width: 0;
  295. flex-direction: column;
  296. gap: 4px;
  297. }
  298. .rank-main code {
  299. overflow: hidden;
  300. color: #334155;
  301. font-size: 11px;
  302. text-overflow: ellipsis;
  303. white-space: nowrap;
  304. }
  305. .rank-main small,
  306. .rank-score small {
  307. color: #94a3b8;
  308. font-size: 10px;
  309. }
  310. .rank-score {
  311. align-items: flex-end;
  312. }
  313. .rank-score strong {
  314. color: #dc2626;
  315. font-size: 13px;
  316. font-variant-numeric: tabular-nums;
  317. }
  318. @media (max-width: 1100px) {
  319. .view {
  320. height: auto;
  321. }
  322. .content-layout {
  323. grid-template-columns: 1fr;
  324. grid-template-rows: 760px minmax(360px, auto);
  325. }
  326. .rank-panel {
  327. min-height: 360px;
  328. }
  329. }
  330. .state {
  331. padding: 64px 24px;
  332. color: #64748b;
  333. font-size: 15px;
  334. text-align: center;
  335. }
  336. .state.error {
  337. color: #b91c1c;
  338. }
  339. </style>