index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. <template>
  2. <div class="chat bg-gray-900 comBg">
  3. <div class="flex flex-col items-center justify-center h-screen px-4">
  4. <!-- onClick={() => navigate('/')} -->
  5. <div
  6. class="text-white px-3 py-2 rounded-lg mb-4 absolute top-2 left-2"
  7. style="background-color: var(--bg-color1);"
  8. @click="goBack"
  9. >
  10. <!-- <i class="fas fa-chevron-left mr-2"></i> -->
  11. 返回
  12. </div>
  13. <div class="content">
  14. <div class="leftImg">
  15. <img :src="baseApi + info.picture" alt="">
  16. <div class="aiInfo">
  17. <span class="photo">
  18. <img :src="baseApi + info.picture">
  19. </span>
  20. <div class="info">
  21. <div class="name">{{ info.characterName }}</div>
  22. <div class="tags">
  23. <span class="tag" v-for="(item, index) in info.labelArr" :key="index">
  24. {{ item }}
  25. </span>
  26. </div>
  27. <div class="infoContent">{{ info.prologue }}</div>
  28. </div>
  29. </div>
  30. </div>
  31. <div class="chat-box">
  32. <div class="messages" ref="messages">
  33. <template v-for="(item, index) in returnMessage">
  34. <div v-if="item.role == 'assistant'" class="text-white mb-4 flex" :key="index">
  35. <!-- ai返回的信息 -->
  36. <div class="pt-2 photo">
  37. <img
  38. :src="baseApi + info.picture"
  39. class="rounded-full w-14 h-14 object-cover"
  40. />
  41. </div>
  42. <div class="message fex-1 ml-4 mt-2 p-2 rounded-r-md rounded-bl-md text-xs" >
  43. <div v-show="!item.content" class="loadingMessage" >
  44. <div v-loading="!item.content" element-loading-background="rgba(0, 0, 0, 0.0)"></div>
  45. </div>
  46. <p v-show="item.content">{{ item.content }} </p>
  47. </div>
  48. </div>
  49. <!-- 用户发送的信息 -->
  50. <div v-if="item.role == 'user'" class="text-white mb-4 flex me" :key="index">
  51. <div class="pt-2 photo">
  52. <img
  53. src="@/assets/images/default_avatar_user.png"
  54. class="rounded-full w-14 h-14 object-cover"
  55. />
  56. </div>
  57. <div class="message fex-1 mr-4 mt-2 p-2 rounded-l-md rounded-br-md text-xs">
  58. <p>{{ item.content }}</p>
  59. </div>
  60. </div>
  61. </template>
  62. </div>
  63. <div class="absolute left-2 right-2 bottom-4 ">
  64. <!-- <div class="flex items-center text-gray-400 text-xs mb-4 ">
  65. <span>@Leon S Kennedy - Resident Evil</span>
  66. </div> -->
  67. <div class="flex">
  68. <el-button class="buttonBg1 mButton" @click="newStart" style="margin-right: 5px;" type="primary" round icon="el-icon-plus"></el-button>
  69. <input
  70. type="text"
  71. placeholder="输入消息..."
  72. class="flex-1 py-2 px-4 rounded-l-lg text-sm text-white focus:outline-none"
  73. style="background: #ffffff0f; border: 1px solid #5b5b5e;"
  74. v-model="content"
  75. @keydown="Enterkey"
  76. />
  77. <button @click="getStreamChatWithWeb" class="buttonBg1 text-white px-4 rounded-r-lg text-sm">
  78. 发送
  79. </button>
  80. </div>
  81. </div>
  82. </div>
  83. </div>
  84. </div>
  85. </div>
  86. </template>
  87. <script>
  88. import { streamChatWithWebApi } from "@/api/chat.js"
  89. import { detailApi } from "@/api/detail.js"
  90. export default {
  91. data() {
  92. return {
  93. info: {},
  94. messageLoading: false,
  95. returnMessage: [],
  96. content: ''
  97. }
  98. },
  99. mounted() {
  100. if (this.$route.query.characterId) {
  101. this.getDetail(this.$route.query.characterId)
  102. }
  103. },
  104. watch: {
  105. returnMessage: {
  106. handler(newVal, lodVal) {
  107. this.$nextTick(() => {
  108. let messages = this.$refs.messages
  109. messages.scrollTop = messages.scrollHeight
  110. })
  111. },
  112. deep: true
  113. }
  114. },
  115. methods: {
  116. getDetail(id) {
  117. detailApi(id).then(res => {
  118. console.log(res, '角色详情');
  119. this.info = res.data
  120. let HistoryMessage = JSON.parse(localStorage.getItem(`[${123},${this.info.id}]`));
  121. if (HistoryMessage) {
  122. this.returnMessage = HistoryMessage
  123. } else {
  124. this.returnMessage.push({
  125. role: 'assistant',
  126. content: this.info.firstContent
  127. })
  128. localStorage.setItem(`[${123},${this.info.id}]`, JSON.stringify(this.returnMessage));
  129. }
  130. })
  131. },
  132. goBack() {
  133. this.$router.back()
  134. },
  135. newStart() {
  136. // 清空历史数据
  137. localStorage.removeItem(`[${123},${this.info.id}]`);
  138. this.returnMessage = []
  139. this.getDetail(this.info.id)
  140. },
  141. Enterkey(e) {
  142. if (e.keyCode == 13) {
  143. this.getStreamChatWithWeb()
  144. }
  145. },
  146. async getStreamChatWithWeb_old() {
  147. this.messageLoading = true
  148. if (!this.content) {
  149. return
  150. }
  151. this.returnMessage.push({
  152. role: 'user',
  153. content: this.content
  154. })
  155. let HistoryMessage = JSON.parse(localStorage.getItem(`[${123},${this.info.id}]`));
  156. // 历史记录取最新的20条传给后端
  157. if (HistoryMessage && HistoryMessage.length > 20) {
  158. HistoryMessage = HistoryMessage.slice(HistoryMessage.length - 1, 20)
  159. }
  160. let params = {
  161. historyMessage: HistoryMessage || [],
  162. characterId: this.info.id,
  163. prompt: JSON.parse(JSON.stringify(this.content))
  164. }
  165. // 清空输入框的值
  166. this.content = ''
  167. // 新增一条ai信息
  168. this.returnMessage.push({
  169. role: 'assistant',
  170. content: ''
  171. })
  172. let res = await streamChatWithWebApi(params)
  173. console.log(res.data.message.content, 'res');
  174. let content = res.data.message.content
  175. let index = 0
  176. let xing = 1
  177. let messageInterval = setInterval(() => {
  178. if (index > content.length - 1) {
  179. console.log('结束');
  180. // 消息全部显示后存到历史localStorage
  181. let HistoryMessage = []
  182. // 历史只存100条,长度超出截取最新的
  183. if (this.returnMessage.length > 100) {
  184. HistoryMessage = JSON.stringify(this.returnMessage.slice(this.returnMessage.length - 100, 100))
  185. } else {
  186. HistoryMessage = JSON.stringify(this.returnMessage)
  187. }
  188. localStorage.setItem(`[${123},${this.info.id}]`, HistoryMessage);
  189. clearInterval(messageInterval)
  190. return
  191. }
  192. console.log(content[index], 'content[index]');
  193. if (content[index] == "*") {
  194. if (xing == 1) {
  195. xing = 2
  196. this.returnMessage[this.returnMessage.length - 1].content += "("
  197. } else if (xing == 2) {
  198. xing = 1
  199. this.returnMessage[this.returnMessage.length - 1].content += ")"
  200. }
  201. } else {
  202. this.returnMessage[this.returnMessage.length - 1].content += content[index]
  203. }
  204. index += 1
  205. }, 50)
  206. },
  207. async getStreamChatWithWeb() {
  208. this.messageLoading = true
  209. if (!this.content) {
  210. return
  211. }
  212. this.returnMessage.push({
  213. role: 'user',
  214. content: this.content
  215. })
  216. let HistoryMessage = JSON.parse(localStorage.getItem(`[${123},${this.info.id}]`));
  217. // 历史记录取最新的20条传给后端
  218. if (HistoryMessage && HistoryMessage.length > 20) {
  219. HistoryMessage = HistoryMessage.slice(HistoryMessage.length - 1, 20)
  220. }
  221. let params = {
  222. HistoryMessage: HistoryMessage || [],
  223. characterId: this.info.id,
  224. prompt: this.content
  225. }
  226. // 新增一条ai信息
  227. this.returnMessage.push({
  228. role: 'assistant',
  229. content: ''
  230. })
  231. // 清空输入框的值
  232. this.content = ''
  233. let res = await streamChatWithWebApi(params)
  234. console.log(res, 'res');
  235. this.messageLoading = false
  236. if (res.status != 200) {
  237. this.$message.error(res.statusText)
  238. this.returnMessage.splice(this.returnMessage.length - 1, 1)
  239. return
  240. }
  241. const reader = res.body.getReader()
  242. const decoder=new TextDecoder()
  243. while(1){
  244. const {done, value} = await reader.read()
  245. // console.log(done, 'done');
  246. if(done){
  247. console.log(value, 'value');
  248. // if (typeof(value) == "undefined") {
  249. // this.$message.error('错误')
  250. // this.returnMessage.splice(this.returnMessage.length - 1, 1)
  251. // }
  252. console.log('结束');
  253. // 消息全部显示后存到历史localStorage
  254. let HistoryMessage = []
  255. // 历史只存100条,长度超出截取最新的
  256. if (this.returnMessage.length > 100) {
  257. HistoryMessage = JSON.stringify(this.returnMessage.slice(this.returnMessage.length - 100, 100))
  258. } else {
  259. HistoryMessage = JSON.stringify(this.returnMessage)
  260. }
  261. localStorage.setItem(`[${123},${this.info.id}]`, HistoryMessage);
  262. break;
  263. }
  264. //txt就是一个一个的字 然后添加到页面上就可以了
  265. const txt = decoder.decode(value).split('data:')
  266. // const txt = decoder.decode(value)
  267. this.addMessage(txt)
  268. // let data = JSON.parse(txt).message.content
  269. // console.log(txt, 'txt');
  270. // if (this.isJSON(txt) && JSON.parse(txt).code != 200) {
  271. // let data = JSON.parse(txt)
  272. // this.$message.error(data.msg)
  273. // this.returnMessage.splice(this.returnMessage.length - 1, 1)
  274. // break;
  275. // }
  276. }
  277. },
  278. addMessage(value) {
  279. for (let i = 0; i < value.length; i++) {
  280. const element = value[i];
  281. if (this.isJSON(element)) {
  282. let txt = JSON.parse(element).content
  283. this.returnMessage[this.returnMessage.length - 1].content += txt
  284. }
  285. }
  286. },
  287. isJSON(str) {
  288. if (typeof str == 'string') {
  289. try {
  290. JSON.parse(str);
  291. return true;
  292. } catch(e) {
  293. // console.log(e);
  294. return false;
  295. }
  296. }
  297. },
  298. }
  299. }
  300. </script>
  301. <style lang="scss" scoped>
  302. .chat {
  303. min-height: 100vh;
  304. }
  305. .content {
  306. margin-top: 30px;
  307. background-size: 100% 100%;
  308. display: flex;
  309. height: 90vh;
  310. justify-content: center;
  311. width: 100vw;
  312. >.leftImg {
  313. border-radius: 16px 16px 16px 16px;
  314. height: 95%;
  315. position: relative;
  316. min-width: 380px;
  317. >img {
  318. border: 6px solid;
  319. border-image: linear-gradient(180deg, #b48733, #e8cf97, #b48733) 6 6;
  320. height: 99%;
  321. margin-right: 5px;
  322. }
  323. >.aiInfo {
  324. background-color: #ffffff1a;
  325. border-radius: 24px 24px 24px 24px;
  326. bottom: -70px;
  327. display: flex;
  328. height: 193px;
  329. justify-content: center;
  330. left: 50%;
  331. position: absolute;
  332. transform: translate(-50%, -50%);
  333. width: 100%;
  334. >.photo {
  335. width: 64px;
  336. height: 64px;
  337. font-size: 18px;
  338. position: absolute;
  339. top: -20px;
  340. box-sizing: border-box;
  341. margin: 0;
  342. padding: 0;
  343. color: #fff;
  344. line-height: 1.5714285714285714;
  345. list-style: none;
  346. font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  347. display: inline-flex;
  348. justify-content: center;
  349. align-items: center;
  350. overflow: hidden;
  351. white-space: nowrap;
  352. text-align: center;
  353. vertical-align: middle;
  354. background: rgba(0, 0, 0, 0.25);
  355. border: 1px solid transparent;
  356. border-radius: 50%;
  357. }
  358. >.info {
  359. padding-top: 50px;
  360. width: 80%;
  361. >.name {
  362. color: #fff;
  363. font-size: 22px;
  364. font-style: normal;
  365. font-weight: 700;
  366. line-height: 31px;
  367. text-align: center;
  368. text-transform: none;
  369. }
  370. >.tags {
  371. padding-bottom: 8px;
  372. text-align: center;
  373. >.tag {
  374. margin-right: 4px;
  375. background: #ffffff1a;
  376. border-radius: 4px 4px 4px 4px;
  377. color: #fff;
  378. font-size: 12px;
  379. font-weight: 500;
  380. line-height: 17px;
  381. border-color: transparent;
  382. }
  383. }
  384. >.infoContent {
  385. color: #fff;
  386. font-size: 16px;
  387. font-style: normal;
  388. font-weight: 500;
  389. height: 80px;
  390. line-height: 22px;
  391. overflow-y: auto;
  392. text-align: center;
  393. text-transform: none;
  394. }
  395. }
  396. }
  397. }
  398. >.chat-box {
  399. margin-left: 5px;
  400. position: relative;
  401. background-color: #ffffff1a;
  402. background-position: 50%;
  403. background-repeat: no-repeat;
  404. background-size: auto;
  405. border: 1px solid #635677 !important;
  406. border-radius: 20px;
  407. box-sizing: border-box;
  408. color: #fff !important;
  409. display: flex;
  410. flex-direction: column;
  411. height: 95%;
  412. // margin: 0 auto;
  413. width: 1010px;
  414. padding: 25px;
  415. }
  416. }
  417. .messages {
  418. height: calc(100% - 68px );
  419. overflow-y: auto;
  420. }
  421. .messages::-webkit-scrollbar {
  422. width: 0px;
  423. }
  424. .message {
  425. background: #ffffff0f;
  426. border: 1px solid #5b5b5e;
  427. max-width: calc(100% - 72px);
  428. // flex: 1;
  429. min-width: 80px;
  430. }
  431. .photo {
  432. width: 56px;
  433. }
  434. .me {
  435. flex-direction: row-reverse;
  436. }
  437. .loadingMessage {
  438. position: relative;
  439. // display: flex;
  440. // justify-content: center;
  441. width: 100%;
  442. height: 100%;
  443. }
  444. </style>
  445. <style scoped>
  446. .loadingMessage >>> .el-loading-parent--relative {
  447. width: 100%;
  448. height: 100%;
  449. }
  450. .loadingMessage >>> .el-loading-spinner {
  451. display: flex;
  452. justify-content: center;
  453. }
  454. .mButton {
  455. border: solid 0px;
  456. }
  457. </style>