| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537 |
- <template>
- <view class="page">
- <!-- 顶部导航栏 -->
- <view class="nav-bar">
- <view class="nav-left" @tap="goBack">
- <text class="nav-icon">‹</text>
- <text class="nav-title">运动打卡</text>
- </view>
- </view>
- <scroll-view class="content" scroll-y>
- <!-- 运动类型 -->
- <view class="card">
- <view class="card-header">
- <text class="card-icon">🏃</text>
- <text class="card-title">运动类型</text>
- </view>
- <view class="card-body">
- <view class="tag-row">
- <view class="tag-btn" v-for="t in exerciseTypes" :key="t.value"
- :class="form.exerciseType === t.value ? 'tag-active' : ''"
- @tap="form.exerciseType = t.value">
- <text>{{ t.label }}</text>
- </view>
- </view>
- </view>
- </view>
- <!-- 时长 & 强度 & 卡路里 -->
- <view class="card">
- <view class="card-header">
- <text class="card-icon">⏱️</text>
- <text class="card-title">运动详情</text>
- </view>
- <view class="card-body">
- <view class="form-group">
- <text class="form-label">时长(分钟)*</text>
- <input class="form-input" type="number" v-model="form.durationMinutes" placeholder="运动了多久?" />
- </view>
- <view class="form-group">
- <text class="form-label">强度</text>
- <view class="tag-row">
- <view class="tag-btn" v-for="s in intensityOptions" :key="s.value"
- :class="form.intensity === s.value ? 'tag-active' : ''"
- @tap="form.intensity = s.value">
- <text>{{ s.label }}</text>
- </view>
- </view>
- </view>
- <view class="form-group">
- <text class="form-label">消耗卡路里(选填)</text>
- <input class="form-input" type="number" v-model="form.caloriesBurned" placeholder="估算卡路里" />
- </view>
- <view class="form-group" v-if="isSelfCheckin">
- <text class="form-label">微信步数(选填)</text>
- <view class="step-sync-row">
- <input class="form-input step-input" type="number" v-model="form.stepCount" placeholder="手动填写步数" />
- <view class="sync-btn" :class="{'syncing': syncingSteps}" @tap="syncWechatSteps">
- <text>{{ syncingSteps ? '同步中…' : '同步微信步数' }}</text>
- </view>
- </view>
- <text class="step-hint" v-if="syncedStepText">{{ syncedStepText }}</text>
- </view>
- <view class="form-group">
- <text class="form-label">备注(选填)</text>
- <input class="form-input" v-model="form.remark" placeholder="运动感受..." />
- </view>
- </view>
- </view>
- <!-- 媒体工具栏 -->
- <view class="card">
- <view class="card-header">
- <text class="card-icon">📎</text>
- <text class="card-title">添加媒体</text>
- </view>
- <view class="card-body">
- <view class="media-toolbar">
- <view class="tool-btn" @tap="choosePhoto">
- <text class="tool-icon">📷</text>
- <text class="tool-label">照片</text>
- <text class="tool-count" v-if="form.photos.length > 0">{{ form.photos.length }}/9</text>
- </view>
- <view class="tool-divider"></view>
- <view class="tool-btn" @tap="toggleRecord">
- <text class="tool-icon">🎙</text>
- <text class="tool-label">{{ isRecording ? '录音中…' : (form.voicePath ? '已录制' : '录音') }}</text>
- <text class="tool-count" v-if="isRecording">{{ recordSeconds }}s</text>
- </view>
- </view>
- <!-- 照片缩略图 -->
- <view class="photo-strip" v-if="form.photos.length > 0">
- <view class="photo-item" v-for="(img, idx) in form.photos" :key="getItemKey(img, idx)" @tap="removePhoto(idx)">
- <image :src="img" mode="aspectFill" class="photo-img"/>
- <view class="photo-delete-overlay">
- <text class="delete-icon">×</text>
- </view>
- </view>
- </view>
- <!-- 录音状态 -->
- <view class="voice-strip" v-if="isRecording">
- <view class="waveform">
- <view class="wave-bar" v-for="n in 7" :key="n" :style="{animationDelay: (n * 0.1) + 's'}"></view>
- </view>
- <view class="voice-controls">
- <view class="mini-btn cancel-mini" @tap="cancelRecord"><text>取消</text></view>
- <view class="mini-btn done-mini" @tap="finishRecord"><text>完成</text></view>
- </view>
- </view>
- <view class="voice-strip" v-else-if="form.voicePath">
- <view class="voice-player">
- <text class="play-icon">▶</text>
- <text class="voice-duration">{{ recordDurationText }}</text>
- </view>
- <view class="delete-voice" @tap="deleteVoice"><text>×</text></view>
- </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-header">
- <text class="card-icon">📜</text>
- <text class="card-title">近期记录</text>
- </view>
- <view class="card-body">
- <view class="history-list">
- <view class="history-item" v-for="item in history" :key="item.id">
- <view class="history-left">
- <text class="history-type">{{ exerciseTypeLabel(item.exerciseType) }}</text>
- <text class="history-content">{{ item.durationMinutes }}分钟{{ item.caloriesBurned ? ' · ' + item.caloriesBurned + '千卡' : '' }}{{ item.stepCount ? ' · ' + item.stepCount + '步' : '' }}</text>
- </view>
- <view class="history-right">
- <text class="history-date">{{ formatDate(item.createdAt) }}</text>
- </view>
- </view>
- </view>
- </view>
- </view>
- <!-- 底部留白 -->
- <view class="bottom-spacer"></view>
- </scroll-view>
- </view>
- </template>
- <script>
- import config from '../../config'
- import { parseDate } from '../../utils/format.js'
- import { sttTranscribe } from '../../utils/api.js'
- export default {
- data() {
- return {
- memberId: null,
- isSelfCheckin: false,
- submitting: false,
- syncingSteps: false,
- syncedStepText: '',
- isRecording: false,
- recordSeconds: 0,
- recordTimer: null,
- recorderManager: null,
- history: [],
- exerciseTypes: [
- { label: '跑步', value: 'running' },
- { label: '游泳', value: 'swimming' },
- { label: '跳绳', value: 'jump_rope' },
- { label: '球类', value: 'ball' },
- { label: '骑行', value: 'cycling' },
- { label: '体操', value: 'gymnastics' },
- { label: '其他', value: 'other' }
- ],
- intensityOptions: [
- { label: '低', value: 'low' },
- { label: '中', value: 'medium' },
- { label: '高', value: 'high' }
- ],
- form: {
- exerciseType: '',
- durationMinutes: '',
- intensity: 'medium',
- caloriesBurned: '',
- stepCount: '',
- remark: '',
- photos: [],
- voicePath: '',
- voiceText: '',
- voiceDuration: 0
- }
- }
- },
- computed: {
- recordDurationText: function() {
- if (!this.form.voicePath) return ''
- return '录音 ' + this.recordSeconds + '秒'
- }
- },
- 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.isSelfCheckin = uni.getStorageSync('currentView') !== 'member'
- this.recorderManager = uni.getRecorderManager()
- this.recorderManager.onStart(() => {
- this.isRecording = true
- this.recordSeconds = 0
- this.recordTimer = setInterval(() => { this.recordSeconds++ }, 1000)
- })
- this.recorderManager.onStop((res) => {
- this.isRecording = false
- if (this.recordTimer) { clearInterval(this.recordTimer); this.recordTimer = null }
- this.form.voicePath = res.tempFilePath
- this.form.voiceDuration = res.duration
- var self = this
- uni.showLoading({ title: '语音识别中...', mask: true })
- sttTranscribe(res.tempFilePath).then(function(text) {
- self.form.voiceText = text
- uni.hideLoading()
- }).catch(function(err) {
- uni.hideLoading()
- console.warn('STT 转写失败:', err)
- })
- })
- this.recorderManager.onError(() => {
- this.isRecording = false
- if (this.recordTimer) { clearInterval(this.recordTimer); this.recordTimer = null }
- uni.showToast({ title: '录音失败', icon: 'none' })
- })
- this.loadHistory()
- },
- onUnload: function() {
- if (this.recordTimer) clearInterval(this.recordTimer)
- },
- methods: {
- goBack: function() { uni.navigateBack() },
- exerciseTypeLabel: function(type) {
- var m = { running: '跑步', swimming: '游泳', jump_rope: '跳绳', ball: '球类', cycling: '骑行', gymnastics: '体操', other: '其他' }
- return m[type] || type || '运动'
- },
- 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/exercise/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)
- }
- }
- })
- },
- choosePhoto: function() {
- var self = this
- uni.chooseImage({
- count: 9 - self.form.photos.length,
- sizeType: ['compressed'],
- sourceType: ['album', 'camera'],
- success: function(res) {
- res.tempFilePaths.forEach(function(path) {
- if (self.form.photos.length < 9) self.form.photos.push(path)
- })
- }
- })
- },
- getPhotoKey: function(img, idx) {
- return img ? img : ('p' + idx)
- },
- removePhoto: function(idx) {
- this.form.photos.splice(idx, 1)
- },
- toggleRecord: function() {
- if (this.isRecording) {
- this.recorderManager.stop()
- } else {
- this.recorderManager.start({ duration: 60000, format: 'mp3' })
- }
- },
- cancelRecord: function() {
- this.recorderManager.stop()
- this.form.voicePath = ''
- },
- finishRecord: function() {
- this.recorderManager.stop()
- },
- deleteVoice: function() {
- this.form.voicePath = ''
- },
- syncWechatSteps: function() {
- var self = this
- if (self.syncingSteps) return
- wx.getSetting({
- success: function(res) {
- var setting = res.authSetting
- if (setting && setting['scope.werun'] === false) {
- uni.showModal({
- title: '需要授权',
- content: '请去设置中开启「微信运动」授权后重试',
- confirmText: '去设置',
- success: function(m) {
- if (m.confirm) uni.openSetting()
- }
- })
- return
- }
- self.doGetWeRunData()
- },
- fail: function() {
- self.doGetWeRunData()
- }
- })
- },
- doGetWeRunData: function() {
- var self = this
- self.syncingSteps = true
- wx.getWeRunData({
- success: function(res) {
- var token = uni.getStorageSync('token')
- uni.request({
- url: config.API_BASE_URL + '/api/health/exercise/sync-wechat-steps',
- method: 'POST',
- data: { encryptedData: res.encryptedData, iv: res.iv },
- header: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
- success: function(r) {
- self.syncingSteps = false
- if (r.data && r.data.code === 200 && r.data.data) {
- var step = r.data.data.stepCount
- self.form.stepCount = step
- self.syncedStepText = '已同步今日步数:' + step + ' 步'
- uni.showToast({ title: '同步成功', icon: 'success' })
- } else {
- var msg = (r.data && r.data.message) || '同步失败'
- uni.showToast({ title: msg, icon: 'none' })
- }
- },
- fail: function() {
- self.syncingSteps = false
- uni.showToast({ title: '网络错误', icon: 'none' })
- }
- })
- },
- fail: function(err) {
- self.syncingSteps = false
- var errMsg = err && err.errMsg || ''
- if (errMsg.indexOf('auth') >= 0 || errMsg.indexOf('denied') >= 0) {
- uni.showModal({
- title: '需要授权',
- content: '请在微信「设置-隐私-授权管理」中开启微信运动权限',
- confirmText: '去设置',
- success: function(m) {
- if (m.confirm) uni.openSetting()
- }
- })
- } else {
- uni.showToast({ title: '获取微信步数失败,请先开启微信运动', icon: 'none' })
- }
- }
- })
- },
- uploadFile: function(filePath) {
- var self = this
- return new Promise(function(resolve, reject) {
- uni.uploadFile({
- url: config.API_BASE_URL + '/api/media/upload',
- filePath: filePath,
- name: 'file',
- header: { 'Authorization': 'Bearer ' + uni.getStorageSync('token') },
- success: function(res) {
- try { var data = JSON.parse(res.data); resolve(data.data || data.url || '') }
- catch (e) { resolve(res.data || '') }
- },
- fail: function() { reject() }
- })
- })
- },
- async doCheckin() {
- var self = this
- if (!self.form.exerciseType) { uni.showToast({ title: '请选择运动类型', icon: 'none' }); return }
- var durationNum = parseInt(self.form.durationMinutes)
- if (!self.form.durationMinutes || isNaN(durationNum) || durationNum <= 0) { uni.showToast({ title: '请填写有效的运动时长', icon: 'none' }); return }
- if (!self.memberId) { uni.showToast({ title: '孩子ID无效', icon: 'none' }); return }
- self.submitting = true
- try {
- var photoUrls = []
- if (self.form.photos && self.form.photos.length > 0) {
- for (var i = 0; i < self.form.photos.length; i++) {
- try { var url = await self.uploadFile(self.form.photos[i]) } catch(e) { url = '' }
- if (url) photoUrls.push(url)
- }
- }
- var voiceUrl = ''
- if (self.form.voicePath) {
- try { voiceUrl = await self.uploadFile(self.form.voicePath) } catch(e) { voiceUrl = '' }
- }
- var token = uni.getStorageSync('token')
- uni.request({
- url: config.API_BASE_URL + '/api/health/exercise/create',
- method: 'POST',
- data: {
- memberId: self.memberId,
- exerciseType: self.form.exerciseType,
- durationMinutes: parseInt(self.form.durationMinutes),
- intensity: self.form.intensity,
- caloriesBurned: self.form.caloriesBurned ? parseInt(self.form.caloriesBurned) : null,
- remark: self.form.remark || '',
- photoUrls: photoUrls.join(','),
- voiceUrl: voiceUrl,
- voiceText: self.form.voiceText || '',
- stepCount: self.form.stepCount ? parseInt(self.form.stepCount) : null
- },
- header: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
- success: function(res) {
- if (res.data && res.data.code === 200) {
- uni.showToast({ title: '打卡成功', icon: 'success' })
- self.syncedStepText = ''
- setTimeout(function() {
- uni.navigateBack()
- }, 800)
- } 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; }
- .nav-bar { height: 88rpx; padding: 24rpx 24rpx 0; display: flex; align-items: flex-end; }
- .nav-left { display: flex; align-items: center; gap: 10rpx; }
- .nav-icon { font-size: 48rpx; color: #333; line-height: 1; }
- .nav-title { font-size: 34rpx; font-weight: 600; color: #333; }
- .content { padding-bottom: 40rpx; }
- /* ===== 卡片通用 ===== */
- .card { margin: 16rpx 20rpx; background: #fff; border-radius: 20rpx; overflow: hidden; box-shadow: 0 4rpx 20rpx rgba(0,0,0,0.05); }
- .card-header { display: flex; align-items: center; gap: 10rpx; padding: 20rpx 24rpx 8rpx; border-bottom: 1rpx solid #f5f5f5; background: linear-gradient(90deg, #FFFAF0, #fff); }
- .card-icon { font-size: 32rpx; }
- .card-title { font-size: 30rpx; font-weight: 600; color: #333; }
- .card-body { padding: 20rpx 24rpx 24rpx; }
- /* ===== 表单 ===== */
- .form-group { margin-bottom: 20rpx; }
- .form-label { font-size: 26rpx; color: #333; font-weight: 500; margin-bottom: 10rpx; display: block; }
- .tag-row { display: flex; flex-wrap: wrap; gap: 10rpx; }
- .tag-btn { padding: 12rpx 24rpx; border-radius: 30rpx; border: 2rpx solid #e8e8e8; background: #fafafa; transition: all 0.2s ease; }
- .tag-active { border-color: #F97316; background: #FFF3E0; box-shadow: 0 4rpx 12rpx rgba(249,115,22,0.15); }
- .tag-btn text { font-size: 26rpx; color: #666; }
- .tag-active text { color: #F97316; font-weight: 600; }
- .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; }
- .step-sync-row { display: flex; align-items: center; gap: 12rpx; }
- .step-input { flex: 1; }
- .sync-btn { padding: 12rpx 28rpx; border-radius: 30rpx; border: 2rpx solid #F97316; background: #FFF3E0; transition: all 0.2s ease; flex-shrink: 0; }
- .sync-btn text { font-size: 24rpx; color: #F97316; font-weight: 500; }
- .syncing { opacity: 0.5; }
- .step-hint { font-size: 22rpx; color: #10B981; margin-top: 6rpx; display: block; }
- /* ===== 媒体工具栏 ===== */
- .media-toolbar { display: flex; align-items: center; gap: 0; padding: 12rpx 0; }
- .tool-btn { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6rpx; padding: 16rpx 0; }
- .tool-icon { font-size: 42rpx; }
- .tool-label { font-size: 24rpx; color: #666; }
- .tool-count { font-size: 20rpx; color: #F97316; font-weight: 600; }
- .tool-divider { width: 1rpx; height: 56rpx; background: linear-gradient(180deg, transparent, #e8e8e8, transparent); }
- /* ===== 照片缩略图 ===== */
- .photo-strip { display: flex; flex-wrap: wrap; gap: 10rpx; padding: 12rpx 0 4rpx; }
- .photo-item { position: relative; width: 132rpx; height: 132rpx; border-radius: 12rpx; overflow: hidden; }
- .photo-img { width: 100%; height: 100%; object-fit: cover; }
- .photo-delete-overlay { position: absolute; top: -6rpx; right: -6rpx; width: 32rpx; height: 32rpx; background: #EF4444; border-radius: 50%; display: flex; align-items: center; justify-content: center; box-shadow: 0 2rpx 8rpx rgba(239,68,68,0.3); z-index: 2; }
- .delete-icon { color: #fff; font-size: 22rpx; font-weight: bold; line-height: 32rpx; }
- /* ===== 录音展开区 ===== */
- .voice-strip { display: flex; align-items: center; justify-content: space-between; padding: 12rpx 0 4rpx; gap: 16rpx; }
- .waveform { display: flex; align-items: center; gap: 5rpx; height: 40rpx; flex: 1; }
- .wave-bar { width: 5rpx; height: 8rpx; background: #F97316; border-radius: 3rpx; animation: wave-anim 0.8s ease-in-out infinite; }
- @keyframes wave-anim { 0%, 100% { height: 8rpx; } 50% { height: 32rpx; } }
- .voice-controls { display: flex; gap: 12rpx; }
- .mini-btn { padding: 10rpx 24rpx; border-radius: 22rpx; font-size: 24rpx; }
- .cancel-mini { background: #f0f0f0; color: #666; }
- .done-mini { background: #F97316; color: #fff; font-weight: 600; }
- .voice-player { display: flex; align-items: center; gap: 10rpx; flex: 1; background: #FFF3E0; padding: 10rpx 20rpx; border-radius: 28rpx; }
- .play-icon { font-size: 26rpx; color: #F97316; }
- .voice-duration { font-size: 24rpx; color: #666; }
- .delete-voice { width: 40rpx; height: 40rpx; display: flex; align-items: center; justify-content: center; color: #999; font-size: 28rpx; }
- /* ===== 提交按钮 ===== */
- .submit-wrap { padding: 24rpx 20rpx 32rpx; }
- .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; }
- .submit-btn:active { transform: scale(0.98); }
- .submitting { opacity: 0.6; }
- /* ===== 历史记录 ===== */
- .history-list { display: flex; flex-direction: column; gap: 12rpx; }
- .history-item { display: flex; justify-content: space-between; align-items: center; padding: 14rpx 0; border-bottom: 1rpx solid #f5f5f5; }
- .history-left { flex: 1; min-width: 0; }
- .history-type { font-size: 26rpx; 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-right { text-align: right; margin-left: 16rpx; flex-shrink: 0; }
- .history-date { font-size: 22rpx; color: #999; }
- /* ===== 底部留白 ===== */
- .bottom-spacer { height: 40rpx; }
- </style>
|