sleep-checkin.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. <text class="card-title">记录睡眠</text>
  15. <view class="form-group">
  16. <text class="form-label">入睡时间 *</text>
  17. <picker mode="time" :value="form.sleepTime" @change="function(e){form.sleepTime=e.detail.value}">
  18. <view class="picker-display">{{ form.sleepTime || '选择入睡时间' }}</view>
  19. </picker>
  20. </view>
  21. <view class="form-group">
  22. <text class="form-label">醒来时间 *</text>
  23. <picker mode="time" :value="form.wakeTime" @change="function(e){form.wakeTime=e.detail.value}">
  24. <view class="picker-display">{{ form.wakeTime || '选择醒来时间' }}</view>
  25. </picker>
  26. </view>
  27. <view class="form-group">
  28. <text class="form-label">睡眠质量(1-10)</text>
  29. <view class="slider-row">
  30. <text class="slider-min">差</text>
  31. <slider min="1" max="10" :value="form.qualityScore" @change="function(e){form.qualityScore=e.detail.value}" block-size="20" activeColor="#FF8C42" />
  32. <text class="slider-val">{{ form.qualityScore }}</text>
  33. <text class="slider-max">好</text>
  34. </view>
  35. </view>
  36. <view class="form-group">
  37. <text class="form-label">备注(选填)</text>
  38. <input class="form-input" v-model="form.remark" placeholder="睡眠感受..." />
  39. </view>
  40. </view>
  41. </view>
  42. <!-- 照片上传 -->
  43. <view class="card">
  44. <view class="card-accent"></view>
  45. <view class="card-body">
  46. <view class="section-header">
  47. <text class="card-title">📷 拍照记录</text>
  48. <text class="photo-count">{{ form.photos.length }}/9</text>
  49. </view>
  50. <view class="photo-grid">
  51. <view class="photo-thumb" v-for="(img, idx) in form.photos" :key="getPhotoKey(img, idx)" @tap="removePhoto(idx)">
  52. <image :src="img" mode="aspectFill" class="photo-img" />
  53. <view class="photo-delete">
  54. <text class="photo-delete-icon">×</text>
  55. </view>
  56. </view>
  57. <view class="photo-add-btn" v-if="form.photos.length < 9" @tap="choosePhoto">
  58. <text class="camera-icon">📷</text>
  59. </view>
  60. </view>
  61. </view>
  62. </view>
  63. <!-- 语音录入 -->
  64. <view class="card">
  65. <view class="card-accent"></view>
  66. <view class="card-body">
  67. <text class="card-title">🎙️ 语音打卡</text>
  68. <view class="voice-recorder">
  69. <view class="voice-active" v-if="isRecording">
  70. <view class="waveform">
  71. <view class="wave-bar wave-1"></view>
  72. <view class="wave-bar wave-2"></view>
  73. <view class="wave-bar wave-3"></view>
  74. <view class="wave-bar wave-4"></view>
  75. <view class="wave-bar wave-5"></view>
  76. </view>
  77. <text class="recording-time">{{ recordSeconds }}s</text>
  78. </view>
  79. <view class="voice-done" v-else-if="form.voicePath">
  80. <text class="play-icon">▶</text>
  81. <text class="done-text">{{ recordDurationText }}</text>
  82. </view>
  83. <view class="record-btn-wrap">
  84. <view class="pulse-ring" v-if="isRecording"></view>
  85. <view class="record-btn" :class="{ 'record-btn-active': isRecording }" @tap="toggleRecord">
  86. <view class="record-btn-dot"></view>
  87. </view>
  88. </view>
  89. </view>
  90. <text class="voice-hint">点击开始录音</text>
  91. </view>
  92. </view>
  93. <!-- 提交按钮 -->
  94. <view class="submit-wrap">
  95. <view class="submit-btn" :class="{ 'submitting': submitting }" @tap="doCheckin">
  96. <text>{{ submitting ? '提交中...' : '✓ 完成打卡' }}</text>
  97. </view>
  98. </view>
  99. <!-- 历史记录 -->
  100. <view class="card" v-if="history.length > 0">
  101. <view class="card-accent"></view>
  102. <view class="card-body">
  103. <text class="card-title">近期睡眠</text>
  104. <view class="history-list">
  105. <view class="history-item" v-for="item in history" :key="item.id">
  106. <view class="history-left">
  107. <text class="history-type">{{ item.sleepTime }} - {{ item.wakeTime }}</text>
  108. <text class="history-content">{{ item.durationMinutes }}分钟 · 质量{{ item.qualityScore }}分</text>
  109. </view>
  110. <text class="history-date">{{ formatDate(item.createdAt) }}</text>
  111. </view>
  112. </view>
  113. </view>
  114. </view>
  115. </scroll-view>
  116. </view>
  117. </template>
  118. <script>
  119. import config from '../../config'
  120. import { parseDate } from '../../utils/format.js'
  121. export default {
  122. data() {
  123. return {
  124. memberId: null,
  125. submitting: false,
  126. isRecording: false,
  127. recordSeconds: 0,
  128. recordTimer: null,
  129. recorderManager: null,
  130. history: [],
  131. form: {
  132. sleepTime: '21:00',
  133. wakeTime: '07:00',
  134. qualityScore: 7,
  135. remark: '',
  136. photos: [],
  137. voicePath: ''
  138. }
  139. }
  140. },
  141. computed: {
  142. recordDurationText: function() {
  143. if (!this.form.voicePath) return ''
  144. return '录音 ' + this.recordSeconds + '秒'
  145. }
  146. },
  147. onLoad: function() {
  148. var role = uni.getStorageSync('currentRole') || uni.getStorageSync('role') || 'parent'
  149. if (role !== 'child') {
  150. this.memberId = uni.getStorageSync('currentChildId') || uni.getStorageSync('currentMemberId') || uni.getStorageSync('userId') || null
  151. }
  152. this.recorderManager = uni.getRecorderManager()
  153. this.recorderManager.onStart(() => {
  154. this.isRecording = true
  155. this.recordSeconds = 0
  156. this.recordTimer = setInterval(() => { this.recordSeconds++ }, 1000)
  157. })
  158. this.recorderManager.onStop((res) => {
  159. this.isRecording = false
  160. if (this.recordTimer) { clearInterval(this.recordTimer); this.recordTimer = null }
  161. this.form.voicePath = res.tempFilePath
  162. this.form.voiceDuration = res.duration
  163. })
  164. this.recorderManager.onError(() => {
  165. this.isRecording = false
  166. if (this.recordTimer) { clearInterval(this.recordTimer); this.recordTimer = null }
  167. uni.showToast({ title: '录音失败', icon: 'none' })
  168. })
  169. this.loadHistory()
  170. },
  171. onUnload: function() {
  172. if (this.recordTimer) clearInterval(this.recordTimer)
  173. },
  174. methods: {
  175. goBack: function() { uni.navigateBack() },
  176. formatDate: function(dateStr) {
  177. if (!dateStr) return ''
  178. var d = parseDate(dateStr)
  179. if (!d) return dateStr
  180. var m2 = (d.getMonth() + 1).toString().padStart(2, '0')
  181. var day = d.getDate().toString().padStart(2, '0')
  182. return m2 + '-' + day
  183. },
  184. loadHistory: function() {
  185. var self = this
  186. var token = uni.getStorageSync('token')
  187. if (!token || !self.memberId) return
  188. uni.request({
  189. url: config.API_BASE_URL + '/api/health/sleep/list',
  190. method: 'POST',
  191. data: { memberId: self.memberId },
  192. header: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
  193. success: function(res) {
  194. if (res.data && res.data.code === 200 && res.data.data) {
  195. self.history = res.data.data.slice(0, 5)
  196. }
  197. }
  198. })
  199. },
  200. choosePhoto: function() {
  201. var self = this
  202. uni.chooseImage({
  203. count: 9 - self.form.photos.length,
  204. sizeType: ['compressed'],
  205. sourceType: ['album', 'camera'],
  206. success: function(res) {
  207. res.tempFilePaths.forEach(function(path) {
  208. if (self.form.photos.length < 9) self.form.photos.push(path)
  209. })
  210. }
  211. })
  212. },
  213. getPhotoKey: function(img, idx) {
  214. return img ? img : ('p' + idx)
  215. },
  216. removePhoto: function(idx) {
  217. this.form.photos.splice(idx, 1)
  218. },
  219. toggleRecord: function() {
  220. if (this.isRecording) {
  221. this.recorderManager.stop()
  222. } else {
  223. this.recorderManager.start({ duration: 60000, format: 'mp3' })
  224. }
  225. },
  226. uploadFile: function(filePath) {
  227. var self = this
  228. return new Promise(function(resolve, reject) {
  229. uni.uploadFile({
  230. url: config.API_BASE_URL + '/api/media/upload',
  231. filePath: filePath,
  232. name: 'file',
  233. header: { 'Authorization': 'Bearer ' + uni.getStorageSync('token') },
  234. success: function(res) {
  235. try { var data = JSON.parse(res.data); resolve(data.data || data.url || '') }
  236. catch (e) { resolve(res.data || '') }
  237. },
  238. fail: function() { reject() }
  239. })
  240. })
  241. },
  242. async doCheckin() {
  243. var self = this
  244. if (!self.form.sleepTime) { uni.showToast({ title: '请选择入睡时间', icon: 'none' }); return }
  245. if (!self.form.wakeTime) { uni.showToast({ title: '请选择醒来时间', icon: 'none' }); return }
  246. if (!self.memberId) { uni.showToast({ title: '孩子ID无效', icon: 'none' }); return }
  247. self.submitting = true
  248. try {
  249. var photoUrls = []
  250. if (self.form.photos && self.form.photos.length > 0) {
  251. for (var i = 0; i < self.form.photos.length; i++) {
  252. try { var url = await self.uploadFile(self.form.photos[i]) } catch(e) { url = '' }
  253. if (url) photoUrls.push(url)
  254. }
  255. }
  256. var voiceUrl = ''
  257. if (self.form.voicePath) {
  258. try { voiceUrl = await self.uploadFile(self.form.voicePath) } catch(e) { voiceUrl = '' }
  259. }
  260. var sleepParts = self.form.sleepTime.split(':')
  261. var wakeParts = self.form.wakeTime.split(':')
  262. var sleepMin = parseInt(sleepParts[0]) * 60 + parseInt(sleepParts[1])
  263. var wakeMin = parseInt(wakeParts[0]) * 60 + parseInt(wakeParts[1])
  264. var duration = wakeMin > sleepMin ? wakeMin - sleepMin : (24 * 60 - sleepMin + wakeMin)
  265. var token = uni.getStorageSync('token')
  266. uni.request({
  267. url: config.API_BASE_URL + '/api/health/sleep/create',
  268. method: 'POST',
  269. data: {
  270. memberId: self.memberId,
  271. sleepTime: self.form.sleepTime,
  272. wakeTime: self.form.wakeTime,
  273. durationMinutes: duration,
  274. qualityScore: self.form.qualityScore,
  275. remark: self.form.remark || '',
  276. photoUrls: photoUrls.join(','),
  277. voiceUrl: voiceUrl
  278. },
  279. header: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
  280. success: function(res) {
  281. self.submitting = false
  282. if (res.data && res.data.code === 200) {
  283. uni.showToast({ title: '打卡成功', icon: 'success' })
  284. self.form.sleepTime = '21:00'
  285. self.form.wakeTime = '07:00'
  286. self.form.qualityScore = 7
  287. self.form.remark = ''
  288. self.form.photos = []
  289. self.form.voicePath = ''
  290. self.recordSeconds = 0
  291. self.loadHistory()
  292. } else {
  293. uni.showToast({ title: (res.data && res.data.message) || '打卡失败', icon: 'none' })
  294. }
  295. },
  296. fail: function() {
  297. self.submitting = false
  298. uni.showToast({ title: '网络错误', icon: 'none' })
  299. }
  300. })
  301. } catch (e) {
  302. self.submitting = false
  303. uni.showToast({ title: '提交失败', icon: 'none' })
  304. }
  305. }
  306. }
  307. }
  308. </script>
  309. <style scoped>
  310. .page { min-height: 100vh; background: #FFF7ED; }
  311. .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; }
  312. .nav-back { position: absolute; left: 24rpx; top: 0; bottom: 0; display: flex; align-items: center; }
  313. .back-text { font-size: 28rpx; color: #F97316; }
  314. .nav-title { flex: 1; text-align: center; font-size: 32rpx; font-weight: bold; color: #333; }
  315. .content { height: calc(100vh - 88rpx); }
  316. .card { margin: 16rpx 24rpx; background: #fff; border-radius: 20rpx; overflow: hidden; box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06); }
  317. .card-accent { height: 6rpx; background: linear-gradient(90deg, #FF8C42, #F97316); }
  318. .card-body { padding: 24rpx; }
  319. .card-title { font-size: 30rpx; font-weight: bold; color: #333; margin-bottom: 20rpx; display: block; }
  320. .form-group { margin-bottom: 20rpx; }
  321. .form-label { font-size: 26rpx; color: #333; font-weight: 500; margin-bottom: 12rpx; display: block; }
  322. .picker-display { border: 1rpx solid #ddd; border-radius: 12rpx; padding: 16rpx 20rpx; font-size: 26rpx; background: #FAFAFA; color: #333; }
  323. .slider-row { display: flex; align-items: center; gap: 12rpx; }
  324. .slider-min { font-size: 22rpx; color: #999; flex-shrink: 0; }
  325. .slider-val { font-size: 24rpx; color: #FF8C42; font-weight: bold; width: 40rpx; text-align: center; }
  326. .slider-max { font-size: 22rpx; color: #999; flex-shrink: 0; }
  327. .form-input { width: 100%; border: 1rpx solid #e0e0e0; border-radius: 12rpx; padding: 16rpx; font-size: 26rpx; background: #FAFAFA; box-sizing: border-box; }
  328. .section-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20rpx; }
  329. .section-header .card-title { margin-bottom: 0; }
  330. .photo-count { font-size: 24rpx; color: #999; }
  331. .photo-grid { display: flex; flex-wrap: wrap; gap: 16rpx; }
  332. .photo-thumb { position: relative; width: 160rpx; height: 160rpx; }
  333. .photo-img { width: 100%; height: 100%; border-radius: 16rpx; }
  334. .photo-delete { position: absolute; top: 0; right: 0; width: 36rpx; height: 36rpx; background: #EF4444; border-radius: 50%; display: flex; align-items: center; justify-content: center; box-shadow: 0 2rpx 8rpx rgba(239,68,68,0.3); }
  335. .photo-delete-icon { color: #fff; font-size: 24rpx; font-weight: bold; }
  336. .photo-add-btn { width: 160rpx; height: 160rpx; border-radius: 16rpx; background: linear-gradient(135deg, #FF8C42, #F97316); display: flex; align-items: center; justify-content: center; box-shadow: 0 4rpx 16rpx rgba(249,115,22,0.25); }
  337. .camera-icon { font-size: 52rpx; }
  338. .voice-recorder { display: flex; flex-direction: column; align-items: center; padding: 24rpx 0 16rpx; }
  339. .voice-active { display: flex; flex-direction: column; align-items: center; margin-bottom: 20rpx; }
  340. .waveform { display: flex; align-items: center; justify-content: center; gap: 8rpx; height: 48rpx; }
  341. .wave-bar { width: 6rpx; height: 8rpx; background: #EF4444; border-radius: 3rpx; }
  342. .wave-1 { animation: waveform-anim 0.8s ease-in-out infinite 0s; }
  343. .wave-2 { animation: waveform-anim 0.8s ease-in-out infinite 0.1s; }
  344. .wave-3 { animation: waveform-anim 0.8s ease-in-out infinite 0.2s; }
  345. .wave-4 { animation: waveform-anim 0.8s ease-in-out infinite 0.3s; }
  346. .wave-5 { animation: waveform-anim 0.8s ease-in-out infinite 0.4s; }
  347. .recording-time { font-size: 28rpx; color: #EF4444; font-weight: bold; margin-top: 8rpx; }
  348. .voice-done { display: flex; align-items: center; gap: 12rpx; margin-bottom: 20rpx; padding: 12rpx 24rpx; background: #FFF7ED; border-radius: 30rpx; }
  349. .play-icon { font-size: 24rpx; color: #F97316; }
  350. .done-text { font-size: 26rpx; color: #666; }
  351. .record-btn-wrap { position: relative; width: 80rpx; height: 80rpx; }
  352. .pulse-ring { position: absolute; top: 0; left: 0; width: 80rpx; height: 80rpx; border-radius: 50%; border: 4rpx solid #EF4444; animation: pulse-expand 1.5s ease-out infinite; }
  353. .record-btn { position: relative; width: 80rpx; height: 80rpx; border-radius: 50%; background: linear-gradient(135deg, #FF8C42, #F97316); display: flex; align-items: center; justify-content: center; box-shadow: 0 4rpx 16rpx rgba(249,115,22,0.3); z-index: 1; }
  354. .record-btn-active { background: linear-gradient(135deg, #EF4444, #DC2626); box-shadow: 0 4rpx 16rpx rgba(239,68,68,0.4); animation: btn-pulse 1s ease-in-out infinite; }
  355. .record-btn-dot { width: 24rpx; height: 24rpx; border-radius: 50%; background: #fff; }
  356. .record-btn-active .record-btn-dot { width: 20rpx; height: 20rpx; border-radius: 4rpx; background: #fff; }
  357. .voice-hint { font-size: 22rpx; color: #999; text-align: center; }
  358. @keyframes waveform-anim { 0%, 100% { height: 8rpx; } 50% { height: 40rpx; } }
  359. @keyframes pulse-expand { 0% { transform: scale(1); opacity: 0.6; } 100% { transform: scale(1.8); opacity: 0; } }
  360. @keyframes btn-pulse { 0%, 100% { box-shadow: 0 4rpx 16rpx rgba(239,68,68,0.4); } 50% { box-shadow: 0 4rpx 24rpx rgba(239,68,68,0.6); } }
  361. .submit-wrap { padding: 24rpx 24rpx 40rpx; }
  362. .submit-btn { background: linear-gradient(135deg, #FF8C42, #F97316); color: #fff; text-align: center; padding: 24rpx 0; border-radius: 16rpx; font-size: 30rpx; font-weight: bold; }
  363. .submitting { opacity: 0.6; }
  364. .history-list { display: flex; flex-direction: column; gap: 16rpx; }
  365. .history-item { display: flex; justify-content: space-between; align-items: center; padding: 12rpx 0; border-bottom: 1rpx solid #f5f5f5; }
  366. .history-left { flex: 1; min-width: 0; }
  367. .history-type { font-size: 26rpx; font-weight: 500; color: #333; display: block; }
  368. .history-content { font-size: 22rpx; color: #888; display: block; margin-top: 4rpx; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  369. .history-date { font-size: 22rpx; color: #999; flex-shrink: 0; margin-left: 16rpx; }
  370. </style>