sleep-checkin.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <template>
  2. <scroll-view class="content" scroll-y>
  3. <!-- 睡眠表单 -->
  4. <view class="card">
  5. <view class="card-accent"></view>
  6. <view class="card-body">
  7. <view class="form-group">
  8. <text class="form-label">入睡时间 *</text>
  9. <picker mode="time" :value="form.sleepTime" @change="function(e){form.sleepTime=e.detail.value}">
  10. <view class="picker-display">{{ form.sleepTime || '选择入睡时间' }}</view>
  11. </picker>
  12. </view>
  13. <view class="form-group">
  14. <text class="form-label">醒来时间 *</text>
  15. <picker mode="time" :value="form.wakeTime" @change="function(e){form.wakeTime=e.detail.value}">
  16. <view class="picker-display">{{ form.wakeTime || '选择醒来时间' }}</view>
  17. </picker>
  18. </view>
  19. <view class="form-group">
  20. <text class="form-label">睡眠质量(1-10)</text>
  21. <view class="slider-row">
  22. <text class="slider-min">差</text>
  23. <slider min="1" max="10" :value="form.qualityScore" @change="function(e){form.qualityScore=e.detail.value}" block-size="20" activeColor="#FF8C42" />
  24. <text class="slider-val">{{ form.qualityScore }}</text>
  25. <text class="slider-max">好</text>
  26. </view>
  27. </view>
  28. <view class="form-group">
  29. <text class="form-label">备注(选填)</text>
  30. <input class="form-input" v-model="form.remark" placeholder="睡眠感受..." />
  31. </view>
  32. </view>
  33. </view>
  34. <!-- 提交按钮 -->
  35. <view class="submit-wrap">
  36. <view class="submit-btn" :class="{ 'submitting': submitting }" @tap="doCheckin">
  37. <text>{{ submitting ? '提交中...' : '✓ 完成打卡' }}</text>
  38. </view>
  39. </view>
  40. <!-- 历史记录 -->
  41. <view class="card" v-if="history.length > 0">
  42. <view class="card-accent"></view>
  43. <view class="card-body">
  44. <text class="card-title">近期睡眠</text>
  45. <view class="history-list">
  46. <view class="history-item" v-for="item in history" :key="item.id">
  47. <view class="history-left">
  48. <text class="history-type">{{ item.sleepTime }} - {{ item.wakeTime }}</text>
  49. <text class="history-content">{{ item.durationMinutes }}分钟 · 质量{{ item.qualityScore }}分</text>
  50. </view>
  51. <text class="history-date">{{ formatDate(item.createdAt) }}</text>
  52. </view>
  53. </view>
  54. </view>
  55. </view>
  56. </scroll-view>
  57. </template>
  58. <script>
  59. import config from '../../config'
  60. import { parseDate } from '../../utils/format.js'
  61. export default {
  62. data() {
  63. return {
  64. memberId: null,
  65. submitting: false,
  66. history: [],
  67. form: {
  68. sleepTime: '21:00',
  69. wakeTime: '07:00',
  70. qualityScore: 7,
  71. remark: ''
  72. }
  73. }
  74. },
  75. onLoad: function() {
  76. var role = uni.getStorageSync('currentRole') || uni.getStorageSync('role') || 'parent'
  77. if (role !== 'child') {
  78. this.memberId = uni.getStorageSync('currentChildId') || uni.getStorageSync('currentMemberId') || uni.getStorageSync('userId') || null
  79. }
  80. this.loadHistory()
  81. },
  82. methods: {
  83. goBack: function() { uni.navigateBack() },
  84. formatDate: function(dateStr) {
  85. if (!dateStr) return ''
  86. var d = parseDate(dateStr)
  87. if (!d) return dateStr
  88. var m2 = (d.getMonth() + 1).toString().padStart(2, '0')
  89. var day = d.getDate().toString().padStart(2, '0')
  90. return m2 + '-' + day
  91. },
  92. loadHistory: function() {
  93. var self = this
  94. var token = uni.getStorageSync('token')
  95. if (!token || !self.memberId) return
  96. uni.request({
  97. url: config.API_BASE_URL + '/api/health/sleep/list',
  98. method: 'POST',
  99. data: { memberId: self.memberId },
  100. header: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
  101. success: function(res) {
  102. if (res.data && res.data.code === 200 && res.data.data) {
  103. self.history = res.data.data.slice(0, 5)
  104. }
  105. }
  106. })
  107. },
  108. async doCheckin() {
  109. var self = this
  110. if (!self.form.sleepTime) { uni.showToast({ title: '请选择入睡时间', icon: 'none' }); return }
  111. if (!self.form.wakeTime) { uni.showToast({ title: '请选择醒来时间', icon: 'none' }); return }
  112. if (!self.memberId) { uni.showToast({ title: '孩子ID无效', icon: 'none' }); return }
  113. self.submitting = true
  114. try {
  115. var sleepParts = self.form.sleepTime.split(':')
  116. var wakeParts = self.form.wakeTime.split(':')
  117. var sleepMin = parseInt(sleepParts[0]) * 60 + parseInt(sleepParts[1])
  118. var wakeMin = parseInt(wakeParts[0]) * 60 + parseInt(wakeParts[1])
  119. var duration = wakeMin > sleepMin ? wakeMin - sleepMin : (24 * 60 - sleepMin + wakeMin)
  120. var token = uni.getStorageSync('token')
  121. uni.request({
  122. url: config.API_BASE_URL + '/api/health/sleep/create',
  123. method: 'POST',
  124. data: {
  125. memberId: self.memberId,
  126. sleepTime: self.form.sleepTime,
  127. wakeTime: self.form.wakeTime,
  128. durationMinutes: duration,
  129. qualityScore: self.form.qualityScore,
  130. remark: self.form.remark || ''
  131. },
  132. header: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
  133. success: function(res) {
  134. self.submitting = false
  135. if (res.data && res.data.code === 200) {
  136. uni.showToast({ title: '打卡成功', icon: 'success' })
  137. self.form.sleepTime = '21:00'
  138. self.form.wakeTime = '07:00'
  139. self.form.qualityScore = 7
  140. self.form.remark = ''
  141. self.loadHistory()
  142. } else {
  143. uni.showToast({ title: (res.data && res.data.message) || '打卡失败', icon: 'none' })
  144. }
  145. },
  146. fail: function() {
  147. self.submitting = false
  148. uni.showToast({ title: '网络错误', icon: 'none' })
  149. }
  150. })
  151. } catch (e) {
  152. self.submitting = false
  153. uni.showToast({ title: '提交失败', icon: 'none' })
  154. }
  155. }
  156. }
  157. }
  158. </script>
  159. <style scoped>
  160. .page { min-height: 100vh; background: #FFF7ED; }
  161. .content { height: calc(100vh - 88rpx); }
  162. .card { margin: 16rpx 24rpx; background: #fff; border-radius: 20rpx; overflow: hidden; box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06); }
  163. .card-accent { height: 6rpx; background: linear-gradient(90deg, #FF8C42, #F97316); }
  164. .card-body { padding: 28rpx; }
  165. .card-title { font-size: 30rpx; font-weight: bold; color: #333; margin-bottom: 20rpx; display: block; }
  166. .form-group { margin-bottom: 24rpx; }
  167. .form-label { font-size: 28rpx; color: #333; font-weight: 600; margin-bottom: 12rpx; display: block; }
  168. .picker-display { border: 1rpx solid #ddd; border-radius: 16rpx; padding: 20rpx; font-size: 28rpx; background: #FAFAFA; color: #333; min-height: 40rpx; }
  169. .slider-row { display: flex; align-items: center; gap: 12rpx; }
  170. .slider-min { font-size: 24rpx; color: #999; flex-shrink: 0; }
  171. .slider-val { font-size: 28rpx; color: #FF8C42; font-weight: bold; width: 40rpx; text-align: center; }
  172. .slider-max { font-size: 24rpx; color: #999; flex-shrink: 0; }
  173. .form-input { width: 100%; height: 80rpx; border: 1rpx solid #e0e0e0; border-radius: 16rpx; padding: 0 20rpx; font-size: 28rpx; background: #FAFAFA; box-sizing: border-box; }
  174. .submit-wrap { padding: 24rpx 24rpx 40rpx; }
  175. .submit-btn { background: linear-gradient(135deg, #FF8C42, #F97316); color: #fff; text-align: center; padding: 24rpx 0; border-radius: 16rpx; font-size: 32rpx; font-weight: bold; }
  176. .submitting { opacity: 0.6; }
  177. .history-list { display: flex; flex-direction: column; gap: 16rpx; }
  178. .history-item { display: flex; justify-content: space-between; align-items: center; padding: 12rpx 0; border-bottom: 1rpx solid #f5f5f5; }
  179. .history-left { flex: 1; min-width: 0; }
  180. .history-type { font-size: 28rpx; font-weight: 500; color: #333; display: block; }
  181. .history-content { font-size: 24rpx; color: #888; display: block; margin-top: 4rpx; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  182. .history-date { font-size: 24rpx; color: #999; flex-shrink: 0; margin-left: 16rpx; }
  183. </style>