index.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <template>
  2. <view class="container">
  3. <scroll-view class="chat-messages" scroll-y="true" :scroll-with-animation="true"
  4. :scroll-into-view="scrollToView" >
  5. <view v-for="(message, index) in messages" :key="index" :id="'msg' + index" style="clear: both;">
  6. <view :class="['message', message.sender === 'user' ? 'user-message' : 'ai-message']">
  7. <text>{{ message.content }}</text>
  8. </view>
  9. </view>
  10. </scroll-view>
  11. <view class="chat-input">
  12. <textarea @linechange="chatMsgMaxHeightChange" auto-height v-model="inputMessage" placeholder="输入消息..." class="input-field"/>
  13. <button class="send-button" @click="sendMessage">发送</button>
  14. </view>
  15. </view>
  16. </template>
  17. <script setup>
  18. import {
  19. ref,
  20. nextTick
  21. } from 'vue';
  22. const inputMessage = ref('');
  23. const messages = ref([{
  24. sender: 'ai',
  25. content: '您好,我是ai助手'
  26. },
  27. ]);
  28. const scrollToView = ref('');
  29. function sendMessage() {
  30. if (inputMessage.value.trim() === '') {
  31. return;
  32. }
  33. messages.value.push({
  34. sender: 'user',
  35. content: inputMessage.value
  36. });
  37. inputMessage.value = '';
  38. scrollToBottom();
  39. getAIResponse();
  40. };
  41. function getAIResponse() {
  42. // 这里调用AI客服接口,获取回复
  43. uni.request({
  44. url: 'http://api.qingyunke.com/api.php?key=free&appid=0&msg=' + messages.value[messages.value.length -
  45. 1].content,
  46. success: function(res) {
  47. messages.value.push({
  48. sender: 'ai',
  49. content: res.data.content
  50. });
  51. scrollToBottom();
  52. }
  53. })
  54. };
  55. function scrollToBottom() {
  56. nextTick(() => {
  57. scrollToView.value = 'msg' + (messages.value.length - 1);
  58. });
  59. };
  60. const $maxHeight = ref(0); // 初始化为0
  61. // 使用 onMounted 钩子在组件挂载后获取窗口高度
  62. // onMounted(() => {
  63. // const screenHeight = window.innerHeight; // 安全地获取当前窗口的高度
  64. // $maxHeight.value = screenHeight - 200; // 减去200,单位可以为px
  65. // });
  66. function chatMsgMaxHeightChange(e){
  67. console.log(e);
  68. }
  69. </script>
  70. <style scoped>
  71. .container {
  72. display: flex;
  73. flex-direction: column;
  74. height: 100vh;
  75. position: relative;
  76. }
  77. .chat-messages {
  78. flex: 1;
  79. padding: 10px;
  80. background-color: #f3f3f3;
  81. overflow-y: auto;
  82. /* 确保消息区域可以滚动 */
  83. max-height: calc(100vh - 200rpx);
  84. }
  85. .message {
  86. max-width: 70%;
  87. margin: 5px 0;
  88. padding: 8px;
  89. border-radius: 10px;
  90. display: inline-block;
  91. word-wrap: break-word;
  92. }
  93. .user-message {
  94. background-color: #DCF8C6;
  95. align-self: flex-end;
  96. float: right;
  97. margin-right: 40rpx;
  98. }
  99. .ai-message {
  100. background-color: #ECECEC;
  101. align-self: flex-start;
  102. }
  103. .chat-input {
  104. display: flex;
  105. padding: 10px;
  106. background-color: #FFFFFF;
  107. border-top: 1px solid #E0E0E0;
  108. position: absolute;
  109. bottom: 0;
  110. left: 0;
  111. right: 0;
  112. }
  113. .input-field {
  114. flex: 1;
  115. border: 1px solid #E0E0E0;
  116. border-radius: 20px;
  117. padding: 10px;
  118. margin-right: 10px;
  119. }
  120. .send-button {
  121. border: none;
  122. border-radius: 20px;
  123. padding: 0 20px;
  124. background-color: #007AFF;
  125. color: white;
  126. height: 90rpx;
  127. }
  128. </style>