exercise-index.vue 13 KB

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