index.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <template>
  2. <div class="chatGptChat" ref="chatEle">
  3. <!-- <div v-for="(item, index) in chat" :key="index">
  4. <div v-if="item.text && item.type === 'robot'" class="chat">
  5. <van-image width="35px" height="35px" fit="contain" :src="robot" />
  6. <div class="cahtText" v-html="item.text">
  7. </div>
  8. </div>
  9. <div v-if="item.text && item.type === 'user'" class="chat chatRight">
  10. <div class="cahtText" v-text="item.text"></div>
  11. <van-image width="35px" height="35px" fit="contain" :src="user" />
  12. </div>
  13. </div> -->
  14. <div v-for="(item, index) in chat" :key="index">
  15. <div v-if="item.text && item.type === 'robot'" class="chat">
  16. <van-image width="35px" height="35px" fit="contain" :src="robot" />
  17. <div class="cahtText">
  18. <textShow :text="item.text" />
  19. </div>
  20. </div>
  21. <div v-if="item.text && item.type === 'user'" class="chat chatRight">
  22. <van-image width="35px" height="35px" fit="contain" :src="userData.UserAvatar || user" />
  23. <div class="cahtText" v-text="item.text"></div>
  24. </div>
  25. </div>
  26. <div class="loading" v-if="load">
  27. <div class="loadSun"></div>
  28. <div class="loadSun" style="animation-delay: 0.2s"></div>
  29. <div class="loadSun" style="animation-delay: 0.4s"></div>
  30. </div>
  31. <van-field
  32. v-model="inputText"
  33. rows="1"
  34. maxlength="200"
  35. autosize
  36. placeholder="请输入您的问题"
  37. type="textarea"
  38. >
  39. <template #button>
  40. <van-button
  41. :loading="load"
  42. size="small"
  43. type="success"
  44. @click="saveText"
  45. >
  46. 发送
  47. </van-button>
  48. </template>
  49. </van-field>
  50. </div>
  51. </template>
  52. <script setup>
  53. import robot from '../../assets/img/chat/robot_2.png';
  54. import user from '../../assets/img/chat/user.png';
  55. import { ref, nextTick } from 'vue';
  56. import textShow from './textShow.vue';
  57. import { ChartGpt } from '@/api/chatGpt';
  58. // import { onMounted, reactive } from "vue";
  59. // import { isIpad, isIpod, isIphone } from "../../utils/isTerminal";
  60. /**
  61. * window.$originData.orginParames.title 页面标题
  62. * window.$originData.orginParames.parameters 固定参数值
  63. * window.$originData.urlParames url参数
  64. */
  65. console.log(window.$shanshipin);
  66. const userData = ref(window.$shanshipin || {});
  67. const chat = ref([]);
  68. const load = ref(false);
  69. const chatEle = ref(null);
  70. const inputText = ref('');
  71. function saveText() {
  72. if (!inputText.value) return;
  73. chat.value.push({
  74. text: inputText.value,
  75. type: 'user',
  76. });
  77. load.value = true;
  78. nextTick(() => {
  79. // 滚动到最底层
  80. if (chatEle.value.scrollHeight > chatEle.value.clientHeight) {
  81. chatEle.value.scrollTop = chatEle.value.scrollHeight;
  82. }
  83. });
  84. inputText.value = '';
  85. let prompt = [];
  86. const startIndex = chat.value.length > 10 ? chat.value.length - 10 : 0;
  87. for (let i = startIndex; i < chat.value.length; i++) {
  88. prompt.push(chat.value[i]);
  89. }
  90. ChartGpt({
  91. prompt,
  92. userId: userData.value.UserId || '',
  93. userName: userData.value.UserName || '',
  94. })
  95. .then(r => {
  96. console.log(r);
  97. load.value = false;
  98. chat.value.push({
  99. text: (r.result || ''),
  100. type: 'robot',
  101. });
  102. nextTick(() => {
  103. // 滚动到最底层
  104. if (chatEle.value.scrollHeight > chatEle.value.clientHeight) {
  105. chatEle.value.scrollTop = chatEle.value.scrollHeight;
  106. }
  107. });
  108. })
  109. .catch(() => {
  110. load.value = false;
  111. });
  112. }
  113. </script>
  114. <style lang="scss">
  115. .chatGptChat {
  116. box-sizing: border-box;
  117. font-weight: 500;
  118. width: 100vw;
  119. height: 100vh;
  120. position: relative;
  121. padding-bottom: 55px;
  122. background-color: #ffffff;
  123. overflow-y: auto;
  124. .van-field {
  125. background-color: #eee;
  126. position: fixed;
  127. padding-bottom: 1em;
  128. bottom: 0;
  129. left: 0;
  130. .van-field__control {
  131. background-color: #ffffff;
  132. padding: 0.4em;
  133. }
  134. }
  135. .chat {
  136. padding: 1em 1.5em;
  137. width: 100%;
  138. background-color: #f4f4f6;
  139. .cahtText {
  140. text-align: left;
  141. line-height: 1.5em;
  142. border-radius: 5px;
  143. padding: 0.5em;
  144. display: inline-block;
  145. max-width: calc(100% - 40px);
  146. vertical-align: middle;
  147. }
  148. .van-image {
  149. vertical-align: top;
  150. }
  151. .cahtText:not(:last-child),
  152. .van-image:not(:last-child) {
  153. margin-right: 5px;
  154. }
  155. }
  156. .chatRight {
  157. background: #ffffff;
  158. }
  159. .loading {
  160. border-radius: 2em;
  161. background-color: #aaa;
  162. width: 3em;
  163. height: 1em;
  164. line-height: 1em;
  165. text-align: center;
  166. margin: 1em auto 0 auto;
  167. .loadSun {
  168. background-color: #ffffff;
  169. border-radius: 50%;
  170. width: 0.6em;
  171. height: 0.6em;
  172. display: inline-block;
  173. animation: 0.5s load infinite alternate;
  174. &:not(:last-child) {
  175. margin-right: 3px;
  176. }
  177. }
  178. }
  179. }
  180. @keyframes load {
  181. from {
  182. background-color: #aaa;
  183. }
  184. to {
  185. background-color: #ffffff;
  186. }
  187. }
  188. </style>