growth-emotion-checkin.vue 16 KB

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