exercise-record.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <template>
  2. <view class="exercise-container">
  3. <view class="nav-bar">
  4. <text class="nav-back" @click="goBack">‹ 返回</text>
  5. <text class="nav-title">运动记录</text>
  6. <view class="nav-placeholder"></view>
  7. </view>
  8. <!-- 成员选择 -->
  9. <view class="member-selector-card">
  10. <text class="member-label">记录人</text>
  11. <picker mode="selector" :range="memberOptions" :value="selectedMemberIndex" @change="onMemberChange">
  12. <view class="picker-display">{{ memberOptions[selectedMemberIndex] }}</view>
  13. </picker>
  14. </view>
  15. <view class="section-card">
  16. <view class="section-title">记录运动</view>
  17. <view class="exercise-types">
  18. <view class="ex-type-item" v-for="et in exerciseTypes" :key="et.value"
  19. :class="{ 'ex-type-active': selectedType === et.value }"
  20. @click="selectedType = et.value">
  21. <text class="ex-type-icon">{{ et.icon }}</text>
  22. <text class="ex-type-label">{{ et.label }}</text>
  23. </view>
  24. </view>
  25. <view class="input-row">
  26. <text class="input-label">时长(分钟)</text>
  27. <input class="input-field" v-model="duration" type="number" placeholder="输入运动时长" />
  28. </view>
  29. <view class="input-row">
  30. <text class="input-label">强度</text>
  31. <view class="intensity-options">
  32. <text class="intensity-item" v-for="v in ['低','中','高']" :key="v"
  33. :class="{ 'intensity-active': intensity === v }" @click="intensity = v">{{ v }}</text>
  34. </view>
  35. </view>
  36. <view class="input-row">
  37. <text class="input-label">备注</text>
  38. <input class="input-field" v-model="remark" placeholder="可选" />
  39. </view>
  40. <view class="submit-btn" @click="submitExercise">保存记录</view>
  41. </view>
  42. <view class="section-card">
  43. <view class="section-title">今日运动摘要</view>
  44. <view class="today-summary">
  45. <text class="summary-stat">总时长:<text class="stat-value">{{ todayTotalMinutes }} 分钟</text></text>
  46. <text class="summary-stat">消耗:<text class="stat-value">{{ todayCalories }} kcal</text></text>
  47. </view>
  48. </view>
  49. <view class="section-card">
  50. <view class="section-title">历史记录</view>
  51. <view class="history-list">
  52. <view class="history-item" v-for="item in exerciseHistory" :key="item.id">
  53. <text class="history-icon">{{ getTypeIcon(item.exerciseType) }}</text>
  54. <view class="history-info">
  55. <text class="history-name">{{ item.exerciseType }}</text>
  56. <text class="history-meta">{{ item.durationMinutes }}分钟 · {{ item.intensity || '中' }}强度</text>
  57. </view>
  58. <text class="history-time">{{ item.createdAt ? item.createdAt.substring(5,16) : '' }}</text>
  59. </view>
  60. <view class="history-empty" v-if="exerciseHistory.length === 0"><text>暂无记录</text></view>
  61. </view>
  62. </view>
  63. <view class="bottom-spacer"></view>
  64. </view>
  65. </template>
  66. <script>
  67. import config from '@/config.js'
  68. import { getChildren } from '@/utils/api.js'
  69. export default {
  70. data() {
  71. return {
  72. childId: null,
  73. selectedMemberIndex: 0,
  74. memberOptions: ['我自己'],
  75. memberIds: [],
  76. selectedType: 'running',
  77. duration: '',
  78. intensity: '中',
  79. remark: '',
  80. exerciseHistory: [],
  81. exerciseTypes: [
  82. { icon: '🏃', label: '跑步', value: 'running' },
  83. { icon: '🚴', label: '骑行', value: 'cycling' },
  84. { icon: '🏊', label: '游泳', value: 'swimming' },
  85. { icon: '🤸', label: '跳绳', value: 'jumping_rope' },
  86. { icon: '⚽', label: '球类', value: 'ball_games' },
  87. { icon: '🧘', label: '拉伸', value: 'stretching' }
  88. ]
  89. }
  90. },
  91. computed: {
  92. todayTotalMinutes: function() { return 0 },
  93. todayCalories: function() { return 0 }
  94. },
  95. onLoad: function(options) {
  96. this.childId = options.childId ? parseInt(options.childId) : null
  97. this.loadChildren()
  98. this.loadHistory()
  99. },
  100. methods: {
  101. loadChildren: function() {
  102. var self = this
  103. getChildren().then(function(res) {
  104. if (res.code === 200 && res.data && res.data.length > 0) {
  105. self.memberIds = res.data.map(function(c) { return c.childId })
  106. self.memberOptions = ['我自己'].concat(res.data.map(function(c) { return c.name || c.nickname || '孩子' }))
  107. }
  108. }).catch(function() {})
  109. },
  110. onMemberChange: function(e) {
  111. this.selectedMemberIndex = parseInt(e.detail.value)
  112. if (this.selectedMemberIndex === 0) {
  113. this.childId = null
  114. } else {
  115. this.childId = this.memberIds[this.selectedMemberIndex - 1]
  116. }
  117. this.loadHistory()
  118. },
  119. submitExercise: function() {
  120. if (!this.duration || parseInt(this.duration) <= 0) {
  121. uni.showToast({ title: '请输入有效时长', icon: 'none' })
  122. return
  123. }
  124. var data = {
  125. exerciseType: this.selectedType,
  126. durationMinutes: parseInt(this.duration),
  127. intensity: this.intensity,
  128. remark: this.remark
  129. }
  130. if (this.childId) {
  131. data.childId = this.childId
  132. data.memberId = this.childId
  133. }
  134. uni.showLoading({ title: '保存中...' })
  135. healthRequest('/api/health/exercise/create', data).then(function() {
  136. uni.hideLoading()
  137. uni.showToast({ title: '保存成功', icon: 'success' })
  138. this.duration = ''
  139. this.remark = ''
  140. this.loadHistory()
  141. }.bind(this)).catch(function() {
  142. uni.hideLoading()
  143. uni.showToast({ title: '保存失败', icon: 'none' })
  144. })
  145. },
  146. loadHistory: function() {
  147. var self = this
  148. var params = {}
  149. if (this.childId) params.childId = this.childId
  150. healthRequest('/api/health/exercise/list', params).then(function(res) {
  151. if (res.data) self.exerciseHistory = res.data
  152. }).catch(function() { self.exerciseHistory = [] })
  153. },
  154. getTypeIcon: function(type) {
  155. var map = { running: '🏃', cycling: '🚴', swimming: '🏊', jumping_rope: '🤸', ball_games: '⚽', stretching: '🧘' }
  156. return map[type] || '🏃'
  157. },
  158. goBack: function() { uni.navigateBack() }
  159. }
  160. }
  161. function healthRequest(url, data) {
  162. var token = uni.getStorageSync('token')
  163. return new Promise(function(resolve, reject) {
  164. uni.request({
  165. url: config.API_BASE_URL + url,
  166. method: 'POST',
  167. data: data,
  168. header: { 'Content-Type': 'application/json', 'Authorization': token ? 'Bearer ' + token : '' },
  169. success: function(res) { if (res.data.code === 200) resolve(res.data); else reject(res.data) },
  170. fail: function(err) { reject(err) }
  171. })
  172. })
  173. }
  174. </script>
  175. <style scoped>
  176. .exercise-container { min-height: 100vh; background: #f5f7fa; padding-bottom: 60rpx; }
  177. .nav-bar { position: sticky; top: 0; background: #fff; display: flex; flex-direction: row; align-items: center; justify-content: space-between; padding: 20rpx 30rpx; box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06); z-index: 100; }
  178. .nav-back { font-size: 32rpx; color: #5B9BD5; }
  179. .nav-title { font-size: 32rpx; font-weight: bold; color: #333; }
  180. .nav-placeholder { width: 80rpx; }
  181. .member-selector-card { margin: 20rpx 30rpx; background: #fff; border-radius: 20rpx; padding: 20rpx 24rpx; box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06); display: flex; flex-direction: row; align-items: center; justify-content: space-between; }
  182. .member-label { font-size: 28rpx; color: #666; }
  183. .picker-display { font-size: 28rpx; color: #333; font-weight: 500; }
  184. .section-card { margin: 20rpx 30rpx; background: #fff; border-radius: 20rpx; padding: 24rpx; box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06); }
  185. .section-title { font-size: 30rpx; font-weight: bold; color: #333; margin-bottom: 20rpx; }
  186. .exercise-types { display: flex; flex-direction: row; flex-wrap: wrap; margin-bottom: 20rpx; }
  187. .ex-type-item { display: flex; flex-direction: column; align-items: center; padding: 16rpx 24rpx; border-radius: 16rpx; background: #f0f0f0; margin: 0 12rpx 12rpx 0; min-width: 100rpx; }
  188. .ex-type-active { background: #E8F5E9; border: 2rpx solid #4CAF50; }
  189. .ex-type-icon { font-size: 36rpx; }
  190. .ex-type-label { font-size: 22rpx; color: #666; margin-top: 6rpx; }
  191. .input-row { display: flex; flex-direction: row; align-items: center; margin-bottom: 16rpx; }
  192. .input-label { font-size: 26rpx; color: #666; width: 140rpx; flex-shrink: 0; }
  193. .input-field { flex: 1; background: #f5f7fa; border-radius: 12rpx; padding: 14rpx 16rpx; font-size: 28rpx; }
  194. .intensity-options { display: flex; flex-direction: row; }
  195. .intensity-item { padding: 8rpx 24rpx; border-radius: 12rpx; background: #f0f0f0; font-size: 26rpx; color: #666; margin-right: 12rpx; }
  196. .intensity-active { background: #FFF3E0; color: #E65100; }
  197. .submit-btn { background: linear-gradient(135deg, #4CAF50, #66BB6A); color: #fff; font-size: 32rpx; font-weight: bold; text-align: center; padding: 22rpx; border-radius: 40rpx; margin-top: 16rpx; }
  198. .today-summary { display: flex; flex-direction: row; justify-content: space-around; }
  199. .summary-stat { font-size: 26rpx; color: #666; }
  200. .stat-value { font-size: 32rpx; color: #4CAF50; font-weight: bold; }
  201. .history-list { display: flex; flex-direction: column; }
  202. .history-item { display: flex; flex-direction: row; align-items: center; padding: 14rpx 0; border-bottom: 1rpx solid #f5f5f5; }
  203. .history-icon { font-size: 32rpx; margin-right: 16rpx; }
  204. .history-info { flex: 1; }
  205. .history-name { font-size: 28rpx; color: #333; }
  206. .history-meta { font-size: 22rpx; color: #999; }
  207. .history-time { font-size: 22rpx; color: #999; }
  208. .history-empty { text-align: center; padding: 30rpx; color: #999; font-size: 26rpx; }
  209. .bottom-spacer { height: 60rpx; }
  210. </style>