Conversation.vue 10 KB

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