index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. <template>
  2. <div class="analytics-page">
  3. <div class="page-header">
  4. <h2>数据分析</h2>
  5. <div class="header-actions">
  6. <el-date-picker
  7. v-model="dateRange"
  8. type="daterange"
  9. range-separator="至"
  10. start-placeholder="开始日期"
  11. end-placeholder="结束日期"
  12. @change="loadData"
  13. />
  14. <el-button
  15. type="primary"
  16. :icon="Refresh"
  17. :loading="refreshing"
  18. @click="handleRefresh"
  19. >
  20. 刷新数据
  21. </el-button>
  22. </div>
  23. </div>
  24. <!-- 统计摘要 -->
  25. <div class="stats-grid">
  26. <div class="stat-card" v-for="(item, index) in summaryItems" :key="index">
  27. <div class="stat-value">{{ formatNumber(item.value) }}</div>
  28. <div class="stat-label">{{ item.label }}</div>
  29. </div>
  30. </div>
  31. <!-- 趋势图 -->
  32. <div class="page-card" style="margin-top: 20px">
  33. <div class="card-header">
  34. <h3>数据趋势</h3>
  35. <el-radio-group v-model="trendMetric" size="small" @change="updateTrendChart">
  36. <el-radio-button label="fansIncrease">涨粉</el-radio-button>
  37. <el-radio-button label="views">播放</el-radio-button>
  38. <el-radio-button label="likes">点赞</el-radio-button>
  39. <el-radio-button label="comments">评论</el-radio-button>
  40. </el-radio-group>
  41. </div>
  42. <div ref="trendChartRef" style="height: 350px" v-loading="loading"></div>
  43. </div>
  44. <!-- 平台对比 -->
  45. <el-row :gutter="20" style="margin-top: 20px">
  46. <el-col :span="12">
  47. <div class="page-card">
  48. <h3>平台粉丝分布</h3>
  49. <div ref="platformChartRef" style="height: 300px"></div>
  50. </div>
  51. </el-col>
  52. <el-col :span="12">
  53. <div class="page-card">
  54. <h3>平台数据对比</h3>
  55. <el-table :data="platformComparison" size="small">
  56. <el-table-column prop="platform" label="平台" width="100">
  57. <template #default="{ row }">
  58. {{ getPlatformName(row.platform) }}
  59. </template>
  60. </el-table-column>
  61. <el-table-column prop="fansCount" label="粉丝" />
  62. <el-table-column prop="fansIncrease" label="粉丝增量" />
  63. <el-table-column prop="viewsCount" label="播放量" />
  64. <el-table-column prop="likesCount" label="点赞数" />
  65. </el-table>
  66. </div>
  67. </el-col>
  68. </el-row>
  69. </div>
  70. </template>
  71. <script setup lang="ts">
  72. import { ref, onMounted, computed } from 'vue';
  73. import * as echarts from 'echarts';
  74. import { Refresh } from '@element-plus/icons-vue';
  75. import { PLATFORMS } from '@media-manager/shared';
  76. import type { PlatformComparison, PlatformType } from '@media-manager/shared';
  77. import { useAuthStore } from '@/stores/auth';
  78. import dayjs from 'dayjs';
  79. import request from '@/api/request';
  80. // 单个平台的趋势数据
  81. interface PlatformTrendItem {
  82. platform: string;
  83. platformName: string;
  84. fansIncrease: number[];
  85. views: number[];
  86. likes: number[];
  87. comments: number[];
  88. }
  89. // 趋势数据类型
  90. interface TrendData {
  91. dates: string[];
  92. platforms: PlatformTrendItem[];
  93. }
  94. const authStore = useAuthStore();
  95. const loading = ref(false);
  96. const refreshing = ref(false);
  97. const trendChartRef = ref<HTMLElement>();
  98. const platformChartRef = ref<HTMLElement>();
  99. let trendChart: echarts.ECharts | null = null;
  100. let platformChart: echarts.ECharts | null = null;
  101. // 刷新数据
  102. async function handleRefresh() {
  103. refreshing.value = true;
  104. try {
  105. await loadData();
  106. } finally {
  107. refreshing.value = false;
  108. }
  109. }
  110. const dateRange = ref<[Date, Date]>([
  111. dayjs().subtract(30, 'day').toDate(),
  112. dayjs().toDate(),
  113. ]);
  114. const trendMetric = ref<'fansIncrease' | 'views' | 'likes' | 'comments'>('fansIncrease');
  115. const trendData = ref<TrendData | null>(null);
  116. const platformComparison = ref<PlatformComparison[]>([]);
  117. // 从趋势数据计算统计摘要
  118. // 口径变更:user_day_statistics 存储的是每日单独值,区间汇总需要直接 SUM
  119. const summaryItems = computed(() => {
  120. const data = trendData.value;
  121. if (!data || data.platforms.length === 0) {
  122. return [
  123. { label: '涨粉总计', value: 0 },
  124. { label: '播放总计', value: 0 },
  125. { label: '点赞总计', value: 0 },
  126. { label: '评论总计', value: 0 },
  127. ];
  128. }
  129. // 计算所有平台的汇总
  130. let totalFansIncrease = 0;
  131. let totalViews = 0;
  132. let totalLikes = 0;
  133. let totalComments = 0;
  134. for (const platform of data.platforms) {
  135. totalFansIncrease += platform.fansIncrease.reduce((sum, v) => sum + v, 0);
  136. totalViews += platform.views.reduce((sum, v) => sum + v, 0);
  137. totalLikes += platform.likes.reduce((sum, v) => sum + v, 0);
  138. totalComments += platform.comments.reduce((sum, v) => sum + v, 0);
  139. }
  140. return [
  141. { label: '涨粉总计', value: totalFansIncrease },
  142. { label: '播放总计', value: totalViews },
  143. { label: '点赞总计', value: totalLikes },
  144. { label: '评论总计', value: totalComments },
  145. ];
  146. });
  147. function getPlatformName(platform: PlatformType) {
  148. return PLATFORMS[platform]?.name || platform;
  149. }
  150. function formatNumber(num: number) {
  151. if (num >= 10000) return (num / 10000).toFixed(1) + 'w';
  152. return num.toString();
  153. }
  154. // 获取趋势数据(通过 Node 转发调用 Python 接口)
  155. async function loadTrendData() {
  156. if (!dateRange.value) return;
  157. const [start, end] = dateRange.value;
  158. const startDate = dayjs(start).format('YYYY-MM-DD');
  159. const endDate = dayjs(end).format('YYYY-MM-DD');
  160. const data = await request.get('/api/analytics/trend-from-python', {
  161. params: {
  162. startDate,
  163. endDate,
  164. },
  165. });
  166. return data as TrendData;
  167. }
  168. // 平台统计数据类型
  169. interface PlatformStats {
  170. platform: string;
  171. fansCount: number;
  172. fansIncrease: number;
  173. viewsCount: number;
  174. likesCount: number;
  175. commentsCount: number;
  176. collectsCount: number;
  177. }
  178. // 获取平台统计数据(通过 Node 转发调用 Python 接口)
  179. async function loadPlatformData() {
  180. if (!dateRange.value) return;
  181. const [start, end] = dateRange.value;
  182. const startDate = dayjs(start).format('YYYY-MM-DD');
  183. const endDate = dayjs(end).format('YYYY-MM-DD');
  184. const data = await request.get('/api/analytics/platforms-from-python', {
  185. params: {
  186. startDate,
  187. endDate,
  188. },
  189. });
  190. return data as PlatformStats[];
  191. }
  192. async function loadData() {
  193. if (!dateRange.value) return;
  194. loading.value = true;
  195. try {
  196. // 并行加载数据(全部从 Python API 获取)
  197. const [pythonTrendData, pythonPlatformData] = await Promise.all([
  198. loadTrendData().catch(() => null),
  199. loadPlatformData().catch(() => null),
  200. ]);
  201. if (pythonTrendData) {
  202. trendData.value = pythonTrendData;
  203. }
  204. if (pythonPlatformData) {
  205. // 转换为 PlatformComparison 格式
  206. platformComparison.value = pythonPlatformData.map(p => ({
  207. platform: p.platform as PlatformType,
  208. fansCount: p.fansCount,
  209. fansIncrease: p.fansIncrease,
  210. viewsCount: p.viewsCount,
  211. likesCount: p.likesCount,
  212. }));
  213. }
  214. updateTrendChart();
  215. updatePlatformChart();
  216. } catch {
  217. // 错误已处理
  218. } finally {
  219. loading.value = false;
  220. }
  221. }
  222. // 平台颜色配置(柔和协调的配色)
  223. const platformColors: Record<string, string> = {
  224. xiaohongshu: '#E91E63',
  225. douyin: '#374151',
  226. kuaishou: '#F59E0B',
  227. weixin: '#10B981',
  228. weixin_video: '#10B981',
  229. shipinhao: '#10B981',
  230. baijiahao: '#3B82F6',
  231. };
  232. // 获取图表标题
  233. function getChartTitle(type: 'fansIncrease' | 'views' | 'likes' | 'comments') {
  234. const titles: Record<string, string> = {
  235. fansIncrease: '涨粉数',
  236. views: '播放量',
  237. likes: '点赞数',
  238. comments: '评论数',
  239. };
  240. return titles[type] || '';
  241. }
  242. function updateTrendChart() {
  243. if (!trendChartRef.value) return;
  244. if (!trendChart) {
  245. trendChart = echarts.init(trendChartRef.value);
  246. }
  247. const dates = trendData.value?.dates || [];
  248. const platforms = trendData.value?.platforms || [];
  249. // 生成每个平台的 series
  250. const series: echarts.SeriesOption[] = platforms.map((p) => {
  251. const data = p[trendMetric.value] || [];
  252. const color = platformColors[p.platform] || '#6B7280';
  253. return {
  254. name: p.platformName,
  255. data: data,
  256. type: 'line',
  257. smooth: 0.3,
  258. showSymbol: false,
  259. symbol: 'circle',
  260. symbolSize: 4,
  261. lineStyle: { width: 2.5, color: color },
  262. itemStyle: { color: color, borderWidth: 2, borderColor: '#fff' },
  263. emphasis: {
  264. focus: 'series',
  265. showSymbol: true,
  266. symbolSize: 6,
  267. lineStyle: { width: 3 },
  268. itemStyle: { borderWidth: 2, borderColor: '#fff' },
  269. },
  270. };
  271. });
  272. const legendData = platforms.map(p => p.platformName);
  273. trendChart.setOption({
  274. tooltip: {
  275. trigger: 'axis',
  276. backgroundColor: 'rgba(255, 255, 255, 0.98)',
  277. borderColor: '#e5e7eb',
  278. borderWidth: 1,
  279. padding: [10, 14],
  280. textStyle: { color: '#374151', fontSize: 13 },
  281. formatter: (params: unknown) => {
  282. const p = params as { seriesName: string; name: string; value: number; color: string }[];
  283. if (!Array.isArray(p) || p.length === 0) return '';
  284. let html = `<div style="font-weight: 600; margin-bottom: 8px;">${p[0].name}</div>`;
  285. for (const item of p) {
  286. html += `<div style="display: flex; align-items: center; gap: 10px; margin-bottom: 4px;">
  287. <span style="display: inline-block; width: 8px; height: 8px; background: ${item.color}; border-radius: 2px;"></span>
  288. <span style="color: #6b7280;">${item.seriesName}</span>
  289. <span style="font-weight: 600; margin-left: auto;">${item.value.toLocaleString()}</span>
  290. </div>`;
  291. }
  292. return html;
  293. },
  294. axisPointer: {
  295. type: 'line',
  296. lineStyle: { color: '#9ca3af', type: 'dashed', width: 1 },
  297. },
  298. },
  299. legend: {
  300. data: legendData,
  301. bottom: 4,
  302. type: 'scroll',
  303. itemWidth: 14,
  304. itemHeight: 10,
  305. itemGap: 20,
  306. textStyle: { color: '#6b7280', fontSize: 12 },
  307. icon: 'roundRect',
  308. },
  309. grid: {
  310. left: '2%',
  311. right: '2%',
  312. top: '8%',
  313. bottom: '36px',
  314. containLabel: true,
  315. },
  316. xAxis: {
  317. type: 'category',
  318. data: dates,
  319. boundaryGap: false,
  320. axisLine: { lineStyle: { color: '#e5e7eb' } },
  321. axisTick: { show: false },
  322. axisLabel: { color: '#9ca3af', fontSize: 11 },
  323. },
  324. yAxis: {
  325. type: 'value',
  326. axisLine: { show: false },
  327. axisTick: { show: false },
  328. splitLine: { lineStyle: { color: '#f3f4f6', type: 'dashed' } },
  329. axisLabel: {
  330. color: '#9ca3af',
  331. fontSize: 11,
  332. formatter: (value: number) => {
  333. if (value >= 10000) return (value / 10000).toFixed(1) + 'w';
  334. return value.toString();
  335. },
  336. },
  337. },
  338. series: series,
  339. }, true);
  340. }
  341. function updatePlatformChart() {
  342. if (!platformChartRef.value) return;
  343. if (!platformChart) {
  344. platformChart = echarts.init(platformChartRef.value);
  345. }
  346. platformChart.setOption({
  347. tooltip: { trigger: 'item' },
  348. series: [{
  349. type: 'pie',
  350. radius: ['40%', '70%'],
  351. data: platformComparison.value.map(p => ({
  352. name: getPlatformName(p.platform),
  353. value: p.fansCount,
  354. })),
  355. }],
  356. });
  357. }
  358. onMounted(() => {
  359. loadData();
  360. window.addEventListener('resize', () => {
  361. trendChart?.resize();
  362. platformChart?.resize();
  363. });
  364. });
  365. </script>
  366. <style lang="scss" scoped>
  367. @use '@/styles/variables.scss' as *;
  368. .page-header {
  369. display: flex;
  370. align-items: center;
  371. justify-content: space-between;
  372. margin-bottom: 20px;
  373. h2 { margin: 0; }
  374. .header-actions {
  375. display: flex;
  376. align-items: center;
  377. gap: 12px;
  378. }
  379. }
  380. .stats-grid {
  381. display: grid;
  382. grid-template-columns: repeat(6, 1fr);
  383. gap: 20px;
  384. margin-bottom: 20px;
  385. }
  386. .stat-card {
  387. background: #fff;
  388. border-radius: 8px;
  389. padding: 20px;
  390. text-align: center;
  391. box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
  392. .stat-value {
  393. font-size: 24px;
  394. font-weight: 600;
  395. color: $primary-color;
  396. }
  397. .stat-label {
  398. font-size: 14px;
  399. color: $text-secondary;
  400. margin-top: 8px;
  401. }
  402. }
  403. @media (max-width: 1200px) {
  404. .stats-grid {
  405. grid-template-columns: repeat(3, 1fr);
  406. }
  407. }
  408. @media (max-width: 768px) {
  409. .stats-grid {
  410. grid-template-columns: repeat(2, 1fr);
  411. }
  412. }
  413. .card-header {
  414. display: flex;
  415. align-items: center;
  416. justify-content: space-between;
  417. margin-bottom: 16px;
  418. h3 { margin: 0; }
  419. }
  420. </style>