exercise-checkin.vue 17 KB

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