Conversation.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. <!-- AI 对话 -->
  2. <template>
  3. <el-aside width="260px" class="conversation-container" style="height: 100%;">
  4. <!-- 左顶部:对话 -->
  5. <div style="height: 100%;">
  6. <el-button class="w-1/1 btn-new-conversation" type="primary" @click="createConversation">
  7. <Icon icon="ep:plus" class="mr-5px"/>
  8. 新建对话
  9. </el-button>
  10. <!-- 左顶部:搜索对话 -->
  11. <el-input
  12. v-model="searchName"
  13. size="large"
  14. class="mt-10px search-input"
  15. placeholder="搜索历史记录"
  16. @keyup="searchConversation"
  17. >
  18. <template #prefix>
  19. <Icon icon="ep:search"/>
  20. </template>
  21. </el-input>
  22. <!-- 左中间:对话列表 -->
  23. <div class="conversation-list">
  24. <el-empty v-if="loading" description="." :v-loading="loading" />
  25. <!-- TODO done @fain:置顶、聊天记录、一星期钱、30天前,前端对数据重新做一下分组,或者后端接口改一下 -->
  26. <div v-for="conversationKey in Object.keys(conversationMap)" :key="conversationKey">
  27. <div class="conversation-item classify-title" v-if="conversationMap[conversationKey].length">
  28. <el-text class="mx-1" size="small" tag="b">{{ conversationKey }}</el-text>
  29. </div>
  30. <div
  31. class="conversation-item"
  32. v-for="conversation in conversationMap[conversationKey]"
  33. :key="conversation.id"
  34. @click="handleConversationClick(conversation.id)"
  35. @mouseover="hoverConversationId = conversation.id"
  36. @mouseout="hoverConversationId = ''"
  37. >
  38. <div
  39. :class="conversation.id === activeConversationId ? 'conversation active' : 'conversation'"
  40. >
  41. <div class="title-wrapper">
  42. <img class="avatar" :src="conversation.roleAvatar"/>
  43. <span class="title">{{ conversation.title }}</span>
  44. </div>
  45. <!-- TODO done @fan:缺一个【置顶】按钮,效果改成 hover 上去展示 -->
  46. <div class="button-wrapper" v-show="hoverConversationId === conversation.id">
  47. <el-button class="btn" link @click.stop="handlerTop(conversation)" >
  48. <el-icon title="置顶" v-if="!conversation.pinned"><Top /></el-icon>
  49. <el-icon title="置顶" v-if="conversation.pinned"><Bottom /></el-icon>
  50. </el-button>
  51. <el-button class="btn" link @click.stop="updateConversationTitle(conversation)">
  52. <el-icon title="编辑" >
  53. <Icon icon="ep:edit"/>
  54. </el-icon>
  55. </el-button>
  56. <el-button class="btn" link @click.stop="deleteChatConversation(conversation)">
  57. <el-icon title="删除会话" >
  58. <Icon icon="ep:delete"/>
  59. </el-icon>
  60. </el-button>
  61. </div>
  62. </div>
  63. </div>
  64. </div>
  65. <!-- 底部站位 -->
  66. <div style="height: 160px; width: 100%;"></div>
  67. </div>
  68. </div>
  69. <!-- 左底部:工具栏 -->
  70. <div class="tool-box">
  71. <div @click="handleRoleRepository">
  72. <Icon icon="ep:user"/>
  73. <el-text size="small">角色仓库</el-text>
  74. </div>
  75. <div @click="handleClearConversation">
  76. <Icon icon="ep:delete"/>
  77. <el-text size="small">清空未置顶对话</el-text>
  78. </div>
  79. </div>
  80. <!-- ============= 额外组件 ============= -->
  81. <!-- 角色仓库抽屉 -->
  82. <el-drawer v-model="drawer" title="角色仓库" size="754px">
  83. <Role/>
  84. </el-drawer>
  85. </el-aside>
  86. </template>
  87. <script setup lang="ts">
  88. import {ChatConversationApi, ChatConversationVO} from '@/api/ai/chat/conversation'
  89. import {ref} from "vue";
  90. import Role from "@/views/ai/chat/role/index.vue";
  91. import {Bottom, Top} from "@element-plus/icons-vue";
  92. const message = useMessage() // 消息弹窗
  93. // 定义属性
  94. const searchName = ref<string>('') // 对话搜索
  95. const activeConversationId = ref<string | null>(null) // 选中的对话,默认为 null
  96. const hoverConversationId = ref<string | null>(null) // 悬浮上去的对话
  97. const conversationList = ref([] as ChatConversationVO[]) // 对话列表
  98. const conversationMap = ref<any>({}) // 对话分组 (置顶、今天、三天前、一星期前、一个月前)
  99. const drawer = ref<boolean>(false) // 角色仓库抽屉
  100. const loading = ref<boolean>(false) // 加载中
  101. const loadingTime = ref<any>() // 加载中定时器
  102. // 定义组件 props
  103. const props = defineProps({
  104. activeId: {
  105. type: String || null,
  106. required: true
  107. }
  108. })
  109. // 定义钩子
  110. const emits = defineEmits(['onConversationClick', 'onConversationClear', 'onConversationDelete'])
  111. /**
  112. * 对话 - 搜索
  113. */
  114. const searchConversation = async (e) => {
  115. // 恢复数据
  116. if (!searchName.value.trim().length) {
  117. conversationMap.value = await conversationTimeGroup(conversationList.value)
  118. } else {
  119. // 过滤
  120. const filterValues = conversationList.value.filter(item => {
  121. return item.title.includes(searchName.value.trim())
  122. })
  123. conversationMap.value = await conversationTimeGroup(filterValues)
  124. }
  125. }
  126. /**
  127. * 对话 - 点击
  128. */
  129. const handleConversationClick = async (id: string) => {
  130. // 过滤出选中的对话
  131. const filterConversation = conversationList.value.filter(item => {
  132. return item.id === id
  133. })
  134. // 回调 onConversationClick
  135. const res = emits('onConversationClick', filterConversation[0])
  136. // 切换对话
  137. if (res) {
  138. activeConversationId.value = id
  139. }
  140. }
  141. /**
  142. * 对话 - 获取列表
  143. */
  144. const getChatConversationList = async () => {
  145. try {
  146. // 0、加载中
  147. loadingTime.value = setTimeout(() => {
  148. loading.value = true
  149. }, 50)
  150. // 1、获取 对话数据
  151. const res = await ChatConversationApi.getChatConversationMyList()
  152. // 2、排序
  153. res.sort((a, b) => {
  154. return b.createTime - a.createTime
  155. })
  156. conversationList.value = res
  157. // 3、默认选中
  158. if (!activeId?.value) {
  159. await handleConversationClick(res[0].id)
  160. } else {
  161. // tip: 删除的刚好是选中的,那么需要重新挑选一个来进行选中
  162. const filterConversationList = conversationList.value.filter(item => {
  163. return item.id === activeId.value
  164. })
  165. if (filterConversationList.length <= 0) {
  166. await handleConversationClick(res[0].id)
  167. }
  168. }
  169. // 4、没有 任何对话情况
  170. if (conversationList.value.length === 0) {
  171. activeConversationId.value = null
  172. conversationMap.value = {}
  173. return
  174. }
  175. // 5、对话根据时间分组(置顶、今天、一天前、三天前、七天前、30天前)
  176. conversationMap.value = await conversationTimeGroup(conversationList.value)
  177. } finally {
  178. // 清理定时器
  179. if (loadingTime.value) {
  180. clearTimeout(loadingTime.value)
  181. }
  182. // 加载完成
  183. loading.value = false
  184. }
  185. }
  186. const conversationTimeGroup = async (list: ChatConversationVO[]) => {
  187. // 排序、指定、时间分组(今天、一天前、三天前、七天前、30天前)
  188. const groupMap = {
  189. '置顶': [],
  190. '今天': [],
  191. '一天前': [],
  192. '三天前': [],
  193. '七天前': [],
  194. '三十天前': []
  195. }
  196. // 当前时间的时间戳
  197. const now = Date.now();
  198. // 定义时间间隔常量(单位:毫秒)
  199. const oneDay = 24 * 60 * 60 * 1000;
  200. const threeDays = 3 * oneDay;
  201. const sevenDays = 7 * oneDay;
  202. const thirtyDays = 30 * oneDay;
  203. for (const conversation: ChatConversationVO of list) {
  204. // 置顶
  205. if (conversation.pinned) {
  206. groupMap['置顶'].push(conversation)
  207. continue
  208. }
  209. // 计算时间差(单位:毫秒)
  210. const diff = now - conversation.updateTime;
  211. // 根据时间间隔判断
  212. if (diff < oneDay) {
  213. groupMap['今天'].push(conversation)
  214. } else if (diff < threeDays) {
  215. groupMap['一天前'].push(conversation)
  216. } else if (diff < sevenDays) {
  217. groupMap['三天前'].push(conversation)
  218. } else if (diff < thirtyDays) {
  219. groupMap['七天前'].push(conversation)
  220. } else {
  221. groupMap['三十天前'].push(conversation)
  222. }
  223. }
  224. console.log('----groupMap', groupMap)
  225. return groupMap
  226. }
  227. /**
  228. * 对话 - 新建
  229. */
  230. const createConversation = async () => {
  231. // 1、新建对话
  232. const conversationId = await ChatConversationApi.createChatConversationMy(
  233. {} as unknown as ChatConversationVO
  234. )
  235. // 2、获取对话内容
  236. await getChatConversationList()
  237. // 3、选中对话
  238. await handleConversationClick(conversationId)
  239. }
  240. /**
  241. * 对话 - 更新标题
  242. */
  243. const updateConversationTitle = async (conversation: ChatConversationVO) => {
  244. // 1、二次确认
  245. const {value} = await ElMessageBox.prompt('修改标题', {
  246. inputPattern: /^[\s\S]*.*\S[\s\S]*$/, // 判断非空,且非空格
  247. inputErrorMessage: '标题不能为空',
  248. inputValue: conversation.title
  249. })
  250. // 2、发起修改
  251. await ChatConversationApi.updateChatConversationMy({
  252. id: conversation.id,
  253. title: value
  254. } as ChatConversationVO)
  255. message.success('重命名成功')
  256. // 刷新列表
  257. await getChatConversationList()
  258. // 过滤当前切换的
  259. const filterConversationList = conversationList.value.filter(item => {
  260. return item.id === conversation.id
  261. })
  262. if (filterConversationList.length > 0) {
  263. emits('onConversationClick', filterConversationList[0])
  264. }
  265. }
  266. /**
  267. * 删除聊天会话
  268. */
  269. const deleteChatConversation = async (conversation: ChatConversationVO) => {
  270. try {
  271. // 删除的二次确认
  272. await message.delConfirm(`是否确认删除会话 - ${conversation.title}?`)
  273. // 发起删除
  274. await ChatConversationApi.deleteChatConversationMy(conversation.id)
  275. message.success('会话已删除')
  276. // 刷新列表
  277. await getChatConversationList()
  278. // 回调
  279. emits('onConversationDelete', conversation)
  280. } catch {
  281. }
  282. }
  283. /**
  284. * 对话置顶
  285. */
  286. const handlerTop = async (conversation: ChatConversationVO) => {
  287. // 更新对话置顶
  288. conversation.pinned = !conversation.pinned
  289. await ChatConversationApi.updateChatConversationMy(conversation)
  290. // 刷新对话
  291. await getChatConversationList()
  292. }
  293. // ============ 角色仓库
  294. /**
  295. * 角色仓库抽屉
  296. */
  297. const handleRoleRepository = async () => {
  298. drawer.value = !drawer.value
  299. }
  300. // ============= 清空对话
  301. /**
  302. * 清空对话
  303. */
  304. const handleClearConversation = async () => {
  305. ElMessageBox.confirm(
  306. '确认后对话会全部清空,置顶的对话除外。',
  307. '确认提示',
  308. {
  309. confirmButtonText: '确认',
  310. cancelButtonText: '取消',
  311. type: 'warning',
  312. })
  313. .then(async () => {
  314. await ChatConversationApi.deleteMyAllExceptPinned()
  315. ElMessage({
  316. message: '操作成功!',
  317. type: 'success'
  318. })
  319. // 清空 对话 和 对话内容
  320. activeConversationId.value = null
  321. // 获取 对话列表
  322. await getChatConversationList()
  323. // 回调 方法
  324. emits('onConversationClear')
  325. })
  326. .catch(() => {
  327. })
  328. }
  329. // ============ 组件 onMounted
  330. const { activeId } = toRefs(props)
  331. watch(activeId, async (newValue, oldValue) => {
  332. // 更新选中
  333. activeConversationId.value = newValue as string
  334. })
  335. onMounted(async () => {
  336. // 默认选中
  337. if (props.activeId != null) {
  338. activeConversationId.value = props.activeId
  339. }
  340. // 获取 对话列表
  341. await getChatConversationList()
  342. })
  343. </script>
  344. <style scoped lang="scss">
  345. .conversation-container {
  346. position: relative;
  347. display: flex;
  348. flex-direction: column;
  349. justify-content: space-between;
  350. padding: 0 10px;
  351. padding-top: 10px;
  352. overflow: hidden;
  353. .btn-new-conversation {
  354. padding: 18px 0;
  355. }
  356. .search-input {
  357. margin-top: 20px;
  358. }
  359. .conversation-list {
  360. overflow: auto;
  361. height: 100%;
  362. .classify-title {
  363. padding-top: 10px;
  364. }
  365. .conversation-item {
  366. margin-top: 5px;
  367. }
  368. .conversation {
  369. display: flex;
  370. flex-direction: row;
  371. justify-content: space-between;
  372. flex: 1;
  373. padding: 0 5px;
  374. cursor: pointer;
  375. border-radius: 5px;
  376. align-items: center;
  377. line-height: 30px;
  378. &.active {
  379. background-color: #e6e6e6;
  380. .button {
  381. display: inline-block;
  382. }
  383. }
  384. .title-wrapper {
  385. display: flex;
  386. flex-direction: row;
  387. align-items: center;
  388. }
  389. .title {
  390. padding: 2px 10px;
  391. max-width: 220px;
  392. font-size: 14px;
  393. font-weight: 400;
  394. color: rgba(0, 0, 0, 0.77);
  395. overflow: hidden;
  396. white-space: nowrap;
  397. text-overflow: ellipsis;
  398. }
  399. .avatar {
  400. width: 25px;
  401. height: 25px;
  402. border-radius: 5px;
  403. display: flex;
  404. flex-direction: row;
  405. justify-items: center;
  406. }
  407. // 对话编辑、删除
  408. .button-wrapper {
  409. right: 2px;
  410. display: flex;
  411. flex-direction: row;
  412. justify-items: center;
  413. color: #606266;
  414. .btn {
  415. margin: 0;
  416. }
  417. }
  418. }
  419. }
  420. // 角色仓库、清空未设置对话
  421. .tool-box {
  422. position: absolute;
  423. bottom: 0;
  424. left: 0;
  425. right: 0;
  426. //width: 100%;
  427. padding: 0 20px;
  428. background-color: #f4f4f4;
  429. box-shadow: 0 0 1px 1px rgba(228, 228, 228, 0.8);
  430. line-height: 35px;
  431. display: flex;
  432. justify-content: space-between;
  433. align-items: center;
  434. color: var(--el-text-color);
  435. > div {
  436. display: flex;
  437. align-items: center;
  438. color: #606266;
  439. padding: 0;
  440. margin: 0;
  441. cursor: pointer;
  442. > span {
  443. margin-left: 5px;
  444. }
  445. }
  446. }
  447. }
  448. </style>