checkin.vue 12 KB

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