exercise-checkin.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. </template>
  105. <script>
  106. import config from '../../config'
  107. import { parseDate } from '../../utils/format.js'
  108. import { sttTranscribe } from '../../utils/api.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. voiceText: '',
  142. voiceDuration: 0
  143. }
  144. }
  145. },
  146. computed: {
  147. recordDurationText: function() {
  148. if (!this.form.voicePath) return ''
  149. return '录音 ' + this.recordSeconds + '秒'
  150. }
  151. },
  152. onLoad: function() {
  153. var role = uni.getStorageSync('currentRole') || uni.getStorageSync('role') || 'parent'
  154. if (role !== 'child') {
  155. this.memberId = uni.getStorageSync('currentChildId') || uni.getStorageSync('currentMemberId') || uni.getStorageSync('userId') || null
  156. }
  157. this.recorderManager = uni.getRecorderManager()
  158. this.recorderManager.onStart(() => {
  159. this.isRecording = true
  160. this.recordSeconds = 0
  161. this.recordTimer = setInterval(() => { this.recordSeconds++ }, 1000)
  162. })
  163. this.recorderManager.onStop((res) => {
  164. this.isRecording = false
  165. if (this.recordTimer) { clearInterval(this.recordTimer); this.recordTimer = null }
  166. this.form.voicePath = res.tempFilePath
  167. this.form.voiceDuration = res.duration
  168. var self = this
  169. uni.showLoading({ title: '语音识别中...', mask: true })
  170. sttTranscribe(res.tempFilePath).then(function(text) {
  171. self.form.voiceText = text
  172. uni.hideLoading()
  173. }).catch(function(err) {
  174. uni.hideLoading()
  175. console.warn('STT 转写失败:', err)
  176. })
  177. })
  178. this.recorderManager.onError(() => {
  179. this.isRecording = false
  180. if (this.recordTimer) { clearInterval(this.recordTimer); this.recordTimer = null }
  181. uni.showToast({ title: '录音失败', icon: 'none' })
  182. })
  183. this.loadHistory()
  184. },
  185. onUnload: function() {
  186. if (this.recordTimer) clearInterval(this.recordTimer)
  187. },
  188. methods: {
  189. goBack: function() { uni.navigateBack() },
  190. exerciseTypeLabel: function(type) {
  191. var m = { running: '跑步', swimming: '游泳', jump_rope: '跳绳', ball: '球类', cycling: '骑行', gymnastics: '体操', other: '其他' }
  192. return m[type] || type || '运动'
  193. },
  194. formatDate: function(dateStr) {
  195. if (!dateStr) return ''
  196. var d = parseDate(dateStr)
  197. if (!d) return dateStr
  198. var m2 = (d.getMonth() + 1).toString().padStart(2, '0')
  199. var day = d.getDate().toString().padStart(2, '0')
  200. return m2 + '-' + day
  201. },
  202. loadHistory: function() {
  203. var self = this
  204. var token = uni.getStorageSync('token')
  205. if (!token || !self.memberId) return
  206. uni.request({
  207. url: config.API_BASE_URL + '/api/health/exercise/list',
  208. method: 'POST',
  209. data: { memberId: self.memberId },
  210. header: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
  211. success: function(res) {
  212. if (res.data && res.data.code === 200 && res.data.data) {
  213. self.history = res.data.data.slice(0, 5)
  214. }
  215. }
  216. })
  217. },
  218. choosePhoto: function() {
  219. var self = this
  220. uni.chooseImage({
  221. count: 9 - self.form.photos.length,
  222. sizeType: ['compressed'],
  223. sourceType: ['album', 'camera'],
  224. success: function(res) {
  225. res.tempFilePaths.forEach(function(path) {
  226. if (self.form.photos.length < 9) self.form.photos.push(path)
  227. })
  228. }
  229. })
  230. },
  231. getPhotoKey: function(img, idx) {
  232. return img ? img : ('p' + idx)
  233. },
  234. removePhoto: function(idx) {
  235. this.form.photos.splice(idx, 1)
  236. },
  237. toggleRecord: function() {
  238. if (this.isRecording) {
  239. this.recorderManager.stop()
  240. } else {
  241. this.recorderManager.start({ duration: 60000, format: 'mp3' })
  242. }
  243. },
  244. cancelRecord: function() {
  245. this.recorderManager.stop()
  246. this.form.voicePath = ''
  247. },
  248. finishRecord: function() {
  249. this.recorderManager.stop()
  250. },
  251. deleteVoice: function() {
  252. this.form.voicePath = ''
  253. },
  254. uploadFile: function(filePath) {
  255. var self = this
  256. return new Promise(function(resolve, reject) {
  257. uni.uploadFile({
  258. url: config.API_BASE_URL + '/api/media/upload',
  259. filePath: filePath,
  260. name: 'file',
  261. header: { 'Authorization': 'Bearer ' + uni.getStorageSync('token') },
  262. success: function(res) {
  263. try { var data = JSON.parse(res.data); resolve(data.data || data.url || '') }
  264. catch (e) { resolve(res.data || '') }
  265. },
  266. fail: function() { reject() }
  267. })
  268. })
  269. },
  270. async doCheckin() {
  271. var self = this
  272. if (!self.form.exerciseType) { uni.showToast({ title: '请选择运动类型', icon: 'none' }); return }
  273. if (!self.form.durationMinutes) { uni.showToast({ title: '请填写运动时长', icon: 'none' }); return }
  274. if (!self.memberId) { uni.showToast({ title: '孩子ID无效', icon: 'none' }); return }
  275. self.submitting = true
  276. try {
  277. var photoUrls = []
  278. if (self.form.photos && self.form.photos.length > 0) {
  279. for (var i = 0; i < self.form.photos.length; i++) {
  280. try { var url = await self.uploadFile(self.form.photos[i]) } catch(e) { url = '' }
  281. if (url) photoUrls.push(url)
  282. }
  283. }
  284. var voiceUrl = ''
  285. if (self.form.voicePath) {
  286. try { voiceUrl = await self.uploadFile(self.form.voicePath) } catch(e) { voiceUrl = '' }
  287. }
  288. var token = uni.getStorageSync('token')
  289. uni.request({
  290. url: config.API_BASE_URL + '/api/health/exercise/create',
  291. method: 'POST',
  292. data: {
  293. memberId: self.memberId,
  294. exerciseType: self.form.exerciseType,
  295. durationMinutes: parseInt(self.form.durationMinutes),
  296. intensity: self.form.intensity,
  297. caloriesBurned: self.form.caloriesBurned ? parseInt(self.form.caloriesBurned) : null,
  298. remark: self.form.remark || '',
  299. photoUrls: photoUrls.join(','),
  300. voiceUrl: voiceUrl,
  301. voiceText: self.form.voiceText || ''
  302. },
  303. header: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
  304. success: function(res) {
  305. if (res.data && res.data.code === 200) {
  306. uni.showToast({ title: '打卡成功', icon: 'success' })
  307. self.form.exerciseType = ''
  308. self.form.durationMinutes = ''
  309. self.form.intensity = 'medium'
  310. self.form.caloriesBurned = ''
  311. self.form.remark = ''
  312. self.form.photos = []
  313. self.form.voicePath = ''
  314. self.recordSeconds = 0
  315. self.loadHistory()
  316. } else {
  317. uni.showToast({ title: (res.data && res.data.message) || '打卡失败', icon: 'none' })
  318. }
  319. },
  320. fail: function() {
  321. self.submitting = false
  322. uni.showToast({ title: '网络错误', icon: 'none' })
  323. }
  324. })
  325. } catch (e) {
  326. self.submitting = false
  327. uni.showToast({ title: '提交失败', icon: 'none' })
  328. }
  329. }
  330. }
  331. }
  332. </script>
  333. <style scoped>
  334. .page { min-height: 100vh; background: #FFF7ED; }
  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>