diet-checkin.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <template>
  2. <view class="page">
  3. <view class="nav-bar">
  4. <view class="nav-back" @tap="goBack">
  5. <text class="back-text">‹ 返回</text>
  6. </view>
  7. <text class="nav-title">饮食打卡</text>
  8. </view>
  9. <scroll-view class="content" scroll-y>
  10. <!-- 餐次选择 -->
  11. <view class="card">
  12. <view class="card-accent"></view>
  13. <view class="card-body">
  14. <text class="card-title">记录饮食</text>
  15. <view class="form-group">
  16. <text class="form-label">餐次 *</text>
  17. <view class="tag-row">
  18. <view class="tag-btn" v-for="m in mealTypes" :key="m.value"
  19. :class="form.mealType === m.value ? 'tag-active' : ''"
  20. @tap="form.mealType = m.value">
  21. <text>{{ m.label }}</text>
  22. </view>
  23. </view>
  24. </view>
  25. <view class="form-group">
  26. <text class="form-label">食物清单</text>
  27. <textarea class="form-textarea" v-model="form.foodItems" placeholder="吃了什么?如:米饭1碗、青菜、鸡腿" maxlength="200"></textarea>
  28. </view>
  29. <view class="form-group">
  30. <text class="form-label">备注(选填)</text>
  31. <input class="form-input" v-model="form.remark" placeholder="今天的饮食感受..." />
  32. </view>
  33. </view>
  34. </view>
  35. <!-- 照片上传 -->
  36. <view class="card">
  37. <view class="card-accent"></view>
  38. <view class="card-body">
  39. <view class="section-header">
  40. <text class="card-title">📷 拍照记录</text>
  41. <text class="photo-count">{{ form.photos.length }}/9</text>
  42. </view>
  43. <view class="photo-grid">
  44. <view class="photo-thumb" v-for="(img, idx) in form.photos" :key="getPhotoKey(img, idx)" @tap="removePhoto(idx)">
  45. <image :src="img" mode="aspectFill" class="photo-img" />
  46. <view class="photo-delete">
  47. <text class="photo-delete-icon">×</text>
  48. </view>
  49. </view>
  50. <view class="photo-add-btn" v-if="form.photos.length < 9" @tap="choosePhoto">
  51. <text class="camera-icon">📷</text>
  52. </view>
  53. </view>
  54. </view>
  55. </view>
  56. <!-- 语音录入 -->
  57. <view class="card">
  58. <view class="card-accent"></view>
  59. <view class="card-body">
  60. <text class="card-title">🎙️ 语音打卡</text>
  61. <view class="voice-recorder">
  62. <view class="voice-active" v-if="isRecording">
  63. <view class="waveform">
  64. <view class="wave-bar wave-1"></view>
  65. <view class="wave-bar wave-2"></view>
  66. <view class="wave-bar wave-3"></view>
  67. <view class="wave-bar wave-4"></view>
  68. <view class="wave-bar wave-5"></view>
  69. </view>
  70. <text class="recording-time">{{ recordSeconds }}s</text>
  71. </view>
  72. <view class="voice-done" v-else-if="form.voicePath">
  73. <text class="play-icon">▶</text>
  74. <text class="done-text">{{ recordDurationText }}</text>
  75. </view>
  76. <view class="record-btn-wrap">
  77. <view class="pulse-ring" v-if="isRecording"></view>
  78. <view class="record-btn" :class="{ 'record-btn-active': isRecording }" @tap="toggleRecord">
  79. <view class="record-btn-dot"></view>
  80. </view>
  81. </view>
  82. </view>
  83. <text class="voice-hint">点击开始录音</text>
  84. </view>
  85. </view>
  86. <!-- 提交按钮 -->
  87. <view class="submit-wrap">
  88. <view class="submit-btn" :class="{ 'submitting': submitting }" @tap="doCheckin">
  89. <text>{{ submitting ? '提交中...' : '✓ 完成打卡' }}</text>
  90. </view>
  91. </view>
  92. <!-- 历史记录 -->
  93. <view class="card" v-if="history.length > 0">
  94. <view class="card-accent"></view>
  95. <view class="card-body">
  96. <text class="card-title">今日记录</text>
  97. <view class="history-list">
  98. <view class="history-item" v-for="item in history" :key="item.id">
  99. <view class="history-left">
  100. <text class="history-type">{{ mealTypeLabel(item.mealType) }}</text>
  101. <text class="history-content">{{ item.foodItems || item.notes || '-' }}</text>
  102. </view>
  103. <view class="history-right">
  104. <text class="history-date">{{ formatDate(item.createdAt) }}</text>
  105. </view>
  106. </view>
  107. </view>
  108. </view>
  109. </view>
  110. </scroll-view>
  111. </view>
  112. </template>
  113. <script>
  114. import config from '../../config'
  115. import { parseDate } from '../../utils/format.js'
  116. export default {
  117. data() {
  118. return {
  119. memberId: null,
  120. submitting: false,
  121. isRecording: false,
  122. recordSeconds: 0,
  123. recordTimer: null,
  124. recorderManager: null,
  125. history: [],
  126. mealTypes: [
  127. { label: '早餐', value: 'breakfast' },
  128. { label: '午餐', value: 'lunch' },
  129. { label: '晚餐', value: 'dinner' },
  130. { label: '加餐', value: 'snack' }
  131. ],
  132. form: {
  133. mealType: '',
  134. foodItems: '',
  135. remark: '',
  136. photos: [],
  137. voicePath: ''
  138. }
  139. }
  140. },
  141. computed: {
  142. recordDurationText: function() {
  143. if (!this.form.voicePath) return ''
  144. var s = this.recordSeconds
  145. return '录音 ' + s + '秒'
  146. }
  147. },
  148. onLoad: function() {
  149. var role = uni.getStorageSync('currentRole') || uni.getStorageSync('role') || 'parent'
  150. if (role !== 'child') {
  151. this.memberId = uni.getStorageSync('currentChildId') || uni.getStorageSync('currentMemberId') || uni.getStorageSync('userId') || null
  152. }
  153. this.recorderManager = uni.getRecorderManager()
  154. this.recorderManager.onStart(() => {
  155. this.isRecording = true
  156. this.recordSeconds = 0
  157. this.recordTimer = setInterval(() => {
  158. this.recordSeconds++
  159. }, 1000)
  160. })
  161. this.recorderManager.onStop((res) => {
  162. this.isRecording = false
  163. if (this.recordTimer) { clearInterval(this.recordTimer); this.recordTimer = null }
  164. this.form.voicePath = res.tempFilePath
  165. this.form.voiceDuration = res.duration
  166. })
  167. this.recorderManager.onError((err) => {
  168. this.isRecording = false
  169. if (this.recordTimer) { clearInterval(this.recordTimer); this.recordTimer = null }
  170. uni.showToast({ title: '录音失败', icon: 'none' })
  171. })
  172. this.loadHistory()
  173. },
  174. onUnload: function() {
  175. if (this.recordTimer) clearInterval(this.recordTimer)
  176. },
  177. methods: {
  178. goBack: function() { uni.navigateBack() },
  179. mealTypeLabel: function(type) {
  180. var m = { breakfast: '早餐', lunch: '午餐', dinner: '晚餐', snack: '加餐' }
  181. return m[type] || type || '饮食'
  182. },
  183. formatDate: function(dateStr) {
  184. if (!dateStr) return ''
  185. var d = parseDate(dateStr)
  186. if (!d) return dateStr
  187. var m2 = (d.getMonth() + 1).toString().padStart(2, '0')
  188. var day = d.getDate().toString().padStart(2, '0')
  189. return m2 + '-' + day
  190. },
  191. loadHistory: function() {
  192. var self = this
  193. var token = uni.getStorageSync('token')
  194. if (!token || !self.memberId) return
  195. uni.request({
  196. url: config.API_BASE_URL + '/api/health/meal/list',
  197. method: 'POST',
  198. data: { memberId: self.memberId },
  199. header: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
  200. success: function(res) {
  201. if (res.data && res.data.code === 200 && res.data.data) {
  202. self.history = res.data.data.slice(0, 5)
  203. }
  204. }
  205. })
  206. },
  207. choosePhoto: function() {
  208. var self = this
  209. uni.chooseImage({
  210. count: 9 - self.form.photos.length,
  211. sizeType: ['compressed'],
  212. sourceType: ['album', 'camera'],
  213. success: function(res) {
  214. res.tempFilePaths.forEach(function(path) {
  215. if (self.form.photos.length < 9) self.form.photos.push(path)
  216. })
  217. }
  218. })
  219. },
  220. getPhotoKey: function(img, idx) {
  221. return img ? img : ('p' + idx)
  222. },
  223. removePhoto: function(idx) {
  224. this.form.photos.splice(idx, 1)
  225. },
  226. toggleRecord: function() {
  227. if (this.isRecording) {
  228. this.recorderManager.stop()
  229. } else {
  230. this.recorderManager.start({ duration: 60000, format: 'mp3' })
  231. }
  232. },
  233. uploadFile: function(filePath) {
  234. var self = this
  235. return new Promise(function(resolve, reject) {
  236. uni.uploadFile({
  237. url: config.API_BASE_URL + '/api/media/upload',
  238. filePath: filePath,
  239. name: 'file',
  240. header: { 'Authorization': 'Bearer ' + uni.getStorageSync('token') },
  241. success: function(res) {
  242. try {
  243. var data = JSON.parse(res.data)
  244. resolve(data.data || data.url || '')
  245. } catch (e) {
  246. resolve(res.data || '')
  247. }
  248. },
  249. fail: function() { reject() }
  250. })
  251. })
  252. },
  253. async doCheckin() {
  254. var self = this
  255. if (!self.form.mealType) { uni.showToast({ title: '请选择餐次', icon: 'none' }); return }
  256. if (!self.memberId) { uni.showToast({ title: '孩子ID无效', icon: 'none' }); return }
  257. self.submitting = true
  258. try {
  259. var photoUrls = []
  260. if (self.form.photos && self.form.photos.length > 0) {
  261. for (var i = 0; i < self.form.photos.length; i++) {
  262. try { var url = await self.uploadFile(self.form.photos[i]) } catch(e) { url = '' }
  263. if (url) photoUrls.push(url)
  264. }
  265. }
  266. var voiceUrl = ''
  267. if (self.form.voicePath) {
  268. try { voiceUrl = await self.uploadFile(self.form.voicePath) } catch(e) { voiceUrl = '' }
  269. }
  270. var token = uni.getStorageSync('token')
  271. uni.request({
  272. url: config.API_BASE_URL + '/api/health/meal/create',
  273. method: 'POST',
  274. data: {
  275. memberId: self.memberId,
  276. mealType: self.form.mealType,
  277. foodItems: self.form.foodItems || '',
  278. remark: self.form.remark || '',
  279. photoUrls: photoUrls.join(','),
  280. voiceUrl: voiceUrl
  281. },
  282. header: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
  283. success: function(res) {
  284. self.submitting = false
  285. if (res.data && res.data.code === 200) {
  286. uni.showToast({ title: '打卡成功', icon: 'success' })
  287. self.form.mealType = ''
  288. self.form.foodItems = ''
  289. self.form.remark = ''
  290. self.form.photos = []
  291. self.form.voicePath = ''
  292. self.recordSeconds = 0
  293. self.loadHistory()
  294. } else {
  295. uni.showToast({ title: (res.data && res.data.message) || '打卡失败', icon: 'none' })
  296. }
  297. },
  298. fail: function() {
  299. self.submitting = false
  300. uni.showToast({ title: '网络错误', icon: 'none' })
  301. }
  302. })
  303. } catch (e) {
  304. self.submitting = false
  305. uni.showToast({ title: '提交失败', icon: 'none' })
  306. }
  307. }
  308. }
  309. }
  310. </script>
  311. <style scoped>
  312. .page { min-height: 100vh; background: #FFF7ED; }
  313. .nav-bar { display: flex; align-items: center; height: 88rpx; padding-top: env(safe-area-inset-top); background: #fff; border-bottom: 1rpx solid #f0f0f0; position: relative; }
  314. .nav-back { position: absolute; left: 24rpx; top: 0; bottom: 0; display: flex; align-items: center; }
  315. .back-text { font-size: 28rpx; color: #F97316; }
  316. .nav-title { flex: 1; text-align: center; font-size: 32rpx; font-weight: bold; color: #333; }
  317. .content { height: calc(100vh - 88rpx); }
  318. .card { margin: 16rpx 24rpx; background: #fff; border-radius: 20rpx; overflow: hidden; box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06); }
  319. .card-accent { height: 6rpx; background: linear-gradient(90deg, #FF8C42, #F97316); }
  320. .card-body { padding: 24rpx; }
  321. .card-title { font-size: 30rpx; font-weight: bold; color: #333; margin-bottom: 20rpx; display: block; }
  322. .form-group { margin-bottom: 20rpx; }
  323. .form-label { font-size: 26rpx; color: #333; font-weight: 500; margin-bottom: 12rpx; display: block; }
  324. .tag-row { display: flex; flex-wrap: wrap; gap: 12rpx; }
  325. .tag-btn { padding: 10rpx 24rpx; border-radius: 30rpx; border: 2rpx solid #e0e0e0; background: #fafafa; }
  326. .tag-active { border-color: #F97316; background: #FFF3E0; }
  327. .tag-btn text { font-size: 24rpx; color: #666; }
  328. .tag-active text { font-size: 24rpx; color: #F97316; font-weight: bold; }
  329. .form-textarea { width: 100%; height: 120rpx; border: 1rpx solid #e0e0e0; border-radius: 12rpx; padding: 16rpx; font-size: 26rpx; background: #FAFAFA; box-sizing: border-box; }
  330. .form-input { width: 100%; border: 1rpx solid #e0e0e0; border-radius: 12rpx; padding: 16rpx; font-size: 26rpx; background: #FAFAFA; box-sizing: border-box; }
  331. .section-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20rpx; }
  332. .section-header .card-title { margin-bottom: 0; }
  333. .photo-count { font-size: 24rpx; color: #999; }
  334. .photo-grid { display: flex; flex-wrap: wrap; gap: 16rpx; }
  335. .photo-thumb { position: relative; width: 160rpx; height: 160rpx; }
  336. .photo-img { width: 100%; height: 100%; border-radius: 16rpx; }
  337. .photo-delete { position: absolute; top: 0; right: 0; width: 36rpx; height: 36rpx; background: #EF4444; border-radius: 50%; display: flex; align-items: center; justify-content: center; box-shadow: 0 2rpx 8rpx rgba(239,68,68,0.3); }
  338. .photo-delete-icon { color: #fff; font-size: 24rpx; font-weight: bold; }
  339. .photo-add-btn { width: 160rpx; height: 160rpx; border-radius: 16rpx; background: linear-gradient(135deg, #FF8C42, #F97316); display: flex; align-items: center; justify-content: center; box-shadow: 0 4rpx 16rpx rgba(249,115,22,0.25); }
  340. .camera-icon { font-size: 52rpx; }
  341. .voice-recorder { display: flex; flex-direction: column; align-items: center; padding: 24rpx 0 16rpx; }
  342. .voice-active { display: flex; flex-direction: column; align-items: center; margin-bottom: 20rpx; }
  343. .waveform { display: flex; align-items: center; justify-content: center; gap: 8rpx; height: 48rpx; }
  344. .wave-bar { width: 6rpx; height: 8rpx; background: #EF4444; border-radius: 3rpx; }
  345. .wave-1 { animation: waveform-anim 0.8s ease-in-out infinite 0s; }
  346. .wave-2 { animation: waveform-anim 0.8s ease-in-out infinite 0.1s; }
  347. .wave-3 { animation: waveform-anim 0.8s ease-in-out infinite 0.2s; }
  348. .wave-4 { animation: waveform-anim 0.8s ease-in-out infinite 0.3s; }
  349. .wave-5 { animation: waveform-anim 0.8s ease-in-out infinite 0.4s; }
  350. .recording-time { font-size: 28rpx; color: #EF4444; font-weight: bold; margin-top: 8rpx; }
  351. .voice-done { display: flex; align-items: center; gap: 12rpx; margin-bottom: 20rpx; padding: 12rpx 24rpx; background: #FFF7ED; border-radius: 30rpx; }
  352. .play-icon { font-size: 24rpx; color: #F97316; }
  353. .done-text { font-size: 26rpx; color: #666; }
  354. .record-btn-wrap { position: relative; width: 80rpx; height: 80rpx; }
  355. .pulse-ring { position: absolute; top: 0; left: 0; width: 80rpx; height: 80rpx; border-radius: 50%; border: 4rpx solid #EF4444; animation: pulse-expand 1.5s ease-out infinite; }
  356. .record-btn { position: relative; width: 80rpx; height: 80rpx; border-radius: 50%; background: linear-gradient(135deg, #FF8C42, #F97316); display: flex; align-items: center; justify-content: center; box-shadow: 0 4rpx 16rpx rgba(249,115,22,0.3); z-index: 1; }
  357. .record-btn-active { background: linear-gradient(135deg, #EF4444, #DC2626); box-shadow: 0 4rpx 16rpx rgba(239,68,68,0.4); animation: btn-pulse 1s ease-in-out infinite; }
  358. .record-btn-dot { width: 24rpx; height: 24rpx; border-radius: 50%; background: #fff; }
  359. .record-btn-active .record-btn-dot { width: 20rpx; height: 20rpx; border-radius: 4rpx; background: #fff; }
  360. .voice-hint { font-size: 22rpx; color: #999; text-align: center; }
  361. @keyframes waveform-anim { 0%, 100% { height: 8rpx; } 50% { height: 40rpx; } }
  362. @keyframes pulse-expand { 0% { transform: scale(1); opacity: 0.6; } 100% { transform: scale(1.8); opacity: 0; } }
  363. @keyframes btn-pulse { 0%, 100% { box-shadow: 0 4rpx 16rpx rgba(239,68,68,0.4); } 50% { box-shadow: 0 4rpx 24rpx rgba(239,68,68,0.6); } }
  364. .submit-wrap { padding: 24rpx 24rpx 40rpx; }
  365. .submit-btn { background: linear-gradient(135deg, #FF8C42, #F97316); color: #fff; text-align: center; padding: 24rpx 0; border-radius: 16rpx; font-size: 30rpx; font-weight: bold; }
  366. .submitting { opacity: 0.6; }
  367. .history-list { display: flex; flex-direction: column; gap: 16rpx; }
  368. .history-item { display: flex; justify-content: space-between; align-items: center; padding: 12rpx 0; border-bottom: 1rpx solid #f5f5f5; }
  369. .history-left { flex: 1; min-width: 0; }
  370. .history-type { font-size: 26rpx; font-weight: 500; color: #333; display: block; }
  371. .history-content { font-size: 22rpx; color: #888; display: block; margin-top: 4rpx; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  372. .history-right { text-align: right; margin-left: 16rpx; flex-shrink: 0; }
  373. .history-date { font-size: 22rpx; color: #999; }
  374. </style>