index.vue 5.6 KB

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