index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. <template>
  2. <div class="dashboard">
  3. <!-- 页面标题和刷新按钮 -->
  4. <div class="page-header">
  5. <h2>数据看板</h2>
  6. <el-button
  7. type="primary"
  8. :icon="Refresh"
  9. :loading="refreshing"
  10. @click="handleRefresh"
  11. >
  12. 刷新数据
  13. </el-button>
  14. </div>
  15. <!-- 统计卡片 -->
  16. <div class="stats-grid">
  17. <div class="stat-card" v-for="stat in stats" :key="stat.label">
  18. <div class="stat-icon" :class="stat.iconClass">
  19. <el-icon><component :is="stat.icon" /></el-icon>
  20. </div>
  21. <div class="stat-content">
  22. <div class="stat-value">{{ stat.value }}</div>
  23. <div class="stat-label">{{ stat.label }}</div>
  24. </div>
  25. </div>
  26. </div>
  27. <!-- 主要内容区 -->
  28. <div class="content-grid">
  29. <!-- 平台账号状态 -->
  30. <div class="content-card">
  31. <div class="card-header">
  32. <h3>平台账号状态</h3>
  33. <el-button type="primary" link @click="handleNavigate('/accounts')">
  34. 管理账号
  35. </el-button>
  36. </div>
  37. <div class="account-list">
  38. <div v-if="accounts.length === 0" class="empty-state">
  39. <el-empty description="暂无账号" :image-size="80">
  40. <el-button type="primary" @click="handleNavigate('/accounts')">
  41. 添加账号
  42. </el-button>
  43. </el-empty>
  44. </div>
  45. <div v-else v-for="account in accounts" :key="account.id" class="account-item">
  46. <el-avatar :size="44" :src="account.avatarUrl || undefined">
  47. {{ account.accountName?.[0] }}
  48. </el-avatar>
  49. <div class="account-info">
  50. <div class="account-name">{{ account.accountName }}</div>
  51. <div class="account-platform">{{ getPlatformName(account.platform) }}</div>
  52. </div>
  53. <el-tag
  54. :type="account.status === 'active' ? 'success' : 'danger'"
  55. size="small"
  56. effect="light"
  57. round
  58. >
  59. {{ account.status === 'active' ? '正常' : '已过期' }}
  60. </el-tag>
  61. </div>
  62. </div>
  63. </div>
  64. <!-- 最近任务 -->
  65. <div class="content-card">
  66. <div class="card-header">
  67. <h3>最近发布任务</h3>
  68. <el-button type="primary" link @click="handleNavigate('/publish')">
  69. 查看全部
  70. </el-button>
  71. </div>
  72. <div class="task-list">
  73. <div v-if="tasks.length === 0" class="empty-state">
  74. <el-empty description="暂无任务" :image-size="80">
  75. <el-button type="primary" @click="handleNavigate('/publish')">
  76. 创建任务
  77. </el-button>
  78. </el-empty>
  79. </div>
  80. <div v-else v-for="task in tasks" :key="task.id" class="task-item">
  81. <div class="task-info">
  82. <div class="task-title">{{ task.title }}</div>
  83. <div class="task-time">{{ formatDate(task.createdAt) }}</div>
  84. </div>
  85. <el-tag
  86. :type="getTaskStatusType(task.status)"
  87. size="small"
  88. effect="light"
  89. round
  90. >
  91. {{ getTaskStatusText(task.status) }}
  92. </el-tag>
  93. </div>
  94. </div>
  95. </div>
  96. </div>
  97. <!-- 数据趋势图 -->
  98. <div class="content-card chart-card">
  99. <div class="card-header">
  100. <h3>数据趋势(近30天)</h3>
  101. <el-radio-group v-model="trendType" size="small">
  102. <el-radio-button label="fansIncrease">涨粉</el-radio-button>
  103. <el-radio-button label="views">播放</el-radio-button>
  104. <el-radio-button label="likes">点赞</el-radio-button>
  105. <el-radio-button label="comments">评论</el-radio-button>
  106. </el-radio-group>
  107. </div>
  108. <div class="chart-container">
  109. <div ref="chartRef" style="width: 100%; height: 280px"></div>
  110. </div>
  111. </div>
  112. </div>
  113. </template>
  114. <script setup lang="ts">
  115. import { ref, onMounted, onUnmounted, onActivated, watch, markRaw, nextTick } from 'vue';
  116. import { User, VideoPlay, UserFilled, TrendCharts, Refresh } from '@element-plus/icons-vue';
  117. import * as echarts from 'echarts';
  118. import { accountsApi } from '@/api/accounts';
  119. import { dashboardApi, type TrendData } from '@/api/dashboard';
  120. import request from '@/api/request';
  121. import { PLATFORMS } from '@media-manager/shared';
  122. import type { PlatformAccount, PublishTask, PlatformType } from '@media-manager/shared';
  123. import { useTabsStore } from '@/stores/tabs';
  124. import { useAuthStore } from '@/stores/auth';
  125. import dayjs from 'dayjs';
  126. const tabsStore = useTabsStore();
  127. const authStore = useAuthStore();
  128. const accounts = ref<PlatformAccount[]>([]);
  129. const tasks = ref<PublishTask[]>([]);
  130. const trendType = ref<'fansIncrease' | 'views' | 'likes' | 'comments'>('fansIncrease');
  131. const chartRef = ref<HTMLElement>();
  132. const trendData = ref<TrendData | null>(null);
  133. const refreshing = ref(false);
  134. let chartInstance: echarts.ECharts | null = null;
  135. let resizeObserver: ResizeObserver | null = null;
  136. const stats = ref([
  137. { label: '平台账号', value: 0, icon: markRaw(User), iconClass: 'blue' },
  138. { label: '总作品数', value: 0, icon: markRaw(VideoPlay), iconClass: 'green' },
  139. { label: '总粉丝数', value: '0', icon: markRaw(UserFilled), iconClass: 'orange' },
  140. { label: '总播放量', value: '0', icon: markRaw(TrendCharts), iconClass: 'pink' },
  141. ]);
  142. function handleNavigate(path: string) {
  143. tabsStore.openPageTab(path);
  144. }
  145. // 刷新数据
  146. async function handleRefresh() {
  147. refreshing.value = true;
  148. try {
  149. await loadData();
  150. } finally {
  151. refreshing.value = false;
  152. }
  153. }
  154. function getPlatformName(platform: PlatformType) {
  155. return PLATFORMS[platform]?.name || platform;
  156. }
  157. function getTaskStatusType(status: string) {
  158. const types: Record<string, string> = {
  159. pending: 'info',
  160. processing: 'warning',
  161. completed: 'success',
  162. failed: 'danger',
  163. cancelled: 'info',
  164. };
  165. return types[status] || 'info';
  166. }
  167. function getTaskStatusText(status: string) {
  168. const texts: Record<string, string> = {
  169. pending: '待发布',
  170. processing: '发布中',
  171. completed: '已完成',
  172. failed: '失败',
  173. cancelled: '已取消',
  174. };
  175. return texts[status] || status;
  176. }
  177. function formatDate(date: string) {
  178. return dayjs(date).format('YYYY-MM-DD HH:mm');
  179. }
  180. // 平台颜色配置(柔和协调的配色)
  181. const platformColors: Record<string, string> = {
  182. xiaohongshu: '#E91E63',
  183. douyin: '#374151',
  184. kuaishou: '#F59E0B',
  185. weixin: '#10B981',
  186. weixin_video: '#10B981',
  187. shipinhao: '#10B981',
  188. baijiahao: '#3B82F6',
  189. };
  190. // 获取图表标题
  191. function getChartTitle(type: 'fansIncrease' | 'views' | 'likes' | 'comments') {
  192. const titles: Record<string, string> = {
  193. fansIncrease: '涨粉数',
  194. views: '播放量',
  195. likes: '点赞数',
  196. comments: '评论数',
  197. };
  198. return titles[type] || '';
  199. }
  200. function initChart() {
  201. if (!chartRef.value) return;
  202. chartInstance = echarts.init(chartRef.value);
  203. updateChart();
  204. }
  205. function updateChart() {
  206. if (!chartInstance) return;
  207. const dates = trendData.value?.dates || [];
  208. const platforms = trendData.value?.platforms || [];
  209. // 生成每个平台的 series
  210. const series: echarts.SeriesOption[] = platforms.map((p) => {
  211. const data = p[trendType.value] || [];
  212. const color = platformColors[p.platform] || '#6B7280';
  213. return {
  214. name: p.platformName,
  215. data: data,
  216. type: 'line',
  217. smooth: 0.3,
  218. showSymbol: false,
  219. symbol: 'circle',
  220. symbolSize: 4,
  221. lineStyle: {
  222. width: 2.5,
  223. color: color,
  224. },
  225. itemStyle: {
  226. color: color,
  227. borderWidth: 2,
  228. borderColor: '#fff',
  229. },
  230. emphasis: {
  231. focus: 'series',
  232. showSymbol: true,
  233. symbolSize: 6,
  234. lineStyle: { width: 3 },
  235. itemStyle: { borderWidth: 2, borderColor: '#fff' },
  236. },
  237. };
  238. });
  239. const legendData = platforms.map(p => p.platformName);
  240. const option: echarts.EChartsOption = {
  241. tooltip: {
  242. trigger: 'axis',
  243. backgroundColor: 'rgba(255, 255, 255, 0.98)',
  244. borderColor: '#e5e7eb',
  245. borderWidth: 1,
  246. padding: [10, 14],
  247. textStyle: {
  248. color: '#374151',
  249. fontSize: 13,
  250. },
  251. formatter: (params: unknown) => {
  252. const p = params as { seriesName: string; name: string; value: number; color: string }[];
  253. if (!Array.isArray(p) || p.length === 0) return '';
  254. let html = `<div style="font-weight: 600; margin-bottom: 8px; font-size: 13px;">${p[0].name}</div>`;
  255. for (const item of p) {
  256. html += `<div style="display: flex; align-items: center; gap: 10px; margin-bottom: 4px;">
  257. <span style="display: inline-block; width: 8px; height: 8px; background: ${item.color}; border-radius: 2px;"></span>
  258. <span style="color: #6b7280;">${item.seriesName}</span>
  259. <span style="font-weight: 600; margin-left: auto;">${item.value.toLocaleString()}</span>
  260. </div>`;
  261. }
  262. return html;
  263. },
  264. axisPointer: {
  265. type: 'line',
  266. lineStyle: {
  267. color: '#9ca3af',
  268. type: 'dashed',
  269. width: 1,
  270. },
  271. },
  272. },
  273. legend: {
  274. data: legendData,
  275. bottom: 4,
  276. type: 'scroll',
  277. itemWidth: 14,
  278. itemHeight: 10,
  279. itemGap: 20,
  280. textStyle: {
  281. color: '#6b7280',
  282. fontSize: 12,
  283. },
  284. icon: 'roundRect',
  285. },
  286. grid: {
  287. left: '2%',
  288. right: '2%',
  289. top: '8%',
  290. bottom: '36px',
  291. containLabel: true,
  292. },
  293. xAxis: {
  294. type: 'category',
  295. data: dates,
  296. boundaryGap: false,
  297. axisLine: {
  298. lineStyle: { color: '#e5e7eb' },
  299. },
  300. axisTick: { show: false },
  301. axisLabel: {
  302. color: '#9ca3af',
  303. fontSize: 11,
  304. },
  305. },
  306. yAxis: {
  307. type: 'value',
  308. axisLine: { show: false },
  309. axisTick: { show: false },
  310. splitLine: {
  311. lineStyle: {
  312. color: '#f3f4f6',
  313. type: 'dashed',
  314. },
  315. },
  316. axisLabel: {
  317. color: '#9ca3af',
  318. fontSize: 11,
  319. formatter: (value: number) => {
  320. if (value >= 10000) return (value / 10000).toFixed(1) + 'w';
  321. return value.toString();
  322. },
  323. },
  324. },
  325. series: series,
  326. };
  327. chartInstance.setOption(option, true);
  328. }
  329. // 加载数据趋势
  330. async function loadTrendData() {
  331. try {
  332. trendData.value = await dashboardApi.getTrend({
  333. days: 30, // 获取最近30天的数据
  334. });
  335. // 更新图表
  336. updateChart();
  337. } catch (error) {
  338. console.error('加载数据趋势失败:', error);
  339. }
  340. }
  341. async function loadData() {
  342. try {
  343. // 并行获取所有数据
  344. const [accountsData, worksStats, tasksData] = await Promise.all([
  345. accountsApi.getAccounts(),
  346. dashboardApi.getWorksStats().catch(() => null),
  347. request.get('/api/publish', { params: { page: 1, pageSize: 5 } }).catch(() => null),
  348. ]);
  349. accounts.value = accountsData;
  350. // 加载最近发布任务
  351. if (tasksData) {
  352. tasks.value = tasksData.items || [];
  353. }
  354. // 更新统计数据
  355. stats.value[0].value = accounts.value.length;
  356. // 总粉丝数:当前用户所有平台账号的 fans_count 总和
  357. const totalFans = accounts.value.reduce((sum, a) => sum + (a.fansCount || 0), 0);
  358. stats.value[2].value = totalFans >= 10000
  359. ? (totalFans / 10000).toFixed(1) + '万'
  360. : totalFans.toString();
  361. if (worksStats) {
  362. stats.value[1].value = worksStats.totalCount || 0;
  363. // 格式化播放量
  364. const playCount = worksStats.totalPlayCount || 0;
  365. stats.value[3].value = playCount >= 10000
  366. ? (playCount / 10000).toFixed(1) + '万'
  367. : playCount.toString();
  368. }
  369. // 加载数据趋势
  370. await loadTrendData();
  371. } catch {
  372. // 错误已在拦截器中处理
  373. }
  374. }
  375. // resize 处理函数
  376. function handleResize() {
  377. if (chartInstance && !chartInstance.isDisposed()) {
  378. chartInstance.resize();
  379. }
  380. }
  381. onMounted(async () => {
  382. loadData();
  383. // 等待 DOM 完全渲染后再初始化图表
  384. await nextTick();
  385. // 延迟初始化以确保容器尺寸正确
  386. setTimeout(() => {
  387. initChart();
  388. // 使用 ResizeObserver 监听容器大小变化
  389. if (chartRef.value) {
  390. resizeObserver = new ResizeObserver(() => {
  391. handleResize();
  392. });
  393. resizeObserver.observe(chartRef.value);
  394. }
  395. }, 100);
  396. window.addEventListener('resize', handleResize);
  397. });
  398. // 页面激活时自动刷新数据(从其他标签页切换回来时)
  399. onActivated(() => {
  400. loadData();
  401. // 确保图表正确显示
  402. nextTick(() => {
  403. handleResize();
  404. });
  405. });
  406. onUnmounted(() => {
  407. window.removeEventListener('resize', handleResize);
  408. resizeObserver?.disconnect();
  409. resizeObserver = null;
  410. chartInstance?.dispose();
  411. chartInstance = null;
  412. });
  413. watch(trendType, () => {
  414. updateChart();
  415. });
  416. </script>
  417. <style lang="scss" scoped>
  418. @use '@/styles/variables.scss' as *;
  419. .dashboard {
  420. max-width: 1400px;
  421. margin: 0 auto;
  422. }
  423. .page-header {
  424. display: flex;
  425. align-items: center;
  426. justify-content: space-between;
  427. margin-bottom: 24px;
  428. h2 {
  429. margin: 0;
  430. font-size: 22px;
  431. font-weight: 600;
  432. color: $text-primary;
  433. }
  434. }
  435. // 统计卡片网格
  436. .stats-grid {
  437. display: grid;
  438. grid-template-columns: repeat(4, 1fr);
  439. gap: 20px;
  440. margin-bottom: 24px;
  441. }
  442. .stat-card {
  443. background: #fff;
  444. border-radius: $radius-lg;
  445. padding: 24px;
  446. display: flex;
  447. align-items: center;
  448. gap: 18px;
  449. box-shadow: $shadow-sm;
  450. border: 1px solid $border-light;
  451. transition: all 0.3s ease;
  452. &:hover {
  453. transform: translateY(-2px);
  454. box-shadow: $shadow-md;
  455. }
  456. .stat-icon {
  457. width: 56px;
  458. height: 56px;
  459. border-radius: $radius-lg;
  460. display: flex;
  461. align-items: center;
  462. justify-content: center;
  463. .el-icon {
  464. font-size: 26px;
  465. color: #fff;
  466. }
  467. &.blue {
  468. background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  469. }
  470. &.green {
  471. background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%);
  472. }
  473. &.orange {
  474. background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
  475. }
  476. &.pink {
  477. background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);
  478. }
  479. }
  480. .stat-content {
  481. flex: 1;
  482. }
  483. .stat-value {
  484. font-size: 32px;
  485. font-weight: 700;
  486. color: $text-primary;
  487. line-height: 1.2;
  488. }
  489. .stat-label {
  490. font-size: 14px;
  491. color: $text-secondary;
  492. margin-top: 6px;
  493. }
  494. }
  495. // 内容卡片网格
  496. .content-grid {
  497. display: grid;
  498. grid-template-columns: repeat(2, 1fr);
  499. gap: 20px;
  500. margin-bottom: 24px;
  501. }
  502. .content-card {
  503. background: #fff;
  504. border-radius: $radius-lg;
  505. padding: 24px;
  506. box-shadow: $shadow-sm;
  507. border: 1px solid $border-light;
  508. &.chart-card {
  509. width: 100%;
  510. overflow: hidden;
  511. .card-header {
  512. margin-bottom: 20px;
  513. }
  514. }
  515. }
  516. .card-header {
  517. display: flex;
  518. align-items: center;
  519. justify-content: space-between;
  520. margin-bottom: 20px;
  521. h3 {
  522. margin: 0;
  523. font-size: 17px;
  524. font-weight: 600;
  525. color: $text-primary;
  526. }
  527. .el-button {
  528. font-weight: 500;
  529. }
  530. :deep(.el-radio-group) {
  531. .el-radio-button__inner {
  532. border-radius: $radius-sm;
  533. border-color: $border-light;
  534. color: $text-secondary;
  535. font-weight: 500;
  536. }
  537. .el-radio-button__original-radio:checked + .el-radio-button__inner {
  538. background: $primary-color;
  539. border-color: $primary-color;
  540. }
  541. }
  542. }
  543. .account-list, .task-list {
  544. min-height: 200px;
  545. }
  546. .account-item, .task-item {
  547. display: flex;
  548. align-items: center;
  549. padding: 14px 0;
  550. border-bottom: 1px solid $border-light;
  551. &:last-child {
  552. border-bottom: none;
  553. padding-bottom: 0;
  554. }
  555. &:first-child {
  556. padding-top: 0;
  557. }
  558. }
  559. .account-item {
  560. gap: 14px;
  561. :deep(.el-avatar) {
  562. flex-shrink: 0;
  563. border: 2px solid $border-light;
  564. background: linear-gradient(135deg, $primary-color-light, #fff);
  565. color: $primary-color;
  566. font-weight: 600;
  567. }
  568. .account-info {
  569. flex: 1;
  570. min-width: 0;
  571. .account-name {
  572. font-weight: 600;
  573. color: $text-primary;
  574. font-size: 15px;
  575. white-space: nowrap;
  576. overflow: hidden;
  577. text-overflow: ellipsis;
  578. }
  579. .account-platform {
  580. font-size: 13px;
  581. color: $text-secondary;
  582. margin-top: 4px;
  583. }
  584. }
  585. }
  586. .task-item {
  587. justify-content: space-between;
  588. .task-info {
  589. flex: 1;
  590. min-width: 0;
  591. }
  592. .task-title {
  593. font-weight: 600;
  594. color: $text-primary;
  595. font-size: 15px;
  596. white-space: nowrap;
  597. overflow: hidden;
  598. text-overflow: ellipsis;
  599. }
  600. .task-time {
  601. font-size: 13px;
  602. color: $text-secondary;
  603. margin-top: 4px;
  604. }
  605. }
  606. .empty-state {
  607. display: flex;
  608. align-items: center;
  609. justify-content: center;
  610. min-height: 200px;
  611. :deep(.el-empty__description p) {
  612. color: $text-secondary;
  613. }
  614. }
  615. .chart-container {
  616. margin-top: 10px;
  617. width: 100%;
  618. min-height: 280px;
  619. > div {
  620. width: 100% !important;
  621. }
  622. }
  623. // 响应式调整
  624. @media (max-width: 1200px) {
  625. .stats-grid {
  626. grid-template-columns: repeat(2, 1fr);
  627. }
  628. .content-grid {
  629. grid-template-columns: 1fr;
  630. }
  631. }
  632. @media (max-width: 768px) {
  633. .stats-grid {
  634. grid-template-columns: 1fr;
  635. }
  636. }
  637. </style>