exercise-checkin.vue 18 KB

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