exercise-checkin.vue 18 KB

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