Conversation.vue 11 KB

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