exercise-checkin.vue 16 KB

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