emotion-checkin.vue 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  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" :memberId="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. this.currentChildId = uni.getStorageSync('currentChildId') || null
  275. this.loadStats()
  276. },
  277. onReady() {
  278. // 估算 grid 尺寸(基于屏幕宽度)
  279. var sysInfo = uni.getWindowInfo()
  280. // 页面左右margin=32rpx, section padding=28rpx
  281. // gridWidth ≈ screenWidth - (32+28)*2 = screenWidth - 120
  282. var screenWidth = sysInfo.windowWidth
  283. var gridRpx = screenWidth - 120 // 估算 rpx
  284. if (gridRpx < 300) gridRpx = 300
  285. this.gridWidth = gridRpx
  286. this.gridHeight = gridRpx
  287. },
  288. methods: {
  289. loadStats() {
  290. const memberId = uni.getStorageSync('currentChildId')
  291. if (!memberId) return
  292. uni.request({
  293. url: config.API_BASE_URL + '/api/mind/checkin/stats',
  294. method: 'POST',
  295. data: { memberId },
  296. success: (res) => {
  297. if (res.data && res.data.code === 200 && res.data.data) {
  298. this.stats = res.data.data
  299. }
  300. }
  301. })
  302. },
  303. // ===== 2D 效价-激活度网格 =====
  304. onAffectGridTap(e) {
  305. // 获取点击相对位置
  306. var rect = e.target
  307. // 使用 touches 或 detail
  308. var x = e.detail && e.detail.x ? e.detail.x : 0
  309. var y = e.detail && e.detail.y ? e.detail.y : 0
  310. // fallback: 从 pageX/pageY 计算(推荐)
  311. if (e.detail && e.detail.x !== undefined) {
  312. // uni-app 在 @tap 事件中 detail 有 x/y 表示相对于目标元素的偏移
  313. x = e.detail.x
  314. y = e.detail.y
  315. }
  316. // px → rpx 转换
  317. var sysInfo = uni.getWindowInfo()
  318. var scale = 750 / sysInfo.windowWidth
  319. // 直接使用事件中的偏移(px),然后转 rpx
  320. // 注意: 如果 e.detail 没有 x/y,尝试从 e.touches 获取
  321. if ((x === 0 && y === 0) || !e.detail.x) {
  322. var touch = e.touches ? e.touches[0] : null
  323. if (touch) {
  324. // 需要根据 grid 的位置计算相对偏移
  325. x = touch.x || 0
  326. y = touch.y || 0
  327. }
  328. }
  329. // 直接用 rpx 计算,简化版:click 的位置相对 grid 容器
  330. // uni-app 的 @tap 的 detail = {x, y} 是相对于被点击元素的 px 值
  331. var pxX = e.detail.x || 0
  332. var pxY = e.detail.y || 0
  333. var rpxX = pxX * scale
  334. var rpxY = pxY * scale
  335. // 约束在有效范围内
  336. var margin = 20
  337. var maxRpx = this.gridWidth - margin
  338. if (rpxX < margin) rpxX = margin
  339. if (rpxX > maxRpx) rpxX = maxRpx
  340. if (rpxY < margin) rpxY = margin
  341. if (rpxY > this.gridHeight - margin) rpxY = this.gridHeight - margin
  342. // 映射到 1-10
  343. var usable = this.gridWidth - 2 * margin
  344. var val = Math.round(1 + ((rpxX - margin) / usable) * 9)
  345. var aro = Math.round(10 - ((rpxY - margin) / usable) * 9)
  346. this.affectValence = Math.max(1, Math.min(10, val))
  347. this.affectArousal = Math.max(1, Math.min(10, aro))
  348. // 自动映射 moodWeather 用于向后兼容
  349. this.updateWeatherFromAffect()
  350. },
  351. updateWeatherFromAffect() {
  352. // 效价高 + 激活度高 = rainbow
  353. if (this.affectValence >= 7 && this.affectArousal >= 7) {
  354. this.selectedWeather = 'rainbow'
  355. } else if (this.affectValence >= 6) {
  356. // 效价高 = sunny
  357. this.selectedWeather = 'sunny'
  358. } else if (this.affectValence <= 4 && this.affectArousal >= 7) {
  359. // 效价低 + 激活度高 = stormy
  360. this.selectedWeather = 'stormy'
  361. } else if (this.affectValence <= 4 && this.affectArousal <= 4) {
  362. // 效价低 + 激活度低 = rainy
  363. this.selectedWeather = 'rainy'
  364. } else {
  365. this.selectedWeather = 'cloudy'
  366. }
  367. },
  368. // ===== 滑条变化 =====
  369. onIntensityChange(e) {
  370. this.intensityLevel = e.detail.value
  371. },
  372. onEnergyChange(e) {
  373. this.energyLevel = e.detail.value
  374. },
  375. onStressChange(e) {
  376. this.stressLevel = e.detail.value
  377. },
  378. // ===== 情绪标签 =====
  379. toggleTag(tag) {
  380. const idx = this.selectedTags.indexOf(tag)
  381. if (idx !== -1) {
  382. this.selectedTags.splice(idx, 1)
  383. } else {
  384. this.selectedTags.push(tag)
  385. }
  386. },
  387. addCustomTag() {
  388. const tag = this.customTag.trim()
  389. if (tag && this.emotionTags.indexOf(tag) === -1 && this.selectedTags.indexOf(tag) === -1) {
  390. this.selectedTags.push(tag)
  391. }
  392. this.customTag = ''
  393. this.showCustomTagInput = false
  394. },
  395. // ===== 拍照/录像 & AI =====
  396. switchCaptureMode(mode) {
  397. this.captureMode = mode
  398. this.photoPath = ''
  399. this.videoPath = ''
  400. this.aiResult = null
  401. },
  402. takeMedia() {
  403. var self = this
  404. if (this.captureMode === 'photo') {
  405. uni.chooseImage({
  406. count: 1,
  407. sourceType: ['camera', 'album'],
  408. success: function(res) {
  409. self.photoPath = res.tempFilePaths[0]
  410. self.videoPath = ''
  411. self.aiResult = null
  412. }
  413. })
  414. } else {
  415. uni.chooseVideo({
  416. sourceType: ['camera', 'album'],
  417. maxDuration: 15,
  418. success: function(res) {
  419. self.videoPath = res.tempFilePath
  420. self.photoPath = ''
  421. self.aiResult = null
  422. }
  423. })
  424. }
  425. },
  426. // ===== 增强型 AI 模拟识别 =====
  427. // 基于效价/激活度/时段/强度等多维参数,生成更自然的模拟结果
  428. analyzeEmotion() {
  429. this.aiAnalyzing = true
  430. this.aiResult = null
  431. var self = this
  432. // 生成伪随机种子(不同照片/视频产生不同结果,模拟真实识别)
  433. var seed = this.genSeed()
  434. // 根据多维参数生成情绪结果
  435. var results = this.genEmotionResults(seed)
  436. // 处理时间随机变化(200-1600ms,让每次等待不一样)
  437. var delay = 600 + (seed % 1000)
  438. setTimeout(function() {
  439. self.aiResult = results
  440. self.aiAnalyzing = false
  441. }, delay)
  442. },
  443. // 基于文件路径 + 时间生成伪随机种子
  444. genSeed() {
  445. var path = this.photoPath || this.videoPath || ''
  446. var hash = 0
  447. for (var i = 0; i < path.length; i++) {
  448. hash = ((hash << 5) - hash) + path.charCodeAt(i)
  449. hash = hash & hash
  450. }
  451. hash = Math.abs(hash)
  452. return (hash + Date.now()) % 7919 // 质数取模
  453. },
  454. // 多维参数 → 情绪结果
  455. genEmotionResults(seed) {
  456. var valence = this.affectValence
  457. var arousal = this.affectArousal
  458. // 1. 从效价-激活度象限推导主要情绪分布
  459. var emotionPool = []
  460. if (valence >= 6 && arousal >= 6) {
  461. // Q1: 高激活 + 积极 → 开心/兴奋/惊喜
  462. emotionPool = [
  463. { label: '开心', base: 38, color: '#10B981' },
  464. { label: '兴奋', base: 22, color: '#8B5CF6' },
  465. { label: '惊喜', base: 18, color: '#FF9FF3' },
  466. { label: '满足', base: 12, color: '#3B82F6' },
  467. { label: '期待', base: 10, color: '#F59E0B' }
  468. ]
  469. } else if (valence >= 6 && arousal < 6) {
  470. // Q2: 低激活 + 积极 → 平静/满足/放松
  471. emotionPool = [
  472. { label: '平静', base: 32, color: '#3B82F6' },
  473. { label: '满足', base: 25, color: '#10B981' },
  474. { label: '放松', base: 20, color: '#6B9BD2' },
  475. { label: '开心', base: 15, color: '#10B981' },
  476. { label: '无聊', base: 8, color: '#9CA3AF' }
  477. ]
  478. } else if (valence < 6 && arousal >= 6) {
  479. // Q3: 高激活 + 消极 → 生气/紧张/恐惧
  480. emotionPool = [
  481. { label: '紧张', base: 30, color: '#F59E0B' },
  482. { label: '生气', base: 25, color: '#EF4444' },
  483. { label: '烦躁', base: 20, color: '#FF6B6B' },
  484. { label: '恐惧', base: 15, color: '#7C3AED' },
  485. { label: '厌恶', base: 10, color: '#A0A0A0' }
  486. ]
  487. } else {
  488. // Q4: 低激活 + 消极 → 难过/疲惫/低落
  489. emotionPool = [
  490. { label: '难过', base: 32, color: '#6B9BD2' },
  491. { label: '疲惫', base: 28, color: '#9CA3AF' },
  492. { label: '低落', base: 20, color: '#6B7280' },
  493. { label: '平静', base: 12, color: '#3B82F6' },
  494. { label: '无聊', base: 8, color: '#9CA3AF' }
  495. ]
  496. }
  497. // 2. 根据强度/压力/能量微调
  498. // 高强度 → 强化 top1
  499. if (this.intensityLevel >= 7) {
  500. emotionPool[0].base += 10
  501. } else if (this.intensityLevel <= 3) {
  502. // 低强度 → 向平静偏移
  503. for (var si = 0; si < emotionPool.length; si++) {
  504. if (emotionPool[si].label === '平静' || emotionPool[si].label === '无聊') {
  505. emotionPool[si].base += 8
  506. }
  507. }
  508. }
  509. // 高压力 → 添加紧张/烦躁倾向
  510. if (this.stressLevel >= 7) {
  511. var tensionIdx = -1
  512. var angerIdx = -1
  513. for (var ti = 0; ti < emotionPool.length; ti++) {
  514. if (emotionPool[ti].label === '紧张') tensionIdx = ti
  515. if (emotionPool[ti].label === '烦躁') angerIdx = ti
  516. }
  517. if (tensionIdx >= 0) emotionPool[tensionIdx].base += 12
  518. if (angerIdx >= 0) emotionPool[angerIdx].base += 8
  519. }
  520. // 3. 根据 Plutchik 已选情绪提升权重(用户选择了说明有倾向)
  521. if (this.selectedEmotionType) {
  522. var typeToLabel = {
  523. joy: '开心', trust: '信任', fear: '恐惧',
  524. surprise: '惊喜', sadness: '难过', disgust: '厌恶',
  525. anger: '生气', anticipation: '期待'
  526. }
  527. var matchedLabel = typeToLabel[this.selectedEmotionType]
  528. for (var mi = 0; mi < emotionPool.length; mi++) {
  529. if (emotionPool[mi].label === matchedLabel) {
  530. emotionPool[mi].base += 15
  531. }
  532. }
  533. }
  534. // 4. 根据时段微调
  535. var hour = new Date().getHours()
  536. if (hour < 6) {
  537. // 深夜:偏向疲惫/低落
  538. this.adjustPoolForTime(emotionPool, '疲惫', 10)
  539. this.adjustPoolForTime(emotionPool, '低落', 8)
  540. } else if (hour < 10) {
  541. // 早晨:偏向积极
  542. this.adjustPoolForTime(emotionPool, '开心', 8)
  543. this.adjustPoolForTime(emotionPool, '期待', 6)
  544. } else if (hour >= 22) {
  545. // 夜晚:偏向平静/疲惫
  546. this.adjustPoolForTime(emotionPool, '平静', 8)
  547. this.adjustPoolForTime(emotionPool, '疲惫', 6)
  548. }
  549. // 5. 引入伪随机噪声(让每次结果有细微差异)
  550. for (var ni = 0; ni < emotionPool.length; ni++) {
  551. var noise = (seed * (ni + 1) * 7) % 11 - 5 // -5 ~ +5
  552. emotionPool[ni].base += noise
  553. if (emotionPool[ni].base < 3) emotionPool[ni].base = 3
  554. }
  555. // 6. 降序排列,取 top 4,归一化到 100%
  556. emotionPool.sort(function(a, b) { return b.base - a.base })
  557. var top4 = emotionPool.slice(0, 4)
  558. var total = 0
  559. for (var sumi = 0; sumi < top4.length; sumi++) {
  560. total += top4[sumi].base
  561. }
  562. for (var normi = 0; normi < top4.length; normi++) {
  563. top4[normi].confidence = Math.round((top4[normi].base / total) * 100)
  564. }
  565. // 补余数确保 =100
  566. var sumConf = 0
  567. for (var ci = 0; ci < top4.length; ci++) {
  568. sumConf += top4[ci].confidence
  569. }
  570. var diff = 100 - sumConf
  571. if (diff !== 0 && top4.length > 0) {
  572. top4[0].confidence += diff
  573. }
  574. return top4
  575. },
  576. adjustPoolForTime(pool, label, delta) {
  577. for (var i = 0; i < pool.length; i++) {
  578. if (pool[i].label === label) {
  579. pool[i].base += delta
  580. return
  581. }
  582. }
  583. },
  584. applyAiTags() {
  585. if (!this.aiResult) return
  586. var detected = []
  587. for (var i = 0; i < this.aiResult.length; i++) {
  588. var em = this.aiResult[i]
  589. if (em.confidence >= 20 && this.emotionTags.indexOf(em.label) !== -1) {
  590. detected.push(em.label)
  591. }
  592. }
  593. var merged = this.selectedTags.slice()
  594. for (var j = 0; j < detected.length; j++) {
  595. if (merged.indexOf(detected[j]) === -1) {
  596. merged.push(detected[j])
  597. }
  598. }
  599. this.selectedTags = merged
  600. uni.showToast({ title: '已应用情绪标签', icon: 'success' })
  601. },
  602. // ===== 提交 =====
  603. submitCheckin() {
  604. if (!this.canSubmit || this.submitting) return
  605. this.submitting = true
  606. const memberId = uni.getStorageSync('currentChildId')
  607. const doSubmit = (mediaUrl) => {
  608. uni.request({
  609. url: config.API_BASE_URL + '/api/mind/checkin/create',
  610. method: 'POST',
  611. data: {
  612. memberId: memberId,
  613. // 旧字段向后兼容
  614. moodWeather: this.selectedWeather,
  615. emotionTags: JSON.stringify(this.selectedTags),
  616. photoUrl: this.captureMode === 'photo' ? (mediaUrl || '') : '',
  617. videoUrl: this.captureMode === 'video' ? (mediaUrl || '') : '',
  618. mediaType: this.captureMode,
  619. note: this.note,
  620. // Phase 1.1 精细情绪字段
  621. moodScore: this.affectValence,
  622. emotionType: this.selectedEmotionType,
  623. stressLevel: this.stressLevel,
  624. arousalLevel: this.affectArousal,
  625. energyLevel: this.energyLevel
  626. },
  627. success: (res) => {
  628. this.submitting = false
  629. if (res.data && res.data.code === 200) {
  630. uni.showToast({
  631. title: '+5 心能量',
  632. icon: 'none',
  633. duration: 2000
  634. })
  635. setTimeout(() => {
  636. uni.navigateTo({ url: '/pages/mind/emotion-trend' })
  637. }, 1500)
  638. } else {
  639. uni.showToast({ title: res.data && res.data.message || '提交失败', icon: 'none' })
  640. }
  641. },
  642. fail: () => {
  643. this.submitting = false
  644. uni.showToast({ title: '网络错误', icon: 'none' })
  645. }
  646. })
  647. }
  648. var mediaPath = this.photoPath || this.videoPath
  649. if (mediaPath) {
  650. uni.uploadFile({
  651. url: config.API_BASE_URL + '/api/media/upload',
  652. filePath: mediaPath,
  653. name: 'file',
  654. success: (uploadRes) => {
  655. let mediaUrl = ''
  656. try {
  657. const data = JSON.parse(uploadRes.data)
  658. mediaUrl = data.data || data.url || ''
  659. } catch (e) {
  660. mediaUrl = uploadRes.data
  661. }
  662. doSubmit(mediaUrl)
  663. },
  664. fail: () => doSubmit('')
  665. })
  666. } else {
  667. doSubmit('')
  668. }
  669. }
  670. }
  671. }
  672. </script>
  673. <style scoped>
  674. .checkin-container {
  675. min-height: 100vh;
  676. background: linear-gradient(180deg, #FFF0F5 0%, #FFFFFF 100%);
  677. padding-bottom: 40rpx;
  678. }
  679. .checkin-header {
  680. display: flex;
  681. align-items: center;
  682. justify-content: space-between;
  683. padding: 30rpx 32rpx;
  684. }
  685. .checkin-date {
  686. font-size: 32rpx;
  687. font-weight: 600;
  688. color: #333;
  689. }
  690. .streak-badge {
  691. display: flex;
  692. align-items: center;
  693. background: linear-gradient(135deg, #FF6B9D, #FF8CB3);
  694. border-radius: 30rpx;
  695. padding: 8rpx 24rpx;
  696. }
  697. .streak-icon {
  698. font-size: 28rpx;
  699. margin-right: 8rpx;
  700. }
  701. .streak-text {
  702. font-size: 24rpx;
  703. color: #fff;
  704. font-weight: 500;
  705. }
  706. .section {
  707. margin: 20rpx 32rpx;
  708. background: #fff;
  709. border-radius: 20rpx;
  710. padding: 28rpx;
  711. box-shadow: 0 2rpx 12rpx rgba(255, 107, 157, 0.08);
  712. }
  713. .section-title {
  714. font-size: 28rpx;
  715. font-weight: 600;
  716. color: #333;
  717. margin-bottom: 20rpx;
  718. display: block;
  719. }
  720. /* ===== 2D 效价-激活度网格 ===== */
  721. .affect-grid {
  722. position: relative;
  723. width: 100%;
  724. /* height is set dynamically equal to width via aspect-ratio or inline style */
  725. aspect-ratio: 1;
  726. background: linear-gradient(135deg, #FFF5F5 0%, #FFF0FF 25%, #F0F0FF 50%, #F0FFF4 75%, #FFFFF0 100%);
  727. border-radius: 16rpx;
  728. border: 2rpx solid #E8E8E8;
  729. margin-bottom: 16rpx;
  730. /* Ensure the grid has a defined height — fallback */
  731. min-height: 300rpx;
  732. /* Overlap grid lines for visual clarity */
  733. overflow: hidden;
  734. }
  735. /* 网格线:用伪元素或背景渐变 */
  736. .affect-grid::before {
  737. content: '';
  738. position: absolute;
  739. left: 50%;
  740. top: 0;
  741. bottom: 0;
  742. width: 2rpx;
  743. background: rgba(0,0,0,0.06);
  744. transform: translateX(-50%);
  745. pointer-events: none;
  746. }
  747. .affect-grid::after {
  748. content: '';
  749. position: absolute;
  750. top: 50%;
  751. left: 0;
  752. right: 0;
  753. height: 2rpx;
  754. background: rgba(0,0,0,0.06);
  755. transform: translateY(-50%);
  756. pointer-events: none;
  757. }
  758. .grid-label {
  759. position: absolute;
  760. font-size: 22rpx;
  761. color: rgba(0,0,0,0.25);
  762. pointer-events: none;
  763. }
  764. .label-nw { top: 20rpx; left: 20rpx; }
  765. .label-ne { top: 20rpx; right: 20rpx; }
  766. .label-sw { bottom: 20rpx; left: 20rpx; }
  767. .label-se { bottom: 20rpx; right: 20rpx; }
  768. .axis-label {
  769. position: absolute;
  770. font-size: 20rpx;
  771. color: rgba(0,0,0,0.2);
  772. pointer-events: none;
  773. }
  774. .axis-bottom {
  775. bottom: 4rpx;
  776. left: 50%;
  777. transform: translateX(-50%);
  778. white-space: nowrap;
  779. }
  780. .axis-left {
  781. left: 4rpx;
  782. top: 50%;
  783. transform: translateY(-50%);
  784. font-size: 18rpx;
  785. line-height: 1.4;
  786. text-align: center;
  787. }
  788. .affect-dot {
  789. position: absolute;
  790. width: 44rpx;
  791. height: 44rpx;
  792. margin-left: -22rpx;
  793. margin-top: -22rpx;
  794. pointer-events: none;
  795. z-index: 2;
  796. }
  797. .affect-dot-inner {
  798. width: 100%;
  799. height: 100%;
  800. border-radius: 50%;
  801. background: #FF6B9D;
  802. box-shadow: 0 0 12rpx rgba(255, 107, 157, 0.5);
  803. border: 4rpx solid #fff;
  804. box-sizing: border-box;
  805. }
  806. .affect-values {
  807. display: flex;
  808. justify-content: center;
  809. gap: 40rpx;
  810. }
  811. .affect-val {
  812. font-size: 24rpx;
  813. color: #888;
  814. }
  815. /* ===== Plutchik 情绪轮 ===== */
  816. .emotion-wheel {
  817. display: flex;
  818. flex-wrap: wrap;
  819. gap: 16rpx;
  820. justify-content: center;
  821. }
  822. .emotion-btn {
  823. display: flex;
  824. flex-direction: column;
  825. align-items: center;
  826. width: 130rpx;
  827. padding: 16rpx 8rpx;
  828. border-radius: 20rpx;
  829. border: 3rpx solid #eee;
  830. background: #FAFAFA;
  831. transition: all 0.2s;
  832. }
  833. .emotion-active {
  834. background: #FFF0F5;
  835. transform: scale(1.05);
  836. }
  837. .emotion-icon {
  838. font-size: 48rpx;
  839. margin-bottom: 6rpx;
  840. }
  841. .emotion-label {
  842. font-size: 24rpx;
  843. color: #555;
  844. }
  845. /* ===== 滑条 ===== */
  846. .slider-row {
  847. display: flex;
  848. align-items: center;
  849. gap: 16rpx;
  850. }
  851. .slider-label {
  852. font-size: 24rpx;
  853. color: #999;
  854. white-space: nowrap;
  855. width: 64rpx;
  856. }
  857. .custom-slider {
  858. flex: 1;
  859. }
  860. .slider-value {
  861. font-size: 28rpx;
  862. font-weight: 600;
  863. color: #FF6B9D;
  864. width: 40rpx;
  865. text-align: center;
  866. }
  867. .intensity-hint {
  868. font-size: 22rpx;
  869. color: #aaa;
  870. text-align: center;
  871. margin-top: 4rpx;
  872. }
  873. /* ===== 情绪标签 ===== */
  874. .tag-grid {
  875. display: flex;
  876. flex-wrap: wrap;
  877. gap: 16rpx;
  878. }
  879. .tag-btn {
  880. padding: 12rpx 28rpx;
  881. border-radius: 30rpx;
  882. background: #F5F5F5;
  883. font-size: 26rpx;
  884. color: #666;
  885. border: 2rpx solid transparent;
  886. }
  887. .tag-active {
  888. background: #FFF0F5;
  889. color: #FF6B9D;
  890. border-color: #FF6B9D;
  891. }
  892. .custom-tag-row {
  893. display: flex;
  894. align-items: center;
  895. margin-top: 16rpx;
  896. gap: 16rpx;
  897. }
  898. .custom-tag-input {
  899. flex: 1;
  900. height: 60rpx;
  901. border: 2rpx solid #E0E0E0;
  902. border-radius: 12rpx;
  903. padding: 0 16rpx;
  904. font-size: 26rpx;
  905. }
  906. .custom-tag-add {
  907. padding: 12rpx 24rpx;
  908. background: #FF6B9D;
  909. color: #fff;
  910. border-radius: 12rpx;
  911. font-size: 26rpx;
  912. }
  913. .custom-tag-trigger {
  914. display: inline-block;
  915. margin-top: 16rpx;
  916. color: #FF6B9D;
  917. font-size: 26rpx;
  918. }
  919. /* ===== 拍照 ===== */
  920. .photo-area {
  921. width: 200rpx;
  922. height: 200rpx;
  923. border-radius: 16rpx;
  924. overflow: hidden;
  925. background: #F5F5F5;
  926. display: flex;
  927. align-items: center;
  928. justify-content: center;
  929. }
  930. .photo-preview {
  931. width: 100%;
  932. height: 100%;
  933. }
  934. .photo-placeholder {
  935. display: flex;
  936. flex-direction: column;
  937. align-items: center;
  938. }
  939. .photo-icon {
  940. font-size: 60rpx;
  941. }
  942. .photo-hint {
  943. font-size: 24rpx;
  944. color: #999;
  945. margin-top: 8rpx;
  946. }
  947. .photo-note {
  948. display: block;
  949. font-size: 22rpx;
  950. color: #999;
  951. margin-top: 12rpx;
  952. }
  953. /* ===== 拍照/录像模式切换 ===== */
  954. .capture-mode-bar {
  955. display: flex;
  956. gap: 20rpx;
  957. margin-bottom: 20rpx;
  958. }
  959. .mode-btn {
  960. padding: 10rpx 28rpx;
  961. border-radius: 30rpx;
  962. font-size: 26rpx;
  963. color: #999;
  964. background: #f5f5f5;
  965. border: 2rpx solid #eee;
  966. }
  967. .mode-btn.mode-active {
  968. color: #FF6B9D;
  969. background: #FFF0F5;
  970. border-color: #FF6B9D;
  971. font-weight: 500;
  972. }
  973. .video-preview {
  974. width: 100%;
  975. height: 100%;
  976. border-radius: 16rpx;
  977. }
  978. /* ===== AI 情绪识别 ===== */
  979. .ai-recognize-btn {
  980. display: flex;
  981. align-items: center;
  982. justify-content: center;
  983. padding: 20rpx;
  984. background: linear-gradient(135deg, #8B5CF6, #A78BFA);
  985. border-radius: 16rpx;
  986. cursor: pointer;
  987. }
  988. .ai-btn-icon {
  989. font-size: 32rpx;
  990. margin-right: 10rpx;
  991. }
  992. .ai-btn-text {
  993. font-size: 26rpx;
  994. color: #fff;
  995. font-weight: 500;
  996. }
  997. .ai-recognize-btn:active {
  998. opacity: 0.8;
  999. }
  1000. .ai-analyzing {
  1001. display: flex;
  1002. align-items: center;
  1003. justify-content: center;
  1004. padding: 30rpx 0;
  1005. }
  1006. .ai-loading-text {
  1007. font-size: 26rpx;
  1008. color: #8B5CF6;
  1009. }
  1010. .ai-result {
  1011. margin-top: 16rpx;
  1012. }
  1013. .ai-result-title {
  1014. font-size: 24rpx;
  1015. color: #666;
  1016. margin-bottom: 12rpx;
  1017. display: block;
  1018. }
  1019. .ai-emotion-list {
  1020. display: flex;
  1021. flex-direction: column;
  1022. gap: 12rpx;
  1023. }
  1024. .ai-emotion-item {
  1025. display: flex;
  1026. align-items: center;
  1027. gap: 12rpx;
  1028. }
  1029. .ai-emotion-label {
  1030. font-size: 24rpx;
  1031. color: #333;
  1032. width: 64rpx;
  1033. flex-shrink: 0;
  1034. }
  1035. .ai-emotion-bar-track {
  1036. flex: 1;
  1037. height: 16rpx;
  1038. background: #F0F0F0;
  1039. border-radius: 8rpx;
  1040. overflow: hidden;
  1041. }
  1042. .ai-emotion-bar-fill {
  1043. height: 100%;
  1044. border-radius: 8rpx;
  1045. transition: width 0.5s;
  1046. }
  1047. .ai-emotion-value {
  1048. font-size: 22rpx;
  1049. color: #999;
  1050. width: 56rpx;
  1051. text-align: right;
  1052. }
  1053. .ai-apply-tags {
  1054. margin-top: 20rpx;
  1055. padding: 16rpx;
  1056. background: #F5F0FF;
  1057. border-radius: 12rpx;
  1058. text-align: center;
  1059. cursor: pointer;
  1060. }
  1061. .ai-apply-text {
  1062. font-size: 26rpx;
  1063. color: #8B5CF6;
  1064. font-weight: 500;
  1065. }
  1066. .ai-apply-tags:active {
  1067. opacity: 0.7;
  1068. }
  1069. .note-input {
  1070. width: 100%;
  1071. min-height: 140rpx;
  1072. background: #F9F9F9;
  1073. border-radius: 12rpx;
  1074. padding: 20rpx;
  1075. font-size: 26rpx;
  1076. color: #333;
  1077. box-sizing: border-box;
  1078. }
  1079. .submit-area {
  1080. margin: 40rpx 32rpx;
  1081. }
  1082. .submit-btn {
  1083. width: 100%;
  1084. height: 88rpx;
  1085. line-height: 88rpx;
  1086. background: linear-gradient(135deg, #FF6B9D, #FF8CB3);
  1087. color: #fff;
  1088. border-radius: 44rpx;
  1089. font-size: 30rpx;
  1090. font-weight: 600;
  1091. display: flex;
  1092. align-items: center;
  1093. justify-content: center;
  1094. }
  1095. .submit-disabled {
  1096. opacity: 0.5;
  1097. }
  1098. .submit-icon {
  1099. margin-right: 8rpx;
  1100. }
  1101. .loading-mask {
  1102. position: fixed;
  1103. top: 0;
  1104. left: 0;
  1105. right: 0;
  1106. bottom: 0;
  1107. background: rgba(0, 0, 0, 0.3);
  1108. display: flex;
  1109. align-items: center;
  1110. justify-content: center;
  1111. z-index: 999;
  1112. }
  1113. .loading-box {
  1114. background: #fff;
  1115. padding: 40rpx 60rpx;
  1116. border-radius: 20rpx;
  1117. }
  1118. .loading-text {
  1119. font-size: 28rpx;
  1120. color: #666;
  1121. }
  1122. </style>