Conversation.vue 11 KB

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