SsInputBox.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. <script setup lang="ts">
  2. import {ref, watch, computed, onMounted, nextTick, defineEmits} from 'vue'
  3. import {getImageUrl} from '@/utils/common'
  4. import {isKioskDevice} from '@/utils/useDeviceDetection'
  5. import {Top} from '@element-plus/icons-vue'
  6. import {useGlobalStore} from '@/stores/global'
  7. import {ElMessage} from "element-plus";
  8. import type {ElInput} from 'element-plus';
  9. import {MessageApi} from '@/api/message'
  10. import SsChatSendMessage from "@/components/ss_chat/components/SsChatSendMessage.vue";
  11. import SsChatReply from "@/components/ss_chat/components/SsChatReply.vue";
  12. import SsRecording from "@/components/SsRecording.vue";
  13. const global = useGlobalStore()
  14. const scrollbarRef = ref<any>(null)
  15. const bottomRef = ref<HTMLElement | null>(null)
  16. const textareaRef = ref<InstanceType<typeof ElInput>>();
  17. const autoScroll = ref(true)
  18. const message = ref('')
  19. const conversationId = ref<string>(global.conversationId)
  20. const replyList = ref<any[]>([])
  21. const replyContent = ref()
  22. let scrollDirection = 'down'
  23. let scrollFlag = false;
  24. let beforeScrollTop = 0;
  25. const sendMessageFlag = computed(() => {
  26. let msg = message.value.trim();
  27. return msg == '' || global.inReply
  28. });
  29. const sendMessage = () => {
  30. const msg = message.value.trim();
  31. if (msg == '') {
  32. ElMessage.closeAll()
  33. ElMessage.info('请输入你想咨询的问题')
  34. } else {
  35. if (sendMessageFlag.value) {
  36. return
  37. }
  38. MessageApi.stop();
  39. // 设置状态参数
  40. global.setAudioChatId('');
  41. global.setSpread(true)
  42. global.setInReply(true)
  43. // 发送的消息
  44. replyList.value.push({
  45. type: 'send_message',
  46. content: msg,
  47. })
  48. message.value = ''
  49. scrollToBottom(true);
  50. // 增加回复消息容器
  51. replyContent.value = {
  52. chatId: '',
  53. source: [],
  54. avatar: '/images/robot-avatar.png',
  55. type: 'text',
  56. title: msg,
  57. content: ''
  58. }
  59. replyList.value.push(replyContent.value)
  60. MessageApi.sendMessage(msg, conversationId.value, (result: any) => {
  61. if (result.type == 'id') {
  62. replyContent.value.chatId = result.id
  63. conversationId.value = result.value
  64. global.setConversationId(result.value)
  65. }
  66. if (result.type == 'text') {
  67. replyContent.value.content += result.value
  68. scrollToBottom()
  69. }
  70. if (result.type == 'source') {
  71. replyContent.value.source = result.value
  72. scrollToBottom()
  73. }
  74. if (result.type == 'completed' || result.type == 'done') {
  75. replyContent.value = JSON.parse(JSON.stringify(replyContent.value))
  76. global.setInReply(false)
  77. MessageApi.stop()
  78. }
  79. if (result.type == 'error') {
  80. global.setInReply(false)
  81. MessageApi.stop()
  82. }
  83. })
  84. }
  85. }
  86. const stopInReply = () => {
  87. MessageApi.stop()
  88. global.setInReply(false)
  89. }
  90. const calculateLines = async (value: any) => {
  91. await nextTick(); // 确保 DOM 更新
  92. const textarea = textareaRef.value?.textarea as HTMLTextAreaElement | undefined;
  93. if (!textarea) return;
  94. // 获取行高(假设未自定义样式,默认值通常是 20px)
  95. const lineHeight = parseInt(getComputedStyle(textarea).lineHeight) || 20;
  96. // 计算实际行数
  97. const lines = Math.round(textarea.scrollHeight / lineHeight);
  98. if (value.length >= 200) {
  99. ElMessage.closeAll();
  100. ElMessage.warning('最多输入200个字')
  101. }
  102. global.setInputLine(lines);
  103. };
  104. const editMessage = (msg: string) => {
  105. message.value = msg
  106. inputFocusAndSelectAll()
  107. }
  108. const quickSend = (msg: string) => {
  109. message.value = msg
  110. scrollToBottom(true)
  111. sendMessage()
  112. }
  113. const loadService = (categoryId: number, title: string, avatar: string) => {
  114. if (global.inReply) {
  115. return
  116. }
  117. scrollToBottom(true);
  118. global.setAudioChatId('');
  119. global.setInReply(true)
  120. global.setSpread(true)
  121. replyContent.value = {
  122. type: 'category',
  123. avatar: getImageUrl(avatar),
  124. title: title,
  125. content: categoryId
  126. }
  127. let [last] = replyList.value.slice(-1);
  128. if (last) {
  129. if (typeof last.content === 'number') {
  130. if (last.content != categoryId) {
  131. replyList.value.push(replyContent.value)
  132. } else {
  133. global.setInReply(false)
  134. }
  135. } else {
  136. replyList.value.push(replyContent.value)
  137. global.setInReply(false)
  138. }
  139. } else {
  140. replyList.value.push(replyContent.value)
  141. }
  142. }
  143. const loadCommonProblem = () => {
  144. if (global.inReply) {
  145. return
  146. }
  147. scrollToBottom(true);
  148. global.setAudioChatId('');
  149. global.setInReply(true)
  150. global.setSpread(true)
  151. replyContent.value = {
  152. type: 'problem',
  153. avatar: '/images/robot-avatar.png',
  154. title: '常见问题',
  155. content: ''
  156. }
  157. let [last] = replyList.value.slice(-1);
  158. if (last) {
  159. if (last.type != 'problem') {
  160. replyList.value.push(replyContent.value)
  161. }
  162. } else {
  163. replyList.value.push(replyContent.value)
  164. }
  165. global.setInReply(false)
  166. }
  167. const inputFocusAndSelectAll = () => {
  168. nextTick(() => {
  169. if (textareaRef.value) {
  170. textareaRef.value.focus()
  171. textareaRef.value.select()
  172. }
  173. })
  174. }
  175. const setMessage = (msg: string) => {
  176. message.value = msg
  177. inputFocusAndSelectAll()
  178. }
  179. // 滚动到底部
  180. const scrollToBottom = (force = false) => {
  181. nextTick(() => {
  182. if (global.spread && autoScroll.value && scrollDirection == 'down' || force) {
  183. bottomRef.value?.scrollIntoView({behavior: 'smooth'})
  184. }
  185. })
  186. }
  187. // 处理滚动事件
  188. const handleScroll = ({scrollTop}: any) => {
  189. if (scrollbarRef.value) {
  190. if (scrollFlag && beforeScrollTop > scrollTop) {
  191. scrollDirection='up'
  192. } else {
  193. scrollDirection='down'
  194. }
  195. let clientHeight = scrollbarRef.value?.$el?.clientHeight ?? 0;
  196. let scrollHeight = scrollbarRef.value?.wrapRef?.scrollHeight ?? 0;
  197. // 如果用户手动向上滚动,暂停自动滚动
  198. autoScroll.value = scrollTop + clientHeight >= scrollHeight - 50;
  199. beforeScrollTop = scrollTop;
  200. }
  201. }
  202. const loaded = () => {
  203. global.setInReply(false)
  204. scrollToBottom()
  205. }
  206. const emit = defineEmits(['loaded'])
  207. onMounted(() => {
  208. scrollToBottom()
  209. emit('loaded', 'input_box')
  210. MessageApi.stop()
  211. })
  212. watch(replyList, () => {
  213. scrollToBottom()
  214. }, {deep: true})
  215. watch(() => global.spread, (newVal, oldVal) => {
  216. if (newVal) {
  217. setTimeout(() => {
  218. scrollFlag = true
  219. }, 500)
  220. }
  221. })
  222. defineExpose({
  223. editMessage,
  224. quickSend,
  225. loadService,
  226. loadCommonProblem
  227. })
  228. </script>
  229. <template>
  230. <div class="chat-container" :class="{spread: global.spread, 'kiosk-chat': isKioskDevice() && global.spread}">
  231. <!-- 立式一体机模式:输入框在顶部 -->
  232. <div v-if="isKioskDevice() && global.spread" class="input-box input-box-top" :class="{spread: global.spread}">
  233. <div class="input-container hover-scale scale101">
  234. <el-input ref="textareaRef" v-model="message" class="input-container-input" placeholder="请输入你想咨询的问题..."
  235. :type="global.spread ? 'textarea' : 'text'"
  236. :autosize="{ minRows: 1, maxRows: 3 }"
  237. @keyup.enter="sendMessage"
  238. maxlength="200"
  239. @input="calculateLines"
  240. @change="calculateLines"
  241. />
  242. <div class="input-buttons">
  243. <div class="input-buttons-limit">{{ message.length }}/200</div>
  244. <div class="input-buttons-right">
  245. <ss-recording @set-message="setMessage"/>
  246. <div class="buttons-separate"></div>
  247. <el-button class="send-button" v-if="global.inReply" circle @click="stopInReply">
  248. <div class="stop-block"></div>
  249. </el-button>
  250. <el-button class="send-button" v-else :icon="Top" circle @click="sendMessage"/>
  251. </div>
  252. </div>
  253. </div>
  254. </div>
  255. <el-scrollbar ref="scrollbarRef" class="ss-chat" @scroll="handleScroll">
  256. <div class="ss-chat-container">
  257. <template v-for="(item, idx) in replyList">
  258. <ss-chat-send-message v-if="item.type == 'send_message'" :message="item.content" @edit-message="editMessage"/>
  259. <ss-chat-reply v-else :reply="item" @loaded="loaded" @quick-send="quickSend" :last="replyList.length <= idx + 1"/>
  260. </template>
  261. </div>
  262. <div ref="bottomRef"></div>
  263. <div style="height: 1rem"></div>
  264. </el-scrollbar>
  265. <!-- 非立式一体机模式:输入框在底部 -->
  266. <div v-if="!(isKioskDevice() && global.spread)" class="input-box" :class="{spread: global.spread}">
  267. <div class="input-container hover-scale scale101">
  268. <el-input ref="textareaRef" v-model="message" class="input-container-input" placeholder="请输入你想咨询的问题..."
  269. :type="global.spread ? 'textarea' : 'text'"
  270. :autosize="{ minRows: 1, maxRows: 3 }"
  271. @keyup.enter="sendMessage"
  272. maxlength="200"
  273. @input="calculateLines"
  274. @change="calculateLines"
  275. />
  276. <div class="input-buttons">
  277. <div class="input-buttons-limit">{{ message.length }}/200</div>
  278. <div class="input-buttons-right">
  279. <ss-recording @set-message="setMessage"/>
  280. <div class="buttons-separate"></div>
  281. <el-button class="send-button" v-if="global.inReply" circle @click="stopInReply">
  282. <div class="stop-block"></div>
  283. </el-button>
  284. <el-button class="send-button" v-else :icon="Top" circle @click="sendMessage"/>
  285. </div>
  286. </div>
  287. </div>
  288. </div>
  289. </div>
  290. </template>
  291. <style scoped lang="scss">
  292. @keyframes rotation {
  293. 0% {
  294. transform: translate(-50%, -50%) rotate(0deg);
  295. }
  296. 100% {
  297. transform: translate(-50%, -50%) rotate(360deg);
  298. }
  299. }
  300. .chat-container {
  301. position: relative;
  302. transition: all 0.3s linear;
  303. &.spread {
  304. height: 100%;
  305. .ss-chat {
  306. height: 100%;
  307. }
  308. }
  309. .ss-chat {
  310. position: relative;
  311. height: 0;
  312. overflow: hidden;
  313. transition: all 0.3s linear;
  314. &::after, &:after {
  315. content: "";
  316. position: absolute;
  317. bottom: 0;
  318. height: 2rem;
  319. width: 100%;
  320. z-index: 100;
  321. background: linear-gradient(to bottom, rgba(255, 255, 255, 0), rgba(255, 255, 255, 1));
  322. }
  323. .ss-chat-container {
  324. max-width: 768px;
  325. margin: 0 auto;
  326. height: 100%;
  327. padding: 0 1rem;
  328. position: relative;
  329. }
  330. }
  331. .input-box {
  332. margin-bottom: 3rem;
  333. margin-right: 1rem;
  334. margin-left: 1rem;
  335. transition: all .3s linear;
  336. .input-container {
  337. max-width: 768px;
  338. margin: 0 auto;
  339. display: flex;
  340. justify-content: space-between;
  341. padding: 1.25rem 1rem;
  342. border-radius: var(--border-radius);
  343. position: relative;
  344. overflow: hidden;
  345. box-shadow: 0 0.25rem 0.44rem 0 rgba(0, 0, 0, 0.08);
  346. min-height: var(--min-touch-target);
  347. &::before, &:before {
  348. content: "";
  349. position: absolute;
  350. width: 110%;
  351. padding-top: 110%;
  352. top: 50%;
  353. left: 50%;
  354. z-index: -2;
  355. transform: translate(-50%, -50%);
  356. background: linear-gradient(45deg, #FFA37C 30%, #FF7BB0 40%, #AD8AFE 60%, #73B9FF 70%);
  357. animation: rotation 10s infinite linear;
  358. }
  359. &::after, &:after {
  360. content: "";
  361. position: absolute;
  362. top: 2px;
  363. left: 2px;
  364. bottom: 2px;
  365. right: 2px;
  366. z-index: -1;
  367. border-radius: calc(var(--border-radius) - 0.05rem);
  368. background-color: #ffffff;
  369. }
  370. .input-container-input {
  371. width: calc(100% - 6rem);
  372. border: 0;
  373. background-color: transparent;
  374. font-size: var(--body-font-size);
  375. &::-webkit-input-placeholder {
  376. color: #AEAEB0;
  377. }
  378. &::-moz-placeholder {
  379. color: #AEAEB0;
  380. }
  381. &:-ms-input-placeholder {
  382. color: #AEAEB0;
  383. }
  384. &:-moz-placeholder {
  385. color: #AEAEB0;
  386. }
  387. }
  388. .input-buttons {
  389. display: flex;
  390. align-items: flex-end;
  391. font-size: 1.2rem;
  392. .input-buttons-limit {
  393. font-size: 0.88rem;
  394. color: #7E8AA2;
  395. display: none;
  396. }
  397. .input-buttons-right {
  398. display: flex;
  399. align-items: center;
  400. .microphone {
  401. cursor: pointer;
  402. color: #101333;
  403. }
  404. .buttons-separate {
  405. background-color: #D5D6D8;
  406. height: 1rem;
  407. width: 1px;
  408. margin: 0 1rem;
  409. }
  410. .send-button {
  411. position: relative;
  412. background-color: #2942D4;
  413. padding: 0.5rem;
  414. color: #ffffff;
  415. border: 0;
  416. font-size: 1.2rem;
  417. min-width: var(--min-touch-target);
  418. min-height: var(--min-touch-target);
  419. .stop-block {
  420. width: 1rem;
  421. height: 1rem;
  422. border-radius: 0.2rem;
  423. background-color: #ffffff;
  424. }
  425. }
  426. }
  427. }
  428. }
  429. &.position-1 {
  430. position: absolute;
  431. z-index: 200;
  432. width: calc(100% - 2rem);
  433. }
  434. &.bottom {
  435. bottom: 0 !important;
  436. }
  437. &.spread {
  438. .input-container {
  439. display: block;
  440. padding: 0.75rem 1rem;
  441. .input-container-input {
  442. width: 100%;
  443. }
  444. .input-buttons {
  445. .input-buttons-limit {
  446. display: block;
  447. }
  448. justify-content: space-between;
  449. }
  450. }
  451. }
  452. }
  453. }
  454. /* 1080x1920 中等尺寸设备 - 输入框优化 */
  455. @media screen and (min-width: 751px) and (max-width: 1200px) and (min-height: 1700px) {
  456. .chat-container {
  457. /* 定义统一的内容区域宽度:与顶部导航栏对齐 */
  458. --kiosk-content-padding: 2rem;
  459. --kiosk-content-width: calc(100vw - var(--kiosk-content-padding) * 2);
  460. .ss-chat {
  461. /* 聊天容器:与顶部导航栏内容区域对齐 */
  462. .ss-chat-container {
  463. max-width: var(--kiosk-content-width);
  464. width: 100%;
  465. margin: 0 auto;
  466. padding: 0;
  467. }
  468. }
  469. .input-box {
  470. margin-bottom: 4rem;
  471. margin-right: 0;
  472. margin-left: 0;
  473. .input-container {
  474. max-width: var(--kiosk-content-width); /* 与顶部导航栏内容区域对齐 */
  475. width: 100%;
  476. margin: 0 auto;
  477. padding: 1.75rem 1.5rem;
  478. box-shadow: 0 0.5rem 1rem 0 rgba(0, 0, 0, 0.12);
  479. /* 增大输入框文字大小 */
  480. .input-container-input {
  481. font-size: 1.125rem;
  482. }
  483. .input-buttons {
  484. font-size: 1.5rem;
  485. .input-buttons-limit {
  486. font-size: 1.125rem; /* 1rem → 1.125rem */
  487. }
  488. .input-buttons-right {
  489. .buttons-separate {
  490. height: 1.5rem;
  491. margin: 0 1.25rem;
  492. }
  493. .send-button {
  494. padding: 0.75rem;
  495. font-size: 1.5rem;
  496. .stop-block {
  497. width: 1.25rem;
  498. height: 1.25rem;
  499. }
  500. }
  501. }
  502. }
  503. }
  504. &.spread {
  505. .input-container {
  506. padding: 1rem 1.5rem;
  507. }
  508. }
  509. }
  510. /* 立式一体机对话模式:输入框固定在对话区顶部 */
  511. &.kiosk-chat {
  512. display: flex;
  513. flex-direction: column;
  514. .input-box-top {
  515. order: 1;
  516. flex-shrink: 0;
  517. margin-top: 2rem;
  518. margin-bottom: 1.5rem;
  519. }
  520. .ss-chat {
  521. order: 2;
  522. flex: 1;
  523. height: auto !important;
  524. }
  525. }
  526. }
  527. }
  528. @media screen and (max-width: 750px) {
  529. .chat-container {
  530. .input-box {
  531. padding: 0 1.25rem;
  532. margin-bottom: 2rem;
  533. &.position-1 {
  534. width: 100%;
  535. }
  536. .input-container {
  537. padding: 0.56rem 0.75rem;
  538. border-radius: 0.75rem;
  539. &::after, &:after {
  540. border-radius: 0.6rem;
  541. }
  542. .input-container-input {
  543. width: calc(100% - 5rem);
  544. }
  545. .input-buttons {
  546. font-size: 1rem;
  547. .buttons-separate {
  548. margin: 0 0.9rem;
  549. }
  550. .send-button {
  551. font-size: 1rem;
  552. }
  553. }
  554. }
  555. }
  556. }
  557. }
  558. </style>