growth-emotion-checkin.vue 15 KB

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