sleep-record.vue 10 KB

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