completion-rate.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. <template>
  2. <view class="container">
  3. <!-- 顶部导航 -->
  4. <view class="header">
  5. <view class="header-left" @click="goBack">
  6. <text class="back-arrow">‹</text>
  7. </view>
  8. <text class="header-title">任务完成率分析</text>
  9. <view class="header-right" @click="togglePeriod">
  10. <text class="period-btn">{{ periodLabel }}</text>
  11. </view>
  12. </view>
  13. <view class="scroll-area">
  14. <!-- 加载状态 -->
  15. <view class="loading-state" v-if="loading">
  16. <view class="loading-spinner"></view>
  17. <text class="loading-text">正在分析数据...</text>
  18. </view>
  19. <template v-if="!loading">
  20. <!-- 整体完成率 (环形图) -->
  21. <view class="rate-card">
  22. <view class="rate-ring-wrapper">
  23. <view class="rate-ring">
  24. <svg viewBox="0 0 120 120" class="ring-svg">
  25. <circle cx="60" cy="60" r="52" fill="none" stroke="#FFF0E6" stroke-width="8" />
  26. <circle cx="60" cy="60" r="52" fill="none" stroke="url(#rateGradient)" stroke-width="8"
  27. stroke-linecap="round"
  28. :stroke-dasharray="circumference"
  29. :stroke-dashoffset="rateOffset" transform="rotate(-90 60 60)" />
  30. <defs>
  31. <linearGradient id="rateGradient" x1="0%" y1="0%" x2="100%" y2="100%">
  32. <stop offset="0%" stop-color="#4ECDC4" />
  33. <stop offset="100%" stop-color="#44B09E" />
  34. </linearGradient>
  35. </defs>
  36. </svg>
  37. <view class="rate-ring-text">
  38. <text class="rate-value">{{ stats.completionRate }}%</text>
  39. <text class="rate-label">完成率</text>
  40. </view>
  41. </view>
  42. </view>
  43. <view class="rate-legend">
  44. <view class="legend-item">
  45. <view class="legend-dot" style="background: #4ECDC4;"></view>
  46. <text class="legend-label">已完成 {{ stats.completedTasks }}</text>
  47. </view>
  48. <view class="legend-item">
  49. <view class="legend-dot" style="background: #FFB088;"></view>
  50. <text class="legend-label">进行中 {{ stats.pendingTasks }}</text>
  51. </view>
  52. <view class="legend-item">
  53. <view class="legend-dot" style="background: #F97316;"></view>
  54. <text class="legend-label">已逾期 {{ stats.overdueTasks }}</text>
  55. </view>
  56. </view>
  57. </view>
  58. <!-- 快捷统计卡片 -->
  59. <view class="quick-stats">
  60. <view class="quick-stat-card">
  61. <text class="quick-stat-value primary-color">{{ stats.totalTasks }}</text>
  62. <text class="quick-stat-label">总任务数</text>
  63. </view>
  64. <view class="quick-stat-card">
  65. <text class="quick-stat-value success-color">{{ stats.completedTasks }}</text>
  66. <text class="quick-stat-label">已完成</text>
  67. </view>
  68. <view class="quick-stat-card">
  69. <text class="quick-stat-value danger-color">{{ stats.overdueTasks }}</text>
  70. <text class="quick-stat-label">已逾期</text>
  71. </view>
  72. </view>
  73. <!-- 逾期任务警示 -->
  74. <view class="alert-card" v-if="stats.overdueTasks > 0">
  75. <text class="alert-icon">⚠️</text>
  76. <view class="alert-body">
  77. <text class="alert-title">有 {{ stats.overdueTasks }} 个任务已逾期</text>
  78. <text class="alert-desc">请督促孩子尽快完成,避免影响成长积分</text>
  79. </view>
  80. </view>
  81. <!-- 按孩子对比 (仅家庭维度) -->
  82. <view class="section-block" v-if="stats.byChild && stats.byChild.length > 1">
  83. <view class="section-header">
  84. <text class="section-title">孩子对比</text>
  85. </view>
  86. <view class="child-compare">
  87. <view class="compare-bar-wrapper" v-for="child in stats.byChild" :key="child.childId">
  88. <view class="compare-row">
  89. <text class="compare-name">{{ child.childName }}</text>
  90. <text class="compare-rate">{{ child.completionRate }}%</text>
  91. </view>
  92. <view class="compare-bar-bg">
  93. <view class="compare-bar-fill" :style="{ width: child.completionRate + '%' }"
  94. :class="'bar-color-' + (child.childId % 5)"></view>
  95. </view>
  96. </view>
  97. </view>
  98. </view>
  99. <!-- 分类完成情况 -->
  100. <view class="section-block" v-if="stats.categoryBreakdown && stats.categoryBreakdown.length > 0">
  101. <view class="section-header">
  102. <text class="section-title">分类详情</text>
  103. </view>
  104. <view class="category-list">
  105. <view class="category-item" v-for="cat in stats.categoryBreakdown" :key="cat.category">
  106. <view class="category-header">
  107. <text class="category-name">{{ cat.category }}</text>
  108. <text class="category-count">{{ cat.completed }}/{{ cat.total }}</text>
  109. </view>
  110. <view class="category-bar-bg">
  111. <view class="category-bar-fill" :style="{ width: cat.rate + '%' }"></view>
  112. </view>
  113. <text class="category-rate">{{ cat.rate }}%</text>
  114. </view>
  115. </view>
  116. </view>
  117. <!-- 无数据占位 -->
  118. <view class="empty-section" v-if="stats.totalTasks === 0">
  119. <view class="empty-icon-area">
  120. <text class="empty-main-icon">📊</text>
  121. </view>
  122. <text class="empty-title">暂无任务数据</text>
  123. <text class="empty-hint">创建任务后将在这里展示完成率分析</text>
  124. </view>
  125. </template>
  126. </view>
  127. </view>
  128. </template>
  129. <script>
  130. import { getChildCompletionStats, getFamilyCompletionStats, getChildren } from '../../utils/api.js'
  131. export default {
  132. data() {
  133. return {
  134. loading: true,
  135. childId: '',
  136. isFamilyView: false,
  137. periodIndex: 0,
  138. periods: [
  139. { label: '本周', days: 7 },
  140. { label: '本月', days: 30 },
  141. { label: '近三月', days: 90 }
  142. ],
  143. stats: {
  144. totalTasks: 0,
  145. completedTasks: 0,
  146. pendingTasks: 0,
  147. overdueTasks: 0,
  148. completionRate: 0,
  149. byChild: [],
  150. categoryBreakdown: []
  151. }
  152. }
  153. },
  154. computed: {
  155. periodLabel() {
  156. return this.periods[this.periodIndex].label
  157. },
  158. circumference() {
  159. return 2 * Math.PI * 52
  160. },
  161. rateOffset() {
  162. const pct = this.stats.completionRate || 0
  163. return this.circumference * (1 - pct / 100)
  164. }
  165. },
  166. onLoad(options) {
  167. if (options.childId) {
  168. this.childId = options.childId
  169. this.isFamilyView = false
  170. } else if (options.familyView === '1') {
  171. this.isFamilyView = true
  172. }
  173. this.loadStats()
  174. },
  175. methods: {
  176. async loadStats() {
  177. this.loading = true
  178. try {
  179. const period = this.periods[this.periodIndex]
  180. const endDate = this.formatDate(new Date())
  181. const startDate = this.formatDate(new Date(Date.now() - period.days * 24 * 60 * 60 * 1000))
  182. const params = { startDate, endDate }
  183. if (this.childId) {
  184. params.childId = this.childId
  185. }
  186. let res
  187. if (this.isFamilyView || !this.childId) {
  188. res = await getFamilyCompletionStats(params)
  189. } else {
  190. res = await getChildCompletionStats(params)
  191. }
  192. if (res && res.data) {
  193. // 兼容后端可能返回 byChild 或 categoryBreakdown
  194. const data = res.data
  195. this.stats = {
  196. totalTasks: data.totalTasks || 0,
  197. completedTasks: data.completedTasks || 0,
  198. pendingTasks: data.pendingTasks || 0,
  199. overdueTasks: data.overdueTasks || 0,
  200. completionRate: data.completionRate || 0,
  201. byChild: data.byChild || [],
  202. categoryBreakdown: data.categoryBreakdown || []
  203. }
  204. }
  205. } catch (e) {
  206. console.error('加载完成率失败', e)
  207. uni.showToast({ title: '数据加载失败', icon: 'none' })
  208. } finally {
  209. this.loading = false
  210. }
  211. },
  212. togglePeriod() {
  213. this.periodIndex = (this.periodIndex + 1) % this.periods.length
  214. this.loadStats()
  215. },
  216. goBack() {
  217. uni.navigateBack()
  218. },
  219. formatDate(date) {
  220. const y = date.getFullYear()
  221. const m = String(date.getMonth() + 1).padStart(2, '0')
  222. const d = String(date.getDate()).padStart(2, '0')
  223. return y + '-' + m + '-' + d
  224. }
  225. }
  226. }
  227. </script>
  228. <style scoped>
  229. .container {
  230. background: #FFF9F0;
  231. min-height: 100vh;
  232. display: flex;
  233. flex-direction: column;
  234. }
  235. /* 顶部导航 */
  236. .header {
  237. display: flex;
  238. align-items: center;
  239. justify-content: space-between;
  240. padding: 24rpx 32rpx;
  241. padding-top: calc(88rpx + env(safe-area-inset-top));
  242. background: #FFF9F0;
  243. position: sticky;
  244. top: 0;
  245. z-index: 10;
  246. }
  247. .header-left, .header-right {
  248. min-width: 120rpx;
  249. }
  250. .header-right {
  251. display: flex;
  252. justify-content: flex-end;
  253. }
  254. .back-arrow {
  255. font-size: 48rpx;
  256. color: #2D3436;
  257. font-weight: 300;
  258. line-height: 1;
  259. }
  260. .header-title {
  261. font-size: 34rpx;
  262. font-weight: 700;
  263. color: #2D3436;
  264. }
  265. .period-btn {
  266. font-size: 26rpx;
  267. color: #FF8C42;
  268. background: #FFF5EB;
  269. padding: 8rpx 20rpx;
  270. border-radius: 24rpx;
  271. font-weight: 500;
  272. }
  273. /* 滚动区 */
  274. .scroll-area {
  275. flex: 1;
  276. padding: 0 32rpx 48rpx;
  277. overflow-y: auto;
  278. }
  279. /* 加载状态 */
  280. .loading-state {
  281. display: flex;
  282. flex-direction: column;
  283. align-items: center;
  284. padding: 160rpx 0;
  285. }
  286. .loading-spinner {
  287. width: 64rpx;
  288. height: 64rpx;
  289. border: 6rpx solid #FFF0E6;
  290. border-top-color: #FF8C42;
  291. border-radius: 50%;
  292. animation: spin 0.8s linear infinite;
  293. }
  294. @keyframes spin {
  295. to { transform: rotate(360deg); }
  296. }
  297. .loading-text {
  298. font-size: 26rpx;
  299. color: #B2BEC3;
  300. margin-top: 20rpx;
  301. }
  302. /* 整体完成率卡片 */
  303. .rate-card {
  304. background: #FFFFFF;
  305. border-radius: 28rpx;
  306. padding: 40rpx 32rpx;
  307. margin-bottom: 24rpx;
  308. display: flex;
  309. align-items: center;
  310. box-shadow: 0 4px 20px rgba(255, 140, 66, 0.08);
  311. }
  312. .rate-ring-wrapper {
  313. flex-shrink: 0;
  314. margin-right: 32rpx;
  315. }
  316. .rate-ring {
  317. width: 200rpx;
  318. height: 200rpx;
  319. position: relative;
  320. }
  321. .ring-svg {
  322. width: 100%;
  323. height: 100%;
  324. }
  325. .rate-ring-text {
  326. position: absolute;
  327. top: 50%;
  328. left: 50%;
  329. transform: translate(-50%, -50%);
  330. display: flex;
  331. flex-direction: column;
  332. align-items: center;
  333. }
  334. .rate-value {
  335. font-size: 40rpx;
  336. font-weight: 800;
  337. color: #2D3436;
  338. line-height: 1.2;
  339. }
  340. .rate-label {
  341. font-size: 22rpx;
  342. color: #B2BEC3;
  343. margin-top: 4rpx;
  344. }
  345. .rate-legend {
  346. flex: 1;
  347. display: flex;
  348. flex-direction: column;
  349. gap: 16rpx;
  350. }
  351. .legend-item {
  352. display: flex;
  353. align-items: center;
  354. }
  355. .legend-dot {
  356. width: 16rpx;
  357. height: 16rpx;
  358. border-radius: 50%;
  359. margin-right: 12rpx;
  360. flex-shrink: 0;
  361. }
  362. .legend-label {
  363. font-size: 26rpx;
  364. color: #636E72;
  365. }
  366. /* 快捷统计卡片 */
  367. .quick-stats {
  368. display: flex;
  369. gap: 16rpx;
  370. margin-bottom: 24rpx;
  371. }
  372. .quick-stat-card {
  373. flex: 1;
  374. background: #FFFFFF;
  375. border-radius: 20rpx;
  376. padding: 24rpx 16rpx;
  377. display: flex;
  378. flex-direction: column;
  379. align-items: center;
  380. box-shadow: 0 4px 20px rgba(255, 140, 66, 0.08);
  381. }
  382. .quick-stat-value {
  383. font-size: 40rpx;
  384. font-weight: 800;
  385. line-height: 1.2;
  386. }
  387. .quick-stat-label {
  388. font-size: 22rpx;
  389. color: #B2BEC3;
  390. margin-top: 8rpx;
  391. }
  392. .primary-color { color: #FF8C42; }
  393. .success-color { color: #4ECDC4; }
  394. .danger-color { color: #F97316; }
  395. /* 逾期警示 */
  396. .alert-card {
  397. background: #FFF5F5;
  398. border-radius: 20rpx;
  399. padding: 24rpx;
  400. margin-bottom: 24rpx;
  401. display: flex;
  402. align-items: flex-start;
  403. border: 1rpx solid #FFE0E0;
  404. }
  405. .alert-icon {
  406. font-size: 40rpx;
  407. margin-right: 16rpx;
  408. flex-shrink: 0;
  409. }
  410. .alert-body {
  411. flex: 1;
  412. }
  413. .alert-title {
  414. font-size: 28rpx;
  415. font-weight: 600;
  416. color: #E74C3C;
  417. display: block;
  418. }
  419. .alert-desc {
  420. font-size: 24rpx;
  421. color: #E17055;
  422. margin-top: 6rpx;
  423. display: block;
  424. }
  425. /* 区块 */
  426. .section-block {
  427. background: #FFFFFF;
  428. border-radius: 24rpx;
  429. padding: 28rpx;
  430. margin-bottom: 24rpx;
  431. box-shadow: 0 4px 20px rgba(255, 140, 66, 0.08);
  432. }
  433. .section-header {
  434. display: flex;
  435. justify-content: space-between;
  436. align-items: center;
  437. margin-bottom: 24rpx;
  438. }
  439. .section-title {
  440. font-size: 30rpx;
  441. font-weight: 700;
  442. color: #2D3436;
  443. }
  444. /* 孩子对比 */
  445. .child-compare {
  446. display: flex;
  447. flex-direction: column;
  448. gap: 20rpx;
  449. }
  450. .compare-row {
  451. display: flex;
  452. justify-content: space-between;
  453. margin-bottom: 8rpx;
  454. }
  455. .compare-name {
  456. font-size: 28rpx;
  457. color: #2D3436;
  458. font-weight: 500;
  459. }
  460. .compare-rate {
  461. font-size: 28rpx;
  462. color: #FF8C42;
  463. font-weight: 700;
  464. }
  465. .compare-bar-bg {
  466. height: 12rpx;
  467. background: #FFF0E6;
  468. border-radius: 999rpx;
  469. overflow: hidden;
  470. }
  471. .compare-bar-fill {
  472. height: 100%;
  473. border-radius: 999rpx;
  474. transition: width 0.5s ease;
  475. }
  476. .bar-color-0 { background: linear-gradient(90deg, #FF8C42, #FFD93D); }
  477. .bar-color-1 { background: linear-gradient(90deg, #4ECDC4, #44B09E); }
  478. .bar-color-2 { background: linear-gradient(90deg, #667eea, #764ba2); }
  479. .bar-color-3 { background: linear-gradient(90deg, #F97316, #FB923C); }
  480. .bar-color-4 { background: linear-gradient(90deg, #A8E063, #56AB2F); }
  481. /* 分类列表 */
  482. .category-list {
  483. display: flex;
  484. flex-direction: column;
  485. gap: 20rpx;
  486. }
  487. .category-item {
  488. display: flex;
  489. align-items: center;
  490. gap: 12rpx;
  491. }
  492. .category-header {
  493. width: 140rpx;
  494. flex-shrink: 0;
  495. }
  496. .category-name {
  497. font-size: 24rpx;
  498. color: #636E72;
  499. display: block;
  500. }
  501. .category-count {
  502. font-size: 20rpx;
  503. color: #B2BEC3;
  504. display: block;
  505. margin-top: 2rpx;
  506. }
  507. .category-bar-bg {
  508. flex: 1;
  509. height: 16rpx;
  510. background: #FFF0E6;
  511. border-radius: 999rpx;
  512. overflow: hidden;
  513. }
  514. .category-bar-fill {
  515. height: 100%;
  516. background: linear-gradient(90deg, #4ECDC4, #44B09E);
  517. border-radius: 999rpx;
  518. transition: width 0.5s ease;
  519. }
  520. .category-rate {
  521. width: 64rpx;
  522. text-align: right;
  523. font-size: 24rpx;
  524. font-weight: 600;
  525. color: #4ECDC4;
  526. flex-shrink: 0;
  527. }
  528. /* 空状态 */
  529. .empty-section {
  530. display: flex;
  531. flex-direction: column;
  532. align-items: center;
  533. padding: 120rpx 0;
  534. }
  535. .empty-icon-area {
  536. width: 160rpx;
  537. height: 160rpx;
  538. background: #FFF5EB;
  539. border-radius: 50%;
  540. display: flex;
  541. align-items: center;
  542. justify-content: center;
  543. margin-bottom: 24rpx;
  544. }
  545. .empty-main-icon {
  546. font-size: 72rpx;
  547. }
  548. .empty-title {
  549. font-size: 30rpx;
  550. font-weight: 600;
  551. color: #2D3436;
  552. margin-bottom: 12rpx;
  553. }
  554. .empty-hint {
  555. font-size: 26rpx;
  556. color: #B2BEC3;
  557. text-align: center;
  558. }
  559. </style>