diet-checkin.vue 16 KB

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