daily-checkin.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <template>
  2. <scroll-view class="content" scroll-y>
  3. <view class="card">
  4. <view class="card-accent"></view>
  5. <view class="card-body">
  6. <text class="card-title">今日综合打卡</text>
  7. <text class="card-subtitle">为你的一天各项健康指标打分吧</text>
  8. <view class="score-group">
  9. <text class="score-label">饮食评分</text>
  10. <view class="score-row">
  11. <text class="score-min">差</text>
  12. <slider min="1" max="10" :value="form.dietScore" @change="function(e){form.dietScore=e.detail.value}" block-size="20" activeColor="#FF8C42" />
  13. <text class="score-val">{{ form.dietScore }}</text>
  14. </view>
  15. </view>
  16. <view class="score-group">
  17. <text class="score-label">运动评分</text>
  18. <view class="score-row">
  19. <text class="score-min">差</text>
  20. <slider min="1" max="10" :value="form.exerciseScore" @change="function(e){form.exerciseScore=e.detail.value}" block-size="20" activeColor="#FF8C42" />
  21. <text class="score-val">{{ form.exerciseScore }}</text>
  22. </view>
  23. </view>
  24. <view class="score-group">
  25. <text class="score-label">睡眠评分</text>
  26. <view class="score-row">
  27. <text class="score-min">差</text>
  28. <slider min="1" max="10" :value="form.sleepScore" @change="function(e){form.sleepScore=e.detail.value}" block-size="20" activeColor="#FF8C42" />
  29. <text class="score-val">{{ form.sleepScore }}</text>
  30. </view>
  31. </view>
  32. <view class="score-group">
  33. <text class="score-label">饮水评分</text>
  34. <view class="score-row">
  35. <text class="score-min">差</text>
  36. <slider min="1" max="10" :value="form.waterScore" @change="function(e){form.waterScore=e.detail.value}" block-size="20" activeColor="#FF8C42" />
  37. <text class="score-val">{{ form.waterScore }}</text>
  38. </view>
  39. </view>
  40. <view class="score-group">
  41. <text class="score-label">心情评分</text>
  42. <view class="score-row">
  43. <text class="score-min">差</text>
  44. <slider min="1" max="10" :value="form.moodScore" @change="function(e){form.moodScore=e.detail.value}" block-size="20" activeColor="#FF8C42" />
  45. <text class="score-val">{{ form.moodScore }}</text>
  46. </view>
  47. </view>
  48. <view class="form-group">
  49. <text class="form-label">备注(选填)</text>
  50. <input class="form-input" v-model="form.remark" placeholder="今天的感受..." />
  51. </view>
  52. <view class="submit-btn" @tap="doCheckin">
  53. <text>{{ submitting ? '提交中...' : '提交打卡' }}</text>
  54. </view>
  55. </view>
  56. </view>
  57. <!-- 历史记录 -->
  58. <view class="card">
  59. <view class="card-accent"></view>
  60. <view class="card-body">
  61. <text class="card-title">打卡记录</text>
  62. <view class="history-list" v-if="history.length > 0">
  63. <view class="history-item" v-for="item in history" :key="item.id">
  64. <view class="history-left">
  65. <text class="history-type">{{ formatDate(item.checkinDate || item.createdAt) }}</text>
  66. <text class="history-duration">
  67. 饮食{{ item.dietScore || '-' }} 运动{{ item.exerciseScore || '-' }}
  68. 睡眠{{ item.sleepScore || '-' }} 饮水{{ item.waterIntake || '-' }}
  69. 心情{{ item.moodScore || '-' }}
  70. </text>
  71. </view>
  72. <view class="history-right">
  73. <text class="history-date">{{ item.remark || '' }}</text>
  74. </view>
  75. </view>
  76. </view>
  77. <view class="empty-state" v-else>
  78. <text>还没有打卡记录</text>
  79. </view>
  80. </view>
  81. </view>
  82. </scroll-view>
  83. </template>
  84. <script>
  85. import config from '@/config.js'
  86. export default {
  87. data() {
  88. return {
  89. memberId: null,
  90. submitting: false,
  91. history: [],
  92. form: {
  93. dietScore: 7,
  94. exerciseScore: 7,
  95. sleepScore: 7,
  96. waterScore: 7,
  97. moodScore: 7,
  98. remark: ''
  99. }
  100. }
  101. },
  102. onLoad: function() {
  103. var role = uni.getStorageSync('currentRole') || uni.getStorageSync('role') || 'parent'
  104. // child 角色:memberId 由后端从 JWT 获取 currentMemberId,前端无需传
  105. if (role !== 'child') {
  106. this.memberId = uni.getStorageSync('currentChildId') || null
  107. }
  108. this.loadHistory()
  109. },
  110. methods: {
  111. goBack: function() {
  112. uni.navigateBack()
  113. },
  114. formatDate: function(dateStr) {
  115. if (!dateStr) return ''
  116. var d = new Date(dateStr)
  117. var m = (d.getMonth() + 1).toString().padStart(2, '0')
  118. var day = d.getDate().toString().padStart(2, '0')
  119. return m + '-' + day
  120. },
  121. loadHistory: function() {
  122. var self = this
  123. var token = uni.getStorageSync('token')
  124. if (!token || !self.memberId) return
  125. uni.request({
  126. url: config.API_BASE_URL + '/api/daily/checkin/list',
  127. method: 'POST',
  128. data: { memberId: self.memberId },
  129. header: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
  130. success: function(res) {
  131. if (res.data && res.data.code === 200 && res.data.data) {
  132. self.history = res.data.data.slice(0, 10)
  133. }
  134. }
  135. })
  136. },
  137. doCheckin: function() {
  138. var self = this
  139. if (!self.memberId) {
  140. uni.showToast({ title: '成员ID无效', icon: 'none' })
  141. return
  142. }
  143. self.submitting = true
  144. var token = uni.getStorageSync('token')
  145. uni.request({
  146. url: config.API_BASE_URL + '/api/daily/checkin/create',
  147. method: 'POST',
  148. data: {
  149. memberId: self.memberId,
  150. dietScore: self.form.dietScore,
  151. exerciseScore: self.form.exerciseScore,
  152. sleepScore: self.form.sleepScore,
  153. waterIntake: self.form.waterScore,
  154. moodScore: self.form.moodScore,
  155. remark: self.form.remark || ''
  156. },
  157. header: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
  158. success: function(res) {
  159. self.submitting = false
  160. if (res.data && res.data.code === 200) {
  161. uni.showToast({ title: '打卡成功', icon: 'success' })
  162. self.form.remark = ''
  163. self.loadHistory()
  164. } else {
  165. var msg = (res.data && res.data.message) || '打卡失败'
  166. uni.showToast({ title: msg, icon: 'none' })
  167. }
  168. },
  169. fail: function() {
  170. self.submitting = false
  171. uni.showToast({ title: '网络错误', icon: 'none' })
  172. }
  173. })
  174. }
  175. }
  176. }
  177. </script>
  178. <style scoped>
  179. .page {
  180. min-height: 100vh;
  181. background-color: #F5F7FA;
  182. display: flex;
  183. flex-direction: column;
  184. }
  185. .content { flex: 1; padding: 30rpx; }
  186. .card {
  187. display: flex; background-color: #FFFFFF; border-radius: 20rpx; margin-bottom: 30rpx;
  188. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.06); overflow: hidden;
  189. }
  190. .card-accent { width: 8rpx; background-color: #FF8C42; flex-shrink: 0; }
  191. .card-body { flex: 1; padding: 30rpx; }
  192. .card-title { font-size: 30rpx; font-weight: 600; color: #333; margin-bottom: 8rpx; display: block; }
  193. .card-subtitle { font-size: 24rpx; color: #999; margin-bottom: 30rpx; display: block; }
  194. .score-group { margin-bottom: 20rpx; }
  195. .score-label { font-size: 26rpx; color: #333; font-weight: 500; margin-bottom: 8rpx; display: block; }
  196. .score-row { display: flex; align-items: center; gap: 16rpx; }
  197. .score-min { font-size: 22rpx; color: #999; flex-shrink: 0; }
  198. .score-val { font-size: 24rpx; color: #FF8C42; font-weight: 500; width: 40rpx; text-align: center; }
  199. .form-group { margin-bottom: 24rpx; }
  200. .form-label { font-size: 26rpx; color: #333; font-weight: 500; margin-bottom: 12rpx; display: block; }
  201. .form-input {
  202. border: 1rpx solid #ddd; border-radius: 12rpx; padding: 16rpx 20rpx;
  203. font-size: 26rpx; background-color: #FAFAFA; width: auto;
  204. }
  205. .submit-btn {
  206. background-color: #FF8C42; color: #FFFFFF; text-align: center; padding: 22rpx 0;
  207. border-radius: 16rpx; font-size: 28rpx; font-weight: 500; margin-top: 10rpx;
  208. }
  209. .history-list { display: flex; flex-direction: column; gap: 16rpx; }
  210. .history-item {
  211. display: flex; justify-content: space-between; align-items: center; padding: 16rpx 0;
  212. border-bottom: 1rpx solid #f0f0f0;
  213. }
  214. .history-left { display: flex; flex-direction: column; gap: 4rpx; }
  215. .history-type { font-size: 26rpx; font-weight: 500; color: #333; }
  216. .history-duration { font-size: 22rpx; color: #999; }
  217. .history-right { text-align: right; max-width: 40%; }
  218. .history-date { font-size: 22rpx; color: #999; }
  219. .empty-state { text-align: center; padding: 40rpx 0; font-size: 26rpx; color: #999; }
  220. </style>