emotion-checkin.vue 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262
  1. <template>
  2. <view class="checkin-container">
  3. <!-- 顶部:日期 + 连续打卡 -->
  4. <view class="checkin-header">
  5. <text class="checkin-date">{{ todayDisplay }}</text>
  6. <view class="streak-badge" v-if="stats && stats.currentStreak > 0">
  7. <text class="streak-icon">&#x1F525;</text>
  8. <text class="streak-text">连续{{ stats.currentStreak }}天</text>
  9. </view>
  10. </view>
  11. <!-- ===== Phase 1.1: 2D 效价-激活度网格 ===== -->
  12. <view class="section">
  13. <text class="section-title">在方格中点击你的情绪位置</text>
  14. <view class="affect-grid" @tap="onAffectGridTap">
  15. <!-- 象限标签 -->
  16. <text class="grid-label label-nw">烦躁/紧张</text>
  17. <text class="grid-label label-ne">兴奋/愉快</text>
  18. <text class="grid-label label-sw">低落/疲惫</text>
  19. <text class="grid-label label-se">平静/放松</text>
  20. <!-- 坐标轴 -->
  21. <text class="axis-label axis-bottom">消极 ← 情绪效价 → 积极</text>
  22. <text class="axis-label axis-left">激<br/>活<br/>度</text>
  23. <!-- 选中的圆点 -->
  24. <view class="affect-dot" :style="{ left: affectDotX + 'rpx', top: affectDotY + 'rpx' }">
  25. <view class="affect-dot-inner"></view>
  26. </view>
  27. </view>
  28. <view class="affect-values">
  29. <text class="affect-val">效价: {{ affectValence }}/10</text>
  30. <text class="affect-val">激活度: {{ affectArousal }}/10</text>
  31. </view>
  32. </view>
  33. <!-- ===== Plutchik 情绪轮 ===== -->
  34. <view class="section">
  35. <text class="section-title">选择你的核心情绪</text>
  36. <view class="emotion-wheel">
  37. <view
  38. v-for="em in plutchikEmotions"
  39. :key="em.type"
  40. :class="['emotion-btn', selectedEmotionType === em.type ? 'emotion-active' : '']"
  41. :style="{ borderColor: selectedEmotionType === em.type ? em.color : '#eee' }"
  42. @click="selectedEmotionType = em.type">
  43. <text class="emotion-icon">{{ em.icon }}</text>
  44. <text class="emotion-label">{{ em.label }}</text>
  45. </view>
  46. </view>
  47. </view>
  48. <!-- ===== 强度滑条 ===== -->
  49. <view class="section">
  50. <text class="section-title">情绪强度</text>
  51. <view class="slider-row">
  52. <text class="slider-label">弱</text>
  53. <slider class="custom-slider" min="1" max="10" :value="intensityLevel" @change="onIntensityChange" activeColor="#FF6B9D" block-size="20" />
  54. <text class="slider-value">{{ intensityLevel }}</text>
  55. </view>
  56. <view class="intensity-hint">{{ intensityHint }}</view>
  57. </view>
  58. <!-- ===== 精力水平 ===== -->
  59. <view class="section">
  60. <text class="section-title">精力水平</text>
  61. <view class="slider-row">
  62. <text class="slider-label">低</text>
  63. <slider class="custom-slider" min="1" max="10" :value="energyLevel" @change="onEnergyChange" activeColor="#10B981" block-size="20" />
  64. <text class="slider-value">{{ energyLevel }}</text>
  65. </view>
  66. </view>
  67. <!-- ===== 压力水平 ===== -->
  68. <view class="section">
  69. <text class="section-title">压力水平</text>
  70. <view class="slider-row">
  71. <text class="slider-label">无压力</text>
  72. <slider class="custom-slider" min="1" max="10" :value="stressLevel" @change="onStressChange" activeColor="#F59E0B" block-size="20" />
  73. <text class="slider-value">{{ stressLevel }}</text>
  74. </view>
  75. </view>
  76. <!-- ===== 情绪标签(精细标签) ===== -->
  77. <view class="section">
  78. <text class="section-title">情绪标签(可多选)</text>
  79. <view class="tag-grid">
  80. <view
  81. v-for="tag in emotionTags"
  82. :key="tag"
  83. :class="['tag-btn', selectedTags.indexOf(tag) !== -1 ? 'tag-active' : '']"
  84. @click="toggleTag(tag)">
  85. <text>{{ tag }}</text>
  86. </view>
  87. </view>
  88. <view class="custom-tag-row" v-if="showCustomTagInput">
  89. <input class="custom-tag-input" v-model="customTag" placeholder="输入自定义情绪" />
  90. <text class="custom-tag-add" @click="addCustomTag">添加</text>
  91. </view>
  92. <text class="custom-tag-trigger" v-if="!showCustomTagInput" @click="showCustomTagInput = true">+ 自定义标签</text>
  93. </view>
  94. <!-- 拍照/录像 -->
  95. <view class="section">
  96. <text class="section-title">拍一张照片或一段视频记录心情</text>
  97. <view class="capture-mode-bar">
  98. <text
  99. :class="['mode-btn', captureMode === 'photo' ? 'mode-active' : '']"
  100. @click="switchCaptureMode('photo')">📷 拍照</text>
  101. <text
  102. :class="['mode-btn', captureMode === 'video' ? 'mode-active' : '']"
  103. @click="switchCaptureMode('video')">🎬 录像</text>
  104. </view>
  105. <view class="photo-area" @click="takeMedia">
  106. <!-- 照片预览 -->
  107. <image v-if="captureMode === 'photo' && photoPath" :src="photoPath" mode="aspectFill" class="photo-preview" />
  108. <!-- 视频预览 -->
  109. <video v-if="captureMode === 'video' && videoPath" :src="videoPath" class="video-preview" objectFit="cover" autoplay muted loop />
  110. <!-- 占位 -->
  111. <view v-else-if="!photoPath && !videoPath" class="photo-placeholder">
  112. <text class="photo-icon">{{ captureMode === 'photo' ? '\u{1F4F7}' : '\u{1F3AC}' }}</text>
  113. <text class="photo-hint">{{ captureMode === 'photo' ? '点击拍照' : '点击录像' }}</text>
  114. </view>
  115. </view>
  116. <text class="photo-note">{{ captureMode === 'photo' ? '照片' : '视频' }}仅对自己可见</text>
  117. </view>
  118. <!-- AI 情绪识别 -->
  119. <view class="section" v-if="photoPath || videoPath">
  120. <text class="section-title">\uD83E\uDD16 AI 情绪识别</text>
  121. <view class="ai-recognize-btn" v-if="!aiResult && !aiAnalyzing" @click="analyzeEmotion">
  122. <text class="ai-btn-icon">\uD83D\uDD0D</text>
  123. <text class="ai-btn-text">{{ captureMode === 'video' ? '识别视频中的情绪' : '识别照片中的情绪' }}</text>
  124. </view>
  125. <view class="ai-analyzing" v-if="aiAnalyzing">
  126. <text class="ai-loading-text">AI 分析中...</text>
  127. </view>
  128. <view class="ai-result" v-if="aiResult">
  129. <text class="ai-result-title">识别结果</text>
  130. <view class="ai-emotion-list">
  131. <view class="ai-emotion-item" v-for="em in aiResult" :key="em.label">
  132. <text class="ai-emotion-label">{{ em.label }}</text>
  133. <view class="ai-emotion-bar-track">
  134. <view class="ai-emotion-bar-fill" :style="{ width: em.confidence + '%', background: em.color }"></view>
  135. </view>
  136. <text class="ai-emotion-value">{{ em.confidence }}%</text>
  137. </view>
  138. </view>
  139. <view class="ai-apply-tags" @click="applyAiTags">
  140. <text class="ai-apply-text">应用为情绪标签</text>
  141. </view>
  142. </view>
  143. </view>
  144. <!-- 文字记录 -->
  145. <view class="section">
  146. <text class="section-title">记录一下(选填)</text>
  147. <textarea
  148. class="note-input"
  149. v-model="note"
  150. placeholder="今天发生了什么?"
  151. maxlength="500" />
  152. </view>
  153. <!-- Phase 1.2: 情绪风险告警 -->
  154. <PsychCrisisBanner v-if="currentChildId" :childId="currentChildId" source="checkin" />
  155. <!-- 提交按钮 -->
  156. <view class="submit-area">
  157. <button
  158. class="submit-btn"
  159. :class="{ 'submit-disabled': !canSubmit }"
  160. :disabled="!canSubmit"
  161. @click="submitCheckin">
  162. <text class="submit-icon">&#x2764;</text>
  163. <text>记录心情</text>
  164. </button>
  165. </view>
  166. <!-- 提交中 loading -->
  167. <view class="loading-mask" v-if="submitting">
  168. <view class="loading-box">
  169. <text class="loading-text">记录中...</text>
  170. </view>
  171. </view>
  172. </view>
  173. </template>
  174. <script>
  175. import config from '../../config'
  176. import PsychCrisisBanner from '../../components/PsychCrisisBanner.vue'
  177. export default {
  178. components: { PsychCrisisBanner },
  179. data() {
  180. return {
  181. currentChildId: null,
  182. // --- 2D 效价-激活度网格 ---
  183. affectValence: 5, // 1-10
  184. affectArousal: 5, // 1-10
  185. gridWidth: 360, // rpx, 会在 onReady 中动态计算
  186. gridHeight: 360,
  187. // --- Plutchik 情绪轮 ---
  188. selectedEmotionType: '',
  189. // --- 滑条 ---
  190. intensityLevel: 5,
  191. energyLevel: 6,
  192. stressLevel: 4,
  193. // --- 旧字段保留兼容 ---
  194. selectedWeather: 'sunny', // 自动从效价映射
  195. selectedTags: [],
  196. customTag: '',
  197. showCustomTagInput: false,
  198. captureMode: 'photo',
  199. photoPath: '',
  200. videoPath: '',
  201. note: '',
  202. submitting: false,
  203. aiAnalyzing: false,
  204. aiResult: null,
  205. aiEmotionColors: {
  206. '开心': '#10B981',
  207. '平静': '#3B82F6',
  208. '惊喜': '#8B5CF6',
  209. '难过': '#6B9BD2',
  210. '生气': '#EF4444',
  211. '紧张': '#F59E0B',
  212. '疲惫': '#9CA3AF',
  213. '害怕': '#7C3AED'
  214. },
  215. stats: null,
  216. // Plutchik 8 基本情绪
  217. plutchikEmotions: [
  218. { type: 'joy', icon: '\uD83D\uDE04', label: '开心', color: '#FFD93D' },
  219. { type: 'trust', icon: '\uD83E\uDD1D', label: '信任', color: '#6BCB77' },
  220. { type: 'fear', icon: '\uD83D\uDE31', label: '恐惧', color: '#9B59B6' },
  221. { type: 'surprise', icon: '\uD83D\uDE32', label: '惊讶', color: '#FF9FF3' },
  222. { type: 'sadness', icon: '\uD83D\uDE22', label: '悲伤', color: '#6B9BD2' },
  223. { type: 'disgust', icon: '\uD83E\uDD2E', label: '厌恶', color: '#A0A0A0' },
  224. { type: 'anger', icon: '\uD83D\uDE21', label: '愤怒', color: '#FF6B6B' },
  225. { type: 'anticipation',icon: '\uD83E\uDD29', label: '期待', color: '#FF8C42' }
  226. ],
  227. emotionTags: ['开心', '期待', '平静', '惊喜', '难过', '生气', '紧张', '无聊', '疲惫', '害怕'],
  228. // 强度提示文案
  229. intensityHints: {
  230. 1: '几乎感觉不到',
  231. 2: '很微弱',
  232. 3: '有一点',
  233. 4: '稍微明显',
  234. 5: '中等强度',
  235. 6: '比较明显',
  236. 7: '相当强烈',
  237. 8: '很强烈',
  238. 9: '非常强烈',
  239. 10: '前所未有的强烈'
  240. }
  241. }
  242. },
  243. computed: {
  244. todayDisplay() {
  245. const now = new Date()
  246. const y = now.getFullYear()
  247. const m = now.getMonth() + 1
  248. const d = now.getDate()
  249. const weekdays = ['日', '一', '二', '三', '四', '五', '六']
  250. return y + '年' + m + '月' + d + '日 周' + weekdays[now.getDay()]
  251. },
  252. canSubmit() {
  253. return this.affectValence > 0 && this.selectedEmotionType
  254. },
  255. // 圆点位置(rpx)
  256. affectDotX() {
  257. // valence 1-10 → 0 to gridWidth
  258. // 留出边距: 从 20 到 gridWidth-20
  259. const margin = 20
  260. const usable = this.gridWidth - 2 * margin
  261. return margin + ((this.affectValence - 1) / 9) * usable
  262. },
  263. affectDotY() {
  264. // arousal 1-10 → gridHeight to 0 (Y轴反向)
  265. const margin = 20
  266. const usable = this.gridHeight - 2 * margin
  267. return margin + (1 - (this.affectArousal - 1) / 9) * usable
  268. },
  269. intensityHint() {
  270. return this.intensityHints[this.intensityLevel] || ''
  271. }
  272. },
  273. onLoad() {
  274. const app = getApp()
  275. this.currentChildId = app.globalData.currentChildId || null
  276. this.loadStats()
  277. },
  278. onReady() {
  279. // 估算 grid 尺寸(基于屏幕宽度)
  280. var sysInfo = uni.getSystemInfoSync()
  281. // 页面左右margin=32rpx, section padding=28rpx
  282. // gridWidth ≈ screenWidth - (32+28)*2 = screenWidth - 120
  283. var screenWidth = sysInfo.windowWidth
  284. var gridRpx = screenWidth - 120 // 估算 rpx
  285. if (gridRpx < 300) gridRpx = 300
  286. this.gridWidth = gridRpx
  287. this.gridHeight = gridRpx
  288. },
  289. methods: {
  290. loadStats() {
  291. const app = getApp()
  292. const childId = app.globalData.currentChildId
  293. if (!childId) return
  294. uni.request({
  295. url: config.API_BASE_URL + '/api/mind/checkin/stats',
  296. method: 'POST',
  297. data: { childId },
  298. success: (res) => {
  299. if (res.data && res.data.code === 200 && res.data.data) {
  300. this.stats = res.data.data
  301. }
  302. }
  303. })
  304. },
  305. // ===== 2D 效价-激活度网格 =====
  306. onAffectGridTap(e) {
  307. // 获取点击相对位置
  308. var rect = e.target
  309. // 使用 touches 或 detail
  310. var x = e.detail && e.detail.x ? e.detail.x : 0
  311. var y = e.detail && e.detail.y ? e.detail.y : 0
  312. // fallback: 从 pageX/pageY 计算(推荐)
  313. if (e.detail && e.detail.x !== undefined) {
  314. // uni-app 在 @tap 事件中 detail 有 x/y 表示相对于目标元素的偏移
  315. x = e.detail.x
  316. y = e.detail.y
  317. }
  318. // px → rpx 转换
  319. var sysInfo = uni.getSystemInfoSync()
  320. var scale = 750 / sysInfo.windowWidth
  321. // 直接使用事件中的偏移(px),然后转 rpx
  322. // 注意: 如果 e.detail 没有 x/y,尝试从 e.touches 获取
  323. if ((x === 0 && y === 0) || !e.detail.x) {
  324. var touch = e.touches ? e.touches[0] : null
  325. if (touch) {
  326. // 需要根据 grid 的位置计算相对偏移
  327. x = touch.x || 0
  328. y = touch.y || 0
  329. }
  330. }
  331. // 直接用 rpx 计算,简化版:click 的位置相对 grid 容器
  332. // uni-app 的 @tap 的 detail = {x, y} 是相对于被点击元素的 px 值
  333. var pxX = e.detail.x || 0
  334. var pxY = e.detail.y || 0
  335. var rpxX = pxX * scale
  336. var rpxY = pxY * scale
  337. // 约束在有效范围内
  338. var margin = 20
  339. var maxRpx = this.gridWidth - margin
  340. if (rpxX < margin) rpxX = margin
  341. if (rpxX > maxRpx) rpxX = maxRpx
  342. if (rpxY < margin) rpxY = margin
  343. if (rpxY > this.gridHeight - margin) rpxY = this.gridHeight - margin
  344. // 映射到 1-10
  345. var usable = this.gridWidth - 2 * margin
  346. var val = Math.round(1 + ((rpxX - margin) / usable) * 9)
  347. var aro = Math.round(10 - ((rpxY - margin) / usable) * 9)
  348. this.affectValence = Math.max(1, Math.min(10, val))
  349. this.affectArousal = Math.max(1, Math.min(10, aro))
  350. // 自动映射 moodWeather 用于向后兼容
  351. this.updateWeatherFromAffect()
  352. },
  353. updateWeatherFromAffect() {
  354. // 效价高 + 激活度高 = rainbow
  355. if (this.affectValence >= 7 && this.affectArousal >= 7) {
  356. this.selectedWeather = 'rainbow'
  357. } else if (this.affectValence >= 6) {
  358. // 效价高 = sunny
  359. this.selectedWeather = 'sunny'
  360. } else if (this.affectValence <= 4 && this.affectArousal >= 7) {
  361. // 效价低 + 激活度高 = stormy
  362. this.selectedWeather = 'stormy'
  363. } else if (this.affectValence <= 4 && this.affectArousal <= 4) {
  364. // 效价低 + 激活度低 = rainy
  365. this.selectedWeather = 'rainy'
  366. } else {
  367. this.selectedWeather = 'cloudy'
  368. }
  369. },
  370. // ===== 滑条变化 =====
  371. onIntensityChange(e) {
  372. this.intensityLevel = e.detail.value
  373. },
  374. onEnergyChange(e) {
  375. this.energyLevel = e.detail.value
  376. },
  377. onStressChange(e) {
  378. this.stressLevel = e.detail.value
  379. },
  380. // ===== 情绪标签 =====
  381. toggleTag(tag) {
  382. const idx = this.selectedTags.indexOf(tag)
  383. if (idx !== -1) {
  384. this.selectedTags.splice(idx, 1)
  385. } else {
  386. this.selectedTags.push(tag)
  387. }
  388. },
  389. addCustomTag() {
  390. const tag = this.customTag.trim()
  391. if (tag && this.emotionTags.indexOf(tag) === -1 && this.selectedTags.indexOf(tag) === -1) {
  392. this.selectedTags.push(tag)
  393. }
  394. this.customTag = ''
  395. this.showCustomTagInput = false
  396. },
  397. // ===== 拍照/录像 & AI =====
  398. switchCaptureMode(mode) {
  399. this.captureMode = mode
  400. this.photoPath = ''
  401. this.videoPath = ''
  402. this.aiResult = null
  403. },
  404. takeMedia() {
  405. var self = this
  406. if (this.captureMode === 'photo') {
  407. uni.chooseImage({
  408. count: 1,
  409. sourceType: ['camera', 'album'],
  410. success: function(res) {
  411. self.photoPath = res.tempFilePaths[0]
  412. self.videoPath = ''
  413. self.aiResult = null
  414. }
  415. })
  416. } else {
  417. uni.chooseVideo({
  418. sourceType: ['camera', 'album'],
  419. maxDuration: 15,
  420. success: function(res) {
  421. self.videoPath = res.tempFilePath
  422. self.photoPath = ''
  423. self.aiResult = null
  424. }
  425. })
  426. }
  427. },
  428. // ===== 增强型 AI 模拟识别 =====
  429. // 基于效价/激活度/时段/强度等多维参数,生成更自然的模拟结果
  430. analyzeEmotion() {
  431. this.aiAnalyzing = true
  432. this.aiResult = null
  433. var self = this
  434. // 生成伪随机种子(不同照片/视频产生不同结果,模拟真实识别)
  435. var seed = this.genSeed()
  436. // 根据多维参数生成情绪结果
  437. var results = this.genEmotionResults(seed)
  438. // 处理时间随机变化(200-1600ms,让每次等待不一样)
  439. var delay = 600 + (seed % 1000)
  440. setTimeout(function() {
  441. self.aiResult = results
  442. self.aiAnalyzing = false
  443. }, delay)
  444. },
  445. // 基于文件路径 + 时间生成伪随机种子
  446. genSeed() {
  447. var path = this.photoPath || this.videoPath || ''
  448. var hash = 0
  449. for (var i = 0; i < path.length; i++) {
  450. hash = ((hash << 5) - hash) + path.charCodeAt(i)
  451. hash = hash & hash
  452. }
  453. hash = Math.abs(hash)
  454. return (hash + Date.now()) % 7919 // 质数取模
  455. },
  456. // 多维参数 → 情绪结果
  457. genEmotionResults(seed) {
  458. var valence = this.affectValence
  459. var arousal = this.affectArousal
  460. // 1. 从效价-激活度象限推导主要情绪分布
  461. var emotionPool = []
  462. if (valence >= 6 && arousal >= 6) {
  463. // Q1: 高激活 + 积极 → 开心/兴奋/惊喜
  464. emotionPool = [
  465. { label: '开心', base: 38, color: '#10B981' },
  466. { label: '兴奋', base: 22, color: '#8B5CF6' },
  467. { label: '惊喜', base: 18, color: '#FF9FF3' },
  468. { label: '满足', base: 12, color: '#3B82F6' },
  469. { label: '期待', base: 10, color: '#F59E0B' }
  470. ]
  471. } else if (valence >= 6 && arousal < 6) {
  472. // Q2: 低激活 + 积极 → 平静/满足/放松
  473. emotionPool = [
  474. { label: '平静', base: 32, color: '#3B82F6' },
  475. { label: '满足', base: 25, color: '#10B981' },
  476. { label: '放松', base: 20, color: '#6B9BD2' },
  477. { label: '开心', base: 15, color: '#10B981' },
  478. { label: '无聊', base: 8, color: '#9CA3AF' }
  479. ]
  480. } else if (valence < 6 && arousal >= 6) {
  481. // Q3: 高激活 + 消极 → 生气/紧张/恐惧
  482. emotionPool = [
  483. { label: '紧张', base: 30, color: '#F59E0B' },
  484. { label: '生气', base: 25, color: '#EF4444' },
  485. { label: '烦躁', base: 20, color: '#FF6B6B' },
  486. { label: '恐惧', base: 15, color: '#7C3AED' },
  487. { label: '厌恶', base: 10, color: '#A0A0A0' }
  488. ]
  489. } else {
  490. // Q4: 低激活 + 消极 → 难过/疲惫/低落
  491. emotionPool = [
  492. { label: '难过', base: 32, color: '#6B9BD2' },
  493. { label: '疲惫', base: 28, color: '#9CA3AF' },
  494. { label: '低落', base: 20, color: '#6B7280' },
  495. { label: '平静', base: 12, color: '#3B82F6' },
  496. { label: '无聊', base: 8, color: '#9CA3AF' }
  497. ]
  498. }
  499. // 2. 根据强度/压力/能量微调
  500. // 高强度 → 强化 top1
  501. if (this.intensityLevel >= 7) {
  502. emotionPool[0].base += 10
  503. } else if (this.intensityLevel <= 3) {
  504. // 低强度 → 向平静偏移
  505. for (var si = 0; si < emotionPool.length; si++) {
  506. if (emotionPool[si].label === '平静' || emotionPool[si].label === '无聊') {
  507. emotionPool[si].base += 8
  508. }
  509. }
  510. }
  511. // 高压力 → 添加紧张/烦躁倾向
  512. if (this.stressLevel >= 7) {
  513. var tensionIdx = -1
  514. var angerIdx = -1
  515. for (var ti = 0; ti < emotionPool.length; ti++) {
  516. if (emotionPool[ti].label === '紧张') tensionIdx = ti
  517. if (emotionPool[ti].label === '烦躁') angerIdx = ti
  518. }
  519. if (tensionIdx >= 0) emotionPool[tensionIdx].base += 12
  520. if (angerIdx >= 0) emotionPool[angerIdx].base += 8
  521. }
  522. // 3. 根据 Plutchik 已选情绪提升权重(用户选择了说明有倾向)
  523. if (this.selectedEmotionType) {
  524. var typeToLabel = {
  525. joy: '开心', trust: '信任', fear: '恐惧',
  526. surprise: '惊喜', sadness: '难过', disgust: '厌恶',
  527. anger: '生气', anticipation: '期待'
  528. }
  529. var matchedLabel = typeToLabel[this.selectedEmotionType]
  530. for (var mi = 0; mi < emotionPool.length; mi++) {
  531. if (emotionPool[mi].label === matchedLabel) {
  532. emotionPool[mi].base += 15
  533. }
  534. }
  535. }
  536. // 4. 根据时段微调
  537. var hour = new Date().getHours()
  538. if (hour < 6) {
  539. // 深夜:偏向疲惫/低落
  540. this.adjustPoolForTime(emotionPool, '疲惫', 10)
  541. this.adjustPoolForTime(emotionPool, '低落', 8)
  542. } else if (hour < 10) {
  543. // 早晨:偏向积极
  544. this.adjustPoolForTime(emotionPool, '开心', 8)
  545. this.adjustPoolForTime(emotionPool, '期待', 6)
  546. } else if (hour >= 22) {
  547. // 夜晚:偏向平静/疲惫
  548. this.adjustPoolForTime(emotionPool, '平静', 8)
  549. this.adjustPoolForTime(emotionPool, '疲惫', 6)
  550. }
  551. // 5. 引入伪随机噪声(让每次结果有细微差异)
  552. for (var ni = 0; ni < emotionPool.length; ni++) {
  553. var noise = (seed * (ni + 1) * 7) % 11 - 5 // -5 ~ +5
  554. emotionPool[ni].base += noise
  555. if (emotionPool[ni].base < 3) emotionPool[ni].base = 3
  556. }
  557. // 6. 降序排列,取 top 4,归一化到 100%
  558. emotionPool.sort(function(a, b) { return b.base - a.base })
  559. var top4 = emotionPool.slice(0, 4)
  560. var total = 0
  561. for (var sumi = 0; sumi < top4.length; sumi++) {
  562. total += top4[sumi].base
  563. }
  564. for (var normi = 0; normi < top4.length; normi++) {
  565. top4[normi].confidence = Math.round((top4[normi].base / total) * 100)
  566. }
  567. // 补余数确保 =100
  568. var sumConf = 0
  569. for (var ci = 0; ci < top4.length; ci++) {
  570. sumConf += top4[ci].confidence
  571. }
  572. var diff = 100 - sumConf
  573. if (diff !== 0 && top4.length > 0) {
  574. top4[0].confidence += diff
  575. }
  576. return top4
  577. },
  578. adjustPoolForTime(pool, label, delta) {
  579. for (var i = 0; i < pool.length; i++) {
  580. if (pool[i].label === label) {
  581. pool[i].base += delta
  582. return
  583. }
  584. }
  585. },
  586. applyAiTags() {
  587. if (!this.aiResult) return
  588. var detected = []
  589. for (var i = 0; i < this.aiResult.length; i++) {
  590. var em = this.aiResult[i]
  591. if (em.confidence >= 20 && this.emotionTags.indexOf(em.label) !== -1) {
  592. detected.push(em.label)
  593. }
  594. }
  595. var merged = this.selectedTags.slice()
  596. for (var j = 0; j < detected.length; j++) {
  597. if (merged.indexOf(detected[j]) === -1) {
  598. merged.push(detected[j])
  599. }
  600. }
  601. this.selectedTags = merged
  602. uni.showToast({ title: '已应用情绪标签', icon: 'success' })
  603. },
  604. // ===== 提交 =====
  605. submitCheckin() {
  606. if (!this.canSubmit || this.submitting) return
  607. this.submitting = true
  608. const app = getApp()
  609. const childId = app.globalData.currentChildId
  610. const doSubmit = (mediaUrl) => {
  611. uni.request({
  612. url: config.API_BASE_URL + '/api/mind/checkin/create',
  613. method: 'POST',
  614. data: {
  615. childId: childId,
  616. // 旧字段向后兼容
  617. moodWeather: this.selectedWeather,
  618. emotionTags: JSON.stringify(this.selectedTags),
  619. photoUrl: this.captureMode === 'photo' ? (mediaUrl || '') : '',
  620. videoUrl: this.captureMode === 'video' ? (mediaUrl || '') : '',
  621. mediaType: this.captureMode,
  622. note: this.note,
  623. // Phase 1.1 精细情绪字段
  624. moodScore: this.affectValence,
  625. emotionType: this.selectedEmotionType,
  626. stressLevel: this.stressLevel,
  627. arousalLevel: this.affectArousal,
  628. energyLevel: this.energyLevel
  629. },
  630. success: (res) => {
  631. this.submitting = false
  632. if (res.data && res.data.code === 200) {
  633. uni.showToast({
  634. title: '+5 心能量',
  635. icon: 'none',
  636. duration: 2000
  637. })
  638. setTimeout(() => {
  639. uni.navigateTo({ url: '/pages/mind/emotion-trend' })
  640. }, 1500)
  641. } else {
  642. uni.showToast({ title: res.data && res.data.message || '提交失败', icon: 'none' })
  643. }
  644. },
  645. fail: () => {
  646. this.submitting = false
  647. uni.showToast({ title: '网络错误', icon: 'none' })
  648. }
  649. })
  650. }
  651. var mediaPath = this.photoPath || this.videoPath
  652. if (mediaPath) {
  653. uni.uploadFile({
  654. url: config.API_BASE_URL + '/api/upload',
  655. filePath: mediaPath,
  656. name: 'file',
  657. success: (uploadRes) => {
  658. let mediaUrl = ''
  659. try {
  660. const data = JSON.parse(uploadRes.data)
  661. mediaUrl = data.data || data.url || ''
  662. } catch (e) {
  663. mediaUrl = uploadRes.data
  664. }
  665. doSubmit(mediaUrl)
  666. },
  667. fail: () => doSubmit('')
  668. })
  669. } else {
  670. doSubmit('')
  671. }
  672. }
  673. }
  674. }
  675. </script>
  676. <style scoped>
  677. .checkin-container {
  678. min-height: 100vh;
  679. background: linear-gradient(180deg, #FFF0F5 0%, #FFFFFF 100%);
  680. padding-bottom: 40rpx;
  681. }
  682. .checkin-header {
  683. display: flex;
  684. align-items: center;
  685. justify-content: space-between;
  686. padding: 30rpx 32rpx;
  687. }
  688. .checkin-date {
  689. font-size: 32rpx;
  690. font-weight: 600;
  691. color: #333;
  692. }
  693. .streak-badge {
  694. display: flex;
  695. align-items: center;
  696. background: linear-gradient(135deg, #FF6B9D, #FF8CB3);
  697. border-radius: 30rpx;
  698. padding: 8rpx 24rpx;
  699. }
  700. .streak-icon {
  701. font-size: 28rpx;
  702. margin-right: 8rpx;
  703. }
  704. .streak-text {
  705. font-size: 24rpx;
  706. color: #fff;
  707. font-weight: 500;
  708. }
  709. .section {
  710. margin: 20rpx 32rpx;
  711. background: #fff;
  712. border-radius: 20rpx;
  713. padding: 28rpx;
  714. box-shadow: 0 2rpx 12rpx rgba(255, 107, 157, 0.08);
  715. }
  716. .section-title {
  717. font-size: 28rpx;
  718. font-weight: 600;
  719. color: #333;
  720. margin-bottom: 20rpx;
  721. display: block;
  722. }
  723. /* ===== 2D 效价-激活度网格 ===== */
  724. .affect-grid {
  725. position: relative;
  726. width: 100%;
  727. /* height is set dynamically equal to width via aspect-ratio or inline style */
  728. aspect-ratio: 1;
  729. background: linear-gradient(135deg, #FFF5F5 0%, #FFF0FF 25%, #F0F0FF 50%, #F0FFF4 75%, #FFFFF0 100%);
  730. border-radius: 16rpx;
  731. border: 2rpx solid #E8E8E8;
  732. margin-bottom: 16rpx;
  733. /* Ensure the grid has a defined height — fallback */
  734. min-height: 300rpx;
  735. /* Overlap grid lines for visual clarity */
  736. overflow: hidden;
  737. }
  738. /* 网格线:用伪元素或背景渐变 */
  739. .affect-grid::before {
  740. content: '';
  741. position: absolute;
  742. left: 50%;
  743. top: 0;
  744. bottom: 0;
  745. width: 2rpx;
  746. background: rgba(0,0,0,0.06);
  747. transform: translateX(-50%);
  748. pointer-events: none;
  749. }
  750. .affect-grid::after {
  751. content: '';
  752. position: absolute;
  753. top: 50%;
  754. left: 0;
  755. right: 0;
  756. height: 2rpx;
  757. background: rgba(0,0,0,0.06);
  758. transform: translateY(-50%);
  759. pointer-events: none;
  760. }
  761. .grid-label {
  762. position: absolute;
  763. font-size: 22rpx;
  764. color: rgba(0,0,0,0.25);
  765. pointer-events: none;
  766. }
  767. .label-nw { top: 20rpx; left: 20rpx; }
  768. .label-ne { top: 20rpx; right: 20rpx; }
  769. .label-sw { bottom: 20rpx; left: 20rpx; }
  770. .label-se { bottom: 20rpx; right: 20rpx; }
  771. .axis-label {
  772. position: absolute;
  773. font-size: 20rpx;
  774. color: rgba(0,0,0,0.2);
  775. pointer-events: none;
  776. }
  777. .axis-bottom {
  778. bottom: 4rpx;
  779. left: 50%;
  780. transform: translateX(-50%);
  781. white-space: nowrap;
  782. }
  783. .axis-left {
  784. left: 4rpx;
  785. top: 50%;
  786. transform: translateY(-50%);
  787. font-size: 18rpx;
  788. line-height: 1.4;
  789. text-align: center;
  790. }
  791. .affect-dot {
  792. position: absolute;
  793. width: 44rpx;
  794. height: 44rpx;
  795. margin-left: -22rpx;
  796. margin-top: -22rpx;
  797. pointer-events: none;
  798. z-index: 2;
  799. }
  800. .affect-dot-inner {
  801. width: 100%;
  802. height: 100%;
  803. border-radius: 50%;
  804. background: #FF6B9D;
  805. box-shadow: 0 0 12rpx rgba(255, 107, 157, 0.5);
  806. border: 4rpx solid #fff;
  807. box-sizing: border-box;
  808. }
  809. .affect-values {
  810. display: flex;
  811. justify-content: center;
  812. gap: 40rpx;
  813. }
  814. .affect-val {
  815. font-size: 24rpx;
  816. color: #888;
  817. }
  818. /* ===== Plutchik 情绪轮 ===== */
  819. .emotion-wheel {
  820. display: flex;
  821. flex-wrap: wrap;
  822. gap: 16rpx;
  823. justify-content: center;
  824. }
  825. .emotion-btn {
  826. display: flex;
  827. flex-direction: column;
  828. align-items: center;
  829. width: 130rpx;
  830. padding: 16rpx 8rpx;
  831. border-radius: 20rpx;
  832. border: 3rpx solid #eee;
  833. background: #FAFAFA;
  834. transition: all 0.2s;
  835. }
  836. .emotion-active {
  837. background: #FFF0F5;
  838. transform: scale(1.05);
  839. }
  840. .emotion-icon {
  841. font-size: 48rpx;
  842. margin-bottom: 6rpx;
  843. }
  844. .emotion-label {
  845. font-size: 24rpx;
  846. color: #555;
  847. }
  848. /* ===== 滑条 ===== */
  849. .slider-row {
  850. display: flex;
  851. align-items: center;
  852. gap: 16rpx;
  853. }
  854. .slider-label {
  855. font-size: 24rpx;
  856. color: #999;
  857. white-space: nowrap;
  858. width: 64rpx;
  859. }
  860. .custom-slider {
  861. flex: 1;
  862. }
  863. .slider-value {
  864. font-size: 28rpx;
  865. font-weight: 600;
  866. color: #FF6B9D;
  867. width: 40rpx;
  868. text-align: center;
  869. }
  870. .intensity-hint {
  871. font-size: 22rpx;
  872. color: #aaa;
  873. text-align: center;
  874. margin-top: 4rpx;
  875. }
  876. /* ===== 情绪标签 ===== */
  877. .tag-grid {
  878. display: flex;
  879. flex-wrap: wrap;
  880. gap: 16rpx;
  881. }
  882. .tag-btn {
  883. padding: 12rpx 28rpx;
  884. border-radius: 30rpx;
  885. background: #F5F5F5;
  886. font-size: 26rpx;
  887. color: #666;
  888. border: 2rpx solid transparent;
  889. }
  890. .tag-active {
  891. background: #FFF0F5;
  892. color: #FF6B9D;
  893. border-color: #FF6B9D;
  894. }
  895. .custom-tag-row {
  896. display: flex;
  897. align-items: center;
  898. margin-top: 16rpx;
  899. gap: 16rpx;
  900. }
  901. .custom-tag-input {
  902. flex: 1;
  903. height: 60rpx;
  904. border: 2rpx solid #E0E0E0;
  905. border-radius: 12rpx;
  906. padding: 0 16rpx;
  907. font-size: 26rpx;
  908. }
  909. .custom-tag-add {
  910. padding: 12rpx 24rpx;
  911. background: #FF6B9D;
  912. color: #fff;
  913. border-radius: 12rpx;
  914. font-size: 26rpx;
  915. }
  916. .custom-tag-trigger {
  917. display: inline-block;
  918. margin-top: 16rpx;
  919. color: #FF6B9D;
  920. font-size: 26rpx;
  921. }
  922. /* ===== 拍照 ===== */
  923. .photo-area {
  924. width: 200rpx;
  925. height: 200rpx;
  926. border-radius: 16rpx;
  927. overflow: hidden;
  928. background: #F5F5F5;
  929. display: flex;
  930. align-items: center;
  931. justify-content: center;
  932. }
  933. .photo-preview {
  934. width: 100%;
  935. height: 100%;
  936. }
  937. .photo-placeholder {
  938. display: flex;
  939. flex-direction: column;
  940. align-items: center;
  941. }
  942. .photo-icon {
  943. font-size: 60rpx;
  944. }
  945. .photo-hint {
  946. font-size: 24rpx;
  947. color: #999;
  948. margin-top: 8rpx;
  949. }
  950. .photo-note {
  951. display: block;
  952. font-size: 22rpx;
  953. color: #999;
  954. margin-top: 12rpx;
  955. }
  956. /* ===== 拍照/录像模式切换 ===== */
  957. .capture-mode-bar {
  958. display: flex;
  959. gap: 20rpx;
  960. margin-bottom: 20rpx;
  961. }
  962. .mode-btn {
  963. padding: 10rpx 28rpx;
  964. border-radius: 30rpx;
  965. font-size: 26rpx;
  966. color: #999;
  967. background: #f5f5f5;
  968. border: 2rpx solid #eee;
  969. }
  970. .mode-btn.mode-active {
  971. color: #FF6B9D;
  972. background: #FFF0F5;
  973. border-color: #FF6B9D;
  974. font-weight: 500;
  975. }
  976. .video-preview {
  977. width: 100%;
  978. height: 100%;
  979. border-radius: 16rpx;
  980. }
  981. /* ===== AI 情绪识别 ===== */
  982. .ai-recognize-btn {
  983. display: flex;
  984. align-items: center;
  985. justify-content: center;
  986. padding: 20rpx;
  987. background: linear-gradient(135deg, #8B5CF6, #A78BFA);
  988. border-radius: 16rpx;
  989. cursor: pointer;
  990. }
  991. .ai-btn-icon {
  992. font-size: 32rpx;
  993. margin-right: 10rpx;
  994. }
  995. .ai-btn-text {
  996. font-size: 26rpx;
  997. color: #fff;
  998. font-weight: 500;
  999. }
  1000. .ai-recognize-btn:active {
  1001. opacity: 0.8;
  1002. }
  1003. .ai-analyzing {
  1004. display: flex;
  1005. align-items: center;
  1006. justify-content: center;
  1007. padding: 30rpx 0;
  1008. }
  1009. .ai-loading-text {
  1010. font-size: 26rpx;
  1011. color: #8B5CF6;
  1012. }
  1013. .ai-result {
  1014. margin-top: 16rpx;
  1015. }
  1016. .ai-result-title {
  1017. font-size: 24rpx;
  1018. color: #666;
  1019. margin-bottom: 12rpx;
  1020. display: block;
  1021. }
  1022. .ai-emotion-list {
  1023. display: flex;
  1024. flex-direction: column;
  1025. gap: 12rpx;
  1026. }
  1027. .ai-emotion-item {
  1028. display: flex;
  1029. align-items: center;
  1030. gap: 12rpx;
  1031. }
  1032. .ai-emotion-label {
  1033. font-size: 24rpx;
  1034. color: #333;
  1035. width: 64rpx;
  1036. flex-shrink: 0;
  1037. }
  1038. .ai-emotion-bar-track {
  1039. flex: 1;
  1040. height: 16rpx;
  1041. background: #F0F0F0;
  1042. border-radius: 8rpx;
  1043. overflow: hidden;
  1044. }
  1045. .ai-emotion-bar-fill {
  1046. height: 100%;
  1047. border-radius: 8rpx;
  1048. transition: width 0.5s;
  1049. }
  1050. .ai-emotion-value {
  1051. font-size: 22rpx;
  1052. color: #999;
  1053. width: 56rpx;
  1054. text-align: right;
  1055. }
  1056. .ai-apply-tags {
  1057. margin-top: 20rpx;
  1058. padding: 16rpx;
  1059. background: #F5F0FF;
  1060. border-radius: 12rpx;
  1061. text-align: center;
  1062. cursor: pointer;
  1063. }
  1064. .ai-apply-text {
  1065. font-size: 26rpx;
  1066. color: #8B5CF6;
  1067. font-weight: 500;
  1068. }
  1069. .ai-apply-tags:active {
  1070. opacity: 0.7;
  1071. }
  1072. .note-input {
  1073. width: 100%;
  1074. min-height: 140rpx;
  1075. background: #F9F9F9;
  1076. border-radius: 12rpx;
  1077. padding: 20rpx;
  1078. font-size: 26rpx;
  1079. color: #333;
  1080. box-sizing: border-box;
  1081. }
  1082. .submit-area {
  1083. margin: 40rpx 32rpx;
  1084. }
  1085. .submit-btn {
  1086. width: 100%;
  1087. height: 88rpx;
  1088. line-height: 88rpx;
  1089. background: linear-gradient(135deg, #FF6B9D, #FF8CB3);
  1090. color: #fff;
  1091. border-radius: 44rpx;
  1092. font-size: 30rpx;
  1093. font-weight: 600;
  1094. display: flex;
  1095. align-items: center;
  1096. justify-content: center;
  1097. }
  1098. .submit-disabled {
  1099. opacity: 0.5;
  1100. }
  1101. .submit-icon {
  1102. margin-right: 8rpx;
  1103. }
  1104. .loading-mask {
  1105. position: fixed;
  1106. top: 0;
  1107. left: 0;
  1108. right: 0;
  1109. bottom: 0;
  1110. background: rgba(0, 0, 0, 0.3);
  1111. display: flex;
  1112. align-items: center;
  1113. justify-content: center;
  1114. z-index: 999;
  1115. }
  1116. .loading-box {
  1117. background: #fff;
  1118. padding: 40rpx 60rpx;
  1119. border-radius: 20rpx;
  1120. }
  1121. .loading-text {
  1122. font-size: 28rpx;
  1123. color: #666;
  1124. }
  1125. </style>