historyComponent.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <script setup lang="js">
  2. import { ref } from 'vue'
  3. import { Search, Delete } from '@element-plus/icons-vue'
  4. import { ElMessageBox, ElMessage } from 'element-plus'
  5. import { getConversation,delConversation } from '@/api/model.js'
  6. import { storeToRefs } from 'pinia';
  7. import { useMsgStore } from '@/store/modules/msg.ts'
  8. import { useUserStore } from '@/store/modules/user';
  9. const msgStore = useMsgStore()
  10. const userStore = useUserStore()
  11. const drawer = ref(false)
  12. const count = ref(0)
  13. const input = ref('')
  14. const loading = ref(false)
  15. const dataList = ref([])
  16. // 获取父组件提供的 Hook 实例
  17. const emit = defineEmits(['currentData'])
  18. const { conversationId} = storeToRefs(useMsgStore())
  19. const fetchList = async () => {
  20. try {
  21. loading.value = true
  22. const res = await getConversation({
  23. page: 1,
  24. size: 100,
  25. sort: ['createTime,desc'],
  26. userId:userStore.userInfo.id
  27. })
  28. dataList.value = res.data.list
  29. } finally{
  30. loading.value = false
  31. }
  32. }
  33. onMounted(async () => {
  34. await fetchList()
  35. conversationId.value = dataList.value[0].id
  36. })
  37. watch(drawer, (newVal) => {
  38. if (newVal) {
  39. fetchList()
  40. }
  41. })
  42. const search = (keyword) => {
  43. const loop = (data) => {
  44. const result = []
  45. data.forEach((item) => {
  46. if (item.title?.toLowerCase().includes(keyword) || item.code?.toLowerCase().includes(keyword)) {
  47. result.push({...item})
  48. }
  49. })
  50. return result
  51. }
  52. return loop(dataList.value)
  53. }
  54. const searchList = computed(() => {
  55. if (!input.value) return dataList.value
  56. return search(input.value.toLowerCase())
  57. })
  58. function handleDeleteStore(e, item,name) {
  59. e.stopPropagation()
  60. ElMessageBox.confirm(
  61. '此操作无法撤销。',
  62. `要删【${name.substring(0,20)}】对话吗?`,
  63. {
  64. confirmButtonText: '确认',
  65. cancelButtonText: '取消',
  66. type: 'warning',
  67. showClose: false,
  68. center: true
  69. }
  70. ).then(() => {
  71. delConversation(item).then(() => {
  72. fetchList()
  73. msgStore.setTitle(null)
  74. msgStore.setConvId(null)
  75. })
  76. })
  77. }
  78. defineExpose({
  79. drawer
  80. })
  81. </script>
  82. <template>
  83. <el-drawer style="height: 70%" v-model="drawer" direction="btt" :show-close="true" :close-on-click-modal="false"
  84. :destroy-on-close="true" class="custom_drawer">
  85. <template #header>
  86. <div class="his_flex"><span class="his_title">历史聊天</span><span class="his_count">({{ dataList.length }})</span>
  87. </div>
  88. </template>
  89. <div style="height: 100%;overflow: hidden;" v-loading="loading">
  90. <div class="his_delete">
  91. <el-input style="margin-right: 12px" v-model="input" placeholder="搜索" clearable :prefix-icon="Search"
  92. />
  93. </div>
  94. <div class="his_content">
  95. <template v-for="item in searchList" :key="item.id">
  96. <div :class="`his_list ${conversationId === item.id ? 'his_list_change' : '' }`"
  97. @click="() => {
  98. msgStore.setTitle(item.title)
  99. msgStore.setConvId(item.id)
  100. }">
  101. <p class="ellipsis" style="color:#000000;font-weight: 900;">{{ item?.title ?? '&#45;&#45;' }}</p>
  102. <p class="his_list_op">
  103. <span style="color: #000">{{ item?.createTime }}</span>
  104. <el-tooltip effect="dark" content="删除" placement="top">
  105. <el-button :icon="Delete" link
  106. @click="(e) => handleDeleteStore(e, item.id, item?.title)" />
  107. </el-tooltip>
  108. </p>
  109. </div>
  110. </template>
  111. </div>
  112. </div>
  113. </el-drawer>
  114. </template>
  115. <style lang="scss">
  116. .his_flex {
  117. display: flex;
  118. align-items: center;
  119. }
  120. .custom_drawer {
  121. height: 70vh !important;
  122. border-top-left-radius: 15px;
  123. border-top-right-radius: 15px;
  124. .el-drawer__header {
  125. padding: 16px;
  126. margin-bottom: 0;
  127. }
  128. .el-drawer__close-btn, .el-drawer__body {
  129. padding: 0;
  130. }
  131. .el-drawer__body {
  132. overflow: hidden;
  133. height: 100%;
  134. }
  135. }
  136. .his_title {
  137. display: inline-block;
  138. color: #000000;
  139. font-size: 20px;
  140. font-weight: 900;
  141. margin-right: 3px;
  142. height: 24px;
  143. line-height: 24px;
  144. }
  145. .his_count {
  146. display: inline-block;
  147. height: 24px;
  148. line-height: 24px;
  149. }
  150. .his_delete {
  151. padding: 0 12px 10px;
  152. display: flex;
  153. position: sticky;
  154. align-items: center;
  155. justify-content: space-between;
  156. .is-circle {
  157. border-radius: 8px;
  158. }
  159. }
  160. .his_content {
  161. height: calc(100% - 40px);
  162. overflow: auto;
  163. padding: 0 12px;
  164. .ellipsis {
  165. white-space: nowrap; /* 强制文本不换行 */
  166. overflow: hidden; /* 隐藏溢出内容 */
  167. text-overflow: ellipsis; /* 显示省略号 */
  168. width: 100%; /* 设置宽度(必须) */
  169. }
  170. .his_list {
  171. font-size: 12px;
  172. padding: 5px 10px;
  173. border-radius: 8px;
  174. margin-bottom: 4px;
  175. box-shadow: 0 0 6px rgba(122, 89, 255, .16);
  176. cursor: pointer;
  177. .his_list_op {
  178. display: flex;
  179. justify-content: space-between;
  180. align-items: center;
  181. }
  182. }
  183. .his_list:hover {
  184. background-color: rgba(122, 89, 255, .06);
  185. }
  186. .his_list_change {
  187. background-color: rgba(122, 89, 255, .2) !important;
  188. }
  189. }
  190. </style>