diet-checkin.vue 15 KB

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