diet-checkin.vue 16 KB

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