Conversation.vue 10.0 KB

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