checkin.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. this.checkinDate = this.formatDate(new Date())
  153. this.loadHistory()
  154. },
  155. methods: {
  156. pickDate: function() {
  157. var self = this
  158. var now = new Date()
  159. uni.chooseDate({
  160. success: function(res) {
  161. self.checkinDate = res.date
  162. }
  163. })
  164. },
  165. toggleBehavior: function(value) {
  166. var idx = this.selectedBehaviors.indexOf(value)
  167. if (idx === -1) {
  168. this.selectedBehaviors.push(value)
  169. } else {
  170. this.selectedBehaviors.splice(idx, 1)
  171. }
  172. },
  173. selectMood: function(value) {
  174. this.selectedMood = value
  175. },
  176. doCheckin: function() {
  177. if (this.selectedBehaviors.length === 0) {
  178. uni.showToast({ title: '请至少选择一项健康行为', icon: 'none' })
  179. return
  180. }
  181. if (!this.childId) {
  182. uni.showToast({ title: '孩子ID无效', icon: 'none' })
  183. return
  184. }
  185. var self = this
  186. uni.showLoading({ title: '提交中...' })
  187. healthRequest('/api/health/checkin/create', {
  188. childId: this.childId,
  189. checkinDate: this.checkinDate,
  190. behaviors: this.selectedBehaviors,
  191. note: this.note,
  192. mood: this.selectedMood
  193. }).then(function(res) {
  194. uni.hideLoading()
  195. uni.showModal({
  196. title: '打卡成功',
  197. content: '恭喜获得 5 能量值!',
  198. showCancel: false
  199. })
  200. // Reset form
  201. self.selectedBehaviors = []
  202. self.selectedMood = ''
  203. self.note = ''
  204. self.checkinDate = self.formatDate(new Date())
  205. // Reload history
  206. self.loadHistory()
  207. }).catch(function(e) {
  208. uni.hideLoading()
  209. if (e && e.message && e.message.indexOf('404') !== -1) {
  210. uni.showToast({ title: '健康打卡功能即将上线', icon: 'none' })
  211. } else {
  212. uni.showToast({ title: '打卡失败,请重试', icon: 'none' })
  213. }
  214. })
  215. },
  216. loadHistory: function() {
  217. if (!this.childId) return
  218. var self = this
  219. this.loadingHistory = true
  220. healthRequest('/api/health/checkin/list', {
  221. childId: this.childId
  222. }).then(function(res) {
  223. self.loadingHistory = false
  224. if (res.data) {
  225. self.checkinList = res.data
  226. }
  227. }).catch(function(e) {
  228. self.loadingHistory = false
  229. console.log('获取打卡记录失败', e)
  230. })
  231. },
  232. parseBehaviors: function(behaviorsStr) {
  233. if (!behaviorsStr) return []
  234. try {
  235. if (typeof behaviorsStr === 'string') {
  236. return JSON.parse(behaviorsStr)
  237. }
  238. return behaviorsStr
  239. } catch (e) {
  240. if (behaviorsStr.indexOf && behaviorsStr.indexOf('[') === 0) {
  241. try { return JSON.parse(behaviorsStr) } catch (e2) { return [behaviorsStr] }
  242. }
  243. return [behaviorsStr]
  244. }
  245. },
  246. getMoodEmoji: function(mood) {
  247. var map = { happy: '😊', normal: '😐', sad: '😔', angry: '😤', sick: '🤒' }
  248. return map[mood] || '😐'
  249. },
  250. formatDate: function(date) {
  251. if (!date) return ''
  252. if (typeof date === 'string') {
  253. try { date = new Date(date) } catch (e) { return date }
  254. }
  255. var y = date.getFullYear()
  256. var m = ('' + (date.getMonth() + 1)).padStart(2, '0')
  257. var d = ('' + date.getDate()).padStart(2, '0')
  258. return y + '-' + m + '-' + d
  259. },
  260. goBack: function() {
  261. uni.navigateBack()
  262. }
  263. }
  264. }
  265. </script>
  266. <style scoped>
  267. .checkin-container {
  268. min-height: 100vh;
  269. background: #f5f7fa;
  270. padding-bottom: 60rpx;
  271. }
  272. /* ===== 导航栏 ===== */
  273. .nav-bar {
  274. position: sticky;
  275. top: 0;
  276. background: #fff;
  277. display: flex;
  278. flex-direction: row;
  279. align-items: center;
  280. justify-content: space-between;
  281. padding: 20rpx 30rpx;
  282. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
  283. z-index: 100;
  284. }
  285. .nav-back {
  286. font-size: 32rpx;
  287. color: #5B9BD5;
  288. }
  289. .nav-title {
  290. font-size: 32rpx;
  291. font-weight: bold;
  292. color: #333;
  293. }
  294. .nav-placeholder {
  295. width: 80rpx;
  296. }
  297. /* ===== 能量提示 ===== */
  298. .energy-tip {
  299. margin: 20rpx 30rpx;
  300. background: linear-gradient(135deg, #FFF8E1, #FFECB3);
  301. border-radius: 16rpx;
  302. padding: 16rpx 24rpx;
  303. display: flex;
  304. flex-direction: row;
  305. align-items: center;
  306. }
  307. .tip-icon {
  308. font-size: 32rpx;
  309. margin-right: 12rpx;
  310. }
  311. .tip-text {
  312. font-size: 26rpx;
  313. color: #E65100;
  314. }
  315. /* ===== 表单 ===== */
  316. .form-section {
  317. margin: 20rpx 30rpx;
  318. background: #fff;
  319. border-radius: 20rpx;
  320. padding: 24rpx;
  321. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  322. }
  323. .form-title {
  324. font-size: 30rpx;
  325. font-weight: bold;
  326. color: #333;
  327. margin-bottom: 24rpx;
  328. }
  329. .form-item {
  330. margin-bottom: 28rpx;
  331. }
  332. .form-label {
  333. font-size: 26rpx;
  334. color: #666;
  335. display: block;
  336. margin-bottom: 12rpx;
  337. }
  338. /* 日期 */
  339. .date-display {
  340. display: flex;
  341. flex-direction: row;
  342. align-items: center;
  343. justify-content: space-between;
  344. background: #f5f7fa;
  345. border-radius: 12rpx;
  346. padding: 16rpx 20rpx;
  347. font-size: 28rpx;
  348. color: #333;
  349. }
  350. .date-arrow {
  351. font-size: 32rpx;
  352. color: #ccc;
  353. }
  354. /* 行为选择 */
  355. .behaviors-grid {
  356. display: flex;
  357. flex-direction: row;
  358. flex-wrap: wrap;
  359. }
  360. .behavior-chip {
  361. background: #f0f0f0;
  362. color: #666;
  363. font-size: 24rpx;
  364. padding: 12rpx 20rpx;
  365. border-radius: 24rpx;
  366. margin-right: 16rpx;
  367. margin-bottom: 12rpx;
  368. }
  369. .behavior-selected {
  370. background: #E8F5E9;
  371. color: #2E7D32;
  372. }
  373. /* 心情 */
  374. .mood-row {
  375. display: flex;
  376. flex-direction: row;
  377. justify-content: space-between;
  378. }
  379. .mood-item {
  380. display: flex;
  381. flex-direction: column;
  382. align-items: center;
  383. padding: 12rpx 16rpx;
  384. border-radius: 12rpx;
  385. }
  386. .mood-selected {
  387. background: #FFF8E1;
  388. }
  389. .mood-emoji {
  390. font-size: 40rpx;
  391. }
  392. .mood-label {
  393. font-size: 20rpx;
  394. color: #666;
  395. margin-top: 4rpx;
  396. }
  397. /* 备注 */
  398. .note-input {
  399. width: 100%;
  400. box-sizing: border-box;
  401. background: #f5f7fa;
  402. border-radius: 12rpx;
  403. padding: 16rpx;
  404. font-size: 28rpx;
  405. color: #333;
  406. min-height: 120rpx;
  407. }
  408. /* 提交按钮 */
  409. .submit-btn {
  410. background: linear-gradient(135deg, #4CAF50, #66BB6A);
  411. color: #fff;
  412. font-size: 32rpx;
  413. font-weight: bold;
  414. text-align: center;
  415. padding: 24rpx;
  416. border-radius: 40rpx;
  417. margin-top: 16rpx;
  418. }
  419. .submit-btn:active {
  420. opacity: 0.8;
  421. }
  422. /* ===== 历史记录 ===== */
  423. .history-section {
  424. margin: 30rpx 30rpx 0;
  425. }
  426. .section-title {
  427. font-size: 30rpx;
  428. font-weight: bold;
  429. color: #333;
  430. margin-bottom: 16rpx;
  431. }
  432. .history-list {
  433. display: flex;
  434. flex-direction: column;
  435. }
  436. .history-item {
  437. background: #fff;
  438. border-radius: 16rpx;
  439. padding: 20rpx;
  440. margin-bottom: 12rpx;
  441. display: flex;
  442. flex-direction: row;
  443. align-items: center;
  444. justify-content: space-between;
  445. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.04);
  446. }
  447. .history-left {
  448. flex: 1;
  449. }
  450. .history-date {
  451. font-size: 26rpx;
  452. color: #333;
  453. font-weight: 500;
  454. display: block;
  455. margin-bottom: 8rpx;
  456. }
  457. .history-behaviors {
  458. display: flex;
  459. flex-direction: row;
  460. flex-wrap: wrap;
  461. }
  462. .bh-tag {
  463. background: #E3F2FD;
  464. color: #1565C0;
  465. font-size: 20rpx;
  466. padding: 4rpx 12rpx;
  467. border-radius: 12rpx;
  468. margin-right: 8rpx;
  469. margin-bottom: 4rpx;
  470. }
  471. .history-right {
  472. display: flex;
  473. flex-direction: column;
  474. align-items: flex-end;
  475. }
  476. .history-mood {
  477. font-size: 32rpx;
  478. }
  479. .history-energy {
  480. font-size: 22rpx;
  481. color: #4CAF50;
  482. margin-top: 4rpx;
  483. }
  484. .history-empty {
  485. text-align: center;
  486. padding: 40rpx;
  487. color: #999;
  488. font-size: 26rpx;
  489. }
  490. /* ===== 底部 ===== */
  491. .bottom-spacer {
  492. height: 60rpx;
  493. }
  494. </style>