emotion-trend.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. <template>
  2. <view class="trend-container">
  3. <!-- 统计概览卡片 -->
  4. <view class="stats-overview">
  5. <view class="stat-item">
  6. <text class="stat-value">{{ stats.totalCheckins || 0 }}</text>
  7. <text class="stat-label">总打卡</text>
  8. </view>
  9. <view class="stat-item">
  10. <text class="stat-value">{{ stats.currentStreak || 0 }}</text>
  11. <text class="stat-label">连续天数</text>
  12. </view>
  13. <view class="stat-item">
  14. <text class="stat-value">{{ stats.thisMonthCheckins || 0 }}</text>
  15. <text class="stat-label">本月打卡</text>
  16. </view>
  17. <view class="stat-item">
  18. <text class="stat-value">{{ stats.totalEnergy || 0 }}</text>
  19. <text class="stat-label">心能量</text>
  20. </view>
  21. </view>
  22. <!-- 周期切换 -->
  23. <view class="period-tabs">
  24. <view
  25. :class="['period-tab', period === 'week' ? 'period-active' : '']"
  26. @click="switchPeriod('week')">
  27. <text>周</text>
  28. </view>
  29. <view
  30. :class="['period-tab', period === 'month' ? 'period-active' : '']"
  31. @click="switchPeriod('month')">
  32. <text>月</text>
  33. </view>
  34. </view>
  35. <!-- 情绪日历 -->
  36. <view class="calendar-card">
  37. <text class="card-title">情绪日历</text>
  38. <view class="calendar-grid">
  39. <view class="calendar-weekday" v-for="wd in weekdays" :key="wd">
  40. <text>{{ wd }}</text>
  41. </view>
  42. <view
  43. v-for="(day, idx) in calendarDays"
  44. :key="idx"
  45. :class="['calendar-day', 'weather-' + (day.weather || 'none'), day.isToday ? 'is-today' : '']">
  46. <text class="day-num">{{ day.dayNum }}</text>
  47. <text v-if="day.weather" class="day-emoji">{{ weatherEmoji(day.weather) }}</text>
  48. </view>
  49. </view>
  50. </view>
  51. <!-- 情绪分布 -->
  52. <view class="distribution-card">
  53. <text class="card-title">情绪分布</text>
  54. <view class="distribution-list">
  55. <view class="dist-item" v-for="dist in distributionData" :key="dist.weather">
  56. <text class="dist-emoji">{{ dist.icon }}</text>
  57. <text class="dist-label">{{ dist.label }}</text>
  58. <view class="dist-bar-track">
  59. <view class="dist-bar-fill" :style="{ width: dist.percent + '%', background: dist.color }"></view>
  60. </view>
  61. <text class="dist-count">{{ dist.count }}次</text>
  62. </view>
  63. </view>
  64. </view>
  65. <!-- 按天详情 -->
  66. <view class="daily-list-card" v-if="dailyData.length > 0">
  67. <text class="card-title">每日记录</text>
  68. <view class="daily-item" v-for="(day, idx) in dailyData" :key="idx">
  69. <text class="daily-date">{{ formatDate(day.date) }}</text>
  70. <text class="daily-emoji">{{ weatherEmoji(day.weather) }}</text>
  71. <text class="daily-weather">{{ weatherLabel(day.weather) }}</text>
  72. </view>
  73. </view>
  74. <view class="empty-state" v-if="!stats || stats.totalCheckins === 0">
  75. <text class="empty-icon">&#x1F4AD;</text>
  76. <text class="empty-text">还没有情绪打卡记录</text>
  77. <button class="empty-btn" @click="goCheckin">去打卡</button>
  78. </view>
  79. </view>
  80. </template>
  81. <script>
  82. import config from '../../config'
  83. export default {
  84. data() {
  85. return {
  86. period: 'week',
  87. stats: null,
  88. trend: null,
  89. weekdays: ['日', '一', '二', '三', '四', '五', '六'],
  90. weatherConfig: {
  91. sunny: { icon: '\u2600\uFE0F', label: '心情很好', color: '#FFB347' },
  92. cloudy: { icon: '\u26C5', label: '一般般', color: '#A0A0A0' },
  93. rainy: { icon: '\uD83C\uDF27\uFE0F', label: '有点难过', color: '#6B9BD2' },
  94. stormy: { icon: '\u26A1', label: '很烦躁', color: '#8B5CF6' },
  95. rainbow: { icon: '\uD83C\uDF08', label: '特别开心', color: '#FF6B9D' }
  96. }
  97. }
  98. },
  99. computed: {
  100. calendarDays() {
  101. const days = []
  102. const now = new Date()
  103. const year = now.getFullYear()
  104. const month = now.getMonth()
  105. const firstDay = new Date(year, month, 1).getDay()
  106. const daysInMonth = new Date(year, month + 1, 0).getDate()
  107. const today = now.getDate()
  108. // Build weather map from daily data
  109. const weatherMap = {}
  110. if (this.trend && this.trend.dailyData) {
  111. for (const d of this.trend.dailyData) {
  112. if (d.date) {
  113. const dateObj = new Date(d.date)
  114. weatherMap[dateObj.getDate()] = d.weather
  115. }
  116. }
  117. }
  118. // Empty leading cells
  119. for (let i = 0; i < firstDay; i++) {
  120. days.push({ dayNum: '', weather: null, isToday: false })
  121. }
  122. for (let d = 1; d <= daysInMonth; d++) {
  123. days.push({
  124. dayNum: d,
  125. weather: weatherMap[d] || null,
  126. isToday: d === today
  127. })
  128. }
  129. return days
  130. },
  131. distributionData() {
  132. const dist = this.trend && this.trend.weatherDistribution
  133. if (!dist) return []
  134. const total = Object.values(dist).reduce((a, b) => a + b, 0)
  135. if (total === 0) return []
  136. return Object.keys(dist).map(w => {
  137. const cfg = this.weatherConfig[w] || { icon: '?', label: w, color: '#ccc' }
  138. return {
  139. weather: w,
  140. icon: cfg.icon,
  141. label: cfg.label,
  142. color: cfg.color,
  143. count: dist[w],
  144. percent: Math.round(dist[w] / total * 100)
  145. }
  146. })
  147. },
  148. dailyData() {
  149. return this.trend && this.trend.dailyData || []
  150. }
  151. },
  152. onLoad() {
  153. this.loadData()
  154. },
  155. methods: {
  156. loadData() {
  157. const app = getApp()
  158. const childId = app.globalData.currentChildId
  159. if (!childId) return
  160. uni.request({
  161. url: config.API_BASE_URL + '/api/mind/checkin/stats',
  162. method: 'POST',
  163. data: { childId },
  164. success: (res) => {
  165. if (res.data && res.data.code === 200) {
  166. this.stats = res.data.data
  167. }
  168. }
  169. })
  170. uni.request({
  171. url: config.API_BASE_URL + '/api/mind/checkin/trend',
  172. method: 'POST',
  173. data: { childId, period: this.period },
  174. success: (res) => {
  175. if (res.data && res.data.code === 200) {
  176. this.trend = res.data.data
  177. }
  178. }
  179. })
  180. },
  181. switchPeriod(p) {
  182. this.period = p
  183. this.loadData()
  184. },
  185. weatherEmoji(weather) {
  186. const cfg = this.weatherConfig[weather]
  187. return cfg ? cfg.icon : ''
  188. },
  189. weatherLabel(weather) {
  190. const cfg = this.weatherConfig[weather]
  191. return cfg ? cfg.label : weather
  192. },
  193. formatDate(dateStr) {
  194. if (!dateStr) return ''
  195. const d = new Date(dateStr)
  196. return (d.getMonth() + 1) + '/' + d.getDate()
  197. },
  198. goCheckin() {
  199. uni.navigateTo({ url: '/pages/mind/emotion-checkin' })
  200. }
  201. }
  202. }
  203. </script>
  204. <style scoped>
  205. .trend-container {
  206. min-height: 100vh;
  207. background: #F8F8F8;
  208. padding-bottom: 40rpx;
  209. }
  210. .stats-overview {
  211. display: flex;
  212. margin: 24rpx 32rpx;
  213. background: linear-gradient(135deg, #FF6B9D, #FF8CB3);
  214. border-radius: 20rpx;
  215. padding: 28rpx 16rpx;
  216. }
  217. .stat-item {
  218. flex: 1;
  219. text-align: center;
  220. }
  221. .stat-value {
  222. font-size: 36rpx;
  223. font-weight: 700;
  224. color: #fff;
  225. display: block;
  226. }
  227. .stat-label {
  228. font-size: 22rpx;
  229. color: rgba(255, 255, 255, 0.8);
  230. margin-top: 6rpx;
  231. display: block;
  232. }
  233. .period-tabs {
  234. display: flex;
  235. margin: 0 32rpx;
  236. background: #fff;
  237. border-radius: 12rpx;
  238. overflow: hidden;
  239. }
  240. .period-tab {
  241. flex: 1;
  242. text-align: center;
  243. padding: 16rpx 0;
  244. font-size: 26rpx;
  245. color: #666;
  246. }
  247. .period-active {
  248. background: #FF6B9D;
  249. color: #fff;
  250. font-weight: 600;
  251. }
  252. .calendar-card,
  253. .distribution-card,
  254. .daily-list-card {
  255. margin: 24rpx 32rpx;
  256. background: #fff;
  257. border-radius: 20rpx;
  258. padding: 28rpx;
  259. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
  260. }
  261. .card-title {
  262. font-size: 28rpx;
  263. font-weight: 600;
  264. color: #333;
  265. display: block;
  266. margin-bottom: 20rpx;
  267. }
  268. .calendar-grid {
  269. display: flex;
  270. flex-wrap: wrap;
  271. }
  272. .calendar-weekday {
  273. width: 14.28%;
  274. text-align: center;
  275. font-size: 22rpx;
  276. color: #999;
  277. padding: 10rpx 0;
  278. }
  279. .calendar-day {
  280. width: 14.28%;
  281. text-align: center;
  282. padding: 8rpx 0;
  283. min-height: 80rpx;
  284. display: flex;
  285. flex-direction: column;
  286. align-items: center;
  287. justify-content: center;
  288. }
  289. .is-today {
  290. background: #FFF0F5;
  291. border-radius: 12rpx;
  292. }
  293. .day-num {
  294. font-size: 26rpx;
  295. color: #333;
  296. }
  297. .day-emoji {
  298. font-size: 24rpx;
  299. margin-top: 4rpx;
  300. }
  301. .weather-sunny { background: #FFF8E1; border-radius: 8rpx; }
  302. .weather-cloudy { background: #F5F5F5; border-radius: 8rpx; }
  303. .weather-rainy { background: #E3F2FD; border-radius: 8rpx; }
  304. .weather-stormy { background: #EDE7F6; border-radius: 8rpx; }
  305. .weather-rainbow { background: #FCE4EC; border-radius: 8rpx; }
  306. .distribution-list {
  307. display: flex;
  308. flex-direction: column;
  309. gap: 16rpx;
  310. }
  311. .dist-item {
  312. display: flex;
  313. align-items: center;
  314. gap: 16rpx;
  315. }
  316. .dist-emoji {
  317. font-size: 32rpx;
  318. width: 48rpx;
  319. text-align: center;
  320. }
  321. .dist-label {
  322. font-size: 24rpx;
  323. color: #666;
  324. width: 120rpx;
  325. }
  326. .dist-bar-track {
  327. flex: 1;
  328. height: 20rpx;
  329. background: #F0F0F0;
  330. border-radius: 10rpx;
  331. overflow: hidden;
  332. }
  333. .dist-bar-fill {
  334. height: 100%;
  335. border-radius: 10rpx;
  336. transition: width 0.3s;
  337. }
  338. .dist-count {
  339. font-size: 24rpx;
  340. color: #999;
  341. width: 60rpx;
  342. text-align: right;
  343. }
  344. .daily-item {
  345. display: flex;
  346. align-items: center;
  347. padding: 16rpx 0;
  348. border-bottom: 2rpx solid #F5F5F5;
  349. gap: 16rpx;
  350. }
  351. .daily-date {
  352. font-size: 24rpx;
  353. color: #999;
  354. width: 100rpx;
  355. }
  356. .daily-emoji {
  357. font-size: 32rpx;
  358. }
  359. .daily-weather {
  360. font-size: 26rpx;
  361. color: #666;
  362. }
  363. .empty-state {
  364. display: flex;
  365. flex-direction: column;
  366. align-items: center;
  367. padding: 120rpx 32rpx;
  368. }
  369. .empty-icon {
  370. font-size: 80rpx;
  371. margin-bottom: 20rpx;
  372. }
  373. .empty-text {
  374. font-size: 28rpx;
  375. color: #999;
  376. margin-bottom: 40rpx;
  377. }
  378. .empty-btn {
  379. background: #FF6B9D;
  380. color: #fff;
  381. padding: 16rpx 60rpx;
  382. border-radius: 40rpx;
  383. font-size: 28rpx;
  384. }
  385. </style>