completion-rate.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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.memberId">
  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.memberId % 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. <!-- 已完成任务 -->
  126. <view class="section-block" v-if="stats.completedTaskList && stats.completedTaskList.length > 0">
  127. <view class="section-header">
  128. <text class="section-title">已完成打卡</text>
  129. </view>
  130. <view class="completed-list">
  131. <view class="completed-item" v-for="item in stats.completedTaskList" :key="item.id">
  132. <text class="completed-title">{{ item.title }}</text>
  133. <text class="completed-meta">+{{ item.points || 0 }} 积分</text>
  134. </view>
  135. </view>
  136. </view>
  137. </template>
  138. </view>
  139. </view>
  140. </template>
  141. <script>
  142. import { getChildCompletionStats, getFamilyCompletionStats, getChildren } from '../../utils/api.js'
  143. export default {
  144. data() {
  145. return {
  146. loading: true,
  147. memberId: '',
  148. isFamilyView: false,
  149. periodIndex: 0,
  150. periods: [
  151. { label: '本周', days: 7 },
  152. { label: '本月', days: 30 },
  153. { label: '近三月', days: 90 }
  154. ],
  155. stats: {
  156. totalTasks: 0,
  157. completedTasks: 0,
  158. pendingTasks: 0,
  159. inReviewTasks: 0,
  160. overdueTasks: 0,
  161. completionRate: 0,
  162. byChild: [],
  163. categoryBreakdown: [],
  164. completedTaskList: []
  165. }
  166. }
  167. },
  168. computed: {
  169. periodLabel() {
  170. return this.periods[this.periodIndex].label
  171. },
  172. circumference() {
  173. return 2 * Math.PI * 52
  174. },
  175. rateOffset() {
  176. const pct = this.stats.completionRate || 0
  177. return this.circumference * (1 - pct / 100)
  178. }
  179. },
  180. onLoad(options) {
  181. if (options.memberId) {
  182. this.memberId = options.memberId
  183. this.isFamilyView = false
  184. } else if (options.familyView === '1') {
  185. this.isFamilyView = true
  186. }
  187. this.loadStats()
  188. },
  189. methods: {
  190. async loadStats() {
  191. this.loading = true
  192. try {
  193. const period = this.periods[this.periodIndex]
  194. const endDate = this.formatDate(new Date())
  195. const startDate = this.formatDate(new Date(Date.now() - period.days * 24 * 60 * 60 * 1000))
  196. const params = { startDate, endDate }
  197. if (this.memberId) {
  198. params.memberId = this.memberId
  199. }
  200. let res
  201. if (this.isFamilyView || !this.memberId) {
  202. res = await getFamilyCompletionStats(params)
  203. } else {
  204. res = await getChildCompletionStats(params)
  205. }
  206. if (res && res.data) {
  207. // 兼容后端可能返回 byChild 或 categoryBreakdown
  208. const data = res.data
  209. this.stats = {
  210. totalTasks: data.totalTasks || 0,
  211. completedTasks: data.completedTasks || 0,
  212. pendingTasks: data.pendingTasks || 0,
  213. inReviewTasks: data.inReviewTasks || 0,
  214. overdueTasks: data.overdueTasks || 0,
  215. completionRate: data.completionRate || 0,
  216. byChild: data.byChild || [],
  217. categoryBreakdown: data.categoryBreakdown || [],
  218. completedTaskList: data.completedTaskList || []
  219. }
  220. }
  221. } catch (e) {
  222. console.error('加载完成率失败', e)
  223. uni.showToast({ title: '数据加载失败', icon: 'none' })
  224. } finally {
  225. this.loading = false
  226. }
  227. },
  228. togglePeriod() {
  229. this.periodIndex = (this.periodIndex + 1) % this.periods.length
  230. this.loadStats()
  231. },
  232. goBack() {
  233. uni.navigateBack()
  234. },
  235. formatDate(date) {
  236. const y = date.getFullYear()
  237. const m = String(date.getMonth() + 1).padStart(2, '0')
  238. const d = String(date.getDate()).padStart(2, '0')
  239. return y + '-' + m + '-' + d
  240. }
  241. }
  242. }
  243. </script>
  244. <style scoped>
  245. .container {
  246. background: #FFF9F0;
  247. min-height: 100vh;
  248. display: flex;
  249. flex-direction: column;
  250. }
  251. /* 顶部导航 */
  252. .header {
  253. display: flex;
  254. align-items: center;
  255. justify-content: space-between;
  256. padding: 24rpx 32rpx;
  257. padding-top: calc(88rpx + env(safe-area-inset-top));
  258. background: #FFF9F0;
  259. position: sticky;
  260. top: 0;
  261. z-index: 10;
  262. }
  263. .header-left, .header-right {
  264. min-width: 120rpx;
  265. }
  266. .header-right {
  267. display: flex;
  268. justify-content: flex-end;
  269. }
  270. .back-arrow {
  271. font-size: 48rpx;
  272. color: #2D3436;
  273. font-weight: 300;
  274. line-height: 1;
  275. }
  276. .header-title {
  277. font-size: 34rpx;
  278. font-weight: 700;
  279. color: #2D3436;
  280. }
  281. .period-btn {
  282. font-size: 26rpx;
  283. color: #FF8C42;
  284. background: #FFF5EB;
  285. padding: 8rpx 20rpx;
  286. border-radius: 24rpx;
  287. font-weight: 500;
  288. }
  289. /* 滚动区 */
  290. .scroll-area {
  291. flex: 1;
  292. padding: 0 32rpx 48rpx;
  293. overflow-y: auto;
  294. }
  295. /* 加载状态 */
  296. .loading-state {
  297. display: flex;
  298. flex-direction: column;
  299. align-items: center;
  300. padding: 160rpx 0;
  301. }
  302. .loading-spinner {
  303. width: 64rpx;
  304. height: 64rpx;
  305. border: 6rpx solid #FFF0E6;
  306. border-top-color: #FF8C42;
  307. border-radius: 50%;
  308. animation: spin 0.8s linear infinite;
  309. }
  310. @keyframes spin {
  311. to { transform: rotate(360deg); }
  312. }
  313. .loading-text {
  314. font-size: 26rpx;
  315. color: #B2BEC3;
  316. margin-top: 20rpx;
  317. }
  318. /* 整体完成率卡片 */
  319. .rate-card {
  320. background: #FFFFFF;
  321. border-radius: 28rpx;
  322. padding: 40rpx 32rpx;
  323. margin-bottom: 24rpx;
  324. display: flex;
  325. align-items: center;
  326. box-shadow: 0 4px 20px rgba(255, 140, 66, 0.08);
  327. }
  328. .rate-ring-wrapper {
  329. flex-shrink: 0;
  330. margin-right: 32rpx;
  331. }
  332. .rate-ring {
  333. width: 200rpx;
  334. height: 200rpx;
  335. position: relative;
  336. }
  337. .ring-svg {
  338. width: 100%;
  339. height: 100%;
  340. }
  341. .rate-ring-text {
  342. position: absolute;
  343. top: 50%;
  344. left: 50%;
  345. transform: translate(-50%, -50%);
  346. display: flex;
  347. flex-direction: column;
  348. align-items: center;
  349. }
  350. .rate-value {
  351. font-size: 40rpx;
  352. font-weight: 800;
  353. color: #2D3436;
  354. line-height: 1.2;
  355. }
  356. .rate-label {
  357. font-size: 22rpx;
  358. color: #B2BEC3;
  359. margin-top: 4rpx;
  360. }
  361. .rate-legend {
  362. flex: 1;
  363. display: flex;
  364. flex-direction: column;
  365. gap: 16rpx;
  366. }
  367. .legend-item {
  368. display: flex;
  369. align-items: center;
  370. }
  371. .legend-dot {
  372. width: 16rpx;
  373. height: 16rpx;
  374. border-radius: 50%;
  375. margin-right: 12rpx;
  376. flex-shrink: 0;
  377. }
  378. .legend-label {
  379. font-size: 26rpx;
  380. color: #636E72;
  381. }
  382. /* 快捷统计卡片 */
  383. .quick-stats {
  384. display: flex;
  385. gap: 16rpx;
  386. margin-bottom: 24rpx;
  387. }
  388. .quick-stat-card {
  389. flex: 1;
  390. background: #FFFFFF;
  391. border-radius: 20rpx;
  392. padding: 24rpx 16rpx;
  393. display: flex;
  394. flex-direction: column;
  395. align-items: center;
  396. box-shadow: 0 4px 20px rgba(255, 140, 66, 0.08);
  397. }
  398. .quick-stat-value {
  399. font-size: 40rpx;
  400. font-weight: 800;
  401. line-height: 1.2;
  402. }
  403. .quick-stat-label {
  404. font-size: 22rpx;
  405. color: #B2BEC3;
  406. margin-top: 8rpx;
  407. }
  408. .primary-color { color: #FF8C42; }
  409. .success-color { color: #4ECDC4; }
  410. .danger-color { color: #F97316; }
  411. /* 逾期警示 */
  412. .alert-card {
  413. background: #FFF5F5;
  414. border-radius: 20rpx;
  415. padding: 24rpx;
  416. margin-bottom: 24rpx;
  417. display: flex;
  418. align-items: flex-start;
  419. border: 1rpx solid #FFE0E0;
  420. }
  421. .alert-icon {
  422. font-size: 40rpx;
  423. margin-right: 16rpx;
  424. flex-shrink: 0;
  425. }
  426. .alert-body {
  427. flex: 1;
  428. }
  429. .alert-title {
  430. font-size: 28rpx;
  431. font-weight: 600;
  432. color: #E74C3C;
  433. display: block;
  434. }
  435. .alert-desc {
  436. font-size: 24rpx;
  437. color: #E17055;
  438. margin-top: 6rpx;
  439. display: block;
  440. }
  441. /* 区块 */
  442. .section-block {
  443. background: #FFFFFF;
  444. border-radius: 24rpx;
  445. padding: 28rpx;
  446. margin-bottom: 24rpx;
  447. box-shadow: 0 4px 20px rgba(255, 140, 66, 0.08);
  448. }
  449. .section-header {
  450. display: flex;
  451. justify-content: space-between;
  452. align-items: center;
  453. margin-bottom: 24rpx;
  454. }
  455. .section-title {
  456. font-size: 30rpx;
  457. font-weight: 700;
  458. color: #2D3436;
  459. }
  460. /* 孩子对比 */
  461. .child-compare {
  462. display: flex;
  463. flex-direction: column;
  464. gap: 20rpx;
  465. }
  466. .compare-row {
  467. display: flex;
  468. justify-content: space-between;
  469. margin-bottom: 8rpx;
  470. }
  471. .compare-name {
  472. font-size: 28rpx;
  473. color: #2D3436;
  474. font-weight: 500;
  475. }
  476. .compare-rate {
  477. font-size: 28rpx;
  478. color: #FF8C42;
  479. font-weight: 700;
  480. }
  481. .compare-bar-bg {
  482. height: 12rpx;
  483. background: #FFF0E6;
  484. border-radius: 999rpx;
  485. overflow: hidden;
  486. }
  487. .compare-bar-fill {
  488. height: 100%;
  489. border-radius: 999rpx;
  490. transition: width 0.5s ease;
  491. }
  492. .bar-color-0 { background: linear-gradient(90deg, #FF8C42, #FFD93D); }
  493. .bar-color-1 { background: linear-gradient(90deg, #4ECDC4, #44B09E); }
  494. .bar-color-2 { background: linear-gradient(90deg, #667eea, #764ba2); }
  495. .bar-color-3 { background: linear-gradient(90deg, #F97316, #FB923C); }
  496. .bar-color-4 { background: linear-gradient(90deg, #A8E063, #56AB2F); }
  497. /* 分类列表 */
  498. .category-list {
  499. display: flex;
  500. flex-direction: column;
  501. gap: 20rpx;
  502. }
  503. .category-item {
  504. display: flex;
  505. align-items: center;
  506. gap: 12rpx;
  507. }
  508. .category-header {
  509. width: 140rpx;
  510. flex-shrink: 0;
  511. }
  512. .category-name {
  513. font-size: 24rpx;
  514. color: #636E72;
  515. display: block;
  516. }
  517. .category-count {
  518. font-size: 20rpx;
  519. color: #B2BEC3;
  520. display: block;
  521. margin-top: 2rpx;
  522. }
  523. .category-bar-bg {
  524. flex: 1;
  525. height: 16rpx;
  526. background: #FFF0E6;
  527. border-radius: 999rpx;
  528. overflow: hidden;
  529. }
  530. .category-bar-fill {
  531. height: 100%;
  532. background: linear-gradient(90deg, #4ECDC4, #44B09E);
  533. border-radius: 999rpx;
  534. transition: width 0.5s ease;
  535. }
  536. .category-rate {
  537. width: 64rpx;
  538. text-align: right;
  539. font-size: 24rpx;
  540. font-weight: 600;
  541. color: #4ECDC4;
  542. flex-shrink: 0;
  543. }
  544. /* 已完成任务列表 */
  545. .completed-list {
  546. display: flex;
  547. flex-direction: column;
  548. gap: 16rpx;
  549. }
  550. .completed-item {
  551. display: flex;
  552. justify-content: space-between;
  553. align-items: center;
  554. background: #fff;
  555. border-radius: 16rpx;
  556. padding: 24rpx;
  557. box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.04);
  558. }
  559. .completed-title {
  560. font-size: 28rpx;
  561. color: #2D3436;
  562. flex: 1;
  563. padding-right: 16rpx;
  564. }
  565. .completed-meta {
  566. font-size: 24rpx;
  567. color: #10B981;
  568. flex-shrink: 0;
  569. }
  570. /* 空状态 */
  571. .empty-section {
  572. display: flex;
  573. flex-direction: column;
  574. align-items: center;
  575. padding: 120rpx 0;
  576. }
  577. .empty-icon-area {
  578. width: 160rpx;
  579. height: 160rpx;
  580. background: #FFF5EB;
  581. border-radius: 50%;
  582. display: flex;
  583. align-items: center;
  584. justify-content: center;
  585. margin-bottom: 24rpx;
  586. }
  587. .empty-main-icon {
  588. font-size: 72rpx;
  589. }
  590. .empty-title {
  591. font-size: 30rpx;
  592. font-weight: 600;
  593. color: #2D3436;
  594. margin-bottom: 12rpx;
  595. }
  596. .empty-hint {
  597. font-size: 26rpx;
  598. color: #B2BEC3;
  599. text-align: center;
  600. }
  601. </style>