sleep-checkin.vue 15 KB

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