index.vue 5.9 KB

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