growth-emotion-checkin.vue 18 KB

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