ChatPC.vue 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866
  1. <template>
  2. <div class="chat bg-white">
  3. <div class="flex items-center h-screen relative">
  4. <div class="leftInfo relative" v-show="!showNodata">
  5. <div class="history">
  6. <!-- historyActive -->
  7. <div class="historyBox" v-for="(item, index) in allRecords" :key="index">
  8. <div class="top flex w-full cursor-pointer" @click="item.open = !item.open">
  9. <div class="photo">
  10. <img class="object-cover" :src="baseApi + item.picture" @error="$AIPohotoError" alt="">
  11. </div>
  12. <div class="info flex-1 ml-2 flex flex-col justify-around">
  13. <div class="text-sm font-semibold">{{ item.characterName }}</div>
  14. <div class="text-sm text-gray-400">{{ item.prologue }}
  15. </div>
  16. </div>
  17. <div class="flex items-center text-lg" style="height: 48px;">
  18. <i v-if="!item.open" class="el-icon-arrow-down" style="font-size: 16px;"></i>
  19. <i v-else class="el-icon-arrow-up" style="font-size: 16px;"></i>
  20. </div>
  21. </div>
  22. <!-- <transition name="el-zoom-in-top"> -->
  23. <div class="dialogueList mt-4" v-show="item.open">
  24. <div class="flex justify-between mb-2">
  25. <el-button v-if="item.isDelete == 0" type="primary" class="flex-1" round
  26. @click="newStart_juese(item)">新的对话</el-button>
  27. <div v-else class="flex-1 rounded border border-gray-200 text-sm p-2 text-gray-600">
  28. {{ item.characterName }}已经被设定为私有或被创建者删除。
  29. </div>
  30. <el-dropdown class="flex justify-end items-center ml-2" trigger="click"
  31. @command="handleCommand2($event, item)">
  32. <div class=" w-8 h-8 bg-gray-100 rounded flex justify-center items-center cursor-pointer">
  33. <i slot="reference" class="el-icon-more"></i>
  34. </div>
  35. <el-dropdown-menu slot="dropdown">
  36. <el-dropdown-item command="2">删除</el-dropdown-item>
  37. </el-dropdown-menu>
  38. </el-dropdown>
  39. </div>
  40. <div
  41. class="dialogue flex justify-between items-center w-full h-16 rounded-md px-2 mb-2 cursor-pointer relative"
  42. :class="info.id == item.id && index2 == recordsIndex && 'historyActive'"
  43. v-for="(item2, index2) in item.chatCharacterList" :key="index2"
  44. @click.self="clickDialogue(item, item2, index2)">
  45. <span @click.self="clickDialogue(item, item2, index2)">
  46. <i v-if="item2.sceneId || item2.sceneIcon" class="el-icon-video-camera"></i>
  47. {{ item2.chatTitle }}
  48. </span>
  49. <span @click.self="clickDialogue(item, item2, index2)" class=" text-sm text-gray-500">{{ item2.createTime
  50. }}</span>
  51. <el-dropdown class="dialogueIconBg absolute h-16 w-20 right-0 flex justify-end items-center pr-4"
  52. trigger="click" @command="handleCommand($event, item, item2)">
  53. <div>
  54. <i slot="reference" class="el-icon-more"></i>
  55. </div>
  56. <el-dropdown-menu slot="dropdown">
  57. <el-dropdown-item command="1">修改标题</el-dropdown-item>
  58. <el-dropdown-item command="2">删除</el-dropdown-item>
  59. </el-dropdown-menu>
  60. </el-dropdown>
  61. </div>
  62. </div>
  63. <!-- </transition> -->
  64. </div>
  65. </div>
  66. <!-- 角色详情 -->
  67. <div class="chatInfo absolute top-0 left-0 w-full h-full z-30 bg-white" v-show="showInfo == 1">
  68. <div class="flex flex-col p-4 h-full">
  69. <div class="flex justify-between items-center text-base mb-3">
  70. <div>角色详情</div>
  71. <i class="el-icon-close cursor-pointer" @click="showInfoChange(0)"></i>
  72. </div>
  73. <div class="flex">
  74. <span class="chatInfo_photo">
  75. <img class="w-16 h-16 rounded-full overflow-hidden object-cover" :src="baseApi + info.picture"
  76. @error="$AIPohotoError">
  77. </span>
  78. <div class="w-0 flex-1 ml-2">
  79. <div class="text-xl font-medium">{{ info.characterName }}</div>
  80. <div class="mt-1 text-gray-500 text-sm">Xchat No.{{ info.id }}</div>
  81. </div>
  82. </div>
  83. <div class="flex mt-4">
  84. <div class="hot flex w-auto items-center px-2 py-1 text-sm mr-2 rounded-full">
  85. <v-icon name="fire" scale="1" />
  86. <span class="ml-1">{{ info.hotCount }}</span>
  87. </div>
  88. </div>
  89. <div class="flex justify-around mt-4">
  90. <div class=" rounded-full w-32 text-center py-2 text-sm cursor-pointer" style="border: solid 1px #000">分享
  91. </div>
  92. <div class=" rounded-full w-32 text-center py-2 text-sm cursor-pointer" style="border: solid 1px #000"
  93. @click="showDetail = !showDetail">角色详情</div>
  94. </div>
  95. <transition name="el-zoom-in-top">
  96. <div v-show="showDetail">
  97. <div class="mt-5 text-base pb-3 border-b border-gray-200">
  98. <div>
  99. <i class="el-icon-document"></i>
  100. 简介
  101. </div>
  102. <div class="flex">
  103. <div ref="prologue" class="mt-3 text-sm max-h-48 prologue" :class="showPrologue && 'prologueOpen'">
  104. <div v-show="showPrologueButton" class="textButton" @click="showPrologue = !showPrologue">{{
  105. showPrologue ? '收起' :
  106. '展开' }}</div>
  107. {{ info.prologue }}
  108. </div>
  109. </div>
  110. </div>
  111. <div class="flex justify-between items-center mt-5 text-base pb-3 border-b border-gray-200">
  112. <div>
  113. <i class="el-icon-user"></i>
  114. 创作人
  115. </div>
  116. <div class="text-sm max-h-48">
  117. {{ info.author }}
  118. </div>
  119. </div>
  120. <div class="flex justify-between items-center mt-5 text-base pb-3 border-b border-gray-200">
  121. <div>
  122. <i class="el-icon-time"></i>
  123. 创建时间
  124. </div>
  125. <div class="text-sm max-h-48">
  126. {{ info.createTime }}
  127. </div>
  128. </div>
  129. <div class="mt-5 text-base">
  130. <i class="el-icon-discount"></i>
  131. 特征
  132. </div>
  133. <div class="tags mt-4 pb-3">
  134. <span class="tag" v-for="(item, index) in info.labelArr" :key="index">
  135. {{ item }}
  136. </span>
  137. </div>
  138. </div>
  139. </transition>
  140. <div class="mt-5 text-base pb-3 flex-1 flex flex-col">
  141. <div>
  142. <!-- <i class="el-icon-document"></i> -->
  143. 查看场景
  144. </div>
  145. <div class="mt-3 text-sm max-h-48">
  146. <div v-if="info.sceneList && info.sceneList.length > 0"
  147. class="grid grid-cols-2 gap-2 mb-4 cursor-pointer">
  148. <template v-for="(item, index) in info.sceneList">
  149. <div v-if="item.isDelete == 0" class="flex flex-col justify-center" :key="index"
  150. @click="sceneChange(item)">
  151. <img class=" rounded w-full h-24 object-cover" :src="baseApi + item.background" alt="">
  152. <div>{{ item.sceneName }}</div>
  153. <div class=" text-gray-400">{{ item.sceneDescription }}</div>
  154. </div>
  155. </template>
  156. </div>
  157. <el-empty v-else description="暂无场景"></el-empty>
  158. <div class="flex justify-center mt-10">
  159. <el-button type="primary" round @click="toCreateScene">创建场景</el-button>
  160. </div>
  161. </div>
  162. </div>
  163. </div>
  164. </div>
  165. <!-- 场景详情 -->
  166. <div class="absolute top-0 left-0 w-full h-full z-30 bg-white overflow-hidden" v-show="showInfo == 2">
  167. <div class="flex flex-col p-4 h-full">
  168. <div class="flex justify-between items-center text-base mb-3">
  169. <div>场景详情</div>
  170. <i class="el-icon-close cursor-pointer" @click="showInfoChange(0)"></i>
  171. </div>
  172. <div class=" text-gray-500">{{ sceneInfo.sceneDescription }}</div>
  173. </div>
  174. </div>
  175. </div>
  176. <div class="content" :class="fullScreen && 'contentFull'" v-show="!showNodata"
  177. :style="{ backgroundImage: `url(${baseApi}${sceneInfo.background})` }">
  178. <!-- <div class="leftImg">
  179. <img :src="baseApi + info.picture" alt="">
  180. <div class="aiInfo">
  181. <span class="photo">
  182. <img :src="baseApi + info.picture">
  183. </span>
  184. <div class="info">
  185. <div class="name">{{ info.characterName }}</div>
  186. <div class="tags">
  187. <span class="tag" v-for="(item, index) in info.labelArr" :key="index">
  188. {{ item }}
  189. </span>
  190. </div>
  191. <div class="infoContent">{{ info.prologue }}</div>
  192. </div>
  193. </div>
  194. </div> -->
  195. <div class="fullScreenButton rounded-full border border-gray-200" @click="clickFullScreen">
  196. <i class="el-icon-arrow-left" v-if="!fullScreen"></i>
  197. <i class="el-icon-arrow-right" v-else></i>
  198. </div>
  199. <div class="chat-box" :class="sceneId && 'mask'">
  200. <div class="chatBoxTitle absolute top-0">
  201. <div>
  202. {{ info.characterName }}
  203. <el-dropdown v-if="sceneId" class="flex justify-end items-center" trigger="click" @command="handleCommand3"
  204. @visible-change="dropdown3Show = !dropdown3Show">
  205. <div>
  206. <div slot="reference" class="flex justify-center items-center cursor-pointer text-white">
  207. 已进入场景:{{ sceneInfo.sceneName }}
  208. <i class="el-icon-arrow-up ml-2" style="font-size: 16px;" v-if="dropdown3Show"></i>
  209. <i class="el-icon-arrow-down ml-2" style="font-size: 16px;" v-else></i>
  210. </div>
  211. </div>
  212. <el-dropdown-menu slot="dropdown">
  213. <el-dropdown-item command="1">场景详情</el-dropdown-item>
  214. <el-dropdown-item command="2">新场景对话</el-dropdown-item>
  215. <el-dropdown-item command="3">回到常规对话</el-dropdown-item>
  216. </el-dropdown-menu>
  217. </el-dropdown>
  218. </div>
  219. <div class="flex w-60 justify-end">
  220. <el-tooltip effect="dark" content="设定" placement="bottom">
  221. <el-popover placement="bottom" width="400" trigger="click">
  222. <div class="p-2">
  223. <div class="flex justify-between mb-4">
  224. <span>语音自动播放</span>
  225. <el-switch v-model="setting.value1" @change="ChangeSettingValue1">
  226. </el-switch>
  227. </div>
  228. <div class="flex justify-between">
  229. <span>全屏幕</span>
  230. <el-switch v-model="fullScreen">
  231. </el-switch>
  232. </div>
  233. </div>
  234. <i slot="reference" class="fa-solid fa-sliders cursor-pointer text-xl mr-6"></i>
  235. </el-popover>
  236. </el-tooltip>
  237. <el-tooltip effect="dark" content="角色详情" placement="bottom">
  238. <i class="fa-solid fa-file-invoice cursor-pointer text-xl mr-6" @click="showInfoChange(1)"></i>
  239. </el-tooltip>
  240. <el-tooltip v-if="!sceneId" effect="dark" content="开始新的对话" placement="bottom">
  241. <i class="fa-solid fa-comment-medical cursor-pointer text-xl mr-6" @click="newStart"></i>
  242. </el-tooltip>
  243. <el-tooltip effect="dark" content="历史对话" placement="bottom">
  244. <i class="fa-solid fa-clock-rotate-left cursor-pointer text-xl" @click="showInfoChange(0)"></i>
  245. </el-tooltip>
  246. </div>
  247. </div>
  248. <audio ref="audio" muted v-show="false" :src="audioUrl" autoplay="" @ended="audioEnd" controls></audio>
  249. <div class="messages" ref="messages">
  250. <template v-for="(item, index) in returnMessage">
  251. <div v-if="item.role == 'assistant'" class="mb-4 flex" :key="index">
  252. <!-- ai返回的信息 -->
  253. <div class="pt-2 photo">
  254. <img :src="baseApi + info.picture" class="rounded-full w-14 h-14 object-cover"
  255. @error="$AIPohotoError" />
  256. </div>
  257. <div class="messageRight">
  258. <div v-show="!item.edit" :ref="`message${index}`"
  259. class="message mt-4 ml-4 p-4 rounded-r-md rounded-bl-md text-xs relative">
  260. <div class="yyPlayBg mb-2 w-16 p-1 cursor-pointer">
  261. <img v-if="audioPlayIndex == index" class="yyPlay" src="@/assets/images/播放/yyPlay1.png" alt="">
  262. <img v-else class="yyPlay" src="@/assets/images/播放/yyPlay.png" alt=""
  263. @click="historyGetVoice(item, index)">
  264. <!-- <svg-icon class="icon" class-name="speech-icon" icon-class="speech" @click.stop="click" /> -->
  265. </div>
  266. <div v-show="!item.content" class="loadingMessage">
  267. <div v-loading="!item.content" element-loading-background="rgba(0, 0, 0, 0.0)"></div>
  268. </div>
  269. <p style="white-space: pre-line;" v-show="item.content">{{ item.content }}</p>
  270. <div class="messageOptions" v-show="returnMessage.length == 1">
  271. <div class="option" v-for="(item, index) in messageOptions" :key="index"
  272. @click="messageOptionClick(item.chatGuidance)">
  273. {{ item.chatGuidance }}
  274. </div>
  275. </div>
  276. <el-dropdown v-if="index != 0" class="messageDropdown" @command="handleCommand4($event, item, index)"
  277. placement="top-end">
  278. <el-button class="messageButton" icon="el-icon-more" circle></el-button>
  279. <el-dropdown-menu slot="dropdown">
  280. <el-dropdown-item command="1">编辑</el-dropdown-item>
  281. <el-dropdown-item command="2">删除</el-dropdown-item>
  282. <el-dropdown-item v-if="index == returnMessage.length - 1" command="3">重新生成</el-dropdown-item>
  283. </el-dropdown-menu>
  284. </el-dropdown>
  285. </div>
  286. <div :ref="`messageEdit${index}`" v-show="item.edit"
  287. class="message messageEdit mt-4 ml-4 p-4 rounded-r-md rounded-bl-md text-xs relative">
  288. <el-input class="eidtTextarea" :ref="`messageEditInput${index}`" type="textarea" resize="none"
  289. v-model="item.editContent">
  290. </el-input>
  291. <div class="flex justify-end mt-2">
  292. <el-button @click="editCancel(item, index)">取消</el-button>
  293. <el-button @click="editSave(item, index)">保存</el-button>
  294. </div>
  295. </div>
  296. </div>
  297. </div>
  298. <!-- 用户发送的信息 -->
  299. <div v-if="item.role == 'user'" class="mb-4 flex me" :key="index">
  300. <div class="pt-2 photo">
  301. <img :src="$store.state.user.avatar" class="rounded-full w-14 h-14 object-cover"
  302. @error="$userPohotoError" />
  303. </div>
  304. <div class="messageRight">
  305. <div :ref="`message${index}`" v-show="!item.edit"
  306. class="messageUser mr-4 mt-4 p-4 rounded-l-md rounded-br-md text-xs relative">
  307. {{ item.content }}
  308. <el-dropdown v-if="index != 0" class="messageDropdown" @command="handleCommand4($event, item, index)"
  309. placement="top-end">
  310. <el-button class="messageButton" icon="el-icon-more" circle></el-button>
  311. <el-dropdown-menu slot="dropdown">
  312. <el-dropdown-item command="1">编辑</el-dropdown-item>
  313. <el-dropdown-item command="2">删除</el-dropdown-item>
  314. </el-dropdown-menu>
  315. </el-dropdown>
  316. </div>
  317. <div :ref="`messageEdit${index}`" v-show="item.edit"
  318. class="messageUser messageEdit mt-4 ml-4 p-4 rounded-r-md rounded-bl-md text-xs relative">
  319. <el-input class="eidtTextarea" :ref="`messageEditInput${index}`" type="textarea" resize="none"
  320. v-model="item.editContent">
  321. </el-input>
  322. <div class="flex justify-end mt-2">
  323. <el-button @click="editCancel(item, index)">取消</el-button>
  324. <el-button @click="editSave(item, index)">保存</el-button>
  325. </div>
  326. </div>
  327. </div>
  328. </div>
  329. </template>
  330. </div>
  331. <div class="inputBox absolute left-2 right-2 bottom-4 " @click="inputBoxClick">
  332. <input v-if="canChat" ref="input" type="text" placeholder="输入消息..."
  333. class="w-full flex-1 py-2 px-4 rounded-l-lg text-sm focus:outline-none" v-model="content"
  334. @keydown="Enterkey" />
  335. <div class="flex pt-6 justify-between" v-if="canChat" style="height: 70px;">
  336. <div class="tools w-36 flex justify-between">
  337. <i class="el-icon-setting text-2xl cursor-pointer" @click="showConfig = true"></i>
  338. <!-- <i class="el-icon-picture-outline text-2xl cursor-pointer"></i> -->
  339. <!-- <i class="el-icon-s-opportunity text-2xl cursor-pointer"></i> -->
  340. </div>
  341. <el-button type="text" :disabled="!content" :loading="notChatSend" @click.prevent="getStreamChatWithWeb"
  342. class="px-4 rounded-r-lg text-sm text-gray-400" :class="content && 'planeColor'">
  343. <v-icon v-show="!notChatSend" name="paper-plane" scale="1.5" />
  344. </el-button>
  345. </div>
  346. <div v-if="!canChat" class=" flex w-full text-sm text-gray-400">
  347. 当前角色或场景已被设定为私有或被创作者删除,但你任然可以查看历史对话。
  348. </div>
  349. </div>
  350. </div>
  351. </div>
  352. <div v-show="showNodata" class=" w-full h-full flex flex-col justify-center items-center">
  353. <el-empty description="暂无聊天记录,点击按钮并探索新角色。"></el-empty>
  354. <el-button type="primary" round @click="$router.push('/')">寻找一个角色</el-button>
  355. </div>
  356. <el-dialog title="配置" :visible.sync="showConfig" width="500px">
  357. <!-- <p class="text-base">身分</p>
  358. <p class="text-sm text-gray-400">选择你和角色聊天的身份</p>
  359. <div class="flex items-start py-3 border-b border-gray-200">
  360. <el-radio-group class="radio" v-model="configForm.radio1">
  361. <el-radio-button label="上海"></el-radio-button>
  362. </el-radio-group>
  363. <el-button>
  364. <i class="el-icon-sort" style="transform: rotate(90deg);"></i>
  365. </el-button>
  366. </div> -->
  367. <p class="text-base mt-3">选择模型</p>
  368. <p class="text-sm text-gray-400">每个模型可能有不同的效果,仅对当前对话有影响</p>
  369. <div class="flex items-start py-3 border-b border-gray-200">
  370. <el-radio-group class="radio" v-model="configForm.radio2">
  371. <el-radio-button v-for="(item, index) in modelList" :key="index" :label="item.id">{{ item.model
  372. }}</el-radio-button>
  373. </el-radio-group>
  374. </div>
  375. <!-- <p class="text-base mt-3">角色语言</p>
  376. <p class="text-sm text-gray-400">请选择角色的回复语言</p>
  377. <div class="flex items-start py-3">
  378. <el-radio-group class="radio" v-model="configForm.radio3">
  379. <el-radio-button label="自动"></el-radio-button>
  380. <el-radio-button label="简体中文"></el-radio-button>
  381. <el-radio-button label="繁體中文"></el-radio-button>
  382. <el-radio-button label="English"></el-radio-button>
  383. <el-radio-button label="Español"></el-radio-button>
  384. </el-radio-group>
  385. </div> -->
  386. <span slot="footer" class="dialog-footer">
  387. <el-button @click="showConfig = false">取 消</el-button>
  388. <el-button type="primary" @click="configConfirm">确 定</el-button>
  389. </span>
  390. </el-dialog>
  391. </div>
  392. </div>
  393. </template>
  394. <script>
  395. import Cookies from 'js-cookie'
  396. import {
  397. streamChatWithWebApi,
  398. getGuidanceApi,
  399. getModelListApi,
  400. addChatApi,
  401. getChatCharacterRecordsApi,
  402. getChatRecordApi,
  403. updateTitleApi,
  404. clearRecordApi,
  405. queryVoiceListApi,
  406. updateRecordApi,
  407. deleteRecordApi
  408. } from "@/api/chat.js"
  409. import { detailApi } from "@/api/detail.js"
  410. import { queryUserBalanceApi } from "@/api/user.js"
  411. import { Message, MessageBox, Notification, Loading } from 'element-ui'
  412. import 'vue-awesome/icons/paper-plane'
  413. import 'vue-awesome/icons/fire'
  414. export default {
  415. components: {
  416. },
  417. data() {
  418. return {
  419. // 展开收起简介按钮是否显示
  420. showPrologueButton: false,
  421. // 展开收起简介
  422. showPrologue: false,
  423. // 对话请求超时参数
  424. timeOut: null,
  425. // 聊天等待
  426. chatLoading: false,
  427. // 是否全屏
  428. fullScreen: false,
  429. // 角色所有聊天列表
  430. allRecords: [],
  431. // 聊天记录id
  432. recordId: null,
  433. // 选中的角色聊天记录
  434. historyRes: [],
  435. recordsIndex: null,
  436. showDetail: true,
  437. showConfig: false,
  438. showInfo: 0,
  439. messageOptions: [],
  440. info: {},
  441. messageLoading: false,
  442. returnMessage: [],
  443. content: '',
  444. audioPlayIndex: null,
  445. configForm: {
  446. radio1: null,
  447. radio2: null,
  448. radio3: null,
  449. },
  450. // 对话接口是否返回错误状态
  451. resError: false,
  452. // 模型列表
  453. modelList: [],
  454. setting: {
  455. value1: false,
  456. value2: false,
  457. },
  458. //音频相关↓
  459. //音频地址
  460. audioUrl: '',
  461. audioUrlArr: [],
  462. audioUrlArrIndx: 0,
  463. videoLoop: null,
  464. videoLoopTime: 0,
  465. loopPlay: false,
  466. //场景下拉菜单
  467. dropdown3Show: false,
  468. // 场景对话相关
  469. sceneId: null,
  470. sceneInfo: {},
  471. }
  472. },
  473. computed: {
  474. canChat() {
  475. console.log(this.info, 'this.info');
  476. if (!this.sceneId) {
  477. if (this.info.isDelete == 1) {
  478. console.log(1);
  479. return false
  480. } else {
  481. console.log(2);
  482. return true
  483. }
  484. } else {
  485. if (this.sceneInfo.isDelete == 1) {
  486. return false
  487. } else {
  488. return true
  489. }
  490. }
  491. },
  492. notChatSend() {
  493. return this.chatLoading != false || this.audioPlayIndex != null
  494. },
  495. showNodata() {
  496. return this.allRecords.length == 0 && !this.$route.query.characterId
  497. }
  498. },
  499. mounted() {
  500. this.init()
  501. this.$nextTick(() => {
  502. })
  503. },
  504. watch: {
  505. '$route.query.characterId': {
  506. handler(newVal, lodVal) {
  507. console.log(newVal, 'newVal');
  508. if (!newVal) {
  509. this.$nextTick(() => {
  510. this.showInfoChange(0)
  511. })
  512. }
  513. },
  514. deep: true
  515. },
  516. returnMessage: {
  517. handler(newVal, lodVal) {
  518. this.$nextTick(() => {
  519. let messages = this.$refs.messages
  520. messages.scrollTop = messages.scrollHeight
  521. })
  522. },
  523. deep: true
  524. }
  525. },
  526. methods: {
  527. async init() {
  528. // 获取模型列表
  529. this.getModelList()
  530. // 获取用户所有聊天记录
  531. await this.getChatCharacterRecords()
  532. // 如果用角色id获取角色详情
  533. if (this.$route.query.characterId) {
  534. if (this.$route.query.sceneId) {
  535. this.sceneId = this.$route.query.sceneId
  536. }
  537. this.showInfoChange(1)
  538. await this.getDetail(this.$route.query.characterId)
  539. } else {
  540. this.allRecords[0].open = true
  541. await this.getDetail(this.allRecords[0].id)
  542. }
  543. // 查找当前角色是否有对话记录
  544. // this.searchHistory(this.info.id)
  545. },
  546. // 左侧面板变化
  547. showInfoChange(index) {
  548. this.fullScreen = false
  549. this.showInfo = index
  550. },
  551. // 全屏
  552. clickFullScreen() {
  553. this.fullScreen = !this.fullScreen
  554. },
  555. // 点击聊天记录切换聊天
  556. async clickDialogue(value1, value2, index) {
  557. console.log(value2, 'value2');
  558. let _this = this
  559. this.audioUrl = ""
  560. this.messageOptions = []
  561. this.returnMessage = []
  562. if (this.$route.query.characterId) {
  563. this.$route.query.characterId = value1.id
  564. }
  565. if (!value2) {
  566. return
  567. }
  568. // 如果点击的是场景聊天
  569. if (value2.sceneId) {
  570. this.sceneId = value2.sceneId
  571. await this.getDetail(value1.id)
  572. // 从角色详情中获取场景列表
  573. let sceneList = this.info.sceneList
  574. // 获取当前聊天对应的场景
  575. for (let i = 0; i < sceneList.length; i++) {
  576. const element = sceneList[i];
  577. if (element.sceneId == value2.sceneId) {
  578. _this.sceneInfo = element
  579. }
  580. }
  581. } else {
  582. this.sceneId = null
  583. this.sceneInfo = {}
  584. await this.getDetail(value1.id)
  585. }
  586. this.recordId = value2.id
  587. this.getChatRecord(value2.id)
  588. this.recordsIndex = index
  589. },
  590. // 场景下拉菜单
  591. handleCommand3(value) {
  592. if (value == 1) {
  593. this.showInfoChange(2)
  594. } else if (value == 2) {
  595. // 情况聊天记录
  596. this.returnMessage = []
  597. // 新建场景对话
  598. this.newStart()
  599. // 增加开场白
  600. this.returnMessage.push({
  601. role: 'assistant',
  602. content: this.sceneInfo.sceneWelcome
  603. })
  604. } else if (value == 3) {
  605. // 回到常规对话
  606. // 遍历当前聊天记录是否有常规对话
  607. let messages = null
  608. for (let i = 0; i < this.allRecords.length; i++) {
  609. const element = this.allRecords[i];
  610. if (element.id == this.info.id) {
  611. messages = element
  612. }
  613. }
  614. console.log(messages, 'messages');
  615. let flag = false
  616. for (let i = 0; i < messages.chatCharacterList.length; i++) {
  617. const element = messages.chatCharacterList[i];
  618. console.log(element, 'element');
  619. if (!element.sceneId) {
  620. flag = true
  621. this.clickDialogue(messages, element, i)
  622. break
  623. }
  624. }
  625. // 如果聊天记录里没有常规对话,则新增一个常规对话
  626. if (!flag) {
  627. this.sceneId = null
  628. this.sceneInfo = {}
  629. this.newStart()
  630. }
  631. }
  632. },
  633. // 聊天记录列表-角色下拉菜单
  634. handleCommand2(value, value1) {
  635. if (value == 2) {
  636. this.$confirm('此操作将永久删除该角色所有聊天记录, 是否继续?', '提示', {
  637. confirmButtonText: '确定',
  638. cancelButtonText: '取消',
  639. type: 'warning'
  640. }).then(() => {
  641. this.clearRecord(value1)
  642. })
  643. }
  644. },
  645. // 聊天记录列表-角色-聊天记录下拉菜单
  646. handleCommand(value, value1, value2) {
  647. console.log(value, 'value');
  648. if (value == 1) {
  649. this.editDialogueTitle(value1, value2)
  650. } else if (value == 2) {
  651. this.$confirm('此操作将永久删除该聊天记录, 是否继续?', '提示', {
  652. confirmButtonText: '确定',
  653. cancelButtonText: '取消',
  654. type: 'warning'
  655. }).then(() => {
  656. this.clearRecord(value1, value2)
  657. })
  658. }
  659. },
  660. // 聊天记录操作相关↓
  661. handleCommand4(value1, value2, value3) {
  662. console.log(value1, 'value1');
  663. console.log(value2, 'value2');
  664. if (value1 == 1) {
  665. // 编辑
  666. let height = this.$refs[`message${value3}`][0].offsetHeight
  667. let width = this.$refs[`message${value3}`][0].offsetWidth
  668. value2.edit = true
  669. this.$nextTick(() => {
  670. this.$refs[`messageEditInput${value3}`][0].$el.children[0].style.height = height + 'px'
  671. this.$refs[`messageEdit${value3}`][0].style.width = width + 'px'
  672. })
  673. } else if (value1 == 2) {
  674. // 删除
  675. this.$confirm('此操作将永久删除该聊天记录, 是否继续?', '提示', {
  676. confirmButtonText: '确定',
  677. cancelButtonText: '取消',
  678. type: 'warning'
  679. }).then(() => {
  680. this.deleteRecord(value2, value3)
  681. })
  682. } else if (value1 == 3) {
  683. // 重新生成最后一句对话
  684. this.resetChat(value2, value3)
  685. }
  686. },
  687. // 重新生成最后一条聊天记录
  688. resetChat(value, index) {
  689. let params = {
  690. recordId: value.id,
  691. role: value.role,
  692. }
  693. deleteRecordApi(params).then(res => {
  694. this.returnMessage.splice(index, 1)
  695. this.getStreamChatWithWeb()
  696. })
  697. },
  698. // 编辑单条聊天记录-取消
  699. editCancel(value, index) {
  700. value.edit = false
  701. value.editContent = value.content
  702. },
  703. // 编辑单条聊天记录-保存
  704. editSave(value, index) {
  705. console.log(value, 'value');
  706. let params = {
  707. content: value.editContent,
  708. recordId: value.id,
  709. role: value.role,
  710. }
  711. updateRecordApi(params).then(res => {
  712. console.log(res, 'ressss');
  713. value.edit = false
  714. this.getChatRecord(this.recordId)
  715. })
  716. },
  717. // 删除单条聊天记录
  718. deleteRecord(value, index) {
  719. let params = {
  720. recordId: value.id,
  721. role: value.role,
  722. }
  723. deleteRecordApi(params).then(res => {
  724. this.$message({
  725. message: '删除成功',
  726. type: 'success'
  727. });
  728. this.getChatRecord(this.recordId)
  729. })
  730. },
  731. // 聊天记录新增对话
  732. async newStart_juese(value) {
  733. console.log(value, 'value');
  734. this.audioUrl = ""
  735. this.sceneId = null
  736. this.sceneInfo = {}
  737. this.messageOptions = []
  738. this.returnMessage = []
  739. // 点击聊天记录切换聊天
  740. if (this.$route.query.characterId) {
  741. this.$route.query.characterId = value.id
  742. }
  743. await this.getDetail(value.id)
  744. this.recordId = await this.addChat()
  745. for (let i = 0; i < this.allRecords.length; i++) {
  746. const element = this.allRecords[i];
  747. if (element.id == this.info.id) {
  748. element.chatCharacterList.push({
  749. chatTitle: "常规聊天",
  750. createTime: '',
  751. id: this.recordId
  752. })
  753. this.recordsIndex = element.chatCharacterList.length - 1
  754. }
  755. }
  756. },
  757. // 修改聊天记录标题
  758. editDialogueTitle(value1, value2) {
  759. this.$prompt('新标题名称', '修改标题', {
  760. confirmButtonText: '确定',
  761. cancelButtonText: '取消',
  762. }).then(({ value }) => {
  763. let params = {
  764. chatTitle: value,
  765. id: value2.id
  766. }
  767. updateTitleApi(params).then(res => {
  768. console.log(res, '修改标题');
  769. this.$message({
  770. type: 'success',
  771. message: '修改成功!'
  772. });
  773. this.getChatCharacterRecords()
  774. })
  775. })
  776. },
  777. clearRecord(value1, value2) {
  778. let params = {
  779. characterId: value1.id,
  780. }
  781. if (value2) {
  782. params.recordId = value2.id
  783. }
  784. clearRecordApi(params).then(res => {
  785. this.$message({
  786. type: 'success',
  787. message: '删除成功!'
  788. });
  789. this.getChatCharacterRecords()
  790. })
  791. },
  792. async getChatRecord(recordId) {
  793. let params = {
  794. characterId: this.info.id,
  795. recordId: recordId
  796. }
  797. getChatRecordApi(params).then(res => {
  798. console.log(res, '查询到的聊天记录');
  799. let array = res.data
  800. // this.returnMessage
  801. this.historyRes = res.data
  802. if (this.returnMessage.length > 1) {
  803. this.returnMessage = this.returnMessage.splice(0, 1)
  804. }
  805. for (let i = 0; i < array.length; i++) {
  806. const element = array[i];
  807. let history = JSON.parse(element.history)
  808. history.map(item => {
  809. item.id = element.id
  810. item.uuid = element.uuid
  811. item.edit = false
  812. item.editContent = item.content
  813. if (item.role == 'assistant') {
  814. item.voiceFilePosition = element.voiceFilePosition
  815. }
  816. })
  817. this.returnMessage = [...this.returnMessage, ...history]
  818. }
  819. this.configForm.radio2 = this.modelList[0].id
  820. this.modelList.map(item => {
  821. if (item.id == array[array.length - 1].modelId) {
  822. this.configForm.radio2 = item.id
  823. }
  824. })
  825. })
  826. },
  827. searchHistory(characterId) {
  828. // 查看对话记录是否有选中的角色
  829. // console.log('查看对话记录是否有选中的角色');
  830. let flg = false
  831. for (let i = 0; i < this.allRecords.length; i++) {
  832. const element = this.allRecords[i];
  833. // 如果有则选中角色的最新一条记录并获取对话聊天记录
  834. if (element.id == characterId) {
  835. console.log(element, '查看对话记录是否有选中的角色');
  836. flg = true
  837. element.open = true
  838. // 二级对话判断当前是普通对话还是场景对话
  839. // 取对应的最后一条消息记录
  840. let chatCharacterList = element.chatCharacterList
  841. let chatCharacter = null
  842. let index = null
  843. for (let i = 0; i < chatCharacterList.length; i++) {
  844. const element2 = chatCharacterList[i];
  845. if (this.$route.query.sceneId == element2.sceneId) {
  846. chatCharacter = element2
  847. index = i
  848. } else if (!this.$route.query.sceneId && !element2.sceneId) {
  849. chatCharacter = element2
  850. index = i
  851. }
  852. }
  853. // 如果有聊天记录则点击
  854. if (chatCharacter) {
  855. this.clickDialogue(element, chatCharacter, index)
  856. } else {
  857. // 如果没有场景聊天记录则新增场景聊天
  858. // 从角色详情中获取场景列表
  859. let sceneList = this.info.sceneList
  860. // 获取当前聊天对应的场景
  861. for (let i = 0; i < sceneList.length; i++) {
  862. const element = sceneList[i];
  863. if (element.sceneId == this.$route.query.sceneId) {
  864. this.sceneInfo = element
  865. }
  866. }
  867. }
  868. // this.recordsIndex = element.chatCharacterList.length - 1
  869. // let recordId = element.chatCharacterList[element.chatCharacterList.length - 1].id
  870. // this.recordId = recordId
  871. // this.getChatRecord(recordId)
  872. }
  873. }
  874. // 如果没有聊天记录,则增加开场聊天引导
  875. if (!flg) {
  876. this.getGuidance(this.info.id)
  877. }
  878. },
  879. async getChatCharacterRecords() {
  880. let res = await getChatCharacterRecordsApi()
  881. console.log(res, '聊天记录');
  882. if (res.data) {
  883. for (let i = 0; i < res.data.length; i++) {
  884. const element = res.data[i];
  885. if (this.info.id == element.id) {
  886. element.open = true
  887. } else {
  888. element.open = false
  889. }
  890. }
  891. this.allRecords = res.data
  892. }
  893. },
  894. // 新增对话记录
  895. async addChat() {
  896. let params = {
  897. characterId: this.info.id,
  898. chatTitle: ""
  899. }
  900. if (this.sceneId) {
  901. params.sceneId = this.sceneId
  902. }
  903. let res = await addChatApi(params)
  904. console.log(res, '新增对话记录');
  905. return res.data
  906. },
  907. getModelList() {
  908. getModelListApi().then(res => {
  909. console.log(res, '模型列表');
  910. this.modelList = res.data
  911. })
  912. },
  913. inputBoxClick() {
  914. this.$refs.input.focus()
  915. },
  916. async getDetail(id) {
  917. let res = await detailApi(id)
  918. console.log(res, '角色详情');
  919. this.info = res.data
  920. this.$nextTick(() => {
  921. this.showPrologueButton = this.$refs.prologue.scrollHeight > this.$refs.prologue.offsetHeight
  922. })
  923. // 模型选中角色默认模型
  924. this.configForm.radio2 = this.info.modelId
  925. // 如果用户有设置过模型使用用户设置模型
  926. if (Cookies.get(`userConfig_${this.$store.state.user.userId}_${this.info.id}`)) {
  927. let userConfig = JSON.parse(Cookies.get(`userConfig_${this.$store.state.user.userId}_${this.info.id}`))
  928. if (userConfig.radio2) {
  929. this.configForm.radio2 = userConfig.radio2
  930. }
  931. }
  932. // let HistoryMessage = JSON.parse(localStorage.getItem(`[userId:${123},aiId:${this.info.id}]`));
  933. // if (HistoryMessage) {
  934. // this.returnMessage = HistoryMessage
  935. // } else {
  936. // this.returnMessage.push({
  937. // role: 'assistant',
  938. // content: this.info.firstContent
  939. // })
  940. // localStorage.setItem(`[userId:${123},aiId:${this.info.id}]`, JSON.stringify(this.returnMessage));
  941. // }
  942. if (this.sceneId) {
  943. console.log(this.sceneId);
  944. let sceneList = this.info.sceneList
  945. for (let i = 0; i < sceneList.length; i++) {
  946. const element = sceneList[i];
  947. if (element.sceneId == this.sceneId) {
  948. this.sceneInfo = element
  949. this.configForm.radio2 = element.modelId
  950. this.returnMessage.push({
  951. role: 'assistant',
  952. content: element.sceneWelcome
  953. })
  954. }
  955. }
  956. } else {
  957. console.log('fffffffffffffffffffffff');
  958. this.returnMessage.push({
  959. role: 'assistant',
  960. content: this.info.firstContent
  961. })
  962. }
  963. },
  964. getGuidance(id) {
  965. getGuidanceApi(id).then(res => {
  966. console.log(res, '聊天引导');
  967. this.messageOptions = res.data
  968. })
  969. },
  970. goBack() {
  971. this.$router.back()
  972. },
  973. async newStart() {
  974. // 新业务,创建一个新的对话记录
  975. this.recordId = await this.addChat()
  976. for (let i = 0; i < this.allRecords.length; i++) {
  977. const element = this.allRecords[i];
  978. if (element.id == this.info.id) {
  979. if (this.sceneId) {
  980. element.chatCharacterList.push({
  981. chatTitle: "场景聊天",
  982. createTime: '',
  983. id: this.recordId,
  984. sceneIcon: true
  985. })
  986. } else {
  987. element.chatCharacterList.push({
  988. chatTitle: "常规聊天",
  989. createTime: '',
  990. id: this.recordId
  991. })
  992. }
  993. this.recordsIndex = element.chatCharacterList.length - 1
  994. }
  995. }
  996. this.returnMessage = []
  997. this.getDetail(this.info.id)
  998. // 废弃》》
  999. // 清空历史数据
  1000. // localStorage.removeItem(`[userId:${123},aiId:${this.info.id}]`);
  1001. // this.returnMessage = []
  1002. // this.getDetail(this.info.id)
  1003. },
  1004. Enterkey(e) {
  1005. if (e.keyCode == 13) {
  1006. console.log(this.notChatSend, 'this.notChatSend');
  1007. if (this.notChatSend) {
  1008. return
  1009. }
  1010. if (!this.content) {
  1011. return
  1012. }
  1013. this.getStreamChatWithWeb()
  1014. }
  1015. },
  1016. // 设置用户配置
  1017. configConfirm() {
  1018. console.log(this.info, 'this.info');
  1019. // 在浏览器缓存用户的配置
  1020. Cookies.set(`userConfig_${this.$store.state.user.userId}_${this.info.id}`, JSON.stringify(this.configForm))
  1021. this.showConfig = false
  1022. },
  1023. // 前往创建场景
  1024. toCreateScene() {
  1025. this.$router.push({
  1026. name: 'CreateScene',
  1027. query: {
  1028. characterId: this.info.id,
  1029. }
  1030. })
  1031. },
  1032. async getStreamChatWithWeb_old() {
  1033. this.messageLoading = true
  1034. if (!this.content) {
  1035. return
  1036. }
  1037. this.returnMessage.push({
  1038. role: 'user',
  1039. content: this.content
  1040. })
  1041. let HistoryMessage = JSON.parse(localStorage.getItem(`[userId:${123},aiId:${this.info.id}]`));
  1042. // 历史记录取最新的20条传给后端
  1043. if (HistoryMessage && HistoryMessage.length > 20) {
  1044. HistoryMessage = HistoryMessage.slice(HistoryMessage.length - 1, 20)
  1045. }
  1046. let params = {
  1047. historyMessage: HistoryMessage || [],
  1048. characterId: this.info.id,
  1049. prompt: JSON.parse(JSON.stringify(this.content))
  1050. }
  1051. // 清空输入框的值
  1052. this.content = ''
  1053. // 新增一条ai信息
  1054. this.returnMessage.push({
  1055. role: 'assistant',
  1056. content: ''
  1057. })
  1058. let res = await streamChatWithWebApi(params)
  1059. console.log(res.data.message.content, 'res');
  1060. let content = res.data.message.content
  1061. let index = 0
  1062. let xing = 1
  1063. let messageInterval = setInterval(() => {
  1064. if (index > content.length - 1) {
  1065. console.log('结束');
  1066. // 消息全部显示后存到历史localStorage
  1067. let HistoryMessage = []
  1068. // 历史只存100条,长度超出截取最新的
  1069. if (this.returnMessage.length > 100) {
  1070. HistoryMessage = JSON.stringify(this.returnMessage.slice(this.returnMessage.length - 100, 100))
  1071. } else {
  1072. HistoryMessage = JSON.stringify(this.returnMessage)
  1073. }
  1074. localStorage.setItem(`[userId:${123},aiId:${this.info.id}]`, HistoryMessage);
  1075. clearInterval(messageInterval)
  1076. return
  1077. }
  1078. console.log(content[index], 'content[index]');
  1079. if (content[index] == "*") {
  1080. if (xing == 1) {
  1081. xing = 2
  1082. this.returnMessage[this.returnMessage.length - 1].content += "("
  1083. } else if (xing == 2) {
  1084. xing = 1
  1085. this.returnMessage[this.returnMessage.length - 1].content += ")"
  1086. }
  1087. } else {
  1088. this.returnMessage[this.returnMessage.length - 1].content += content[index]
  1089. }
  1090. index += 1
  1091. }, 50)
  1092. },
  1093. async getStreamChatWithWeb() {
  1094. this.audioUrl = []
  1095. this.audioUrlArrIndx = 0
  1096. this.textCache = ''
  1097. this.resError = false
  1098. this.messageLoading = true
  1099. this.chatLoading = true
  1100. // 初始化定时器和audio的状态
  1101. if (this.videoLoop) {
  1102. clearInterval(this.videoLoop)
  1103. this.videoLoop = null
  1104. this.audioUrl = ''
  1105. this.$refs.audio.currentTime = 0;
  1106. this.$refs.audio.pause()
  1107. }
  1108. // 如果没有对话记录id,新增对话记录
  1109. if (!this.recordId) {
  1110. this.recordId = await this.addChat()
  1111. }
  1112. if (this.content) {
  1113. this.returnMessage.push({
  1114. role: 'user',
  1115. content: this.content
  1116. })
  1117. }
  1118. let HistoryMessage = JSON.parse(JSON.stringify(this.returnMessage))
  1119. console.log(HistoryMessage, 'HistoryMessage1');
  1120. HistoryMessage = HistoryMessage.splice(1, HistoryMessage.length)
  1121. console.log(HistoryMessage, 'HistoryMessage2');
  1122. // 历史记录取最新的20条传给后端
  1123. if (HistoryMessage && HistoryMessage.length > 20) {
  1124. HistoryMessage = HistoryMessage.slice(HistoryMessage.length - 1, 20)
  1125. }
  1126. let params = {
  1127. historyMessage: HistoryMessage,
  1128. characterId: this.info.id,
  1129. prompt: this.content,
  1130. modelId: this.configForm.radio2,
  1131. recordId: this.recordId,
  1132. }
  1133. // 如果当前是场景对话则需要传sceneId
  1134. if (this.sceneId) {
  1135. params.sceneId = this.sceneId
  1136. }
  1137. // 新增一条ai信息
  1138. this.returnMessage.push({
  1139. role: 'assistant',
  1140. content: '',
  1141. voiceFilePosition: '',
  1142. })
  1143. // 清空输入框的值
  1144. this.content = ''
  1145. let res = await streamChatWithWebApi(params)
  1146. console.log(res, 'res');
  1147. this.messageLoading = false
  1148. if (res.status != 200) {
  1149. this.$message.error(res.statusText)
  1150. this.returnMessage.splice(this.returnMessage.length - 1, 2)
  1151. this.chatLoading = false
  1152. return
  1153. }
  1154. const reader = res.body.getReader()
  1155. const decoder = new TextDecoder()
  1156. while (1) {
  1157. const { done, value } = await reader.read()
  1158. // console.log(done, 'done');
  1159. if (done) {
  1160. console.log(value, '结束value');
  1161. this.chatLoading = false
  1162. // if (typeof(value) == "undefined") {
  1163. // this.$message.error('错误')
  1164. // this.returnMessage.splice(this.returnMessage.length - 1, 1)
  1165. // }
  1166. // 每次对话完刷新聊天记录
  1167. this.getChatCharacterRecords()
  1168. let params = {
  1169. characterId: this.info.id,
  1170. recordId: this.recordId
  1171. }
  1172. let historyRes = await getChatRecordApi(params)
  1173. this.historyRes = historyRes.data
  1174. // 如果开启自动播放,则查询并播放语音
  1175. // this.audioUrl = ""
  1176. // if (!this.resError) {
  1177. // }
  1178. console.log('结束');
  1179. break;
  1180. }
  1181. //txt就是一个一个的字 然后添加到页面上就可以了
  1182. const txt = decoder.decode(value).split('data:')
  1183. // 超时处理
  1184. if (this.timeOut) {
  1185. clearTimeout(this.timeOut)
  1186. this.timeOut = setTimeout(() => {
  1187. this.returnMessage[this.returnMessage.length - 1].content = 'AI智能累得打起了小盹儿,咱们让他缓一缓,再来试试!'
  1188. this.chatLoading = false
  1189. }, 10000)
  1190. }
  1191. // const txt = decoder.decode(value)
  1192. this.addMessage(txt)
  1193. // let data = JSON.parse(txt).message.content
  1194. // console.log(txt, 'txt');
  1195. // if (this.isJSON(txt) && JSON.parse(txt).code != 200) {
  1196. // let data = JSON.parse(txt)
  1197. // this.$message.error(data.msg)
  1198. // this.returnMessage.splice(this.returnMessage.length - 1, 1)
  1199. // break;
  1200. // }
  1201. }
  1202. },
  1203. addMessage(value) {
  1204. // console.log(value, 'value');
  1205. for (let i = 0; i < value.length; i++) {
  1206. const element = value[i];
  1207. if (this.isJSON(element)) {
  1208. let value = JSON.parse(element)
  1209. if (value.code == 401) {
  1210. this.returnMessage.splice(this.returnMessage.length - 1, 1)
  1211. this.noLogin(element)
  1212. this.chatLoading = false
  1213. break;
  1214. }
  1215. if (value.code == 500) {
  1216. console.log(value, 'JSON.parse(element)');
  1217. this.returnMessage.splice(this.returnMessage.length - 2, 2)
  1218. this.$message.error(value.content || '系统错误请联系管理员')
  1219. this.resError = true
  1220. this.chatLoading = false
  1221. break;
  1222. }
  1223. // 获取语音id
  1224. if (value.code == -1) {
  1225. this.returnMessage[this.returnMessage.length - 1].uuid = value.content
  1226. // 如果开启了自动播放开始轮巡查询语音
  1227. if (this.setting.value1) {
  1228. this.audioUrlArrIndx = 0
  1229. this.videoLoopTime = 0
  1230. this.audioUrlArr = []
  1231. this.loopPlay = false
  1232. this.audioPlayIndex = this.returnMessage.length - 1
  1233. this.loopGetVoice(value.content, this.returnMessage.length - 1)
  1234. }
  1235. break;
  1236. }
  1237. if (!value.done) {
  1238. let txt = value.content
  1239. this.returnMessage[this.returnMessage.length - 1].content += txt
  1240. } else {
  1241. // 对话流结束
  1242. // 临时插入的ai对话记录和用户记录插入聊天id
  1243. this.returnMessage[this.returnMessage.length - 1].id = value.content
  1244. this.returnMessage[this.returnMessage.length - 2].id = value.content
  1245. }
  1246. }
  1247. }
  1248. },
  1249. noLogin(value) {
  1250. let _this = this
  1251. MessageBox.confirm(
  1252. value.content,
  1253. "系统提示",
  1254. {
  1255. confirmButtonText: "前往登录",
  1256. cancelButtonText: "取消",
  1257. type: "warning",
  1258. }
  1259. )
  1260. .then(() => {
  1261. // isRelogin.show = false;
  1262. this.$store.dispatch("LogOut").then(() => {
  1263. // location.href = "/";
  1264. _this.$refs.Header.showLogin()
  1265. });
  1266. })
  1267. .catch(() => {
  1268. // isRelogin.show = false;
  1269. });
  1270. },
  1271. isJSON(str) {
  1272. if (typeof str == 'string') {
  1273. try {
  1274. JSON.parse(str);
  1275. return true;
  1276. } catch (e) {
  1277. // console.log(e);
  1278. return false;
  1279. }
  1280. }
  1281. },
  1282. messageOptionClick(value) {
  1283. this.content = value
  1284. this.getStreamChatWithWeb()
  1285. },
  1286. // 播放聊天语音
  1287. initAudio(index) {
  1288. // 从聊天记录获取id
  1289. console.log(this.returnMessage[index], 'swssssssss');
  1290. let id = this.returnMessage[index].id
  1291. let history = this.historyRes.map((item) => {
  1292. if (item.id == id) {
  1293. return item
  1294. }
  1295. })
  1296. console.log(history, 'history');
  1297. return
  1298. this.loopGetVoice(history.id, index)
  1299. },
  1300. playAudio(index) {
  1301. if (index) {
  1302. this.audioPlayIndex = index
  1303. }
  1304. // this.audioUrlArrIndx = 0
  1305. console.log(this.audioUrlArrIndx, 'this.audioUrlArrIndx');
  1306. console.log(this.audioUrlArr, 'this.audioUrlArr');
  1307. // console.log(audioUrlArr[this.audioUrlArrIndx].voiceAddress, 'audioUrlArr[this.audioUrlArrIndx].voiceAddress');
  1308. // 如果当前播放的语音地址为空,则等待2秒后重新获取
  1309. if (!this.audioUrlArr[this.audioUrlArrIndx].voiceAddress && this.videoLoopTime <= 50) {
  1310. setTimeout(() => {
  1311. this.playAudio(index)
  1312. }, 2000)
  1313. return
  1314. }
  1315. // 如果当前播放的语音地址为false,则跳过当前语音
  1316. if (this.audioUrlArr[this.audioUrlArrIndx].voiceAddress == 'false' && this.audioUrlArrIndx < this.audioUrlArr.length - 1) {
  1317. this.audioUrlArrIndx += 1
  1318. this.playAudio(index)
  1319. return
  1320. } else if (this.audioUrlArrIndx > this.audioUrlArr.length - 1) {
  1321. this.audioUrl = ''
  1322. this.audioUrlArrIndx = 0
  1323. this.videoLoopTime = 0
  1324. this.audioUrlArr = []
  1325. return
  1326. }
  1327. this.audioUrl = this.audioUrlArr[this.audioUrlArrIndx].voiceAddress
  1328. setTimeout(() => {
  1329. if (this.audioUrl && this.audioUrl != 'false') {
  1330. this.$refs.audio.play()
  1331. } else {
  1332. this.audioClose()
  1333. }
  1334. }, 1000)
  1335. },
  1336. async historyGetVoice(value, index) {
  1337. console.log(value, 'value');
  1338. if (!value.uuid) {
  1339. return
  1340. }
  1341. let params = {
  1342. uuid: value.uuid
  1343. }
  1344. let fileRes = await queryVoiceListApi(params)
  1345. console.log(fileRes.data, 'file>>>');
  1346. this.audioUrlArr = fileRes.data.voices
  1347. this.audioUrlArrIndx = 0
  1348. this.videoLoopTime = 0
  1349. this.loopPlay = false
  1350. this.playAudio(index)
  1351. },
  1352. async loopGetVoice(id, index) {
  1353. this.videoLoopTime += 1
  1354. // 获取语音文件
  1355. let params = {
  1356. uuid: id
  1357. }
  1358. let fileRes = await queryVoiceListApi(params)
  1359. console.log(fileRes.data, 'file>>>');
  1360. let voices = fileRes.data.voices
  1361. let _continue = fileRes.data.continue
  1362. this.apiHaveVideo = _continue
  1363. // 判断语音文件是否获取完毕
  1364. let trueLength = 0
  1365. for (let i = 0; i < voices.length; i++) {
  1366. const element = voices[i];
  1367. if (element.voiceAddress) {
  1368. trueLength += 1
  1369. }
  1370. }
  1371. if (!_continue && trueLength == voices.length) {
  1372. if (this.videoLoop) {
  1373. clearTimeout(this.videoLoop)
  1374. this.videoLoop = null
  1375. }
  1376. // this.returnMessage[index].voiceFilePosition = fileRes.data.voiceFilePosition
  1377. // 有语音文件则开始播放
  1378. if (voices.length > 0) {
  1379. this.audioUrlArr = voices
  1380. console.log(this.audioUrlArr, '进入播放流程1');
  1381. if (this.$refs.audio.paused || this.audioUrlArrIndx > this.audioUrlArr.length - 1) {
  1382. if (this.loopPlay == false) {
  1383. this.playAudio(index)
  1384. this.loopPlay = true
  1385. }
  1386. }
  1387. }
  1388. } else {
  1389. if (voices.length > 0) {
  1390. this.audioUrlArr = voices
  1391. console.log(this.audioUrlArr, '进入播放流程2');
  1392. if (this.$refs.audio.paused || this.audioUrlArrIndx > this.audioUrlArr.length - 1) {
  1393. if (this.loopPlay == false) {
  1394. this.playAudio(index)
  1395. this.loopPlay = true
  1396. }
  1397. }
  1398. }
  1399. // 没有语音文件则开启定时器每两秒查询一次
  1400. if (this.videoLoopTime > 50) {
  1401. // 如果调用次数超过30次则停止
  1402. return
  1403. }
  1404. this.videoLoop = setTimeout(() => {
  1405. this.loopGetVoice(id, index)
  1406. }, 2000)
  1407. }
  1408. },
  1409. audioEnd() {
  1410. this.audioUrlArrIndx += 1
  1411. console.log(this.audioUrlArrIndx, 'audioUrlArrIndx');
  1412. if (this.audioUrlArrIndx < this.audioUrlArr.length && this.videoLoopTime <= 50) {
  1413. // 如果当前播放的语音地址为空,则等待2秒后重新获取
  1414. if (!this.audioUrlArr[this.audioUrlArrIndx].voiceAddress) {
  1415. setTimeout(() => {
  1416. this.playAudio()
  1417. }, 2000)
  1418. return
  1419. }
  1420. // 如果当前播放的语音地址为false,则跳过当前语音
  1421. if (this.audioUrlArr[this.audioUrlArrIndx].voiceAddress == 'false' && this.audioUrlArrIndx < this.audioUrlArr.length - 1) {
  1422. this.audioUrlArrIndx += 1
  1423. this.playAudio()
  1424. return
  1425. }
  1426. this.audioUrl = this.audioUrlArr[this.audioUrlArrIndx].voiceAddress
  1427. setTimeout(() => {
  1428. if (this.audioUrl && this.audioUrl != 'false') {
  1429. this.$refs.audio.play()
  1430. } else {
  1431. this.audioClose()
  1432. }
  1433. }, 100)
  1434. } else {
  1435. this.audioClose()
  1436. }
  1437. },
  1438. audioClose() {
  1439. this.audioUrlArrIndx = 0
  1440. this.audioPlayIndex = null
  1441. },
  1442. ChangeSettingValue1(value) {
  1443. console.log(value, 'value');
  1444. if (value) {
  1445. queryUserBalanceApi().then(res => {
  1446. console.log(res, '用户余额');
  1447. if (res.data <= 0) {
  1448. this.setting.value1 = false
  1449. this.$message({
  1450. message: '余额不足,无法开启次功能',
  1451. type: 'error'
  1452. });
  1453. }
  1454. })
  1455. }
  1456. },
  1457. // 场景业务相关↓
  1458. async sceneChange(item) {
  1459. console.log(item, 'item');
  1460. this.$message({
  1461. message: `已进入场景${item.sceneName}`,
  1462. type: 'success'
  1463. });
  1464. // 当用户选中场景
  1465. this.sceneInfo = item
  1466. // 切换当前场景id
  1467. this.sceneId = item.sceneId
  1468. // 情况聊天记录
  1469. this.returnMessage = []
  1470. // 新建场景对话
  1471. this.newStart()
  1472. // 增加开场白
  1473. // this.returnMessage.push({
  1474. // role: 'assistant',
  1475. // content: item.sceneWelcome
  1476. // })
  1477. }
  1478. }
  1479. }
  1480. </script>
  1481. <style lang="scss" scoped>
  1482. .fullScreenButton {
  1483. position: absolute;
  1484. top: 50%;
  1485. left: 0;
  1486. transform: translateX(-50%) translateY(-50%);
  1487. z-index: 50;
  1488. background: #fff;
  1489. display: flex;
  1490. justify-content: center;
  1491. align-items: center;
  1492. width: 1.6rem;
  1493. height: 1.6rem;
  1494. cursor: pointer;
  1495. }
  1496. .chat {
  1497. height: 100vh;
  1498. }
  1499. .leftInfo {
  1500. width: 375px;
  1501. height: 100%;
  1502. padding: 32px 24px;
  1503. overflow-y: auto;
  1504. >.history {
  1505. width: 100%;
  1506. height: 100%;
  1507. display: flex;
  1508. flex-direction: column;
  1509. >.historyBox {
  1510. padding: 8px;
  1511. border-radius: 15px;
  1512. border: solid 1px var(--color1);
  1513. margin-bottom: 10px;
  1514. .top {
  1515. >.photo {
  1516. >img {
  1517. width: 48px;
  1518. height: 48px;
  1519. border-radius: 50%;
  1520. }
  1521. }
  1522. >.info {
  1523. width: calc(100% - 60px - 16px);
  1524. >div {
  1525. overflow: hidden;
  1526. /* 确保超出容器的文本被裁剪 */
  1527. white-space: nowrap;
  1528. /* 确保文本在一行内显示 */
  1529. text-overflow: ellipsis;
  1530. /* 使用省略号表示文本超出 */
  1531. }
  1532. }
  1533. }
  1534. }
  1535. }
  1536. >.chatInfo {
  1537. overflow: hidden;
  1538. .chatInfo_photo {
  1539. >img {}
  1540. }
  1541. .hot {
  1542. color: #f03d3d;
  1543. background: #fff1f1;
  1544. }
  1545. .tags {
  1546. margin-top: 10px;
  1547. .tag {
  1548. display: inline-flex;
  1549. justify-content: center;
  1550. padding: 5px 10px;
  1551. border-radius: 4px;
  1552. border: solid 1px var(--color1);
  1553. margin-right: 5px;
  1554. }
  1555. }
  1556. }
  1557. }
  1558. .content {
  1559. z-index: 40;
  1560. position: absolute;
  1561. right: 0;
  1562. background-repeat: no-repeat;
  1563. // background-attachment: fixed;
  1564. background-size: cover;
  1565. background-color: #fff;
  1566. background-position: center center;
  1567. display: flex;
  1568. height: 100%;
  1569. justify-content: center;
  1570. width: calc(100% - 375px);
  1571. border-left: solid 1px var(--color1);
  1572. transition: width 0.5s ease;
  1573. >.chat-box {
  1574. position: relative;
  1575. background: linear-gradient(to bottom, rgba(0, 0, 0, 0.4) 0%, rgba(255, 255, 255, 0) 20%, rgba(255, 255, 255, 0) 75%, rgba(0, 0, 0, 0.4) 100%);
  1576. background-size: auto;
  1577. // border: 1px solid #635677 !important;
  1578. box-sizing: border-box;
  1579. display: flex;
  1580. flex-direction: column;
  1581. height: 100%;
  1582. width: 100%;
  1583. padding: 55px 25px 170px 25px;
  1584. >.chatBoxTitle {
  1585. color: #fff;
  1586. margin-top: 15px;
  1587. width: calc(100% - 50px);
  1588. height: 30px;
  1589. display: flex;
  1590. justify-content: space-between;
  1591. align-items: center;
  1592. }
  1593. }
  1594. >.mask {
  1595. background: rgba(0, 0, 0, 0.5);
  1596. }
  1597. }
  1598. .contentFull {
  1599. width: 100%;
  1600. }
  1601. .historyActive {
  1602. background: var(--color1);
  1603. }
  1604. .dialogue:hover {
  1605. background: var(--color1);
  1606. .dialogueIconBg {
  1607. display: flex;
  1608. }
  1609. }
  1610. .dialogueIconBg {
  1611. display: none;
  1612. background: linear-gradient(90deg, rgba(0, 0, 0, 0) 10%, var(--color1) 50%, var(--color1) 100%);
  1613. }
  1614. .messages {
  1615. height: calc(100% - 68px);
  1616. overflow-y: auto;
  1617. }
  1618. .messages::-webkit-scrollbar {
  1619. width: 0px;
  1620. }
  1621. .messageRight {
  1622. max-width: calc(100% - 72px);
  1623. // min-width: 400px;
  1624. }
  1625. .yyPlayBg {
  1626. // background: rgba(103, 103, 168, 1);
  1627. // border-radius: 999px;
  1628. // position: absolute;
  1629. .icon {
  1630. height: 24px;
  1631. width: 96px;
  1632. }
  1633. }
  1634. .yyPlay {
  1635. width: 15px;
  1636. height: 15px;
  1637. // background: url('../../assets/images/播放/yyPlay.gif') no-repeat;
  1638. // background-size: 170% 170%;
  1639. // background-position: 50% 50%;
  1640. // animation: heart-burst steps(28) 0.9s infinite both;
  1641. // animation-play-state: paused;
  1642. // height: 50px;
  1643. // background: #fff;
  1644. }
  1645. .message {
  1646. // background: #ffffff0f;
  1647. // border: 1px solid #5b5b5e;
  1648. background: var(--bg-color3);
  1649. color: #fff;
  1650. font-size: 14px;
  1651. // flex: 1;
  1652. }
  1653. .messageUser {
  1654. background: rgba(255, 255, 255, 0.9);
  1655. border: 1px solid var(--color1);
  1656. // max-width: calc(100% - 72px);
  1657. font-size: 14px;
  1658. width: auto;
  1659. // min-width: 400px;
  1660. }
  1661. .photo {
  1662. width: 56px;
  1663. }
  1664. .me {
  1665. flex-direction: row-reverse;
  1666. }
  1667. .loadingMessage {
  1668. position: relative;
  1669. // display: flex;
  1670. // justify-content: center;
  1671. width: 120px;
  1672. height: 64px;
  1673. }
  1674. .messageOptions {
  1675. // margin-top: 20px;
  1676. display: flex;
  1677. flex-wrap: wrap;
  1678. justify-content: space-around;
  1679. min-width: 400px;
  1680. .option {
  1681. background: #ade4ff32;
  1682. // border: 1px solid #5b5b5e;
  1683. border-radius: 10px;
  1684. padding: 10px;
  1685. width: 40%;
  1686. height: 50px;
  1687. margin-top: 10px;
  1688. cursor: pointer;
  1689. }
  1690. }
  1691. .inputBox {
  1692. border: solid 1px var(--color1);
  1693. border-radius: 10px;
  1694. padding: 24px;
  1695. background: #fff;
  1696. max-width: 900px;
  1697. margin: 0 auto;
  1698. }
  1699. .planeColor {
  1700. color: var(--bg-color1);
  1701. }
  1702. .prologue {
  1703. position: relative;
  1704. display: -webkit-box;
  1705. -webkit-line-clamp: 3;
  1706. -webkit-box-orient: vertical;
  1707. overflow: hidden;
  1708. padding-bottom: 4px;
  1709. .textButton {
  1710. // display: inline-block;
  1711. float: right;
  1712. clear: both;
  1713. color: #fff;
  1714. padding: 0 5px;
  1715. border-radius: 5px;
  1716. background: var(--bg-color1);
  1717. font-size: 12px;
  1718. cursor: pointer;
  1719. }
  1720. }
  1721. .prologueOpen {
  1722. -webkit-line-clamp: 20;
  1723. }
  1724. .prologueOpen::before {}
  1725. .prologue::before {
  1726. content: '';
  1727. float: right;
  1728. // width: 10px;
  1729. height: calc(100% - 20px);
  1730. /*先随便设置一个高度*/
  1731. background: red
  1732. }
  1733. </style>
  1734. <style scoped>
  1735. .loadingMessage>>>.el-loading-parent--relative {
  1736. width: 100%;
  1737. height: 100%;
  1738. }
  1739. .loadingMessage>>>.el-loading-spinner {
  1740. display: flex;
  1741. justify-content: center;
  1742. }
  1743. .mButton {
  1744. border: solid 0px;
  1745. }
  1746. .radio>>>.el-radio-button {
  1747. margin-bottom: 5px;
  1748. margin-right: 10px;
  1749. /* border-left: solid 1px #DCDFE6; */
  1750. }
  1751. .radio>>>.el-radio-button__inner {
  1752. border-left: solid 1px #DCDFE6;
  1753. border-radius: 4px;
  1754. }
  1755. .radio>>>.el-radio-button__orig-radio:checked+.el-radio-button__inner {
  1756. border-left: solid 1px var(--bg-color1);
  1757. background-color: var(--bg-color1);
  1758. border-color: var(--bg-color1);
  1759. -webkit-box-shadow: -1px 0 0 0 var(--bg-color1);
  1760. box-shadow: -1px 0 0 0 var(--bg-color1);
  1761. }
  1762. .message:hover>>>.messageButton,
  1763. .messageUser:hover>>>.messageButton {
  1764. display: block;
  1765. }
  1766. .messageButton {
  1767. display: none;
  1768. width: 25px;
  1769. height: 25px;
  1770. padding: 0;
  1771. }
  1772. .messageDropdown {
  1773. position: absolute;
  1774. bottom: -12px;
  1775. right: 10px;
  1776. }
  1777. .eidtTextarea>>>.el-textarea__inner {
  1778. padding: 0;
  1779. background: none;
  1780. border: 0;
  1781. color: #fff;
  1782. }
  1783. .messageUser>>>.el-textarea__inner {
  1784. color: #000;
  1785. }
  1786. .messageEdit {
  1787. min-width: 400px;
  1788. }
  1789. </style>