dashboard.component.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. import { Component, OnInit } from '@angular/core';
  2. import { CommonModule, NgFor, DatePipe } from '@angular/common';
  3. import { debounceTime } from 'rxjs/operators';
  4. import { DashboardService } from '../services/dashboard.service';
  5. // NG-ZORRO 组件
  6. import { NzCardModule } from 'ng-zorro-antd/card';
  7. import { NzIconModule } from 'ng-zorro-antd/icon';
  8. import { NzGridModule } from 'ng-zorro-antd/grid';
  9. import { NzStatisticModule } from 'ng-zorro-antd/statistic';
  10. import { NzSpinModule } from 'ng-zorro-antd/spin';
  11. import { NzTableModule } from 'ng-zorro-antd/table';
  12. import { NzDividerModule } from 'ng-zorro-antd/divider';
  13. import { NzProgressModule } from 'ng-zorro-antd/progress';
  14. import { NzPageHeaderModule } from 'ng-zorro-antd/page-header';
  15. import { NzTagModule } from 'ng-zorro-antd/tag';
  16. import { NzToolTipModule } from 'ng-zorro-antd/tooltip';
  17. import { NzModalService } from 'ng-zorro-antd/modal';
  18. import { NzMessageService } from 'ng-zorro-antd/message';
  19. @Component({
  20. selector: 'app-dashboard',
  21. standalone: true,
  22. imports: [
  23. CommonModule,
  24. NgFor,
  25. DatePipe,
  26. // NG-ZORRO 模块
  27. NzCardModule,
  28. NzIconModule,
  29. NzGridModule,
  30. NzStatisticModule,
  31. NzSpinModule,
  32. NzTableModule,
  33. NzDividerModule,
  34. NzProgressModule,
  35. NzPageHeaderModule,
  36. NzTagModule,
  37. NzToolTipModule,
  38. ],
  39. providers: [NzModalService, NzMessageService],
  40. template: `
  41. <div class="dashboard-container">
  42. <nz-page-header [nzGhost]="false">
  43. <nz-page-header-title>数据看板</nz-page-header-title>
  44. <nz-page-header-extra>
  45. <span
  46. nz-icon
  47. nzType="sync"
  48. nzTheme="outline"
  49. (click)="refreshData()"
  50. ></span>
  51. </nz-page-header-extra>
  52. <nz-page-header-content>
  53. <p>
  54. 最后更新时间:{{ lastUpdateTime | date : 'yyyy-MM-dd HH:mm:ss' }}
  55. </p>
  56. </nz-page-header-content>
  57. </nz-page-header>
  58. <nz-spin [nzSpinning]="isLoading" nzTip="数据加载中...">
  59. <!-- 核心指标卡片 -->
  60. <div class="metrics-container">
  61. <nz-row [nzGutter]="16">
  62. <!-- 日活用户数 -->
  63. <nz-col [nzXs]="24" [nzSm]="12" [nzMd]="8" [nzLg]="6">
  64. <nz-card nzHoverable>
  65. <nz-statistic
  66. [nzTitle]="'日活用户(DAU)'"
  67. [nzValue]="activeUsersToday"
  68. [nzPrefix]="userIcon"
  69. [nzSuffix]="'人'"
  70. [nzValueStyle]="{ color: '#1890ff' }"
  71. ></nz-statistic>
  72. <ng-template #userIcon>
  73. <span nz-icon nzType="user" nzTheme="outline"></span>
  74. </ng-template>
  75. <div class="compare">
  76. <span>实时数据自动更新</span>
  77. </div>
  78. </nz-card>
  79. </nz-col>
  80. <!-- 日均广告收益 -->
  81. <nz-col [nzXs]="24" [nzSm]="12" [nzMd]="8" [nzLg]="6">
  82. <nz-card nzHoverable>
  83. <nz-statistic
  84. [nzTitle]="'日广告收益'"
  85. [nzValue]="dailyRevenue"
  86. [nzPrefix]="revenueIcon"
  87. [nzSuffix]="'元'"
  88. [nzValueStyle]="{ color: '#faad14' }"
  89. ></nz-statistic>
  90. <ng-template #revenueIcon>
  91. <span nz-icon nzType="dollar" nzTheme="outline"></span>
  92. </ng-template>
  93. <div class="compare">
  94. <span>来自广告平台</span>
  95. </div>
  96. </nz-card>
  97. </nz-col>
  98. <!-- DAU 趋势占位 -->
  99. <nz-col [nzXs]="24" [nzSm]="12" [nzMd]="8" [nzLg]="6">
  100. <nz-card nzHoverable>
  101. <nz-statistic
  102. [nzTitle]="'7日平均DAU'"
  103. [nzValue]="avgDau7d"
  104. [nzPrefix]="trendIcon"
  105. [nzSuffix]="'人'"
  106. [nzValueStyle]="{ color: '#13c2c2' }"
  107. ></nz-statistic>
  108. <ng-template #trendIcon>
  109. <span nz-icon nzType="line-chart" nzTheme="outline"></span>
  110. </ng-template>
  111. <div class="compare">
  112. <span>近 7 天平均值</span>
  113. </div>
  114. </nz-card>
  115. </nz-col>
  116. <!-- 收益趋势占位 -->
  117. <nz-col [nzXs]="24" [nzSm]="12" [nzMd]="8" [nzLg]="6">
  118. <nz-card nzHoverable>
  119. <nz-statistic
  120. [nzTitle]="'7日收益总额'"
  121. [nzValue]="totalRevenue7d"
  122. [nzPrefix]="totalRevenueIcon"
  123. [nzSuffix]="'元'"
  124. [nzValueStyle]="{ color: '#eb2f96' }"
  125. ></nz-statistic>
  126. <ng-template #totalRevenueIcon>
  127. <span nz-icon nzType="bar-chart" nzTheme="outline"></span>
  128. </ng-template>
  129. <div class="compare">
  130. <span>近 7 天累计</span>
  131. </div>
  132. </nz-card>
  133. </nz-col>
  134. </nz-row>
  135. </div>
  136. <!-- 广告收益部分 -->
  137. <div class="section">
  138. <h3 class="section-title">今日广告收益</h3>
  139. <nz-row [nzGutter]="16">
  140. <nz-col [nzXs]="24" [nzSm]="12" [nzMd]="8">
  141. <nz-card nzHoverable>
  142. <nz-statistic
  143. [nzTitle]="'Banner广告收益'"
  144. [nzValue]="bannerRevenue"
  145. [nzPrefix]="bannerIcon"
  146. [nzSuffix]="'元'"
  147. [nzValueStyle]="{ color: '#fa8c16' }"
  148. ></nz-statistic>
  149. <ng-template #bannerIcon>
  150. <span nz-icon nzType="picture" nzTheme="outline"></span>
  151. </ng-template>
  152. <div class="compare">
  153. <span>展示量 {{ bannerImpressions }} 次</span>
  154. </div>
  155. </nz-card>
  156. </nz-col>
  157. <nz-col [nzXs]="24" [nzSm]="12" [nzMd]="8">
  158. <nz-card nzHoverable>
  159. <nz-statistic
  160. [nzTitle]="'插屏广告收益'"
  161. [nzValue]="interstitialRevenue"
  162. [nzPrefix]="interstitialIcon"
  163. [nzSuffix]="'元'"
  164. [nzValueStyle]="{ color: '#fa541c' }"
  165. ></nz-statistic>
  166. <ng-template #interstitialIcon>
  167. <span nz-icon nzType="switcher" nzTheme="outline"></span>
  168. </ng-template>
  169. <div class="compare">
  170. <span>展示量 {{ interstitialImpressions }} 次</span>
  171. </div>
  172. </nz-card>
  173. </nz-col>
  174. <nz-col [nzXs]="24" [nzSm]="12" [nzMd]="8">
  175. <nz-card nzHoverable>
  176. <nz-statistic
  177. [nzTitle]="'激励广告收益'"
  178. [nzValue]="rewardedRevenue"
  179. [nzPrefix]="rewardedIcon"
  180. [nzSuffix]="'元'"
  181. [nzValueStyle]="{ color: '#eb2f96' }"
  182. ></nz-statistic>
  183. <ng-template #rewardedIcon>
  184. <span nz-icon nzType="gift" nzTheme="outline"></span>
  185. </ng-template>
  186. <div class="compare">
  187. <span>展示量 {{ rewardedImpressions }} 次</span>
  188. </div>
  189. </nz-card>
  190. </nz-col>
  191. </nz-row>
  192. </div>
  193. <!-- 今日上新作品 -->
  194. <div class="section">
  195. <h3 class="section-title">今日上新作品</h3>
  196. <nz-table
  197. #worksTable
  198. [nzData]="newWorksToday"
  199. [nzLoading]="isLoading"
  200. [nzFrontPagination]="false"
  201. [nzBordered]="true"
  202. [nzSize]="'small'"
  203. >
  204. <thead>
  205. <tr>
  206. <th>作品</th>
  207. <th>名称</th>
  208. <th>填色开始数</th>
  209. <th>填色完成数</th>
  210. <th>完成率</th>
  211. <th>操作</th>
  212. </tr>
  213. </thead>
  214. <tbody>
  215. <tr *ngFor="let work of worksTable.data">
  216. <td>
  217. <div
  218. class="work-thumbnail"
  219. [style.backgroundImage]="'url(' + work.thumbnail + ')'"
  220. ></div>
  221. </td>
  222. <td>{{ work.name }}</td>
  223. <td>{{ work.startedCount }}</td>
  224. <td>{{ work.completedCount }}</td>
  225. <td>
  226. <nz-progress
  227. [nzPercent]="work.completionRate"
  228. [nzStrokeColor]="
  229. work.completionRate > 70
  230. ? '#52c41a'
  231. : work.completionRate > 30
  232. ? '#faad14'
  233. : '#f5222d'
  234. "
  235. [nzShowInfo]="true"
  236. [nzStrokeWidth]="5"
  237. ></nz-progress>
  238. </td>
  239. <td>
  240. <a (click)="showWorkDetails(work)">详情</a>
  241. </td>
  242. </tr>
  243. </tbody>
  244. </nz-table>
  245. </div>
  246. </nz-spin>
  247. </div>
  248. `,
  249. styles: [
  250. `
  251. .dashboard-container {
  252. padding: 24px;
  253. background: #f0f2f5;
  254. min-height: 100%;
  255. }
  256. .metrics-container {
  257. margin-bottom: 24px;
  258. }
  259. .section {
  260. background: #fff;
  261. padding: 16px 24px;
  262. border-radius: 8px;
  263. margin-bottom: 24px;
  264. box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.03);
  265. }
  266. .section-title {
  267. margin-bottom: 16px;
  268. font-size: 18px;
  269. color: rgba(0, 0, 0, 0.85);
  270. }
  271. nz-card {
  272. margin-bottom: 16px;
  273. transition: all 0.3s;
  274. }
  275. nz-card:hover {
  276. box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  277. transform: translateY(-4px);
  278. }
  279. .compare {
  280. margin-top: 8px;
  281. font-size: 12px;
  282. color: rgba(0, 0, 0, 0.45);
  283. }
  284. .work-thumbnail {
  285. width: 60px;
  286. height: 60px;
  287. background-size: cover;
  288. background-position: center;
  289. border-radius: 4px;
  290. }
  291. .ant-statistic-content {
  292. display: flex;
  293. flex-direction: column;
  294. align-items: center;
  295. }
  296. .ant-statistic-title {
  297. font-size: 14px;
  298. color: rgba(0, 0, 0, 0.65);
  299. }
  300. .ant-statistic-content-value {
  301. font-size: 28px;
  302. }
  303. .ant-statistic-content-suffix {
  304. font-size: 16px;
  305. }
  306. `,
  307. ],
  308. })
  309. export class DashboardComponent implements OnInit {
  310. isLoading = true;
  311. lastUpdateTime = new Date();
  312. Math = Math; // 暴露 Math 对象给模板使antml
  313. // 核心指标数据(来自 KPI 接口)
  314. activeUsersToday = 0; // 日活用户数
  315. dailyRevenue = 0; // 当日广告收益
  316. // DAU 趋势数据
  317. dauTrendLabels: string[] = [];
  318. dauTrendData: number[] = [];
  319. avgDau7d = 0;
  320. // 收益趋势数据
  321. revenueTrendLabels: string[] = [];
  322. revenueTrendData: number[] = [];
  323. totalRevenue7d = 0;
  324. // 广告收益数据(暂时保留,后续可删除)
  325. bannerRevenue = 0;
  326. interstitialRevenue = 0;
  327. rewardedRevenue = 0;
  328. bannerImpressions = 0;
  329. interstitialImpressions = 0;
  330. rewardedImpressions = 0;
  331. // 今日上新作品
  332. newWorksToday: any[] = [];
  333. constructor(
  334. private modalService: NzModalService,
  335. private message: NzMessageService,
  336. private dashboardService: DashboardService
  337. ) {}
  338. ngOnInit(): void {
  339. this.loadDashboardData();
  340. }
  341. refreshData(): void {
  342. this.isLoading = true;
  343. this.loadDashboardData();
  344. }
  345. loadDashboardData(): void {
  346. this.dashboardService.getKpi(7).subscribe({
  347. next: (response) => {
  348. if (response.success) {
  349. const { dau, revenue } = response.data;
  350. // 更新 KPI 数据
  351. this.activeUsersToday = dau.today;
  352. this.dailyRevenue = revenue.today;
  353. // 更新 DAU 趋势
  354. this.dauTrendLabels = dau.trend.map((item) => item.date);
  355. this.dauTrendData = dau.trend.map((item) => item.dau);
  356. const dauSum = this.dauTrendData.reduce((sum, value) => sum + value, 0);
  357. this.avgDau7d = this.dauTrendData.length > 0 ? Math.round(dauSum / this.dauTrendData.length) : 0;
  358. // 更新收益趋势
  359. this.revenueTrendLabels = revenue.trend.map((item) => item.date);
  360. this.revenueTrendData = revenue.trend.map((item) => item.revenue);
  361. const revenueSum = this.revenueTrendData.reduce((sum, value) => sum + value, 0);
  362. this.totalRevenue7d = Math.round(revenueSum);
  363. // 模拟广告收益细分(后续可从其他接口获取)
  364. this.bannerRevenue = revenue.today * 0.35;
  365. this.interstitialRevenue = revenue.today * 0.40;
  366. this.rewardedRevenue = revenue.today * 0.25;
  367. this.bannerImpressions = Math.floor(Math.random() * 30000);
  368. this.interstitialImpressions = Math.floor(Math.random() * 10000);
  369. this.rewardedImpressions = Math.floor(Math.random() * 8000);
  370. // 模拟上新作品数据(后续可从接口获取)
  371. this.newWorksToday = [
  372. {
  373. id: 1,
  374. name: '作品 #1',
  375. thumbnail: 'https://via.placeholder.com/60',
  376. startedCount: Math.floor(Math.random() * 2000),
  377. completedCount: Math.floor(Math.random() * 1000),
  378. },
  379. {
  380. id: 2,
  381. name: '作品 #2',
  382. thumbnail: 'https://via.placeholder.com/60',
  383. startedCount: Math.floor(Math.random() * 2000),
  384. completedCount: Math.floor(Math.random() * 1000),
  385. },
  386. {
  387. id: 3,
  388. name: '作品 #3',
  389. thumbnail: 'https://via.placeholder.com/60',
  390. startedCount: Math.floor(Math.random() * 2000),
  391. completedCount: Math.floor(Math.random() * 1000),
  392. },
  393. ];
  394. this.newWorksToday = this.newWorksToday.map((work) => ({
  395. ...work,
  396. completionRate: work.startedCount > 0
  397. ? Math.round((work.completedCount / work.startedCount) * 100)
  398. : 0,
  399. }));
  400. this.lastUpdateTime = new Date();
  401. this.isLoading = false;
  402. } else {
  403. this.message.error('获取 KPI 数据失败');
  404. this.isLoading = false;
  405. }
  406. },
  407. error: (error) => {
  408. console.error('Failed to load KPI data:', error);
  409. this.message.error('加载数据出错,请重试');
  410. this.isLoading = false;
  411. },
  412. });
  413. }
  414. showWorkDetails(work: any): void {
  415. this.modalService.create({
  416. nzTitle: '作品详情',
  417. nzContent: `
  418. <div style="padding: 16px;">
  419. <div style="text-align: center; margin-bottom: 16px;">
  420. <img src="${work.thumbnail}" style="max-width: 100%; max-height: 300px; border-radius: 4px;">
  421. </div>
  422. <p><strong>作品名称:</strong> ${work.name}</p>
  423. <p><strong>填色开始数:</strong> ${work.startedCount}</p>
  424. <p><strong>填色完成数:</strong> ${work.completedCount}</p>
  425. <p><strong>完成率:</strong> ${work.completionRate}%</p>
  426. </div>
  427. `,
  428. nzFooter: null,
  429. });
  430. }
  431. }