exercise-checkin.vue 16 KB

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