diet-checkin.vue 17 KB

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