index.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <template>
  2. <div class="chat-page">
  3. <!-- 左侧会话列表 -->
  4. <div class="conversation-list">
  5. <div class="list-header">
  6. <h3>会话列表</h3>
  7. <button class="new-chat-btn" @click="createNewChat">
  8. <span>新建会话</span>
  9. </button>
  10. </div>
  11. <div class="conversations">
  12. <div
  13. v-for="(chat, index) in chatList"
  14. :key="index"
  15. class="conversation-item"
  16. :class="{ active: currentChatId === chat.id }"
  17. @click="switchChat(chat.id)"
  18. >
  19. <div class="chat-title">{{ chat.title }}</div>
  20. <div class="chat-time">{{ formatTime(chat.lastTime) }}</div>
  21. </div>
  22. </div>
  23. </div>
  24. <!-- 聊天区域 -->
  25. <div class="chat-container" >
  26. <chat-messages />
  27. <!-- 输入区域 -->
  28. <chat-input />
  29. </div>
  30. </div>
  31. </template>
  32. <script lang="ts" setup>
  33. import { ref } from 'vue'
  34. import ChatMessages from './components/chat-messages.vue' // 导入 ChatRecord 组件
  35. import ChatInput from './components/chat-input.vue' // 导入 ChatInput 组件
  36. interface Message {
  37. role: 'user' | 'assistant'
  38. content: string
  39. }
  40. interface Chat {
  41. id: string
  42. title: string
  43. lastTime: Date
  44. messages: Message[]
  45. }
  46. // 聊天消息
  47. const messages = ref<Message[]>([])
  48. const inputMessage = ref('')
  49. const userAvatar = '/user-avatar.png' // 替换为实际的用户头像路径
  50. const aiAvatar = '/ai-avatar.png' // 替换为实际的 AI 头像路径
  51. // 会话列表
  52. const chatList = ref<Chat[]>([
  53. {
  54. id: '1',
  55. title: '关于Vue3的讨论',
  56. lastTime: new Date(),
  57. messages: []
  58. },
  59. {
  60. id: '2',
  61. title: 'TypeScript学习',
  62. lastTime: new Date(Date.now() - 24 * 60 * 60 * 1000),
  63. messages: []
  64. }
  65. ])
  66. const currentChatId = ref('1')
  67. // 格式化时间
  68. const formatTime = (date: Date) => {
  69. const now = new Date()
  70. const diff = now.getTime() - date.getTime()
  71. // 如果是今天,显示时间
  72. if (diff < 24 * 60 * 60 * 1000) {
  73. return date.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' })
  74. } else if (diff < 7 * 24 * 60 * 60 * 1000) {
  75. const days = ['日', '一', '二', '三', '四', '五', '六']
  76. return `星期${days[date.getDay()]}`
  77. }
  78. // 否则显示日期
  79. else {
  80. return date.toLocaleDateString('zh-CN')
  81. }
  82. }
  83. // 切换会话
  84. const switchChat = (chatId: string) => {
  85. currentChatId.value = chatId
  86. const chat = chatList.value.find(c => c.id === chatId)
  87. if (chat) {
  88. messages.value = [...chat.messages]
  89. }
  90. }
  91. // 创建新会话
  92. const createNewChat = () => {
  93. const newId = Date.now().toString()
  94. chatList.value.unshift({
  95. id: newId,
  96. title: `新会话 ${chatList.value.length + 1}`,
  97. lastTime: new Date(),
  98. messages: []
  99. })
  100. switchChat(newId)
  101. }
  102. </script>
  103. <style lang="less" scoped>
  104. .chat-page {
  105. display: flex;
  106. height: 100vh;
  107. background-color: #fcfafa;
  108. position: relative;
  109. .chat-container {
  110. width: 100%;
  111. position: relative;
  112. }
  113. }
  114. .conversation-list {
  115. width: 260px;
  116. border-right: 1px solid #e5e7eb;
  117. display: flex;
  118. flex-direction: column;
  119. background-color: #f5f5f5; /* 添加灰色背景 */
  120. .list-header {
  121. padding: 16px;
  122. border-bottom: 1px solid #e5e7eb;
  123. display: flex;
  124. flex-direction: column;
  125. gap: 12px;
  126. background-color: #f5f5f5; /* 保持标题区域也是灰色背景 */
  127. h3 {
  128. margin: 0;
  129. font-size: 16px;
  130. }
  131. .new-chat-btn {
  132. padding: 8px 12px;
  133. background-color: #3b82f6;
  134. color: white;
  135. border: none;
  136. border-radius: 4px;
  137. cursor: pointer;
  138. font-size: 14px;
  139. display: flex;
  140. align-items: center;
  141. justify-content: center;
  142. &:hover {
  143. background-color: #2563eb;
  144. }
  145. }
  146. }
  147. .conversations {
  148. flex: 1;
  149. overflow-y: auto;
  150. .conversation-item {
  151. padding: 12px 16px;
  152. cursor: pointer;
  153. border-bottom: 1px solid #e5e7eb;
  154. &:hover {
  155. background-color: #eaeaea;
  156. }
  157. &.active {
  158. background-color: #e0e0e0;
  159. }
  160. .chat-title {
  161. font-size: 14px;
  162. margin-bottom: 4px;
  163. white-space: nowrap;
  164. overflow: hidden;
  165. text-overflow: ellipsis;
  166. }
  167. .chat-time {
  168. font-size: 12px;
  169. color: #6b7280;
  170. }
  171. }
  172. }
  173. }
  174. </style>