chat.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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 || idx">
  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. }
  105. },
  106. onLoad() {
  107. this.loadConversations()
  108. },
  109. onShow() {
  110. if (this.pageLoaded) {
  111. this.loadConversations()
  112. }
  113. this.pageLoaded = true
  114. },
  115. methods: {
  116. goBack() {
  117. uni.navigateBack()
  118. },
  119. async loadConversations() {
  120. try {
  121. const res = await aiGetConversations()
  122. this.conversations = res.data || []
  123. } catch (e) {
  124. console.log('加载会话列表失败', e)
  125. }
  126. },
  127. async switchConversation(convId) {
  128. this.currentConversationId = convId
  129. this.showConversations = false
  130. this.messages = []
  131. this.$nextTick(() => {
  132. this.scrollToBottom()
  133. })
  134. try {
  135. const res = await aiGetMessages({ conversationId: convId })
  136. this.messages = res.data || []
  137. this.$nextTick(() => {
  138. this.scrollToBottom()
  139. })
  140. } catch (e) {
  141. console.log('加载消息失败', e)
  142. }
  143. },
  144. newConversation() {
  145. this.currentConversationId = ''
  146. this.showConversations = false
  147. this.messages = []
  148. this.inputText = ''
  149. this.$nextTick(() => {
  150. this.scrollToBottom()
  151. })
  152. },
  153. async sendMessage() {
  154. const text = this.inputText.trim()
  155. if (!text || this.sending) return
  156. // 立即显示用户消息
  157. const tempMsg = { id: 'temp_' + Date.now(), query: text, answer: '' }
  158. this.messages.push(tempMsg)
  159. this.inputText = ''
  160. this.sending = true
  161. this.$nextTick(() => {
  162. this.scrollToBottom()
  163. })
  164. try {
  165. const res = await aiSendMessage({
  166. query: text,
  167. conversationId: this.currentConversationId
  168. })
  169. const data = res.data || {}
  170. // 更新最后一条消息的回复
  171. const lastMsg = this.messages[this.messages.length - 1]
  172. if (lastMsg) {
  173. lastMsg.answer = data.answer || ''
  174. }
  175. // 更新 conversationId(新建会话时 Dify 返回)
  176. if (data.conversationId && !this.currentConversationId) {
  177. this.currentConversationId = data.conversationId
  178. // 刷新会话列表
  179. this.loadConversations()
  180. }
  181. } catch (e) {
  182. // 显示错误
  183. const lastMsg = this.messages[this.messages.length - 1]
  184. if (lastMsg) {
  185. lastMsg.answer = '抱歉,我暂时无法回答,请稍后再试。'
  186. }
  187. } finally {
  188. this.sending = false
  189. this.$nextTick(() => {
  190. this.scrollToBottom()
  191. })
  192. }
  193. },
  194. quickQuestion(text) {
  195. this.inputText = text
  196. this.sendMessage()
  197. },
  198. loadMoreMessages() {
  199. // 分页加载历史(当前简化)
  200. },
  201. scrollToBottom() {
  202. this.scrollToId = 'scrollBottom'
  203. },
  204. formatTime(timestamp) {
  205. if (!timestamp) return ''
  206. const d = new Date(timestamp * 1000)
  207. const now = new Date()
  208. const diff = now - d
  209. if (diff < 86400000) {
  210. return d.getHours().toString().padStart(2, '0') + ':' + d.getMinutes().toString().padStart(2, '0')
  211. }
  212. return (d.getMonth() + 1) + '/' + d.getDate()
  213. }
  214. }
  215. }
  216. </script>
  217. <style scoped>
  218. .chat-container {
  219. display: flex;
  220. flex-direction: column;
  221. height: 100vh;
  222. background: var(--bg, #FFF7ED);
  223. position: relative;
  224. }
  225. /* 导航栏 */
  226. .chat-nav {
  227. display: flex;
  228. align-items: center;
  229. justify-content: space-between;
  230. padding: 88rpx 24rpx 16rpx;
  231. background: #fff;
  232. border-bottom: 1rpx solid var(--border-light, #E2E8F0);
  233. position: relative;
  234. z-index: 100;
  235. }
  236. .nav-back {
  237. width: 60rpx;
  238. height: 60rpx;
  239. display: flex;
  240. align-items: center;
  241. justify-content: center;
  242. }
  243. .nav-back-icon {
  244. font-size: 40rpx;
  245. color: var(--text, #1E293B);
  246. font-weight: 600;
  247. }
  248. .nav-title {
  249. font-size: 32rpx;
  250. font-weight: 600;
  251. color: var(--text, #1E293B);
  252. }
  253. .nav-actions {
  254. width: 60rpx;
  255. height: 60rpx;
  256. display: flex;
  257. align-items: center;
  258. justify-content: center;
  259. }
  260. .nav-action {
  261. font-size: 36rpx;
  262. color: var(--text-secondary, #64748B);
  263. }
  264. /* 会话列表侧拉 */
  265. .conv-overlay {
  266. position: fixed;
  267. top: 0;
  268. left: 0;
  269. right: 0;
  270. bottom: 0;
  271. background: rgba(0,0,0,0.3);
  272. z-index: 200;
  273. }
  274. .conv-sidebar {
  275. position: fixed;
  276. top: 0;
  277. right: -500rpx;
  278. width: 460rpx;
  279. height: 100vh;
  280. background: #fff;
  281. z-index: 210;
  282. transition: right 0.3s ease;
  283. display: flex;
  284. flex-direction: column;
  285. }
  286. .conv-sidebar-open {
  287. right: 0;
  288. }
  289. .conv-header {
  290. display: flex;
  291. justify-content: space-between;
  292. align-items: center;
  293. padding: 100rpx 24rpx 16rpx;
  294. border-bottom: 1rpx solid var(--border-light, #E2E8F0);
  295. }
  296. .conv-header-title {
  297. font-size: 28rpx;
  298. font-weight: 600;
  299. color: var(--text, #1E293B);
  300. }
  301. .conv-header-new {
  302. font-size: 24rpx;
  303. color: var(--color-primary, #5B9BD5);
  304. font-weight: 500;
  305. }
  306. .conv-list {
  307. flex: 1;
  308. overflow-y: auto;
  309. }
  310. .conv-item {
  311. padding: 20rpx 24rpx;
  312. border-bottom: 1rpx solid var(--border-light, #E2E8F0);
  313. }
  314. .conv-item-active {
  315. background: rgba(91, 155, 213, 0.06);
  316. }
  317. .conv-item-name {
  318. font-size: 26rpx;
  319. color: var(--text, #1E293B);
  320. display: block;
  321. margin-bottom: 6rpx;
  322. overflow: hidden;
  323. text-overflow: ellipsis;
  324. white-space: nowrap;
  325. }
  326. .conv-item-time {
  327. font-size: 20rpx;
  328. color: var(--muted, #94A3B8);
  329. }
  330. /* 消息列表 */
  331. .message-list {
  332. flex: 1;
  333. overflow-y: auto;
  334. padding: 20rpx 24rpx;
  335. }
  336. .message-list-inner {
  337. min-height: 100%;
  338. }
  339. /* 欢迎卡片 */
  340. .welcome-card {
  341. text-align: center;
  342. padding: 60rpx 20rpx 40rpx;
  343. }
  344. .welcome-icon {
  345. font-size: 80rpx;
  346. margin-bottom: 20rpx;
  347. }
  348. .welcome-title {
  349. font-size: 32rpx;
  350. font-weight: 600;
  351. color: var(--text, #1E293B);
  352. display: block;
  353. margin-bottom: 12rpx;
  354. }
  355. .welcome-desc {
  356. font-size: 24rpx;
  357. color: var(--text-secondary, #64748B);
  358. display: block;
  359. margin-bottom: 32rpx;
  360. }
  361. .welcome-suggestions {
  362. display: flex;
  363. flex-direction: column;
  364. gap: 16rpx;
  365. padding: 0 20rpx;
  366. }
  367. .welcome-suggestion {
  368. display: flex;
  369. align-items: center;
  370. background: #fff;
  371. padding: 20rpx 24rpx;
  372. border-radius: 16rpx;
  373. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.04);
  374. }
  375. .suggestion-icon {
  376. font-size: 28rpx;
  377. margin-right: 12rpx;
  378. }
  379. .suggestion-text {
  380. font-size: 24rpx;
  381. color: var(--text, #1E293B);
  382. }
  383. /* 消息 */
  384. .message {
  385. margin-bottom: 24rpx;
  386. }
  387. .message-user {
  388. display: flex;
  389. justify-content: flex-end;
  390. }
  391. .message-ai {
  392. display: flex;
  393. align-items: flex-start;
  394. }
  395. .message-avatar {
  396. width: 48rpx;
  397. height: 48rpx;
  398. font-size: 32rpx;
  399. margin-right: 12rpx;
  400. flex-shrink: 0;
  401. display: flex;
  402. align-items: center;
  403. justify-content: center;
  404. }
  405. .message-bubble {
  406. max-width: 70%;
  407. padding: 16rpx 20rpx;
  408. font-size: 26rpx;
  409. line-height: 1.6;
  410. border-radius: 16rpx;
  411. word-break: break-word;
  412. }
  413. .user-bubble {
  414. background: var(--color-primary, #5B9BD5);
  415. color: #fff;
  416. border-bottom-right-radius: 4rpx;
  417. }
  418. .ai-bubble {
  419. background: #fff;
  420. color: var(--text, #1E293B);
  421. border-bottom-left-radius: 4rpx;
  422. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.04);
  423. }
  424. /* 输入区 */
  425. .chat-input-area {
  426. display: flex;
  427. align-items: center;
  428. padding: 16rpx 24rpx;
  429. padding-bottom: calc(16rpx + env(safe-area-inset-bottom));
  430. background: #fff;
  431. border-top: 1rpx solid var(--border-light, #E2E8F0);
  432. }
  433. .chat-input-wrap {
  434. flex: 1;
  435. background: var(--bg, #FFF7ED);
  436. border-radius: 40rpx;
  437. padding: 0 24rpx;
  438. margin-right: 16rpx;
  439. }
  440. .chat-input {
  441. height: 72rpx;
  442. font-size: 26rpx;
  443. color: var(--text, #1E293B);
  444. }
  445. .chat-send-btn {
  446. width: 72rpx;
  447. height: 72rpx;
  448. border-radius: 50%;
  449. background: var(--border-light, #E2E8F0);
  450. display: flex;
  451. align-items: center;
  452. justify-content: center;
  453. flex-shrink: 0;
  454. }
  455. .chat-send-active {
  456. background: var(--color-primary, #5B9BD5);
  457. }
  458. .send-icon {
  459. font-size: 28rpx;
  460. color: #fff;
  461. }
  462. /* 打字动画 */
  463. .typing {
  464. display: flex;
  465. align-items: center;
  466. gap: 4rpx;
  467. min-height: 40rpx;
  468. }
  469. .typing-dot {
  470. font-size: 40rpx;
  471. line-height: 1;
  472. animation: typingBounce 1.4s infinite;
  473. }
  474. .typing-dot:nth-child(2) {
  475. animation-delay: 0.2s;
  476. }
  477. .typing-dot:nth-child(3) {
  478. animation-delay: 0.4s;
  479. }
  480. @keyframes typingBounce {
  481. 0%, 60%, 100% { opacity: 0.3; transform: translateY(0); }
  482. 30% { opacity: 1; transform: translateY(-8rpx); }
  483. }
  484. </style>