growth-emotion-checkin.vue 17 KB

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