sleep-checkin.vue 8.9 KB

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