chat.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. <template>
  2. <view class="chat-container">
  3. <!-- 导航栏 -->
  4. <view class="chat-nav">
  5. <view class="nav-back" @click="goBack">
  6. <text class="nav-back-icon">‹</text>
  7. </view>
  8. <text class="nav-title">AI 家庭助手</text>
  9. <view class="nav-actions">
  10. <text class="nav-action" @click="showConversations = !showConversations">☰</text>
  11. </view>
  12. </view>
  13. <!-- 会话列表(侧拉) -->
  14. <view class="conv-overlay" v-if="showConversations" @click="showConversations = false"></view>
  15. <view class="conv-sidebar" :class="{ 'conv-sidebar-open': showConversations }">
  16. <view class="conv-header">
  17. <text class="conv-header-title">会话列表</text>
  18. <text class="conv-header-new" @click="newConversation">+ 新会话</text>
  19. </view>
  20. <scroll-view class="conv-list" scroll-y>
  21. <view class="conv-item" v-for="conv in conversations" :key="conv.id"
  22. :class="{ 'conv-item-active': conv.id === currentConversationId }"
  23. @click="switchConversation(conv.id)">
  24. <text class="conv-item-name">{{ conv.name || '新会话' }}</text>
  25. <text class="conv-item-time">{{ formatTime(conv.updated_at) }}</text>
  26. </view>
  27. <BaseEmpty v-if="conversations.length === 0" text="暂无会话" icon="💬" />
  28. </scroll-view>
  29. </view>
  30. <!-- 消息列表 -->
  31. <scroll-view class="message-list" scroll-y :scroll-into-view="scrollToId" scroll-with-animation
  32. @scrolltoupper="loadMoreMessages" id="messageList">
  33. <view class="message-list-inner">
  34. <view class="welcome-card" v-if="messages.length === 0 && !sending">
  35. <view class="welcome-icon">🤖</view>
  36. <text class="welcome-title">你好!我是你的家庭助手</text>
  37. <text class="welcome-desc">我可以帮你了解孩子的成长情况,比如:</text>
  38. <view class="welcome-suggestions">
  39. <view class="welcome-suggestion" @click="quickQuestion('小明最近任务完成怎么样?')">
  40. <text class="suggestion-icon">📋</text>
  41. <text class="suggestion-text">小明最近任务完成怎么样?</text>
  42. </view>
  43. <view class="welcome-suggestion" @click="quickQuestion('我家孩子的能量评分怎么样?')">
  44. <text class="suggestion-icon">⚡</text>
  45. <text class="suggestion-text">孩子的能量评分怎么样?</text>
  46. </view>
  47. <view class="welcome-suggestion" @click="quickQuestion('今天有什么需要我做的事情?')">
  48. <text class="suggestion-icon">✅</text>
  49. <text class="suggestion-text">今天有什么需要我做的事情?</text>
  50. </view>
  51. </view>
  52. </view>
  53. <view class="message-group" v-for="(msg, idx) in messages" :key="msg.id">
  54. <!-- 用户消息 -->
  55. <view class="message message-user" v-if="msg.query">
  56. <view class="message-bubble user-bubble">{{ msg.query }}</view>
  57. </view>
  58. <!-- AI 回复 -->
  59. <view class="message message-ai" v-if="msg.answer">
  60. <view class="message-avatar">🤖</view>
  61. <view class="message-bubble ai-bubble">{{ msg.answer }}</view>
  62. </view>
  63. </view>
  64. <!-- 加载中 -->
  65. <view class="message message-ai" v-if="sending">
  66. <view class="message-avatar">🤖</view>
  67. <view class="message-bubble ai-bubble typing">
  68. <text class="typing-dot">.</text>
  69. <text class="typing-dot">.</text>
  70. <text class="typing-dot">.</text>
  71. </view>
  72. </view>
  73. <view id="scrollBottom"></view>
  74. </view>
  75. </scroll-view>
  76. <!-- 输入区 -->
  77. <view class="chat-input-area">
  78. <view class="chat-input-wrap">
  79. <input class="chat-input" v-model="inputText" placeholder="问我关于家庭和孩子的问题..."
  80. :disabled="sending" @confirm="sendMessage" confirm-type="send" />
  81. </view>
  82. <view class="chat-send-btn" :class="{ 'chat-send-active': inputText.trim() }"
  83. @click="sendMessage">
  84. <text class="send-icon">➤</text>
  85. </view>
  86. </view>
  87. </view>
  88. </template>
  89. <script>
  90. import { aiSendMessage, aiGetConversations, aiGetMessages, aiDeleteConversation } from '../../utils/api.js'
  91. import BaseEmpty from '../../components/BaseEmpty.vue'
  92. export default {
  93. components: { BaseEmpty },
  94. data() {
  95. return {
  96. inputText: '',
  97. messages: [],
  98. conversations: [],
  99. currentConversationId: '',
  100. sending: false,
  101. showConversations: false,
  102. scrollToId: '',
  103. pageLoaded: false,
  104. reportId: null,
  105. surveyId: null,
  106. pendingFirstMessage: ''
  107. }
  108. },
  109. onLoad: function(options) {
  110. this.reportId = options.reportId ? parseInt(options.reportId) : null
  111. this.surveyId = options.surveyId ? parseInt(options.surveyId) : null
  112. if (options.firstMessage) {
  113. this.pendingFirstMessage = decodeURIComponent(options.firstMessage)
  114. }
  115. this.loadConversations()
  116. // 如果有预填消息,自动发送
  117. var self = this
  118. if (this.pendingFirstMessage) {
  119. this.$nextTick(function() {
  120. self.inputText = self.pendingFirstMessage
  121. self.pendingFirstMessage = ''
  122. self.sendMessage()
  123. })
  124. }
  125. },
  126. onShow() {
  127. if (this.pageLoaded) {
  128. this.loadConversations()
  129. }
  130. this.pageLoaded = true
  131. },
  132. methods: {
  133. goBack() {
  134. uni.navigateBack()
  135. },
  136. async loadConversations() {
  137. try {
  138. const res = await aiGetConversations()
  139. this.conversations = res.data || []
  140. } catch (e) {
  141. console.log('加载会话列表失败', e)
  142. }
  143. },
  144. async switchConversation(convId) {
  145. this.currentConversationId = convId
  146. this.showConversations = false
  147. this.messages = []
  148. this.$nextTick(() => {
  149. this.scrollToBottom()
  150. })
  151. try {
  152. const res = await aiGetMessages({ conversationId: convId })
  153. this.messages = res.data || []
  154. this.$nextTick(() => {
  155. this.scrollToBottom()
  156. })
  157. } catch (e) {
  158. console.log('加载消息失败', e)
  159. }
  160. },
  161. newConversation() {
  162. this.currentConversationId = ''
  163. this.showConversations = false
  164. this.messages = []
  165. this.inputText = ''
  166. this.$nextTick(() => {
  167. this.scrollToBottom()
  168. })
  169. },
  170. async sendMessage() {
  171. const text = this.inputText.trim()
  172. if (!text || this.sending) return
  173. // 立即显示用户消息
  174. const tempMsg = { id: 'temp_' + Date.now(), query: text, answer: '' }
  175. this.messages.push(tempMsg)
  176. this.inputText = ''
  177. this.sending = true
  178. this.$nextTick(() => {
  179. this.scrollToBottom()
  180. })
  181. try {
  182. var params = {
  183. query: text,
  184. conversationId: this.currentConversationId
  185. }
  186. if (this.reportId) {
  187. params.reportId = this.reportId
  188. }
  189. if (this.surveyId) {
  190. params.surveyId = this.surveyId
  191. }
  192. const res = await aiSendMessage(params)
  193. const data = res.data || {}
  194. // 更新最后一条消息的回复
  195. const lastMsg = this.messages[this.messages.length - 1]
  196. if (lastMsg) {
  197. lastMsg.answer = data.answer || ''
  198. }
  199. // 更新 conversationId(新建会话时 Dify 返回)
  200. if (data.conversationId && !this.currentConversationId) {
  201. this.currentConversationId = data.conversationId
  202. // 刷新会话列表
  203. this.loadConversations()
  204. }
  205. } catch (e) {
  206. // 显示错误
  207. const lastMsg = this.messages[this.messages.length - 1]
  208. if (lastMsg) {
  209. lastMsg.answer = '抱歉,我暂时无法回答,请稍后再试。'
  210. }
  211. } finally {
  212. this.sending = false
  213. this.$nextTick(() => {
  214. this.scrollToBottom()
  215. })
  216. }
  217. },
  218. quickQuestion(text) {
  219. this.inputText = text
  220. this.sendMessage()
  221. },
  222. loadMoreMessages() {
  223. // 分页加载历史(当前简化)
  224. },
  225. scrollToBottom() {
  226. this.scrollToId = 'scrollBottom'
  227. },
  228. formatTime(timestamp) {
  229. if (!timestamp) return ''
  230. const d = new Date(timestamp * 1000)
  231. const now = new Date()
  232. const diff = now - d
  233. if (diff < 86400000) {
  234. return d.getHours().toString().padStart(2, '0') + ':' + d.getMinutes().toString().padStart(2, '0')
  235. }
  236. return (d.getMonth() + 1) + '/' + d.getDate()
  237. }
  238. }
  239. }
  240. </script>
  241. <style scoped>
  242. .chat-container {
  243. display: flex;
  244. flex-direction: column;
  245. height: 100vh;
  246. background: var(--bg, #FFF7ED);
  247. position: relative;
  248. }
  249. /* 导航栏 */
  250. .chat-nav {
  251. display: flex;
  252. align-items: center;
  253. justify-content: space-between;
  254. padding: 88rpx 24rpx 16rpx;
  255. background: #fff;
  256. border-bottom: 1rpx solid var(--border-light, #E2E8F0);
  257. position: relative;
  258. z-index: 100;
  259. }
  260. .nav-back {
  261. width: 60rpx;
  262. height: 60rpx;
  263. display: flex;
  264. align-items: center;
  265. justify-content: center;
  266. }
  267. .nav-back-icon {
  268. font-size: 40rpx;
  269. color: var(--text, #1E293B);
  270. font-weight: 600;
  271. }
  272. .nav-title {
  273. font-size: 32rpx;
  274. font-weight: 600;
  275. color: var(--text, #1E293B);
  276. }
  277. .nav-actions {
  278. width: 60rpx;
  279. height: 60rpx;
  280. display: flex;
  281. align-items: center;
  282. justify-content: center;
  283. }
  284. .nav-action {
  285. font-size: 36rpx;
  286. color: var(--text-secondary, #64748B);
  287. }
  288. /* 会话列表侧拉 */
  289. .conv-overlay {
  290. position: fixed;
  291. top: 0;
  292. left: 0;
  293. right: 0;
  294. bottom: 0;
  295. background: rgba(0,0,0,0.3);
  296. z-index: 200;
  297. }
  298. .conv-sidebar {
  299. position: fixed;
  300. top: 0;
  301. right: -500rpx;
  302. width: 460rpx;
  303. height: 100vh;
  304. background: #fff;
  305. z-index: 210;
  306. transition: right 0.3s ease;
  307. display: flex;
  308. flex-direction: column;
  309. }
  310. .conv-sidebar-open {
  311. right: 0;
  312. }
  313. .conv-header {
  314. display: flex;
  315. justify-content: space-between;
  316. align-items: center;
  317. padding: 100rpx 24rpx 16rpx;
  318. border-bottom: 1rpx solid var(--border-light, #E2E8F0);
  319. }
  320. .conv-header-title {
  321. font-size: 28rpx;
  322. font-weight: 600;
  323. color: var(--text, #1E293B);
  324. }
  325. .conv-header-new {
  326. font-size: 24rpx;
  327. color: var(--color-primary, #5B9BD5);
  328. font-weight: 500;
  329. }
  330. .conv-list {
  331. flex: 1;
  332. overflow-y: auto;
  333. }
  334. .conv-item {
  335. padding: 20rpx 24rpx;
  336. border-bottom: 1rpx solid var(--border-light, #E2E8F0);
  337. }
  338. .conv-item-active {
  339. background: rgba(91, 155, 213, 0.06);
  340. }
  341. .conv-item-name {
  342. font-size: 26rpx;
  343. color: var(--text, #1E293B);
  344. display: block;
  345. margin-bottom: 6rpx;
  346. overflow: hidden;
  347. text-overflow: ellipsis;
  348. white-space: nowrap;
  349. }
  350. .conv-item-time {
  351. font-size: 20rpx;
  352. color: var(--muted, #94A3B8);
  353. }
  354. /* 消息列表 */
  355. .message-list {
  356. flex: 1;
  357. overflow-y: auto;
  358. padding: 20rpx 24rpx;
  359. }
  360. .message-list-inner {
  361. min-height: 100%;
  362. }
  363. /* 欢迎卡片 */
  364. .welcome-card {
  365. text-align: center;
  366. padding: 60rpx 20rpx 40rpx;
  367. }
  368. .welcome-icon {
  369. font-size: 80rpx;
  370. margin-bottom: 20rpx;
  371. }
  372. .welcome-title {
  373. font-size: 32rpx;
  374. font-weight: 600;
  375. color: var(--text, #1E293B);
  376. display: block;
  377. margin-bottom: 12rpx;
  378. }
  379. .welcome-desc {
  380. font-size: 24rpx;
  381. color: var(--text-secondary, #64748B);
  382. display: block;
  383. margin-bottom: 32rpx;
  384. }
  385. .welcome-suggestions {
  386. display: flex;
  387. flex-direction: column;
  388. gap: 16rpx;
  389. padding: 0 20rpx;
  390. }
  391. .welcome-suggestion {
  392. display: flex;
  393. align-items: center;
  394. background: #fff;
  395. padding: 20rpx 24rpx;
  396. border-radius: 16rpx;
  397. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.04);
  398. }
  399. .suggestion-icon {
  400. font-size: 28rpx;
  401. margin-right: 12rpx;
  402. }
  403. .suggestion-text {
  404. font-size: 24rpx;
  405. color: var(--text, #1E293B);
  406. }
  407. /* 消息 */
  408. .message {
  409. margin-bottom: 24rpx;
  410. }
  411. .message-user {
  412. display: flex;
  413. justify-content: flex-end;
  414. }
  415. .message-ai {
  416. display: flex;
  417. align-items: flex-start;
  418. }
  419. .message-avatar {
  420. width: 48rpx;
  421. height: 48rpx;
  422. font-size: 32rpx;
  423. margin-right: 12rpx;
  424. flex-shrink: 0;
  425. display: flex;
  426. align-items: center;
  427. justify-content: center;
  428. }
  429. .message-bubble {
  430. max-width: 70%;
  431. padding: 16rpx 20rpx;
  432. font-size: 26rpx;
  433. line-height: 1.6;
  434. border-radius: 16rpx;
  435. word-break: break-word;
  436. }
  437. .user-bubble {
  438. background: var(--color-primary, #5B9BD5);
  439. color: #fff;
  440. border-bottom-right-radius: 4rpx;
  441. }
  442. .ai-bubble {
  443. background: #fff;
  444. color: var(--text, #1E293B);
  445. border-bottom-left-radius: 4rpx;
  446. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.04);
  447. }
  448. /* 输入区 */
  449. .chat-input-area {
  450. display: flex;
  451. align-items: center;
  452. padding: 16rpx 24rpx;
  453. padding-bottom: calc(16rpx + env(safe-area-inset-bottom));
  454. background: #fff;
  455. border-top: 1rpx solid var(--border-light, #E2E8F0);
  456. }
  457. .chat-input-wrap {
  458. flex: 1;
  459. background: var(--bg, #FFF7ED);
  460. border-radius: 40rpx;
  461. padding: 0 24rpx;
  462. margin-right: 16rpx;
  463. }
  464. .chat-input {
  465. height: 72rpx;
  466. font-size: 26rpx;
  467. color: var(--text, #1E293B);
  468. }
  469. .chat-send-btn {
  470. width: 72rpx;
  471. height: 72rpx;
  472. border-radius: 50%;
  473. background: var(--border-light, #E2E8F0);
  474. display: flex;
  475. align-items: center;
  476. justify-content: center;
  477. flex-shrink: 0;
  478. }
  479. .chat-send-active {
  480. background: var(--color-primary, #5B9BD5);
  481. }
  482. .send-icon {
  483. font-size: 28rpx;
  484. color: #fff;
  485. }
  486. /* 打字动画 */
  487. .typing {
  488. display: flex;
  489. align-items: center;
  490. gap: 4rpx;
  491. min-height: 40rpx;
  492. }
  493. .typing-dot {
  494. font-size: 40rpx;
  495. line-height: 1;
  496. animation: typingBounce 1.4s infinite;
  497. }
  498. .typing-dot:nth-child(2) {
  499. animation-delay: 0.2s;
  500. }
  501. .typing-dot:nth-child(3) {
  502. animation-delay: 0.4s;
  503. }
  504. @keyframes typingBounce {
  505. 0%, 60%, 100% { opacity: 0.3; transform: translateY(0); }
  506. 30% { opacity: 1; transform: translateY(-8rpx); }
  507. }
  508. </style>