exercise-index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <template>
  2. <scroll-view class="content" scroll-y>
  3. <view class="card">
  4. <view class="card-accent"></view>
  5. <view class="card-body">
  6. <text class="card-title">每日运动建议</text>
  7. <view class="info-list">
  8. <view class="info-item">
  9. <text class="info-age">3-6 岁(学龄前)</text>
  10. <text class="info-desc">每天至少 60 分钟中等强度运动,以游戏和趣味活动为主,如跑步、跳跃、球类游戏。</text>
  11. </view>
  12. <view class="info-item">
  13. <text class="info-age">6-12 岁(学龄期)</text>
  14. <text class="info-desc">每天 60 分钟以上中等强度运动,每周至少 3 次高强度有氧运动,如游泳、骑自行车、跳绳。</text>
  15. </view>
  16. <view class="info-item">
  17. <text class="info-age">12-18 岁(青少年)</text>
  18. <text class="info-desc">每天 60 分钟中高强度运动,每周 3 次力量训练,培养终身运动习惯。</text>
  19. </view>
  20. </view>
  21. </view>
  22. </view>
  23. <view class="card">
  24. <view class="card-accent"></view>
  25. <view class="card-body">
  26. <text class="card-title">运动类型推荐</text>
  27. <view class="info-list">
  28. <view class="info-item">
  29. <text class="info-age">🏃 有氧运动</text>
  30. <text class="info-desc">提高心肺功能,增强耐力。推荐:跑步、游泳、骑自行车、跳绳、球类运动。</text>
  31. </view>
  32. <view class="info-item">
  33. <text class="info-age">💪 力量训练</text>
  34. <text class="info-desc">增强肌肉力量和骨骼密度。推荐:俯卧撑、仰卧起坐、深蹲、弹力带训练。</text>
  35. </view>
  36. <view class="info-item">
  37. <text class="info-age">🧘 柔韧性训练</text>
  38. <text class="info-desc">改善身体灵活性,预防运动损伤。推荐:拉伸、瑜伽、舞蹈、体操。</text>
  39. </view>
  40. </view>
  41. </view>
  42. </view>
  43. <view class="card">
  44. <view class="card-accent"></view>
  45. <view class="card-body">
  46. <text class="card-title">运动安全提示</text>
  47. <view class="info-list">
  48. <view class="info-item">
  49. <text class="info-age">做好热身</text>
  50. <text class="info-desc">运动前进行 5-10 分钟动态热身,如慢跑、关节活动,预防肌肉拉伤。</text>
  51. </view>
  52. <view class="info-item">
  53. <text class="info-age">补充水分</text>
  54. <text class="info-desc">运动前中后适量饮水,每 15-20 分钟补充 100-150ml 水,避免脱水。</text>
  55. </view>
  56. <view class="info-item">
  57. <text class="info-age">正确姿势</text>
  58. <text class="info-desc">保持正确运动姿势,避免过度训练。如有不适立即停止,必要时就医。</text>
  59. </view>
  60. </view>
  61. </view>
  62. </view>
  63. <!-- 记录运动表单 -->
  64. <view class="card">
  65. <view class="card-accent"></view>
  66. <view class="card-body">
  67. <text class="card-title">记录运动</text>
  68. <view class="form-group">
  69. <text class="form-label">运动类型 *</text>
  70. <view class="tag-row">
  71. <view class="tag-btn" v-for="t in exerciseTypes" :key="t.value"
  72. :class="form.exerciseType === t.value ? 'tag-active' : ''"
  73. @tap="form.exerciseType = t.value">
  74. <text>{{ t.label }}</text>
  75. </view>
  76. </view>
  77. </view>
  78. <view class="form-group">
  79. <text class="form-label">时长(分钟)*</text>
  80. <input class="form-input" type="number" v-model="form.durationMinutes" placeholder="请输入运动时长" />
  81. </view>
  82. <view class="form-group">
  83. <text class="form-label">强度</text>
  84. <view class="tag-row">
  85. <view class="tag-btn" v-for="s in intensityOptions" :key="s.value"
  86. :class="form.intensity === s.value ? 'tag-active' : ''"
  87. @tap="form.intensity = s.value">
  88. <text>{{ s.label }}</text>
  89. </view>
  90. </view>
  91. </view>
  92. <view class="form-group">
  93. <text class="form-label">消耗卡路里(选填)</text>
  94. <input class="form-input" type="number" v-model="form.caloriesBurned" placeholder="估算消耗的卡路里" />
  95. </view>
  96. <view class="form-group">
  97. <text class="form-label">备注(选填)</text>
  98. <input class="form-input" v-model="form.remark" placeholder="记录运动感受..." />
  99. </view>
  100. <view class="submit-btn" @tap="doCheckin">
  101. <text>{{ submitting ? '提交中...' : '提交打卡' }}</text>
  102. </view>
  103. </view>
  104. </view>
  105. <!-- 历史记录 -->
  106. <view class="card">
  107. <view class="card-accent"></view>
  108. <view class="card-body">
  109. <text class="card-title">运动记录</text>
  110. <view class="history-list" v-if="history.length > 0">
  111. <view class="history-item" v-for="item in history" :key="item.id">
  112. <view class="history-left">
  113. <text class="history-type">{{ item.exerciseType }}</text>
  114. <text class="history-duration">{{ item.durationMinutes }}分钟</text>
  115. </view>
  116. <view class="history-right">
  117. <text class="history-date">{{ formatDate(item.createdAt) }}</text>
  118. </view>
  119. </view>
  120. </view>
  121. <view class="empty-state" v-else>
  122. <text>还没有运动记录</text>
  123. </view>
  124. </view>
  125. </view>
  126. </scroll-view>
  127. </template>
  128. <script>
  129. import config from '@/config.js'
  130. import { parseDate } from '@/utils/format.js'
  131. export default {
  132. data() {
  133. return {
  134. memberId: null,
  135. submitting: false,
  136. history: [],
  137. exerciseTypes: [
  138. { label: '跑步', value: 'running' },
  139. { label: '游泳', value: 'swimming' },
  140. { label: '骑行', value: 'cycling' },
  141. { label: '跳绳', value: 'jump_rope' },
  142. { label: '球类', value: 'ball_game' },
  143. { label: '力量训练', value: 'strength' },
  144. { label: '其他', value: 'other' }
  145. ],
  146. intensityOptions: [
  147. { label: '低强度', value: 'low' },
  148. { label: '中等', value: 'medium' },
  149. { label: '高强度', value: 'high' }
  150. ],
  151. form: {
  152. exerciseType: '',
  153. durationMinutes: '',
  154. intensity: 'medium',
  155. caloriesBurned: '',
  156. remark: ''
  157. }
  158. }
  159. },
  160. onLoad: function() {
  161. var role = uni.getStorageSync('currentRole') || uni.getStorageSync('role') || 'parent'
  162. // child 角色:memberId 由后端从 JWT 获取 currentMemberId,前端无需传
  163. if (role !== 'child') {
  164. this.memberId = uni.getStorageSync('currentChildId') || null
  165. }
  166. this.loadHistory()
  167. },
  168. methods: {
  169. goBack: function() {
  170. uni.navigateBack()
  171. },
  172. formatDate: function(dateStr) {
  173. if (!dateStr) return ''
  174. var d = parseDate(dateStr)
  175. if (!d) return dateStr
  176. var m = (d.getMonth() + 1).toString().padStart(2, '0')
  177. var day = d.getDate().toString().padStart(2, '0')
  178. return m + '-' + day
  179. },
  180. loadHistory: function() {
  181. var self = this
  182. var token = uni.getStorageSync('token')
  183. if (!token || !self.memberId) return
  184. uni.request({
  185. url: config.API_BASE_URL + '/api/health/exercise/list',
  186. method: 'POST',
  187. data: { memberId: self.memberId },
  188. header: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
  189. success: function(res) {
  190. if (res.data && res.data.code === 200 && res.data.data) {
  191. self.history = res.data.data.slice(0, 10)
  192. }
  193. }
  194. })
  195. },
  196. doCheckin: function() {
  197. var self = this
  198. if (!self.form.exerciseType) {
  199. uni.showToast({ title: '请选择运动类型', icon: 'none' })
  200. return
  201. }
  202. if (!self.form.durationMinutes || parseInt(self.form.durationMinutes) <= 0) {
  203. uni.showToast({ title: '请输入有效时长', icon: 'none' })
  204. return
  205. }
  206. if (!self.memberId) {
  207. uni.showToast({ title: '孩子ID无效', icon: 'none' })
  208. return
  209. }
  210. self.submitting = true
  211. var token = uni.getStorageSync('token')
  212. uni.request({
  213. url: config.API_BASE_URL + '/api/health/exercise/create',
  214. method: 'POST',
  215. data: {
  216. memberId: self.memberId,
  217. exerciseType: self.form.exerciseType,
  218. durationMinutes: parseInt(self.form.durationMinutes),
  219. intensity: self.form.intensity,
  220. caloriesBurned: self.form.caloriesBurned ? parseInt(self.form.caloriesBurned) : null,
  221. remark: self.form.remark || ''
  222. },
  223. header: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
  224. success: function(res) {
  225. self.submitting = false
  226. if (res.data && res.data.code === 200) {
  227. uni.showToast({ title: '打卡成功', icon: 'success' })
  228. self.form.exerciseType = ''
  229. self.form.durationMinutes = ''
  230. self.form.intensity = 'medium'
  231. self.form.caloriesBurned = ''
  232. self.form.remark = ''
  233. self.loadHistory()
  234. } else {
  235. var msg = (res.data && res.data.message) || '打卡失败'
  236. uni.showToast({ title: msg, icon: 'none' })
  237. }
  238. },
  239. fail: function() {
  240. self.submitting = false
  241. uni.showToast({ title: '网络错误', icon: 'none' })
  242. }
  243. })
  244. }
  245. }
  246. }
  247. </script>
  248. <style scoped>
  249. .page {
  250. min-height: 100vh;
  251. background-color: #F5F7FA;
  252. display: flex;
  253. flex-direction: column;
  254. }
  255. .content {
  256. flex: 1;
  257. padding: 30rpx;
  258. }
  259. .card {
  260. display: flex;
  261. background-color: #FFFFFF;
  262. border-radius: 20rpx;
  263. margin-bottom: 30rpx;
  264. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.06);
  265. overflow: hidden;
  266. }
  267. .card-accent {
  268. width: 8rpx;
  269. background-color: #FF8C42;
  270. flex-shrink: 0;
  271. }
  272. .card-body {
  273. flex: 1;
  274. padding: 30rpx;
  275. }
  276. .card-title {
  277. font-size: 30rpx;
  278. font-weight: 600;
  279. color: #333;
  280. margin-bottom: 24rpx;
  281. display: block;
  282. }
  283. .info-list {
  284. display: flex;
  285. flex-direction: column;
  286. gap: 20rpx;
  287. }
  288. .info-item {
  289. display: flex;
  290. flex-direction: column;
  291. gap: 6rpx;
  292. }
  293. .info-age {
  294. font-size: 26rpx;
  295. font-weight: 500;
  296. color: #FF8C42;
  297. display: block;
  298. }
  299. .info-desc {
  300. font-size: 24rpx;
  301. color: #666;
  302. line-height: 1.6;
  303. display: block;
  304. }
  305. .form-group {
  306. margin-bottom: 24rpx;
  307. }
  308. .form-label {
  309. font-size: 26rpx;
  310. color: #333;
  311. font-weight: 500;
  312. margin-bottom: 12rpx;
  313. display: block;
  314. }
  315. .form-input {
  316. border: 1rpx solid #ddd;
  317. border-radius: 12rpx;
  318. padding: 16rpx 20rpx;
  319. font-size: 26rpx;
  320. background-color: #FAFAFA;
  321. width: auto;
  322. }
  323. .tag-row {
  324. display: flex;
  325. flex-wrap: wrap;
  326. gap: 16rpx;
  327. }
  328. .tag-btn {
  329. padding: 12rpx 24rpx;
  330. border: 1rpx solid #ddd;
  331. border-radius: 30rpx;
  332. font-size: 24rpx;
  333. color: #666;
  334. background-color: #FAFAFA;
  335. }
  336. .tag-active {
  337. border-color: #FF8C42;
  338. color: #FF8C42;
  339. background-color: #FFF5EB;
  340. }
  341. .submit-btn {
  342. background-color: #FF8C42;
  343. color: #FFFFFF;
  344. text-align: center;
  345. padding: 22rpx 0;
  346. border-radius: 16rpx;
  347. font-size: 28rpx;
  348. font-weight: 500;
  349. margin-top: 10rpx;
  350. }
  351. .history-list {
  352. display: flex;
  353. flex-direction: column;
  354. gap: 16rpx;
  355. }
  356. .history-item {
  357. display: flex;
  358. justify-content: space-between;
  359. align-items: center;
  360. padding: 16rpx 0;
  361. border-bottom: 1rpx solid #f0f0f0;
  362. }
  363. .history-left {
  364. display: flex;
  365. flex-direction: column;
  366. gap: 4rpx;
  367. }
  368. .history-type {
  369. font-size: 26rpx;
  370. font-weight: 500;
  371. color: #333;
  372. }
  373. .history-duration {
  374. font-size: 22rpx;
  375. color: #999;
  376. }
  377. .history-right {
  378. text-align: right;
  379. }
  380. .history-date {
  381. font-size: 22rpx;
  382. color: #999;
  383. }
  384. .empty-state {
  385. text-align: center;
  386. padding: 40rpx 0;
  387. font-size: 26rpx;
  388. color: #999;
  389. }
  390. </style>