sleep-record.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <template>
  2. <view class="sleep-container">
  3. <view class="nav-bar">
  4. <text class="nav-back" @click="goBack">‹ 返回</text>
  5. <text class="nav-title">作息记录</text>
  6. <view class="nav-placeholder"></view>
  7. </view>
  8. <view class="section-card">
  9. <view class="section-title">记录睡眠</view>
  10. <view class="input-row">
  11. <text class="input-label">入睡时间</text>
  12. <picker mode="time" :value="sleepTime" @change="onSleepTimeChange">
  13. <view class="picker-display">{{ sleepTime || '选择时间' }}</view>
  14. </picker>
  15. </view>
  16. <view class="input-row">
  17. <text class="input-label">起床时间</text>
  18. <picker mode="time" :value="wakeTime" @change="onWakeTimeChange">
  19. <view class="picker-display">{{ wakeTime || '选择时间' }}</view>
  20. </picker>
  21. </view>
  22. <view class="input-row">
  23. <text class="input-label">睡眠质量</text>
  24. <view class="quality-options">
  25. <text class="quality-item" v-for="q in qualityOptions" :key="q.value"
  26. :class="{ 'quality-active': quality === q.value }" @click="quality = q.value">
  27. <text class="quality-emoji">{{ q.emoji }}</text>
  28. <text class="quality-label">{{ q.label }}</text>
  29. </text>
  30. </view>
  31. </view>
  32. <view class="input-row">
  33. <text class="input-label">备注</text>
  34. <input class="input-field" v-model="remark" placeholder="可选" />
  35. </view>
  36. <view class="submit-btn" @click="submitSleep">保存记录</view>
  37. </view>
  38. <view class="section-card">
  39. <view class="section-title">本周睡眠概况</view>
  40. <view class="sleep-stats">
  41. <view class="stat-item">
  42. <text class="stat-value">{{ avgDuration || '--' }}</text>
  43. <text class="stat-label">平均时长(h)</text>
  44. </view>
  45. <view class="stat-item">
  46. <text class="stat-value">{{ avgQuality || '--' }}</text>
  47. <text class="stat-label">平均评分</text>
  48. </view>
  49. <view class="stat-item">
  50. <text class="stat-value">{{ totalRecords || 0 }}</text>
  51. <text class="stat-label">记录天数</text>
  52. </view>
  53. </view>
  54. </view>
  55. <view class="section-card">
  56. <view class="section-title">历史记录</view>
  57. <view class="history-list">
  58. <view class="history-item" v-for="item in sleepHistory" :key="item.id">
  59. <view class="history-info">
  60. <text class="history-date">{{ item.createdAt ? item.createdAt.substring(0,10) : '' }}</text>
  61. <text class="history-detail">{{ item.sleepTime ? item.sleepTime.substring(11,16) : '--' }} → {{ item.wakeTime ? item.wakeTime.substring(11,16) : '--' }}</text>
  62. </view>
  63. <view class="history-quality">
  64. <text>{{ getQualityEmoji(item.qualityScore) }}</text>
  65. <text class="quality-num" v-if="item.durationMinutes">{{ Math.round(item.durationMinutes/60) }}h</text>
  66. </view>
  67. </view>
  68. <view class="history-empty" v-if="sleepHistory.length === 0"><text>暂无记录</text></view>
  69. </view>
  70. </view>
  71. <view class="bottom-spacer"></view>
  72. </view>
  73. </template>
  74. <script>
  75. import config from '@/config.js'
  76. export default {
  77. data() {
  78. return {
  79. childId: null,
  80. sleepTime: '',
  81. wakeTime: '',
  82. quality: '',
  83. remark: '',
  84. sleepHistory: [],
  85. avgDuration: null,
  86. avgQuality: null,
  87. totalRecords: 0,
  88. qualityOptions: [
  89. { label: '很好', value: 'excellent', emoji: '😴' },
  90. { label: '还行', value: 'good', emoji: '🙂' },
  91. { label: '一般', value: 'fair', emoji: '😐' },
  92. { label: '很差', value: 'poor', emoji: '😫' }
  93. ]
  94. }
  95. },
  96. onLoad: function(options) {
  97. this.childId = options.childId ? parseInt(options.childId) : null
  98. if (this.childId) this.loadHistory()
  99. },
  100. methods: {
  101. onSleepTimeChange: function(e) { this.sleepTime = e.detail.value },
  102. onWakeTimeChange: function(e) { this.wakeTime = e.detail.value },
  103. getQualityEmoji: function(score) {
  104. if (!score) return '🙂'
  105. if (score >= 8) return '😴'
  106. if (score >= 6) return '🙂'
  107. if (score >= 4) return '😐'
  108. return '😫'
  109. },
  110. submitSleep: function() {
  111. if (!this.sleepTime || !this.wakeTime) {
  112. uni.showToast({ title: '请选择睡眠时间', icon: 'none' })
  113. return
  114. }
  115. uni.showLoading({ title: '保存中...' })
  116. healthRequest('/api/health/sleep/create', {
  117. childId: this.childId,
  118. memberId: this.childId,
  119. sleepTime: this.sleepTime,
  120. wakeTime: this.wakeTime,
  121. qualityScore: this.quality ? { excellent: 9, good: 7, fair: 5, poor: 2 }[this.quality] : null,
  122. remark: this.remark
  123. }).then(function() {
  124. uni.hideLoading()
  125. uni.showToast({ title: '保存成功', icon: 'success' })
  126. this.sleepTime = ''
  127. this.wakeTime = ''
  128. this.quality = ''
  129. this.remark = ''
  130. this.loadHistory()
  131. }.bind(this)).catch(function() {
  132. uni.hideLoading()
  133. uni.showToast({ title: '保存失败', icon: 'none' })
  134. })
  135. },
  136. loadHistory: function() {
  137. var self = this
  138. healthRequest('/api/health/sleep/list', { childId: this.childId }).then(function(res) {
  139. if (res.data) {
  140. self.sleepHistory = res.data.list || res.data
  141. self.avgDuration = res.data.avgDuration || null
  142. self.avgQuality = res.data.avgQuality || null
  143. self.totalRecords = res.data.totalRecords || self.sleepHistory.length
  144. }
  145. }).catch(function() {})
  146. },
  147. goBack: function() { uni.navigateBack() }
  148. }
  149. }
  150. function healthRequest(url, data) {
  151. var token = uni.getStorageSync('token')
  152. return new Promise(function(resolve, reject) {
  153. uni.request({
  154. url: config.API_BASE_URL + url,
  155. method: 'POST',
  156. data: data,
  157. header: { 'Content-Type': 'application/json', 'Authorization': token ? 'Bearer ' + token : '' },
  158. success: function(res) { if (res.data.code === 200) resolve(res.data); else reject(res.data) },
  159. fail: function(err) { reject(err) }
  160. })
  161. })
  162. }
  163. </script>
  164. <style scoped>
  165. .sleep-container { min-height: 100vh; background: #f5f7fa; padding-bottom: 60rpx; }
  166. .nav-bar { position: sticky; top: 0; background: #fff; display: flex; flex-direction: row; align-items: center; justify-content: space-between; padding: 20rpx 30rpx; box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06); z-index: 100; }
  167. .nav-back { font-size: 32rpx; color: #5B9BD5; }
  168. .nav-title { font-size: 32rpx; font-weight: bold; color: #333; }
  169. .nav-placeholder { width: 80rpx; }
  170. .section-card { margin: 20rpx 30rpx; background: #fff; border-radius: 20rpx; padding: 24rpx; box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06); }
  171. .section-title { font-size: 30rpx; font-weight: bold; color: #333; margin-bottom: 20rpx; }
  172. .input-row { display: flex; flex-direction: row; align-items: center; margin-bottom: 16rpx; }
  173. .input-label { font-size: 26rpx; color: #666; width: 140rpx; flex-shrink: 0; }
  174. .input-field { flex: 1; background: #f5f7fa; border-radius: 12rpx; padding: 14rpx 16rpx; font-size: 28rpx; }
  175. .picker-display { background: #f5f7fa; border-radius: 12rpx; padding: 14rpx 16rpx; font-size: 28rpx; color: #333; min-width: 200rpx; }
  176. .quality-options { display: flex; flex-direction: row; }
  177. .quality-item { display: flex; flex-direction: column; align-items: center; padding: 12rpx 20rpx; border-radius: 12rpx; background: #f0f0f0; margin-right: 12rpx; }
  178. .quality-active { background: #E3F2FD; border: 2rpx solid #5B9BD5; }
  179. .quality-emoji { font-size: 32rpx; }
  180. .quality-label { font-size: 20rpx; color: #666; margin-top: 4rpx; }
  181. .submit-btn { background: linear-gradient(135deg, #5B9BD5, #7EB8E0); color: #fff; font-size: 32rpx; font-weight: bold; text-align: center; padding: 22rpx; border-radius: 40rpx; margin-top: 16rpx; }
  182. .sleep-stats { display: flex; flex-direction: row; justify-content: space-around; }
  183. .stat-item { display: flex; flex-direction: column; align-items: center; }
  184. .stat-value { font-size: 40rpx; font-weight: bold; color: #5B9BD5; }
  185. .stat-label { font-size: 22rpx; color: #999; }
  186. .history-list { display: flex; flex-direction: column; }
  187. .history-item { display: flex; flex-direction: row; align-items: center; padding: 14rpx 0; border-bottom: 1rpx solid #f5f5f5; }
  188. .history-info { flex: 1; }
  189. .history-date { font-size: 24rpx; color: #999; display: block; }
  190. .history-detail { font-size: 28rpx; color: #333; }
  191. .history-quality { display: flex; flex-direction: column; align-items: center; }
  192. .history-quality text { font-size: 28rpx; }
  193. .quality-num { font-size: 22rpx; color: #999; }
  194. .history-empty { text-align: center; padding: 30rpx; color: #999; font-size: 26rpx; }
  195. .bottom-spacer { height: 60rpx; }
  196. </style>