index.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <template>
  2. <div :class="{ chatGptChat: true, ortherP: !userData.UserId }" 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">
  7. <textShow :text="item.text" />
  8. </div>
  9. </div>
  10. <div v-if="item.text && item.type === 'user'" class="chat chatRight">
  11. <van-image
  12. width="35px"
  13. height="35px"
  14. fit="contain"
  15. :src="userData.UserAvatar || user"
  16. />
  17. <div class="cahtText" v-text="item.text"></div>
  18. </div>
  19. </div>
  20. <div class="loading" v-if="load">
  21. <div class="loadSun"></div>
  22. <div class="loadSun" style="animation-delay: 0.2s"></div>
  23. <div class="loadSun" style="animation-delay: 0.4s"></div>
  24. </div>
  25. <van-field
  26. v-model="inputText"
  27. rows="1"
  28. maxlength="200"
  29. autosize
  30. placeholder="请输入您的问题"
  31. type="textarea"
  32. @focus="focus"
  33. >
  34. <template #button>
  35. <van-button
  36. :loading="load"
  37. size="small"
  38. type="success"
  39. @click="saveText"
  40. >
  41. 发送
  42. </van-button>
  43. </template>
  44. </van-field>
  45. <shanshipin v-if="!userData.UserId" />
  46. </div>
  47. </template>
  48. <script setup>
  49. import shanshipin from '../../components/shanshipin.vue';
  50. import robot from '../../assets/img/chat/robot_2.png';
  51. import user from '../../assets/img/chat/user.png';
  52. import { showToast } from 'vant';
  53. import { ref, nextTick } from 'vue';
  54. import textShow from './textShow.vue';
  55. import { ChartGpt } from '@/api/chatGpt';
  56. // import { onMounted, reactive } from "vue";
  57. // import { isIpad, isIpod, isIphone } from "../../utils/isTerminal";
  58. /**
  59. * window.$originData.orginParames.title 页面标题
  60. * window.$originData.orginParames.parameters 固定参数值
  61. * window.$originData.urlParames url参数
  62. */
  63. console.log(window.$shanshipin);
  64. const userData = ref(window.$shanshipin || {});
  65. const chat = ref([]);
  66. const load = ref(false);
  67. const chatEle = ref(null);
  68. const inputText = ref('');
  69. if (!userData.value.UserId) {
  70. chat.value.push(
  71. ...[
  72. {
  73. text: '闪视频是什么?',
  74. type: 'user',
  75. },
  76. {
  77. text: '“闪视频”是全新打造的视听新媒体平台,专注于以视频形式传播陕西文化、讲好陕西故事,汇聚陕西省内党政机关、主流媒体、企事业单位内容资源,深度聚焦移动互联网时代的陕西省域文化建设,为全体关注陕西、热爱陕西的移动互联网用户提供了解陕西、展示自我的窗口和舞台,内容主要以视频内容为主。\n客服联系方式:\n联系电话:18502918086\n邮箱:service_ssp@163.com',
  78. type: 'robot',
  79. },
  80. ]
  81. );
  82. }
  83. function saveText() {
  84. if (!userData.value.UserId) return showToast('请下载闪视频进行完整体验');
  85. if (!inputText.value) return;
  86. load.value = true;
  87. nextTick(() => {
  88. // 滚动到最底层
  89. if (chatEle.value.scrollHeight > chatEle.value.clientHeight) {
  90. chatEle.value.scrollTop = chatEle.value.scrollHeight;
  91. }
  92. });
  93. let prompt = [];
  94. const startIndex = chat.value.length > 9 ? chat.value.length - 9 : 0;
  95. for (let i = startIndex; i < chat.value.length; i++) {
  96. prompt.push(chat.value[i]);
  97. }
  98. ChartGpt({
  99. prompt: [
  100. ...prompt,
  101. {
  102. text: inputText.value,
  103. type: 'user',
  104. },
  105. ],
  106. userId: userData.value.UserId || '',
  107. userName: userData.value.UserName || '',
  108. })
  109. .then(r => {
  110. load.value = false;
  111. chat.value.push({
  112. text: inputText.value,
  113. type: 'user',
  114. });
  115. inputText.value = '';
  116. chat.value.push({
  117. text: r.result || '',
  118. type: 'robot',
  119. });
  120. nextTick(() => {
  121. // 滚动到最底层
  122. if (chatEle.value.scrollHeight > chatEle.value.clientHeight) {
  123. chatEle.value.scrollTop = chatEle.value.scrollHeight;
  124. }
  125. });
  126. })
  127. .catch(() => {
  128. load.value = false;
  129. });
  130. }
  131. function focus() {
  132. chatEle.value.scrollTop = chatEle.value.scrollHeight;
  133. document.getElementById('van-field-6-input') &&
  134. document.getElementById('van-field-6-input').scrollIntoView();
  135. }
  136. </script>
  137. <style lang="scss">
  138. .chatGptChat {
  139. box-sizing: border-box;
  140. font-weight: 500;
  141. width: 100vw;
  142. height: 100vh;
  143. position: relative;
  144. padding-bottom: 55px;
  145. background: url('../../assets/img/chat/bg.png') no-repeat;
  146. background-size: 100% 10%;
  147. background-position: center center;
  148. overflow-y: auto;
  149. .van-field {
  150. background-color: #eee;
  151. position: fixed;
  152. padding-bottom: 1em;
  153. bottom: 0;
  154. left: 0;
  155. .van-field__control {
  156. background-color: #ffffff;
  157. padding: 0.4em;
  158. }
  159. }
  160. .chat {
  161. padding: 1em 1.5em;
  162. width: 100%;
  163. background-color: #f4f4f680;
  164. .cahtText {
  165. text-align: left;
  166. line-height: 1.5em;
  167. border-radius: 5px;
  168. padding: 0.5em;
  169. display: inline-block;
  170. max-width: calc(100% - 40px);
  171. vertical-align: middle;
  172. }
  173. .van-image {
  174. vertical-align: top;
  175. }
  176. .cahtText:not(:last-child),
  177. .van-image:not(:last-child) {
  178. margin-right: 5px;
  179. }
  180. }
  181. .chatRight {
  182. background: #ffffff80;
  183. }
  184. .loading {
  185. border-radius: 2em;
  186. background-color: #aaa;
  187. width: 3em;
  188. height: 1em;
  189. line-height: 1em;
  190. text-align: center;
  191. margin: 1em auto 0 auto;
  192. .loadSun {
  193. background-color: #ffffff;
  194. border-radius: 50%;
  195. width: 0.6em;
  196. height: 0.6em;
  197. display: inline-block;
  198. animation: 0.5s load infinite alternate;
  199. &:not(:last-child) {
  200. margin-right: 3px;
  201. }
  202. }
  203. }
  204. }
  205. .ortherP {
  206. padding-top: 70px;
  207. .shanshipin {
  208. top: 0;
  209. background-color: #eee;
  210. }
  211. }
  212. @keyframes load {
  213. from {
  214. background-color: #aaa;
  215. }
  216. to {
  217. background-color: #ffffff;
  218. }
  219. }
  220. </style>