exercise-checkin.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. <!-- 并排两个按钮 + 展开区域 -->
  51. <view class="media-toolbar">
  52. <view class="tool-btn" @tap="choosePhoto">
  53. <text class="tool-icon">📷</text>
  54. <text class="tool-label">上传照片</text>
  55. <text class="tool-count" v-if="form.photos.length > 0">{{ form.photos.length }}/9</text>
  56. </view>
  57. <view class="tool-divider"></view>
  58. <view class="tool-btn" @tap="toggleRecord">
  59. <text class="tool-icon">🎙</text>
  60. <text class="tool-label">{{ isRecording ? '录音中…' : (form.voicePath ? '已录制' : '录制音频') }}</text>
  61. <text class="tool-count" v-if="isRecording">{{ recordSeconds }}s</text>
  62. </view>
  63. </view>
  64. <!-- 照片缩略图 -->
  65. <view class="photo-strip" v-if="form.photos.length > 0">
  66. <view class="photo-item" v-for="(img, idx) in form.photos" :key="getPhotoKey(img, idx)" @tap="removePhoto(idx)">
  67. <image :src="img" mode="aspectFill" class="photo-img"/>
  68. <view class="photo-delete-overlay">
  69. <text class="delete-icon">×</text>
  70. </view>
  71. </view>
  72. </view>
  73. <!-- 录音状态 -->
  74. <view class="voice-strip" v-if="isRecording">
  75. <view class="waveform">
  76. <view class="wave-bar" v-for="n in 7" :key="n" :style="{animationDelay: (n * 0.1) + 's'}"></view>
  77. </view>
  78. <view class="voice-controls">
  79. <view class="mini-btn cancel-mini" @tap="cancelRecord"><text>取消</text></view>
  80. <view class="mini-btn done-mini" @tap="finishRecord"><text>完成</text></view>
  81. </view>
  82. </view>
  83. <view class="voice-strip" v-else-if="form.voicePath">
  84. <view class="voice-player">
  85. <text class="play-icon">▶</text>
  86. <text class="voice-duration">{{ recordDurationText }}</text>
  87. </view>
  88. <view class="delete-voice" @tap="deleteVoice"><text>×</text></view>
  89. </view>
  90. <!-- 提交按钮 -->
  91. <view class="submit-wrap">
  92. <view class="submit-btn" :class="{ 'submitting': submitting }" @tap="doCheckin">
  93. <text>{{ submitting ? '提交中...' : '✓ 完成打卡' }}</text>
  94. </view>
  95. </view>
  96. <!-- 历史记录 -->
  97. <view class="card" v-if="history.length > 0">
  98. <view class="card-accent"></view>
  99. <view class="card-body">
  100. <text class="card-title">近期记录</text>
  101. <view class="history-list">
  102. <view class="history-item" v-for="item in history" :key="item.id">
  103. <view class="history-left">
  104. <text class="history-type">{{ exerciseTypeLabel(item.exerciseType) }}</text>
  105. <text class="history-content">{{ item.durationMinutes }}分钟{{ item.caloriesBurned ? ' · ' + item.caloriesBurned + '千卡' : '' }}</text>
  106. </view>
  107. <text class="history-date">{{ formatDate(item.createdAt) }}</text>
  108. </view>
  109. </view>
  110. </view>
  111. </view>
  112. </scroll-view>
  113. </view>
  114. </template>
  115. <script>
  116. import config from '../../config'
  117. import { parseDate } from '../../utils/format.js'
  118. export default {
  119. data() {
  120. return {
  121. memberId: null,
  122. submitting: false,
  123. isRecording: false,
  124. recordSeconds: 0,
  125. recordTimer: null,
  126. recorderManager: null,
  127. history: [],
  128. exerciseTypes: [
  129. { label: '跑步', value: 'running' },
  130. { label: '游泳', value: 'swimming' },
  131. { label: '跳绳', value: 'jump_rope' },
  132. { label: '球类', value: 'ball' },
  133. { label: '骑行', value: 'cycling' },
  134. { label: '体操', value: 'gymnastics' },
  135. { label: '其他', value: 'other' }
  136. ],
  137. intensityOptions: [
  138. { label: '低', value: 'low' },
  139. { label: '中', value: 'medium' },
  140. { label: '高', value: 'high' }
  141. ],
  142. form: {
  143. exerciseType: '',
  144. durationMinutes: '',
  145. intensity: 'medium',
  146. caloriesBurned: '',
  147. remark: '',
  148. photos: [],
  149. voicePath: ''
  150. }
  151. }
  152. },
  153. computed: {
  154. recordDurationText: function() {
  155. if (!this.form.voicePath) return ''
  156. return '录音 ' + this.recordSeconds + '秒'
  157. }
  158. },
  159. onLoad: function() {
  160. var role = uni.getStorageSync('currentRole') || uni.getStorageSync('role') || 'parent'
  161. if (role !== 'child') {
  162. this.memberId = uni.getStorageSync('currentChildId') || uni.getStorageSync('currentMemberId') || uni.getStorageSync('userId') || null
  163. }
  164. this.recorderManager = uni.getRecorderManager()
  165. this.recorderManager.onStart(() => {
  166. this.isRecording = true
  167. this.recordSeconds = 0
  168. this.recordTimer = setInterval(() => { this.recordSeconds++ }, 1000)
  169. })
  170. this.recorderManager.onStop((res) => {
  171. this.isRecording = false
  172. if (this.recordTimer) { clearInterval(this.recordTimer); this.recordTimer = null }
  173. this.form.voicePath = res.tempFilePath
  174. this.form.voiceDuration = res.duration
  175. })
  176. this.recorderManager.onError(() => {
  177. this.isRecording = false
  178. if (this.recordTimer) { clearInterval(this.recordTimer); this.recordTimer = null }
  179. uni.showToast({ title: '录音失败', icon: 'none' })
  180. })
  181. this.loadHistory()
  182. },
  183. onUnload: function() {
  184. if (this.recordTimer) clearInterval(this.recordTimer)
  185. },
  186. methods: {
  187. goBack: function() { uni.navigateBack() },
  188. exerciseTypeLabel: function(type) {
  189. var m = { running: '跑步', swimming: '游泳', jump_rope: '跳绳', ball: '球类', cycling: '骑行', gymnastics: '体操', other: '其他' }
  190. return m[type] || type || '运动'
  191. },
  192. formatDate: function(dateStr) {
  193. if (!dateStr) return ''
  194. var d = parseDate(dateStr)
  195. if (!d) return dateStr
  196. var m2 = (d.getMonth() + 1).toString().padStart(2, '0')
  197. var day = d.getDate().toString().padStart(2, '0')
  198. return m2 + '-' + day
  199. },
  200. loadHistory: function() {
  201. var self = this
  202. var token = uni.getStorageSync('token')
  203. if (!token || !self.memberId) return
  204. uni.request({
  205. url: config.API_BASE_URL + '/api/health/exercise/list',
  206. method: 'POST',
  207. data: { memberId: self.memberId },
  208. header: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
  209. success: function(res) {
  210. if (res.data && res.data.code === 200 && res.data.data) {
  211. self.history = res.data.data.slice(0, 5)
  212. }
  213. }
  214. })
  215. },
  216. choosePhoto: function() {
  217. var self = this
  218. uni.chooseImage({
  219. count: 9 - self.form.photos.length,
  220. sizeType: ['compressed'],
  221. sourceType: ['album', 'camera'],
  222. success: function(res) {
  223. res.tempFilePaths.forEach(function(path) {
  224. if (self.form.photos.length < 9) self.form.photos.push(path)
  225. })
  226. }
  227. })
  228. },
  229. getPhotoKey: function(img, idx) {
  230. return img ? img : ('p' + idx)
  231. },
  232. removePhoto: function(idx) {
  233. this.form.photos.splice(idx, 1)
  234. },
  235. toggleRecord: function() {
  236. if (this.isRecording) {
  237. this.recorderManager.stop()
  238. } else {
  239. this.recorderManager.start({ duration: 60000, format: 'mp3' })
  240. }
  241. },
  242. cancelRecord: function() {
  243. this.recorderManager.stop()
  244. this.form.voicePath = ''
  245. },
  246. finishRecord: function() {
  247. this.recorderManager.stop()
  248. },
  249. deleteVoice: function() {
  250. this.form.voicePath = ''
  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. /* 录入框下方的并排工具栏 */
  351. .media-toolbar { display: flex; align-items: center; margin-top: 16rpx; padding: 18rpx 0; border-top: 1rpx solid #f0f0f0; }
  352. .tool-btn { flex: 1; display: flex; align-items: center; justify-content: center; gap: 10rpx; padding: 8rpx 0; }
  353. .tool-icon { font-size: 36rpx; }
  354. .tool-label { font-size: 26rpx; color: #555; }
  355. .tool-count { font-size: 22rpx; color: #F97316; font-weight: bold; margin-left: 4rpx; }
  356. .tool-divider { width: 1rpx; height: 36rpx; background: #e8e8e8; }
  357. /* 照片缩略图条 */
  358. .photo-strip { display: flex; flex-wrap: wrap; gap: 12rpx; padding: 16rpx 0 4rpx; }
  359. .photo-item { position: relative; width: 120rpx; height: 120rpx; }
  360. .photo-img { width: 100%; height: 100%; border-radius: 12rpx; }
  361. .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 6rpx rgba(239,68,68,0.3); z-index: 2; }
  362. .delete-icon { color: #fff; font-size: 22rpx; font-weight: bold; line-height: 32rpx; }
  363. /* 录音展开区 */
  364. .voice-strip { display: flex; align-items: center; justify-content: space-between; padding: 16rpx 0 4rpx; gap: 16rpx; }
  365. .waveform { display: flex; align-items: center; gap: 6rpx; height: 48rpx; flex: 1; }
  366. .wave-bar { width: 6rpx; height: 10rpx; background: #F97316; border-radius: 3rpx; animation: wave-anim 0.8s ease-in-out infinite; }
  367. @keyframes wave-anim { 0%, 100% { height: 10rpx; } 50% { height: 36rpx; } }
  368. .voice-controls { display: flex; gap: 16rpx; }
  369. .mini-btn { padding: 12rpx 24rpx; border-radius: 24rpx; font-size: 24rpx; }
  370. .cancel-mini { background: #f0f0f0; color: #666; }
  371. .done-mini { background: #F97316; color: #fff; font-weight: bold; }
  372. .voice-player { display: flex; align-items: center; gap: 12rpx; flex: 1; background: #FFF3E0; padding: 12rpx 20rpx; border-radius: 30rpx; }
  373. .play-icon { font-size: 28rpx; color: #F97316; }
  374. .voice-duration { font-size: 24rpx; color: #666; }
  375. .delete-voice { width: 40rpx; height: 40rpx; display: flex; align-items: center; justify-content: center; color: #999; font-size: 32rpx; }
  376. .submit-wrap { padding: 24rpx 24rpx 40rpx; }
  377. .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; }
  378. .submitting { opacity: 0.6; }
  379. .history-list { display: flex; flex-direction: column; gap: 16rpx; }
  380. .history-item { display: flex; justify-content: space-between; align-items: center; padding: 12rpx 0; border-bottom: 1rpx solid #f5f5f5; }
  381. .history-left { flex: 1; min-width: 0; }
  382. .history-type { font-size: 26rpx; font-weight: 500; color: #333; display: block; }
  383. .history-content { font-size: 22rpx; color: #888; display: block; margin-top: 4rpx; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  384. .history-date { font-size: 22rpx; color: #999; flex-shrink: 0; margin-left: 16rpx; }
  385. </style>