Conversation.vue 9.9 KB

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