checkin.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. <template>
  2. <view class="checkin-container">
  3. <!-- 顶部导航 -->
  4. <view class="nav-bar">
  5. <text class="nav-back" @click="goBack">‹ 返回</text>
  6. <text class="nav-title">健康打卡</text>
  7. <view class="nav-placeholder"></view>
  8. </view>
  9. <!-- 能量奖励提示 -->
  10. <view class="energy-tip">
  11. <text class="tip-icon">⚡</text>
  12. <text class="tip-text">每次打卡可获得 5 能量值</text>
  13. </view>
  14. <!-- 打卡表单 -->
  15. <view class="form-section">
  16. <view class="form-title">今日打卡</view>
  17. <!-- 日期选择 -->
  18. <view class="form-item">
  19. <text class="form-label">打卡日期</text>
  20. <view class="date-display" @click="pickDate">
  21. <text>{{ checkinDate }}</text>
  22. <text class="date-arrow">›</text>
  23. </view>
  24. </view>
  25. <!-- 行为选择 -->
  26. <view class="form-item">
  27. <text class="form-label">健康行为</text>
  28. <view class="behaviors-grid">
  29. <view
  30. class="behavior-chip"
  31. v-for="bh in behaviorOptions"
  32. :key="bh.value"
  33. :class="{ 'behavior-selected': selectedBehaviors.indexOf(bh.value) !== -1 }"
  34. @click="toggleBehavior(bh.value)"
  35. >
  36. <text>{{ bh.label }}</text>
  37. </view>
  38. </view>
  39. </view>
  40. <!-- 心情选择 -->
  41. <view class="form-item">
  42. <text class="form-label">今日心情</text>
  43. <view class="mood-row">
  44. <view
  45. class="mood-item"
  46. v-for="m in moodOptions"
  47. :key="m.value"
  48. :class="{ 'mood-selected': selectedMood === m.value }"
  49. @click="selectMood(m.value)"
  50. >
  51. <text class="mood-emoji">{{ m.emoji }}</text>
  52. <text class="mood-label">{{ m.label }}</text>
  53. </view>
  54. </view>
  55. </view>
  56. <!-- 备注 -->
  57. <view class="form-item">
  58. <text class="form-label">备注(选填)</text>
  59. <textarea
  60. class="note-input"
  61. v-model="note"
  62. placeholder="记录今天的健康小故事..."
  63. maxlength="200"
  64. ></textarea>
  65. </view>
  66. <!-- 提交按钮 -->
  67. <view class="submit-btn" @click="doCheckin">
  68. <text>提交打卡</text>
  69. </view>
  70. </view>
  71. <!-- 打卡记录 -->
  72. <view class="history-section">
  73. <view class="section-title">打卡记录</view>
  74. <view class="history-list">
  75. <view class="history-item" v-for="item in checkinList" :key="item.id">
  76. <view class="history-left">
  77. <text class="history-date">{{ formatDate(item.checkinDate) }}</text>
  78. <view class="history-behaviors">
  79. <text class="bh-tag" v-for="bh in parseBehaviors(item.behaviors)" :key="bh">{{ bh }}</text>
  80. </view>
  81. </view>
  82. <view class="history-right">
  83. <text class="history-mood">{{ getMoodEmoji(item.mood) }}</text>
  84. <text class="history-energy">+{{ item.earnedEnergy || 5 }}⚡</text>
  85. </view>
  86. </view>
  87. <view class="history-empty" v-if="checkinList.length === 0 && !loadingHistory">
  88. <text>还没有打卡记录,快来打卡吧!</text>
  89. </view>
  90. </view>
  91. </view>
  92. <view class="bottom-spacer"></view>
  93. </view>
  94. </template>
  95. <script>
  96. import config from '@/config.js'
  97. const healthRequest = function(url, data) {
  98. var token = uni.getStorageSync('token')
  99. return new Promise(function(resolve, reject) {
  100. uni.request({
  101. url: config.API_BASE_URL + url,
  102. method: 'POST',
  103. data: data,
  104. header: {
  105. 'Content-Type': 'application/json',
  106. 'Authorization': token ? 'Bearer ' + token : ''
  107. },
  108. success: function(res) {
  109. if (res.data.code === 200) {
  110. resolve(res.data)
  111. } else {
  112. reject(res.data)
  113. }
  114. },
  115. fail: function(err) {
  116. reject(err)
  117. }
  118. })
  119. })
  120. }
  121. export default {
  122. data() {
  123. return {
  124. childId: null,
  125. checkinDate: '',
  126. selectedBehaviors: [],
  127. selectedMood: '',
  128. note: '',
  129. checkinList: [],
  130. loadingHistory: false,
  131. behaviorOptions: [
  132. { label: '运动30分钟', value: '运动30分钟' },
  133. { label: '喝足8杯水', value: '喝足8杯水' },
  134. { label: '按时睡觉', value: '按时睡觉' },
  135. { label: '均衡饮食', value: '均衡饮食' },
  136. { label: '户外活动', value: '户外活动2小时' },
  137. { label: '视力保护', value: '视力保护' },
  138. { label: '情绪愉快', value: '情绪愉快' },
  139. { label: '无零食', value: '无零食' }
  140. ],
  141. moodOptions: [
  142. { label: '开心', value: 'happy', emoji: '😊' },
  143. { label: '平静', value: 'normal', emoji: '😐' },
  144. { label: '低落', value: 'sad', emoji: '😔' },
  145. { label: '烦躁', value: 'angry', emoji: '😤' },
  146. { label: '不适', value: 'sick', emoji: '🤒' }
  147. ]
  148. }
  149. },
  150. onLoad: function(options) {
  151. this.childId = options.childId ? parseInt(options.childId) : null
  152. // 如果没有传 childId,尝试从 storage 读取
  153. if (!this.childId) {
  154. var savedChildId = uni.getStorageSync('currentChildId')
  155. if (savedChildId) this.childId = parseInt(savedChildId)
  156. }
  157. this.checkinDate = this.formatDate(new Date())
  158. this.loadHistory()
  159. },
  160. methods: {
  161. pickDate: function() {
  162. var self = this
  163. var now = new Date()
  164. uni.chooseDate({
  165. success: function(res) {
  166. self.checkinDate = res.date
  167. }
  168. })
  169. },
  170. toggleBehavior: function(value) {
  171. var idx = this.selectedBehaviors.indexOf(value)
  172. if (idx === -1) {
  173. this.selectedBehaviors.push(value)
  174. } else {
  175. this.selectedBehaviors.splice(idx, 1)
  176. }
  177. },
  178. selectMood: function(value) {
  179. this.selectedMood = value
  180. },
  181. doCheckin: function() {
  182. if (this.selectedBehaviors.length === 0) {
  183. uni.showToast({ title: '请至少选择一项健康行为', icon: 'none' })
  184. return
  185. }
  186. if (!this.childId) {
  187. uni.showToast({ title: '孩子ID无效', icon: 'none' })
  188. return
  189. }
  190. var self = this
  191. uni.showLoading({ title: '提交中...' })
  192. healthRequest('/api/health/checkin/create', {
  193. childId: this.childId,
  194. checkinDate: this.checkinDate,
  195. behaviors: this.selectedBehaviors,
  196. note: this.note,
  197. mood: this.selectedMood
  198. }).then(function(res) {
  199. uni.hideLoading()
  200. uni.showModal({
  201. title: '打卡成功',
  202. content: '恭喜获得 5 能量值!',
  203. showCancel: false
  204. })
  205. // Reset form
  206. self.selectedBehaviors = []
  207. self.selectedMood = ''
  208. self.note = ''
  209. self.checkinDate = self.formatDate(new Date())
  210. // Reload history
  211. self.loadHistory()
  212. }).catch(function(e) {
  213. uni.hideLoading()
  214. if (e && e.message && e.message.indexOf('404') !== -1) {
  215. uni.showToast({ title: '健康打卡功能即将上线', icon: 'none' })
  216. } else {
  217. uni.showToast({ title: '打卡失败,请重试', icon: 'none' })
  218. }
  219. })
  220. },
  221. loadHistory: function() {
  222. if (!this.childId) return
  223. var self = this
  224. this.loadingHistory = true
  225. healthRequest('/api/health/checkin/list', {
  226. childId: this.childId
  227. }).then(function(res) {
  228. self.loadingHistory = false
  229. if (res.data) {
  230. self.checkinList = res.data
  231. }
  232. }).catch(function(e) {
  233. self.loadingHistory = false
  234. console.log('获取打卡记录失败', e)
  235. })
  236. },
  237. parseBehaviors: function(behaviorsStr) {
  238. if (!behaviorsStr) return []
  239. try {
  240. if (typeof behaviorsStr === 'string') {
  241. return JSON.parse(behaviorsStr)
  242. }
  243. return behaviorsStr
  244. } catch (e) {
  245. if (behaviorsStr.indexOf && behaviorsStr.indexOf('[') === 0) {
  246. try { return JSON.parse(behaviorsStr) } catch (e2) { return [behaviorsStr] }
  247. }
  248. return [behaviorsStr]
  249. }
  250. },
  251. getMoodEmoji: function(mood) {
  252. var map = { happy: '😊', normal: '😐', sad: '😔', angry: '😤', sick: '🤒' }
  253. return map[mood] || '😐'
  254. },
  255. formatDate: function(date) {
  256. if (!date) return ''
  257. if (typeof date === 'string') {
  258. try { date = new Date(date) } catch (e) { return date }
  259. }
  260. var y = date.getFullYear()
  261. var m = ('' + (date.getMonth() + 1)).padStart(2, '0')
  262. var d = ('' + date.getDate()).padStart(2, '0')
  263. return y + '-' + m + '-' + d
  264. },
  265. goBack: function() {
  266. uni.navigateBack()
  267. }
  268. }
  269. }
  270. </script>
  271. <style scoped>
  272. .checkin-container {
  273. min-height: 100vh;
  274. background: #f5f7fa;
  275. padding-bottom: 60rpx;
  276. }
  277. /* ===== 导航栏 ===== */
  278. .nav-bar {
  279. position: sticky;
  280. top: 0;
  281. background: #fff;
  282. display: flex;
  283. flex-direction: row;
  284. align-items: center;
  285. justify-content: space-between;
  286. padding: 20rpx 30rpx;
  287. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
  288. z-index: 100;
  289. }
  290. .nav-back {
  291. font-size: 32rpx;
  292. color: #5B9BD5;
  293. }
  294. .nav-title {
  295. font-size: 32rpx;
  296. font-weight: bold;
  297. color: #333;
  298. }
  299. .nav-placeholder {
  300. width: 80rpx;
  301. }
  302. /* ===== 能量提示 ===== */
  303. .energy-tip {
  304. margin: 20rpx 30rpx;
  305. background: linear-gradient(135deg, #FFF8E1, #FFECB3);
  306. border-radius: 16rpx;
  307. padding: 16rpx 24rpx;
  308. display: flex;
  309. flex-direction: row;
  310. align-items: center;
  311. }
  312. .tip-icon {
  313. font-size: 32rpx;
  314. margin-right: 12rpx;
  315. }
  316. .tip-text {
  317. font-size: 26rpx;
  318. color: #E65100;
  319. }
  320. /* ===== 表单 ===== */
  321. .form-section {
  322. margin: 20rpx 30rpx;
  323. background: #fff;
  324. border-radius: 20rpx;
  325. padding: 24rpx;
  326. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  327. }
  328. .form-title {
  329. font-size: 30rpx;
  330. font-weight: bold;
  331. color: #333;
  332. margin-bottom: 24rpx;
  333. }
  334. .form-item {
  335. margin-bottom: 28rpx;
  336. }
  337. .form-label {
  338. font-size: 26rpx;
  339. color: #666;
  340. display: block;
  341. margin-bottom: 12rpx;
  342. }
  343. /* 日期 */
  344. .date-display {
  345. display: flex;
  346. flex-direction: row;
  347. align-items: center;
  348. justify-content: space-between;
  349. background: #f5f7fa;
  350. border-radius: 12rpx;
  351. padding: 16rpx 20rpx;
  352. font-size: 28rpx;
  353. color: #333;
  354. }
  355. .date-arrow {
  356. font-size: 32rpx;
  357. color: #ccc;
  358. }
  359. /* 行为选择 */
  360. .behaviors-grid {
  361. display: flex;
  362. flex-direction: row;
  363. flex-wrap: wrap;
  364. }
  365. .behavior-chip {
  366. background: #f0f0f0;
  367. color: #666;
  368. font-size: 24rpx;
  369. padding: 12rpx 20rpx;
  370. border-radius: 24rpx;
  371. margin-right: 16rpx;
  372. margin-bottom: 12rpx;
  373. }
  374. .behavior-selected {
  375. background: #E8F5E9;
  376. color: #2E7D32;
  377. }
  378. /* 心情 */
  379. .mood-row {
  380. display: flex;
  381. flex-direction: row;
  382. justify-content: space-between;
  383. }
  384. .mood-item {
  385. display: flex;
  386. flex-direction: column;
  387. align-items: center;
  388. padding: 12rpx 16rpx;
  389. border-radius: 12rpx;
  390. }
  391. .mood-selected {
  392. background: #FFF8E1;
  393. }
  394. .mood-emoji {
  395. font-size: 40rpx;
  396. }
  397. .mood-label {
  398. font-size: 20rpx;
  399. color: #666;
  400. margin-top: 4rpx;
  401. }
  402. /* 备注 */
  403. .note-input {
  404. width: 100%;
  405. box-sizing: border-box;
  406. background: #f5f7fa;
  407. border-radius: 12rpx;
  408. padding: 16rpx;
  409. font-size: 28rpx;
  410. color: #333;
  411. min-height: 120rpx;
  412. }
  413. /* 提交按钮 */
  414. .submit-btn {
  415. background: linear-gradient(135deg, #4CAF50, #66BB6A);
  416. color: #fff;
  417. font-size: 32rpx;
  418. font-weight: bold;
  419. text-align: center;
  420. padding: 24rpx;
  421. border-radius: 40rpx;
  422. margin-top: 16rpx;
  423. }
  424. .submit-btn:active {
  425. opacity: 0.8;
  426. }
  427. /* ===== 历史记录 ===== */
  428. .history-section {
  429. margin: 30rpx 30rpx 0;
  430. }
  431. .section-title {
  432. font-size: 30rpx;
  433. font-weight: bold;
  434. color: #333;
  435. margin-bottom: 16rpx;
  436. }
  437. .history-list {
  438. display: flex;
  439. flex-direction: column;
  440. }
  441. .history-item {
  442. background: #fff;
  443. border-radius: 16rpx;
  444. padding: 20rpx;
  445. margin-bottom: 12rpx;
  446. display: flex;
  447. flex-direction: row;
  448. align-items: center;
  449. justify-content: space-between;
  450. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.04);
  451. }
  452. .history-left {
  453. flex: 1;
  454. }
  455. .history-date {
  456. font-size: 26rpx;
  457. color: #333;
  458. font-weight: 500;
  459. display: block;
  460. margin-bottom: 8rpx;
  461. }
  462. .history-behaviors {
  463. display: flex;
  464. flex-direction: row;
  465. flex-wrap: wrap;
  466. }
  467. .bh-tag {
  468. background: #E3F2FD;
  469. color: #1565C0;
  470. font-size: 20rpx;
  471. padding: 4rpx 12rpx;
  472. border-radius: 12rpx;
  473. margin-right: 8rpx;
  474. margin-bottom: 4rpx;
  475. }
  476. .history-right {
  477. display: flex;
  478. flex-direction: column;
  479. align-items: flex-end;
  480. }
  481. .history-mood {
  482. font-size: 32rpx;
  483. }
  484. .history-energy {
  485. font-size: 22rpx;
  486. color: #4CAF50;
  487. margin-top: 4rpx;
  488. }
  489. .history-empty {
  490. text-align: center;
  491. padding: 40rpx;
  492. color: #999;
  493. font-size: 26rpx;
  494. }
  495. /* ===== 底部 ===== */
  496. .bottom-spacer {
  497. height: 60rpx;
  498. }
  499. </style>