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. import { parseDate } from '@/utils/format.js'
  98. const healthRequest = function(url, data) {
  99. var token = uni.getStorageSync('token')
  100. return new Promise(function(resolve, reject) {
  101. uni.request({
  102. url: config.API_BASE_URL + url,
  103. method: 'POST',
  104. data: data,
  105. header: {
  106. 'Content-Type': 'application/json',
  107. 'Authorization': token ? 'Bearer ' + token : ''
  108. },
  109. success: function(res) {
  110. if (res.data.code === 200) {
  111. resolve(res.data)
  112. } else {
  113. reject(res.data)
  114. }
  115. },
  116. fail: function(err) {
  117. reject(err)
  118. }
  119. })
  120. })
  121. }
  122. export default {
  123. data() {
  124. return {
  125. memberId: null,
  126. checkinDate: '',
  127. selectedBehaviors: [],
  128. selectedMood: '',
  129. note: '',
  130. checkinList: [],
  131. loadingHistory: false,
  132. behaviorOptions: [
  133. { label: '运动30分钟', value: '运动30分钟' },
  134. { label: '喝足8杯水', value: '喝足8杯水' },
  135. { label: '按时睡觉', value: '按时睡觉' },
  136. { label: '均衡饮食', value: '均衡饮食' },
  137. { label: '户外活动', value: '户外活动2小时' },
  138. { label: '视力保护', value: '视力保护' },
  139. { label: '情绪愉快', value: '情绪愉快' },
  140. { label: '无零食', value: '无零食' }
  141. ],
  142. moodOptions: [
  143. { label: '开心', value: 'happy', emoji: '😊' },
  144. { label: '平静', value: 'normal', emoji: '😐' },
  145. { label: '低落', value: 'sad', emoji: '😔' },
  146. { label: '烦躁', value: 'angry', emoji: '😤' },
  147. { label: '不适', value: 'sick', emoji: '🤒' }
  148. ]
  149. }
  150. },
  151. onLoad: function(options) {
  152. this.memberId = options.memberId ? parseInt(options.memberId) : null
  153. // 如果没有传 memberId,尝试从 storage 读取
  154. if (!this.memberId) {
  155. var savedChildId = uni.getStorageSync('currentChildId')
  156. if (savedChildId) this.memberId = parseInt(savedChildId)
  157. }
  158. this.checkinDate = this.formatDate(new Date())
  159. this.loadHistory()
  160. },
  161. methods: {
  162. pickDate: function() {
  163. var self = this
  164. var now = new Date()
  165. uni.chooseDate({
  166. success: function(res) {
  167. self.checkinDate = res.date
  168. }
  169. })
  170. },
  171. toggleBehavior: function(value) {
  172. var idx = this.selectedBehaviors.indexOf(value)
  173. if (idx === -1) {
  174. this.selectedBehaviors.push(value)
  175. } else {
  176. this.selectedBehaviors.splice(idx, 1)
  177. }
  178. },
  179. selectMood: function(value) {
  180. this.selectedMood = value
  181. },
  182. doCheckin: function() {
  183. if (this.selectedBehaviors.length === 0) {
  184. uni.showToast({ title: '请至少选择一项健康行为', icon: 'none' })
  185. return
  186. }
  187. if (!this.memberId) {
  188. uni.showToast({ title: '孩子ID无效', icon: 'none' })
  189. return
  190. }
  191. var self = this
  192. uni.showLoading({ title: '提交中...' })
  193. healthRequest('/api/health/checkin/create', {
  194. memberId: this.memberId,
  195. checkinDate: this.checkinDate,
  196. behaviors: this.selectedBehaviors,
  197. note: this.note,
  198. mood: this.selectedMood
  199. }).then(function(res) {
  200. uni.hideLoading()
  201. uni.showModal({
  202. title: '打卡成功',
  203. content: '恭喜获得 5 能量值!',
  204. showCancel: false
  205. })
  206. // Reset form
  207. self.selectedBehaviors = []
  208. self.selectedMood = ''
  209. self.note = ''
  210. self.checkinDate = self.formatDate(new Date())
  211. // Reload history
  212. self.loadHistory()
  213. }).catch(function(e) {
  214. uni.hideLoading()
  215. if (e && e.message && e.message.indexOf('404') !== -1) {
  216. uni.showToast({ title: '健康打卡功能即将上线', icon: 'none' })
  217. } else {
  218. uni.showToast({ title: '打卡失败,请重试', icon: 'none' })
  219. }
  220. })
  221. },
  222. loadHistory: function() {
  223. if (!this.memberId) return
  224. var self = this
  225. this.loadingHistory = true
  226. healthRequest('/api/health/checkin/list', {
  227. memberId: this.memberId
  228. }).then(function(res) {
  229. self.loadingHistory = false
  230. if (res.data) {
  231. self.checkinList = res.data
  232. }
  233. }).catch(function(e) {
  234. self.loadingHistory = false
  235. console.log('获取打卡记录失败', e)
  236. })
  237. },
  238. parseBehaviors: function(behaviorsStr) {
  239. if (!behaviorsStr) return []
  240. try {
  241. if (typeof behaviorsStr === 'string') {
  242. return JSON.parse(behaviorsStr)
  243. }
  244. return behaviorsStr
  245. } catch (e) {
  246. if (behaviorsStr.indexOf && behaviorsStr.indexOf('[') === 0) {
  247. try { return JSON.parse(behaviorsStr) } catch (e2) { return [behaviorsStr] }
  248. }
  249. return [behaviorsStr]
  250. }
  251. },
  252. getMoodEmoji: function(mood) {
  253. var map = { happy: '😊', normal: '😐', sad: '😔', angry: '😤', sick: '🤒' }
  254. return map[mood] || '😐'
  255. },
  256. formatDate: function(date) {
  257. if (!date) return ''
  258. var d = parseDate(date)
  259. if (!d) return date
  260. var y = d.getFullYear()
  261. var m = ('' + (d.getMonth() + 1)).padStart(2, '0')
  262. var day = ('' + d.getDate()).padStart(2, '0')
  263. return y + '-' + m + '-' + day
  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>