index.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 { createSocket } from '@/utils/socket';
  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 { isIpad, isIpod, isIphone } from '../../utils/isTerminal';
  56. import textShow from './textShow.vue';
  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. let chatlist = ref([]);
  71. if (!userData.value.UserId) {
  72. chat.value.push(
  73. ...[
  74. {
  75. text: '闪视频是什么?',
  76. type: 'user',
  77. },
  78. {
  79. text: '“闪视频”是全新打造的视听新媒体平台,专注于以视频形式传播陕西文化、讲好陕西故事,汇聚陕西省内党政机关、主流媒体、企事业单位内容资源,深度聚焦移动互联网时代的陕西省域文化建设,为全体关注陕西、热爱陕西的移动互联网用户提供了解陕西、展示自我的窗口和舞台,内容主要以视频内容为主。\n客服联系方式:\n联系电话:18502918086\n邮箱:service_ssp@163.com',
  80. type: 'robot',
  81. },
  82. ]
  83. );
  84. }
  85. function saveText() {
  86. if (!userData.value.UserId) return showToast('请下载闪视频进行完整体验');
  87. if (!inputText.value) return;
  88. load.value = true;
  89. nextTick(() => {
  90. // 滚动到最底层
  91. if (chatEle.value.scrollHeight > chatEle.value.clientHeight) {
  92. chatEle.value.scrollTop = chatEle.value.scrollHeight;
  93. }
  94. });
  95. chat.value.push({
  96. text: inputText.value,
  97. type: 'user',
  98. });
  99. chat.value.push({
  100. text: '',
  101. type: 'robot',
  102. });
  103. const idnex = chatlist.value.length === 0 ? 0 : chatlist.value.length - 1;
  104. const t = inputText.value;
  105. inputText.value = '';
  106. createSocket(
  107. ws => {
  108. const data = ws.data || {};
  109. let session_hash = Math.random().toString(36).substring(2);
  110. if (data.msg == 'send_hash')
  111. ws.ws.send(
  112. JSON.stringify({
  113. fn_index: idnex,
  114. session_hash,
  115. })
  116. );
  117. if (data.msg == 'send_data')
  118. ws.ws.send(
  119. JSON.stringify({
  120. fn_index: idnex,
  121. data: [t, chatlist.value, 2048, 0.7, 0.95, null],
  122. event_data: null,
  123. session_hash,
  124. })
  125. );
  126. if (data.msg == 'process_generating') {
  127. const li = data.output.data[0] || [];
  128. const olist = li[li.length - 1];
  129. chat.value[chat.value.length - 1].text = olist[1];
  130. nextTick(() => {
  131. // 滚动到最底层
  132. if (chatEle.value.scrollHeight > chatEle.value.clientHeight) {
  133. chatEle.value.scrollTop = chatEle.value.scrollHeight;
  134. }
  135. });
  136. }
  137. if (data.msg == 'process_completed') {
  138. chatlist.value = data.output.data[0] || [];
  139. load.value = false;
  140. nextTick(() => {
  141. // 滚动到最底层
  142. if (chatEle.value.scrollHeight > chatEle.value.clientHeight) {
  143. chatEle.value.scrollTop = chatEle.value.scrollHeight;
  144. }
  145. });
  146. }
  147. },
  148. () => {
  149. // ERROR
  150. load.value = false;
  151. }
  152. );
  153. }
  154. function focus() {
  155. if (!isIpad && !isIpod && !isIphone) return;
  156. !inputText.value && (inputText.value = ' ');
  157. }
  158. </script>
  159. <style lang="scss">
  160. .chatGptChat {
  161. box-sizing: border-box;
  162. font-weight: 500;
  163. width: 100vw;
  164. height: 100vh;
  165. position: relative;
  166. padding-bottom: 55px;
  167. overflow-y: auto;
  168. .van-field {
  169. background-color: #eee;
  170. position: fixed;
  171. padding-bottom: 1em;
  172. bottom: 0;
  173. left: 0;
  174. .van-field__control {
  175. background-color: #ffffff;
  176. padding: 0.4em;
  177. }
  178. }
  179. .chat {
  180. padding: 1em 1.5em;
  181. width: 100%;
  182. background-color: #f4f4f680;
  183. .cahtText {
  184. text-align: left;
  185. line-height: 1.5em;
  186. border-radius: 5px;
  187. padding: 0.5em;
  188. display: inline-block;
  189. max-width: calc(100% - 40px);
  190. vertical-align: middle;
  191. }
  192. .van-image {
  193. vertical-align: top;
  194. }
  195. .cahtText:not(:last-child),
  196. .van-image:not(:last-child) {
  197. margin-right: 5px;
  198. }
  199. }
  200. .chatRight {
  201. background: #ffffff80;
  202. }
  203. .loading {
  204. border-radius: 2em;
  205. background-color: #aaa;
  206. width: 3em;
  207. height: 1em;
  208. line-height: 1em;
  209. text-align: center;
  210. margin: 1em auto 0 auto;
  211. .loadSun {
  212. background-color: #ffffff;
  213. border-radius: 50%;
  214. width: 0.6em;
  215. height: 0.6em;
  216. display: inline-block;
  217. animation: 0.5s load infinite alternate;
  218. &:not(:last-child) {
  219. margin-right: 3px;
  220. }
  221. }
  222. }
  223. }
  224. .ortherP {
  225. padding-top: 70px;
  226. .shanshipin {
  227. top: 0;
  228. background-color: #eee;
  229. }
  230. }
  231. @keyframes load {
  232. from {
  233. background-color: #aaa;
  234. }
  235. to {
  236. background-color: #ffffff;
  237. }
  238. }
  239. </style>