exercise-checkin.vue 17 KB

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