sleep-checkin.vue 9.7 KB

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