exercise-index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <template>
  2. <scroll-view class="content" scroll-y :style="{ '--font-scale': fontScale }">
  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. <TagChip v-for="t in exerciseTypes" :key="t.value" :label="t.label" variant="body"
  72. :active="form.exerciseType === t.value"
  73. @select="form.exerciseType = t.value" />
  74. </view>
  75. </view>
  76. <view class="form-group">
  77. <text class="form-label">时长(分钟)*</text>
  78. <input class="form-input" type="number" v-model="form.durationMinutes" placeholder="请输入运动时长" />
  79. </view>
  80. <view class="form-group">
  81. <text class="form-label">强度</text>
  82. <view class="tag-row">
  83. <TagChip v-for="s in intensityOptions" :key="s.value" :label="s.label" variant="body"
  84. :active="form.intensity === s.value"
  85. @select="form.intensity = s.value" />
  86. </view>
  87. </view>
  88. <view class="form-group">
  89. <text class="form-label">消耗卡路里(选填)</text>
  90. <input class="form-input" type="number" v-model="form.caloriesBurned" placeholder="估算消耗的卡路里" />
  91. </view>
  92. <view class="form-group">
  93. <text class="form-label">备注(选填)</text>
  94. <input class="form-input" v-model="form.remark" placeholder="记录运动感受..." />
  95. </view>
  96. <view class="submit-btn" @click="doCheckin">
  97. <text>{{ submitting ? '提交中...' : '提交打卡' }}</text>
  98. </view>
  99. </view>
  100. </view>
  101. <!-- 历史记录 -->
  102. <view class="card">
  103. <view class="card-accent"></view>
  104. <view class="card-body">
  105. <text class="card-title">运动记录</text>
  106. <view class="history-list" v-if="history.length > 0">
  107. <view class="history-item" v-for="item in history" :key="item.id">
  108. <view class="history-left">
  109. <text class="history-type">{{ exerciseTypeLabel(item.exerciseType) }}</text>
  110. <text class="history-duration">{{ item.durationMinutes }}分钟</text>
  111. </view>
  112. <view class="history-right">
  113. <text class="history-date">{{ formatDate(item.createdAt) }}</text>
  114. </view>
  115. </view>
  116. </view>
  117. <view class="empty-state" v-else>
  118. <text>还没有运动记录</text>
  119. </view>
  120. </view>
  121. </view>
  122. </scroll-view>
  123. </template>
  124. <script>
  125. import config from '@/config.js'
  126. import { parseDate } from '@/utils/format.js'
  127. import { getPublicConfig } from '@/utils/api.js'
  128. import { DEFAULT_EXERCISE_TYPES, loadExerciseTypes, resolveExerciseTypeLabel } from '@/utils/exerciseTypes.js'
  129. import TagChip from '@/components/TagChip.vue'
  130. import fontScaleMixin from '@/mixins/font-scale.js'
  131. export default {
  132. mixins: [fontScaleMixin],
  133. components: { TagChip },
  134. data() {
  135. return {
  136. memberId: null,
  137. submitting: false,
  138. history: [],
  139. // 运动类型:首屏用统一默认(离线可用),onLoad 拉取后台配置覆盖
  140. exerciseTypes: DEFAULT_EXERCISE_TYPES.slice(),
  141. intensityOptions: [
  142. { label: '低强度', value: 'low' },
  143. { label: '中等', value: 'medium' },
  144. { label: '高强度', value: 'high' }
  145. ],
  146. form: {
  147. exerciseType: '',
  148. durationMinutes: '',
  149. intensity: 'medium',
  150. caloriesBurned: '',
  151. remark: ''
  152. }
  153. }
  154. },
  155. onLoad: function() {
  156. var role = uni.getStorageSync('currentRole') || uni.getStorageSync('role') || 'parent'
  157. // child 角色:memberId 由后端从 JWT 获取 currentMemberId,前端无需传
  158. if (role !== 'child') {
  159. this.memberId = uni.getStorageSync('currentChildId') || null
  160. }
  161. this.loadHistory()
  162. this.loadExerciseTypes()
  163. },
  164. methods: {
  165. // 拉取后台配置(sys_config.exercise_types),失败回退默认;缓存到 uni storage 供离线复用
  166. loadExerciseTypes: function() {
  167. var self = this
  168. loadExerciseTypes(function(key) { return getPublicConfig(key) }, {
  169. getItem: function(k) { return uni.getStorageSync(k) },
  170. setItem: function(k, v) { uni.setStorageSync(k, v) }
  171. }).then(function(types) { self.exerciseTypes = types })
  172. },
  173. // 历史 value(ball_game 等)经公共函数归一/映射,老记录 label 不丢
  174. exerciseTypeLabel: function(type) {
  175. return resolveExerciseTypeLabel(type, this.exerciseTypes)
  176. },
  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 lang="scss">
  257. .page {
  258. min-height: 100vh;
  259. background-color: $app-bg;
  260. display: flex;
  261. flex-direction: column;
  262. }
  263. .content {
  264. flex: 1;
  265. padding: $uni-space-page;
  266. }
  267. .card {
  268. display: flex;
  269. background-color: $app-surface;
  270. border-radius: 24rpx;
  271. margin-bottom: $uni-space-lg;
  272. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.06);
  273. overflow: hidden;
  274. }
  275. .card-accent {
  276. width: 8rpx;
  277. background-color: $dim-body;
  278. flex-shrink: 0;
  279. }
  280. .card-body {
  281. flex: 1;
  282. padding: $uni-space-page;
  283. }
  284. .card-title {
  285. font-size: calc(30rpx * var(--font-scale, 1));
  286. font-weight: 600;
  287. color: $facade-text-strong;
  288. margin-bottom: 24rpx;
  289. display: block;
  290. }
  291. .info-list {
  292. display: flex;
  293. flex-direction: column;
  294. gap: 20rpx;
  295. }
  296. .info-item {
  297. display: flex;
  298. flex-direction: column;
  299. gap: 6rpx;
  300. }
  301. .info-age {
  302. font-size: calc(26rpx * var(--font-scale, 1));
  303. font-weight: 500;
  304. color: $dim-body;
  305. display: block;
  306. }
  307. .info-desc {
  308. font-size: calc(24rpx * var(--font-scale, 1));
  309. color: $facade-text-body;
  310. line-height: 1.6;
  311. display: block;
  312. }
  313. .form-group {
  314. margin-bottom: 24rpx;
  315. }
  316. .form-label {
  317. font-size: calc(26rpx * var(--font-scale, 1));
  318. color: $facade-text-strong;
  319. font-weight: 500;
  320. margin-bottom: 12rpx;
  321. display: block;
  322. }
  323. .form-input {
  324. border: 1rpx solid $facade-border-soft;
  325. border-radius: 16rpx;
  326. padding: 16rpx 20rpx;
  327. font-size: calc(26rpx * var(--font-scale, 1));
  328. background-color: $facade-fill-ghost;
  329. width: auto;
  330. }
  331. .tag-row {
  332. display: flex;
  333. flex-wrap: wrap;
  334. gap: 16rpx;
  335. }
  336. .submit-btn {
  337. background-color: $dim-body;
  338. color: $app-surface;
  339. text-align: center;
  340. padding: 22rpx 0;
  341. border-radius: 16rpx;
  342. font-size: calc(28rpx * var(--font-scale, 1));
  343. font-weight: 500;
  344. margin-top: 10rpx;
  345. }
  346. .history-list {
  347. display: flex;
  348. flex-direction: column;
  349. gap: 16rpx;
  350. }
  351. .history-item {
  352. display: flex;
  353. justify-content: space-between;
  354. align-items: center;
  355. padding: 16rpx 0;
  356. border-bottom: 1rpx solid $facade-fill;
  357. }
  358. .history-left {
  359. display: flex;
  360. flex-direction: column;
  361. gap: 4rpx;
  362. }
  363. .history-type {
  364. font-size: calc(26rpx * var(--font-scale, 1));
  365. font-weight: 500;
  366. color: $facade-text-strong;
  367. }
  368. .history-duration {
  369. font-size: calc(22rpx * var(--font-scale, 1));
  370. color: $facade-text-muted;
  371. }
  372. .history-right {
  373. text-align: right;
  374. }
  375. .history-date {
  376. font-size: calc(22rpx * var(--font-scale, 1));
  377. color: $facade-text-muted;
  378. }
  379. .empty-state {
  380. text-align: center;
  381. padding: 40rpx 0;
  382. font-size: calc(26rpx * var(--font-scale, 1));
  383. color: $facade-text-muted;
  384. }
  385. </style>