completion-rate.vue 17 KB

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