| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <template>
- <scroll-view class="content" scroll-y>
- <!-- 睡眠表单 -->
- <view class="card">
- <view class="card-accent"></view>
- <view class="card-body">
- <view class="form-group">
- <text class="form-label">入睡时间 *</text>
- <picker mode="time" :value="form.sleepTime" @change="function(e){form.sleepTime=e.detail.value}">
- <view class="picker-display">{{ form.sleepTime || '选择入睡时间' }}</view>
- </picker>
- </view>
- <view class="form-group">
- <text class="form-label">醒来时间 *</text>
- <picker mode="time" :value="form.wakeTime" @change="function(e){form.wakeTime=e.detail.value}">
- <view class="picker-display">{{ form.wakeTime || '选择醒来时间' }}</view>
- </picker>
- </view>
- <view class="form-group">
- <text class="form-label">睡眠质量(1-10)</text>
- <view class="slider-row">
- <text class="slider-min">差</text>
- <slider min="1" max="10" :value="form.qualityScore" @change="function(e){form.qualityScore=e.detail.value}" block-size="20" activeColor="#FF8C42" />
- <text class="slider-val">{{ form.qualityScore }}</text>
- <text class="slider-max">好</text>
- </view>
- </view>
- <view class="form-group">
- <text class="form-label">备注(选填)</text>
- <input class="form-input" v-model="form.remark" placeholder="睡眠感受..." />
- </view>
- </view>
- </view>
- <!-- 提交按钮 -->
- <view class="submit-wrap">
- <view class="submit-btn" :class="{ 'submitting': submitting }" @tap="doCheckin">
- <text>{{ submitting ? '提交中...' : '✓ 完成打卡' }}</text>
- </view>
- </view>
- <!-- 历史记录 -->
- <view class="card" v-if="history.length > 0">
- <view class="card-accent"></view>
- <view class="card-body">
- <text class="card-title">近期睡眠</text>
- <view class="history-list">
- <view class="history-item" v-for="item in history" :key="item.id">
- <view class="history-left">
- <text class="history-type">{{ item.sleepTime }} - {{ item.wakeTime }}</text>
- <text class="history-content">{{ item.durationMinutes }}分钟 · 质量{{ item.qualityScore }}分</text>
- </view>
- <text class="history-date">{{ formatDate(item.createdAt) }}</text>
- </view>
- </view>
- </view>
- </view>
- </scroll-view>
- </template>
- <script>
- import config from '../../config'
- import { parseDate } from '../../utils/format.js'
- export default {
- data() {
- return {
- memberId: null,
- submitting: false,
- history: [],
- form: {
- sleepTime: '21:00',
- wakeTime: '07:00',
- qualityScore: 7,
- remark: ''
- }
- }
- },
- onLoad: function() {
- var role = uni.getStorageSync('currentRole') || uni.getStorageSync('role') || 'parent'
- if (role !== 'child') {
- this.memberId = uni.getStorageSync('currentChildId') || uni.getStorageSync('currentMemberId') || uni.getStorageSync('userId') || null
- }
- this.loadHistory()
- },
- methods: {
- goBack: function() { uni.navigateBack() },
- formatDate: function(dateStr) {
- if (!dateStr) return ''
- var d = parseDate(dateStr)
- if (!d) return dateStr
- var m2 = (d.getMonth() + 1).toString().padStart(2, '0')
- var day = d.getDate().toString().padStart(2, '0')
- return m2 + '-' + day
- },
- loadHistory: function() {
- var self = this
- var token = uni.getStorageSync('token')
- if (!token || !self.memberId) return
- uni.request({
- url: config.API_BASE_URL + '/api/health/sleep/list',
- method: 'POST',
- data: { memberId: self.memberId },
- header: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
- success: function(res) {
- if (res.data && res.data.code === 200 && res.data.data) {
- self.history = res.data.data.slice(0, 5)
- }
- }
- })
- },
- async doCheckin() {
- var self = this
- if (!self.form.sleepTime) { uni.showToast({ title: '请选择入睡时间', icon: 'none' }); return }
- if (!self.form.wakeTime) { uni.showToast({ title: '请选择醒来时间', icon: 'none' }); return }
- if (!self.memberId) { uni.showToast({ title: '孩子ID无效', icon: 'none' }); return }
- self.submitting = true
- try {
- var sleepParts = self.form.sleepTime.split(':')
- var wakeParts = self.form.wakeTime.split(':')
- var sleepMin = parseInt(sleepParts[0]) * 60 + parseInt(sleepParts[1])
- var wakeMin = parseInt(wakeParts[0]) * 60 + parseInt(wakeParts[1])
- var duration = wakeMin > sleepMin ? wakeMin - sleepMin : (24 * 60 - sleepMin + wakeMin)
- var token = uni.getStorageSync('token')
- uni.request({
- url: config.API_BASE_URL + '/api/health/sleep/create',
- method: 'POST',
- data: {
- memberId: self.memberId,
- sleepTime: self.form.sleepTime,
- wakeTime: self.form.wakeTime,
- durationMinutes: duration,
- qualityScore: self.form.qualityScore,
- remark: self.form.remark || ''
- },
- header: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
- success: function(res) {
- self.submitting = false
- if (res.data && res.data.code === 200) {
- uni.showToast({ title: '打卡成功', icon: 'success' })
- self.form.sleepTime = '21:00'
- self.form.wakeTime = '07:00'
- self.form.qualityScore = 7
- self.form.remark = ''
- self.loadHistory()
- } else {
- uni.showToast({ title: (res.data && res.data.message) || '打卡失败', icon: 'none' })
- }
- },
- fail: function() {
- self.submitting = false
- uni.showToast({ title: '网络错误', icon: 'none' })
- }
- })
- } catch (e) {
- self.submitting = false
- uni.showToast({ title: '提交失败', icon: 'none' })
- }
- }
- }
- }
- </script>
- <style scoped>
- .page { min-height: 100vh; background: #FFF7ED; }
- .content { height: calc(100vh - 88rpx); }
- .card { margin: 16rpx 24rpx; background: #fff; border-radius: 20rpx; overflow: hidden; box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06); }
- .card-accent { height: 6rpx; background: linear-gradient(90deg, #FF8C42, #F97316); }
- .card-body { padding: 28rpx; }
- .card-title { font-size: 30rpx; font-weight: bold; color: #333; margin-bottom: 20rpx; display: block; }
- .form-group { margin-bottom: 24rpx; }
- .form-label { font-size: 28rpx; color: #333; font-weight: 600; margin-bottom: 12rpx; display: block; }
- .picker-display { border: 1rpx solid #ddd; border-radius: 16rpx; padding: 20rpx; font-size: 28rpx; background: #FAFAFA; color: #333; min-height: 40rpx; }
- .slider-row { display: flex; align-items: center; gap: 12rpx; }
- .slider-min { font-size: 24rpx; color: #999; flex-shrink: 0; }
- .slider-val { font-size: 28rpx; color: #FF8C42; font-weight: bold; width: 40rpx; text-align: center; }
- .slider-max { font-size: 24rpx; color: #999; flex-shrink: 0; }
- .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; }
- .submit-wrap { padding: 24rpx 24rpx 40rpx; }
- .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; }
- .submitting { opacity: 0.6; }
- .history-list { display: flex; flex-direction: column; gap: 16rpx; }
- .history-item { display: flex; justify-content: space-between; align-items: center; padding: 12rpx 0; border-bottom: 1rpx solid #f5f5f5; }
- .history-left { flex: 1; min-width: 0; }
- .history-type { font-size: 28rpx; font-weight: 500; color: #333; display: block; }
- .history-content { font-size: 24rpx; color: #888; display: block; margin-top: 4rpx; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
- .history-date { font-size: 24rpx; color: #999; flex-shrink: 0; margin-left: 16rpx; }
- </style>
|