Chat.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <template>
  2. <div class="chat-container">
  3. <div class="chat-header">
  4. <h2>蜀道安全管理AI智能助手</h2>
  5. </div>
  6. <div class="chat-messages" ref="messagesContainer">
  7. <div v-for="(msg, index) in messages" :key="index" :class="['message', msg.role]">
  8. <div class="avatar">{{ msg.role === 'user' ? '👤' : '🤖' }}</div>
  9. <div class="content">
  10. <div v-if="msg.role === 'ai'" v-html="renderMarkdown(msg.content)" class="markdown-content"></div>
  11. <div v-else>{{ msg.content }}</div>
  12. <div class="message-actions" v-if="msg.role === 'ai'">
  13. <button @click="copyMessage(msg.content)" class="action-btn">📋 复制</button>
  14. </div>
  15. </div>
  16. </div>
  17. <div v-if="isLoading" class="message ai">
  18. <div class="avatar">🤖</div>
  19. <div class="content">
  20. <div class="typing-indicator">思考中...</div>
  21. </div>
  22. </div>
  23. </div>
  24. <div class="chat-input-area">
  25. <div class="quick-questions" v-if="messages.length === 0">
  26. <div class="section-title">常见问题</div>
  27. <div class="question-list">
  28. <button
  29. v-for="q in quickQuestions"
  30. :key="q"
  31. @click="sendQuickQuestion(q)"
  32. class="quick-btn"
  33. >{{ q }}</button>
  34. </div>
  35. </div>
  36. <div class="input-wrapper">
  37. <textarea
  38. v-model="inputMessage"
  39. @keydown.enter.exact.prevent="sendMessage"
  40. placeholder="请输入您的问题..."
  41. :disabled="isLoading"
  42. rows="1"
  43. ></textarea>
  44. <button
  45. @click="sendMessage"
  46. :disabled="!inputMessage.trim() || isLoading"
  47. class="send-btn"
  48. >发送</button>
  49. </div>
  50. </div>
  51. </div>
  52. </template>
  53. <script setup>
  54. import { ref, nextTick, watch } from 'vue'
  55. import { marked } from 'marked'
  56. import { getMockResponse, getQuickQuestions } from '@/mock/chatMock'
  57. const messages = ref([])
  58. const inputMessage = ref('')
  59. const isLoading = ref(false)
  60. const messagesContainer = ref(null)
  61. const quickQuestions = ref(getQuickQuestions())
  62. const renderMarkdown = (content) => {
  63. return marked.parse(content)
  64. }
  65. const scrollToBottom = async () => {
  66. await nextTick()
  67. if (messagesContainer.value) {
  68. messagesContainer.value.scrollTop = messagesContainer.value.scrollHeight
  69. }
  70. }
  71. const sendMessage = async () => {
  72. const message = inputMessage.value.trim()
  73. if (!message || isLoading.value) return
  74. messages.value.push({ role: 'user', content: message })
  75. inputMessage.value = ''
  76. isLoading.value = true
  77. await scrollToBottom()
  78. // Mock AI response
  79. setTimeout(async () => {
  80. const response = getMockResponse(message)
  81. messages.value.push({ role: 'ai', content: response })
  82. isLoading.value = false
  83. await scrollToBottom()
  84. }, 1000)
  85. }
  86. const sendQuickQuestion = (question) => {
  87. inputMessage.value = question
  88. sendMessage()
  89. }
  90. const copyMessage = async (content) => {
  91. await navigator.clipboard.writeText(content)
  92. }
  93. </script>
  94. <style scoped lang="scss">
  95. .chat-container {
  96. display: flex;
  97. flex-direction: column;
  98. height: 100%;
  99. background: #f8f9fa;
  100. }
  101. .chat-header {
  102. padding: 20px 24px;
  103. background: #fff;
  104. border-bottom: 1px solid #e8e8e8;
  105. h2 {
  106. font-size: 18px;
  107. font-weight: 500;
  108. color: #333;
  109. }
  110. }
  111. .chat-messages {
  112. flex: 1;
  113. overflow-y: auto;
  114. padding: 24px;
  115. .message {
  116. display: flex;
  117. margin-bottom: 20px;
  118. gap: 12px;
  119. &.user {
  120. flex-direction: row-reverse;
  121. }
  122. .avatar {
  123. font-size: 32px;
  124. flex-shrink: 0;
  125. }
  126. .content {
  127. max-width: 70%;
  128. padding: 12px 16px;
  129. border-radius: 12px;
  130. background: #fff;
  131. box-shadow: 0 1px 2px rgba(0,0,0,0.05);
  132. .markdown-content {
  133. :deep(p) {
  134. margin-bottom: 8px;
  135. }
  136. :deep(p:last-child) {
  137. margin-bottom: 0;
  138. }
  139. }
  140. }
  141. &.ai .content {
  142. background: #fff;
  143. }
  144. &.user .content {
  145. background: #1890ff;
  146. color: #fff;
  147. }
  148. .message-actions {
  149. margin-top: 8px;
  150. padding-top: 8px;
  151. border-top: 1px solid #f0f0f0;
  152. .action-btn {
  153. background: none;
  154. border: none;
  155. color: #999;
  156. cursor: pointer;
  157. font-size: 12px;
  158. padding: 4px 8px;
  159. &:hover {
  160. color: #666;
  161. }
  162. }
  163. }
  164. }
  165. .typing-indicator {
  166. color: #999;
  167. font-style: italic;
  168. }
  169. }
  170. .chat-input-area {
  171. padding: 20px 24px;
  172. background: #fff;
  173. border-top: 1px solid #e8e8e8;
  174. }
  175. .quick-questions {
  176. margin-bottom: 16px;
  177. .section-title {
  178. font-size: 14px;
  179. color: #666;
  180. margin-bottom: 12px;
  181. }
  182. .question-list {
  183. display: flex;
  184. flex-wrap: wrap;
  185. gap: 8px;
  186. .quick-btn {
  187. padding: 8px 16px;
  188. border: 1px solid #d9d9d9;
  189. border-radius: 20px;
  190. background: #fff;
  191. cursor: pointer;
  192. font-size: 13px;
  193. color: #666;
  194. transition: all 0.2s;
  195. &:hover {
  196. border-color: #1890ff;
  197. color: #1890ff;
  198. }
  199. }
  200. }
  201. }
  202. .input-wrapper {
  203. display: flex;
  204. gap: 12px;
  205. align-items: flex-end;
  206. textarea {
  207. flex: 1;
  208. padding: 12px 16px;
  209. border: 1px solid #d9d9d9;
  210. border-radius: 8px;
  211. resize: none;
  212. font-size: 14px;
  213. line-height: 1.5;
  214. outline: none;
  215. transition: border-color 0.2s;
  216. &:focus {
  217. border-color: #1890ff;
  218. }
  219. &:disabled {
  220. background: #f5f5f5;
  221. }
  222. }
  223. .send-btn {
  224. padding: 12px 24px;
  225. background: #1890ff;
  226. color: #fff;
  227. border: none;
  228. border-radius: 8px;
  229. cursor: pointer;
  230. font-size: 14px;
  231. transition: background 0.2s;
  232. &:hover:not(:disabled) {
  233. background: #40a9ff;
  234. }
  235. &:disabled {
  236. background: #d9d9d9;
  237. cursor: not-allowed;
  238. }
  239. }
  240. }
  241. </style>