history.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. <template>
  2. <view class="container" :style="{ '--font-scale': fontScale }">
  3. <login-sheet ref="loginSheet" @success="onLoginSuccess" />
  4. <view v-if="!isLoggedIn">
  5. <view class="gg-page">
  6. <view class="gg-hero">
  7. <text class="gg-eyebrow">浠艾福 · 成长历史记录</text>
  8. <text class="gg-hero-line gg-hero-line-top">看见改变</text>
  9. <text class="gg-hero-line">正在发生</text>
  10. <view class="gg-hero-bar"></view>
  11. <text class="gg-hero-sub">记录不是目的,看见改变才是。每一份饮食、运动、睡眠、心情的记录,都在为成长提供依据。</text>
  12. </view>
  13. <view class="gg-guide">
  14. <LoginGuideCard
  15. title="登录后查看完整成长历史"
  16. :items="[
  17. '查看饮食、运动、睡眠、心情的所有打卡记录',
  18. '查看成长档案测评记录',
  19. '筛选分类、按时间排序'
  20. ]" />
  21. </view>
  22. </view>
  23. </view>
  24. <view v-else>
  25. <!-- 筛选栏 -->
  26. <view class="filter-bar">
  27. <view class="filter-tabs">
  28. <view
  29. class="filter-tab"
  30. :class="{ active: currentFilter === 'all' }"
  31. @click="setFilter('all')"
  32. >
  33. 全部
  34. </view>
  35. <view
  36. class="filter-tab"
  37. :class="{ active: currentFilter === 'diet' }"
  38. @click="setFilter('diet')"
  39. >
  40. 🍽️ 饮食
  41. </view>
  42. <view
  43. class="filter-tab"
  44. :class="{ active: currentFilter === 'exercise' }"
  45. @click="setFilter('exercise')"
  46. >
  47. 🏃 运动
  48. </view>
  49. <view
  50. class="filter-tab"
  51. :class="{ active: currentFilter === 'sleep' }"
  52. @click="setFilter('sleep')"
  53. >
  54. 😴 睡眠
  55. </view>
  56. <view
  57. class="filter-tab"
  58. :class="{ active: currentFilter === 'emotion' }"
  59. @click="setFilter('emotion')"
  60. >
  61. 😊 心情
  62. </view>
  63. <view
  64. class="filter-tab"
  65. :class="{ active: currentFilter === 'growth' }"
  66. @click="setFilter('growth')"
  67. >
  68. 📈 成长档案
  69. </view>
  70. </view>
  71. </view>
  72. <!-- 记录列表 -->
  73. <view class="records-section">
  74. <view v-if="loading" class="loading-state">
  75. <view class="loading-spinner"></view>
  76. <text class="loading-text">加载中...</text>
  77. </view>
  78. <view v-else-if="filteredRecords.length === 0" class="empty-state">
  79. <text class="empty-icon">📭</text>
  80. <text class="empty-title">暂无记录</text>
  81. <text class="empty-desc">{{ emptyMessage }}</text>
  82. </view>
  83. <view v-else class="records-list">
  84. <view
  85. class="record-card"
  86. v-for="(record, index) in filteredRecords"
  87. :key="getRecordKey(record, index)"
  88. @click="goToDetail(record)"
  89. >
  90. <view class="record-type-badge" :class="'type-' + record.type">{{ record.typeLabel }}</view>
  91. <view class="record-content">
  92. <view class="record-header">
  93. <text class="record-title">{{ record.title }}</text>
  94. <text class="record-date">{{ record.formattedDate }}</text>
  95. </view>
  96. <view class="record-meta" v-if="record.meta">
  97. <text class="meta-item" v-for="(item, i) in record.meta" :key="i">
  98. {{ item }}
  99. </text>
  100. </view>
  101. <view class="record-summary" v-if="record.summary">
  102. {{ record.summary }}
  103. </view>
  104. </view>
  105. <text class="record-chevron">›</text>
  106. </view>
  107. </view>
  108. <!-- 加载更多 -->
  109. <view class="load-more" v-if="!loading && hasMore && filteredRecords.length > 0" @click="loadMore">
  110. <text class="load-more-text">加载更多</text>
  111. </view>
  112. </view>
  113. </view>
  114. </view>
  115. </template>
  116. <script>
  117. import LoginGuideCard from '../../components/LoginGuideCard.vue'
  118. import {
  119. healthCheckinList,
  120. getHealthMealList,
  121. getExerciseRecords,
  122. getSleepRecords,
  123. getEmotionCheckinList,
  124. getGrowthRecordList,
  125. getGrowthRecordDetail
  126. } from '../../utils/api.js'
  127. import { parseDate, recordDateOf } from '../../utils/format.js'
  128. import fontScaleMixin from '@/mixins/font-scale.js'
  129. export default {
  130. mixins: [fontScaleMixin],
  131. components: { LoginGuideCard },
  132. data() {
  133. return {
  134. memberId: '',
  135. isLoggedIn: false,
  136. currentFilter: 'all',
  137. loading: false,
  138. loadingMore: false,
  139. page: 1,
  140. pageSize: 20,
  141. hasMore: true,
  142. // 原始数据
  143. allDietRecords: [],
  144. allExerciseRecords: [],
  145. allSleepRecords: [],
  146. allEmotionRecords: [],
  147. allGrowthRecords: [],
  148. // 合并后的记录(用于显示)
  149. mergedRecords: [],
  150. }
  151. },
  152. computed: {
  153. filteredRecords() {
  154. if (this.currentFilter === 'all') {
  155. return this.mergedRecords
  156. }
  157. return this.mergedRecords.filter(r => r.type === this.currentFilter)
  158. },
  159. emptyMessage() {
  160. const messages = {
  161. all: '暂无任何成长记录,快去打卡吧!',
  162. diet: '暂无饮食记录',
  163. exercise: '暂无运动记录',
  164. sleep: '暂无睡眠记录',
  165. emotion: '暂无心情记录',
  166. growth: '暂无成长档案记录'
  167. }
  168. return messages[this.currentFilter] || messages.all
  169. }
  170. },
  171. onLoad() {
  172. // 初始化时不加载,等 onShow
  173. },
  174. onShow() {
  175. this.isLoggedIn = !!uni.getStorageSync('token')
  176. if (this.isLoggedIn) {
  177. this.memberId = uni.getStorageSync('currentChildId') || uni.getStorageSync('currentMemberId') || uni.getStorageSync('userId') || ''
  178. if (!this._dataLoaded) {
  179. this._dataLoaded = true
  180. this.loadAllData()
  181. }
  182. }
  183. },
  184. methods: {
  185. setFilter(filter) {
  186. this.currentFilter = filter
  187. },
  188. async loadAllData() {
  189. this.loading = true
  190. try {
  191. // 并行加载所有类型的记录
  192. await Promise.all([
  193. this.loadDietRecords(),
  194. this.loadExerciseRecords(),
  195. this.loadSleepRecords(),
  196. this.loadEmotionRecords(),
  197. this.loadGrowthRecords()
  198. ])
  199. this.mergeAndSortRecords()
  200. } catch (e) {
  201. console.error('加载成长历史失败', e)
  202. uni.showToast({ title: '加载失败', icon: 'none' })
  203. } finally {
  204. this.loading = false
  205. }
  206. },
  207. async loadDietRecords() {
  208. try {
  209. const res = await getHealthMealList(this.memberId ? { memberId: this.memberId } : {})
  210. if (res.code === 200 && res.data) {
  211. this.allDietRecords = res.data.map(item => this.formatDietRecord(item))
  212. }
  213. } catch (e) {
  214. console.error('加载饮食记录失败', e)
  215. }
  216. },
  217. async loadExerciseRecords() {
  218. try {
  219. const res = await getExerciseRecords(this.memberId ? { memberId: this.memberId } : {})
  220. if (res.code === 200 && res.data) {
  221. this.allExerciseRecords = res.data.map(item => this.formatExerciseRecord(item))
  222. }
  223. } catch (e) {
  224. console.error('加载运动记录失败', e)
  225. }
  226. },
  227. async loadSleepRecords() {
  228. try {
  229. const res = await getSleepRecords(this.memberId ? { memberId: this.memberId } : {})
  230. if (res.code === 200 && res.data) {
  231. this.allSleepRecords = res.data.map(item => this.formatSleepRecord(item))
  232. }
  233. } catch (e) {
  234. console.error('加载睡眠记录失败', e)
  235. }
  236. },
  237. async loadEmotionRecords() {
  238. try {
  239. const res = await getEmotionCheckinList({ page: 1, size: 100, memberId: this.memberId })
  240. if (res.code === 200 && res.data) {
  241. this.allEmotionRecords = res.data.map(item => this.formatEmotionRecord(item))
  242. }
  243. } catch (e) {
  244. console.error('加载心情记录失败', e)
  245. }
  246. },
  247. async loadGrowthRecords() {
  248. try {
  249. const res = await getGrowthRecordList()
  250. if (res.code === 200 && res.data) {
  251. this.allGrowthRecords = res.data.map(item => this.formatGrowthRecord(item))
  252. }
  253. } catch (e) {
  254. console.error('加载成长档案失败', e)
  255. }
  256. },
  257. formatDietRecord(item) {
  258. const date = recordDateOf(item)
  259. return {
  260. id: item.id,
  261. type: 'diet',
  262. typeLabel: '饮食',
  263. title: this.mealTypeLabel(item.mealType) || '饮食记录',
  264. date: date,
  265. formattedDate: date ? this.formatDisplayDate(date) : '',
  266. meta: [
  267. item.foods && item.foods.length > 0 ? `包含 ${item.foods.length} 种食物` : '',
  268. item.totalCalories ? `约 ${item.totalCalories} kcal` : ''
  269. ].filter(Boolean),
  270. summary: item.remark || '',
  271. rawData: item
  272. }
  273. },
  274. formatExerciseRecord(item) {
  275. const date = recordDateOf(item)
  276. return {
  277. id: item.id,
  278. type: 'exercise',
  279. typeLabel: '运动',
  280. title: item.exerciseName || '运动记录',
  281. date: date,
  282. formattedDate: date ? this.formatDisplayDate(date) : '',
  283. meta: [
  284. item.duration ? `时长 ${item.duration} 分钟` : '',
  285. item.calories ? `消耗 ${item.calories} kcal` : ''
  286. ].filter(Boolean),
  287. summary: item.remark || '',
  288. rawData: item
  289. }
  290. },
  291. formatSleepRecord(item) {
  292. const date = recordDateOf(item)
  293. return {
  294. id: item.id,
  295. type: 'sleep',
  296. typeLabel: '睡眠',
  297. title: '睡眠记录',
  298. date: date,
  299. formattedDate: date ? this.formatDisplayDate(date) : '',
  300. meta: [
  301. item.startTime && item.endTime ? `${this.formatTime(item.startTime)} - ${this.formatTime(item.endTime)}` : '',
  302. item.duration ? `时长 ${this.formatDuration(item.duration)}` : '',
  303. item.quality ? `质量 ${item.quality}` : ''
  304. ].filter(Boolean),
  305. summary: item.remark || '',
  306. rawData: item
  307. }
  308. },
  309. formatEmotionRecord(item) {
  310. const date = recordDateOf(item)
  311. return {
  312. id: item.id,
  313. type: 'emotion',
  314. typeLabel: '心情',
  315. title: item.emotionLabel || '心情记录',
  316. date: date,
  317. formattedDate: date ? this.formatDisplayDate(date) : '',
  318. meta: [
  319. item.intensity ? `强度 ${item.intensity}` : '',
  320. item.tags && item.tags.length > 0 ? `标签: ${item.tags.join('、')}` : ''
  321. ].filter(Boolean),
  322. summary: item.note || '',
  323. rawData: item
  324. }
  325. },
  326. formatGrowthRecord(item) {
  327. const date = item.reportDate || item.createdAt
  328. return {
  329. id: item.id,
  330. type: 'growth',
  331. typeLabel: '成长档案',
  332. title: item.recordTitle || '成长档案',
  333. date: date,
  334. formattedDate: date ? this.formatDisplayDate(date) : '',
  335. meta: [
  336. item.overallScore != null ? `综合得分: ${item.overallScore}` : '',
  337. item.sourceType ? `来源: ${this.sourceTypeLabel(item.sourceType)}` : ''
  338. ].filter(Boolean),
  339. summary: item.assessmentSummary || item.growthSuggestions || '',
  340. rawData: item
  341. }
  342. },
  343. mergeAndSortRecords() {
  344. const allRecords = [
  345. ...this.allDietRecords,
  346. ...this.allExerciseRecords,
  347. ...this.allSleepRecords,
  348. ...this.allEmotionRecords,
  349. ...this.allGrowthRecords
  350. ]
  351. // 按日期倒序排序
  352. allRecords.sort((a, b) => {
  353. if (!a.date && !b.date) return 0
  354. if (!a.date) return 1
  355. if (!b.date) return -1
  356. return new Date(b.date) - new Date(a.date)
  357. })
  358. this.mergedRecords = allRecords
  359. },
  360. formatDisplayDate(dateStr) {
  361. if (!dateStr) return ''
  362. const d = parseDate(dateStr)
  363. if (!d) return dateStr.substring(0, 10)
  364. const month = d.getMonth() + 1
  365. const day = d.getDate()
  366. const hour = d.getHours()
  367. const minute = d.getMinutes()
  368. return `${month}月${day}日 ${this.pad(hour)}:${this.pad(minute)}`
  369. },
  370. formatTime(timeStr) {
  371. if (!timeStr) return ''
  372. const d = parseDate(timeStr)
  373. if (!d) return timeStr.substring(11, 16)
  374. return `${this.pad(d.getHours())}:${this.pad(d.getMinutes())}`
  375. },
  376. formatDuration(minutes) {
  377. if (!minutes) return ''
  378. const h = Math.floor(minutes / 60)
  379. const m = minutes % 60
  380. return h > 0 ? `${h}小时${m}分` : `${m}分钟`
  381. },
  382. mealTypeLabel(type) {
  383. const m = { breakfast: '早餐', lunch: '午餐', dinner: '晚餐', snack: '加餐', extra: '零食' }
  384. if (!type) return '饮食'
  385. const parts = String(type).split(',')
  386. const labels = []
  387. for (let i = 0; i < parts.length; i++) {
  388. const v = parts[i] && parts[i].trim()
  389. if (!v) continue
  390. labels.push(m[v] || v)
  391. }
  392. return labels.length > 0 ? labels.join('、') : '饮食'
  393. },
  394. sourceTypeLabel(type) {
  395. const map = { manual: '手动创建', assessment: '测评录入', upload: '报告上传', supplement: '补充材料' }
  396. return map[type] || type
  397. },
  398. pad(n) {
  399. return n < 10 ? '0' + n : '' + n
  400. },
  401. getRecordKey(record, index) {
  402. return record.id || ('idx-' + index)
  403. },
  404. goToDetail(record) {
  405. if (!record) return
  406. const type = record.type
  407. const raw = record.rawData
  408. let url = ''
  409. if (type === 'diet') {
  410. url = '/pages/growth/diet-checkin?recordId=' + raw.id + '&viewOnly=1'
  411. } else if (type === 'exercise') {
  412. url = '/pages/growth/exercise-checkin?recordId=' + raw.id + '&viewOnly=1'
  413. } else if (type === 'sleep') {
  414. url = '/pages/growth/sleep-checkin?recordId=' + raw.id + '&viewOnly=1'
  415. } else if (type === 'emotion') {
  416. url = '/pages/mind-detail/emotion-checkin?recordId=' + raw.id + '&viewOnly=1'
  417. } else if (type === 'growth') {
  418. url = '/pages/growth/detail?recordId=' + raw.id
  419. }
  420. if (url && this.memberId) {
  421. url += (url.includes('?') ? '&' : '?') + 'memberId=' + this.memberId
  422. }
  423. if (url) {
  424. uni.navigateTo({ url })
  425. }
  426. },
  427. async loadMore() {
  428. // 暂时不实现分页加载,因为已经一次性加载了所有数据
  429. uni.showToast({ title: '已加载全部记录', icon: 'none' })
  430. },
  431. onLoginSuccess() {
  432. this.isLoggedIn = true
  433. this.memberId = uni.getStorageSync('currentChildId') || uni.getStorageSync('currentMemberId') || uni.getStorageSync('userId') || ''
  434. this._dataLoaded = false
  435. this.loadAllData()
  436. }
  437. }
  438. }
  439. </script>
  440. <style scoped lang="scss">
  441. .container {
  442. background: $facade-brand-tint;
  443. min-height: 100vh;
  444. padding-bottom: calc(160rpx + env(safe-area-inset-bottom));
  445. }
  446. // ==================== 游客态 ====================
  447. .gg-page { background: $app-bg; min-height: 100vh; padding: 28rpx 0 40rpx; }
  448. .gg-hero { margin: 0 32rpx 28rpx; display: flex; flex-direction: column; }
  449. .gg-eyebrow { font-size: calc(22rpx * var(--font-scale, 1)); font-weight: 600; color: $brand-primary; letter-spacing: 2rpx; }
  450. .gg-hero-line { font-size: calc(52rpx * var(--font-scale, 1)); font-weight: 700; color: $facade-text-strong; line-height: 1.28; }
  451. .gg-hero-line-top { margin-top: 16rpx; }
  452. .gg-hero-bar { width: 84rpx; height: 8rpx; border-radius: 999rpx; margin: 20rpx 0 18rpx; background: linear-gradient(135deg, $brand-primary, $brand-primary-dark); }
  453. .gg-hero-sub { font-size: calc(24rpx * var(--font-scale, 1)); color: $facade-text-body; line-height: 1.7; }
  454. .gg-guide { margin: 0 32rpx 8rpx; }
  455. // ==================== 已登录态 ====================
  456. .filter-bar {
  457. position: sticky;
  458. top: 0;
  459. z-index: 100;
  460. background: $app-surface;
  461. border-bottom: 1rpx solid $app-border-subtle;
  462. padding: 16rpx 24rpx;
  463. }
  464. .filter-tabs {
  465. display: flex;
  466. flex-wrap: wrap;
  467. gap: 12rpx;
  468. justify-content: flex-start;
  469. }
  470. .filter-tab {
  471. font-size: calc(24rpx * var(--font-scale, 1));
  472. padding: 8rpx 16rpx;
  473. border-radius: 20rpx;
  474. background: $facade-fill-2;
  475. color: $facade-text-body;
  476. white-space: nowrap;
  477. transition: background 0.2s ease, color 0.2s ease, transform 0.2s ease;
  478. }
  479. .filter-tab.active {
  480. background: linear-gradient(135deg, $brand-primary, $brand-primary-dark);
  481. color: $app-surface;
  482. }
  483. .filter-tab:active {
  484. opacity: 0.85;
  485. transform: scale(0.98);
  486. }
  487. .records-section {
  488. padding: 0 24rpx 40rpx;
  489. }
  490. .loading-state {
  491. display: flex;
  492. flex-direction: column;
  493. align-items: center;
  494. padding: 80rpx 0;
  495. }
  496. .loading-spinner {
  497. width: 48rpx;
  498. height: 48rpx;
  499. border: 3rpx solid $facade-fill-2;
  500. border-top-color: $brand-primary;
  501. border-radius: 50%;
  502. animation: spin 1s linear infinite;
  503. }
  504. @keyframes spin {
  505. to { transform: rotate(360deg); }
  506. }
  507. .loading-text {
  508. margin-top: 20rpx;
  509. font-size: calc(26rpx * var(--font-scale, 1));
  510. color: $facade-text-muted;
  511. }
  512. .empty-state {
  513. display: flex;
  514. flex-direction: column;
  515. align-items: center;
  516. padding: 100rpx 0;
  517. }
  518. .empty-icon {
  519. font-size: calc(80rpx * var(--font-scale, 1));
  520. margin-bottom: 20rpx;
  521. }
  522. .empty-title {
  523. font-size: calc(30rpx * var(--font-scale, 1));
  524. font-weight: 600;
  525. color: $facade-text-strong;
  526. margin-bottom: 10rpx;
  527. }
  528. .empty-desc {
  529. font-size: calc(24rpx * var(--font-scale, 1));
  530. color: $facade-text-muted;
  531. text-align: center;
  532. }
  533. .records-list {
  534. display: flex;
  535. flex-direction: column;
  536. gap: 16rpx;
  537. }
  538. .record-card {
  539. display: flex;
  540. flex-direction: row;
  541. align-items: flex-start;
  542. background: $app-surface;
  543. border-radius: 20rpx;
  544. padding: 20rpx;
  545. border: 1rpx solid $app-border-subtle;
  546. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.04);
  547. }
  548. .record-card:active {
  549. opacity: 0.85;
  550. background: $facade-fill-2;
  551. }
  552. .record-type-badge {
  553. font-size: calc(20rpx * var(--font-scale, 1));
  554. font-weight: 600;
  555. padding: 4rpx 10rpx;
  556. border-radius: 12rpx;
  557. margin-right: 16rpx;
  558. flex-shrink: 0;
  559. margin-top: 4rpx;
  560. }
  561. .type-diet { background: $dim-body-tint; color: $dim-body-deep; }
  562. .type-exercise { background: $dim-action-tint; color: $dim-action-deep; }
  563. .type-sleep { background: $dim-wisdom-tint; color: $dim-wisdom-deep; }
  564. .type-emotion { background: $dim-mind-tint; color: $dim-mind-deep; }
  565. .type-growth { background: $accent-indigo-tint; color: $accent-indigo; }
  566. .record-content {
  567. flex: 1;
  568. min-width: 0;
  569. }
  570. .record-header {
  571. display: flex;
  572. justify-content: space-between;
  573. align-items: flex-start;
  574. margin-bottom: 8rpx;
  575. }
  576. .record-title {
  577. font-size: calc(28rpx * var(--font-scale, 1));
  578. font-weight: 600;
  579. color: $facade-text-strong;
  580. flex: 1;
  581. margin-right: 12rpx;
  582. }
  583. .record-date {
  584. font-size: calc(22rpx * var(--font-scale, 1));
  585. color: $facade-text-muted;
  586. flex-shrink: 0;
  587. white-space: nowrap;
  588. }
  589. .record-meta {
  590. display: flex;
  591. flex-wrap: wrap;
  592. gap: 16rpx;
  593. margin-bottom: 8rpx;
  594. }
  595. .meta-item {
  596. font-size: calc(22rpx * var(--font-scale, 1));
  597. color: $facade-text-body;
  598. }
  599. .record-summary {
  600. font-size: calc(22rpx * var(--font-scale, 1));
  601. color: $facade-text-muted;
  602. line-height: 1.5;
  603. overflow: hidden;
  604. text-overflow: ellipsis;
  605. display: -webkit-box;
  606. -webkit-line-clamp: 2;
  607. -webkit-box-orient: vertical;
  608. }
  609. .record-chevron {
  610. font-size: calc(32rpx * var(--font-scale, 1));
  611. color: $facade-text-ghost;
  612. margin-left: 12rpx;
  613. flex-shrink: 0;
  614. }
  615. .load-more {
  616. display: flex;
  617. justify-content: center;
  618. padding: 32rpx 0;
  619. }
  620. .load-more-text {
  621. font-size: calc(26rpx * var(--font-scale, 1));
  622. color: $brand-primary;
  623. }
  624. </style>