completion-rate.vue 18 KB

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