DocSearch.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. <template>
  2. <div class="doc-search-container">
  3. <!-- 左侧知识库类型选择 -->
  4. <div class="sidebar">
  5. <h2 class="sidebar-title" @click="router.push('/knowledge-list')">知识库</h2>
  6. <ul class="type-list">
  7. <!-- <li
  8. class="type-item"
  9. :class="{ active: activeType === '' }"
  10. @click="handleTypeSelect('')"
  11. >
  12. <div class="type-content">
  13. <span>全部类型</span>
  14. </div>
  15. </li> -->
  16. <li
  17. v-for="type in typeOptions"
  18. :key="type.value"
  19. class="type-item"
  20. :class="{ active: activeType === type.value }"
  21. @click="handleTypeSelect(type.value)"
  22. >
  23. <div class="type-content">
  24. <img :src="type.icon" :alt="type.label" class="type-icon">
  25. <span>{{ type.label }}</span>
  26. </div>
  27. </li>
  28. </ul>
  29. </div>
  30. <!-- 右侧搜索和结果区域 -->
  31. <div class="main-content">
  32. <!-- 搜索框 -->
  33. <div class="search-section">
  34. <el-input
  35. v-model="searchQuery"
  36. placeholder="请输入关键词搜索文档..."
  37. class="search-input"
  38. clearable
  39. @keyup.enter="handleSearch"
  40. >
  41. <!-- <template #prefix>
  42. <el-icon><Search /></el-icon>
  43. </template> -->
  44. <template #append>
  45. <el-button type="primary" :loading="loading" @click="handleSearch">
  46. <el-icon :size="20"><Search /></el-icon>
  47. <!-- 搜索 -->
  48. </el-button>
  49. </template>
  50. </el-input>
  51. </div>
  52. <!-- 提示语 -->
  53. <div class="search-tips">
  54. 选择知识库搜索会更快
  55. </div>
  56. <!-- 搜索结果 -->
  57. <div class="result-section" v-loading="loading">
  58. <template v-if="searchResults.length">
  59. <div
  60. v-for="(item, index) in searchResults"
  61. :key="index"
  62. class="result-item"
  63. >
  64. <div class="result-content">
  65. <p class="result-summary ellipsis-2" v-html="highlightText(item.fileSnapshot)"></p>
  66. <div class="result-content-bottom">
  67. <div class="file-icon">
  68. <el-icon :size="24">
  69. <Document v-if="item.fileType === 'doc'" />
  70. <Files v-else-if="item.fileType === 'pdf'" />
  71. <Grid v-else-if="item.fileType === 'excel'" />
  72. <PictureFilled v-else-if="item.fileType === 'ppt'" />
  73. <Folder v-else />
  74. </el-icon>
  75. </div>
  76. <div class="result-info">
  77. <h3 class="result-title" @click="viewDocument(item)">
  78. {{ item.fileName }}
  79. </h3>
  80. </div>
  81. <div class="result-actions">
  82. <el-icon color="#FF7575" size="24" @click="toggleFavorite(item)">
  83. <Star v-if="item.isCollect == 0" />
  84. <StarFilled v-else />
  85. </el-icon>
  86. <!-- <el-button
  87. type="primary"
  88. size="small"
  89. @click="toggleFavorite(item)"
  90. :icon="item.isFavorite ? 'Star' : 'StarFilled'"
  91. :class="{ 'is-favorite': item.isFavorite }"
  92. >
  93. {{ item.isFavorite ? '已收藏' : '收藏' }}
  94. </el-button> -->
  95. <el-icon color="#FF7575" size="24" @click="downloadDocument(item)">
  96. <Download />
  97. </el-icon>
  98. <!-- <el-button
  99. type="primary"
  100. size="small"
  101. @click="downloadDocument(item)"
  102. icon="Download"
  103. >
  104. 下载
  105. </el-button> -->
  106. </div>
  107. </div>
  108. </div>
  109. </div>
  110. </template>
  111. <el-empty
  112. v-else-if="!loading && searchQuery"
  113. description="未找到相关文档"
  114. />
  115. <el-empty
  116. v-else-if="!loading"
  117. description="请输入关键词搜索文档"
  118. />
  119. </div>
  120. <!-- 分页 -->
  121. <div class="pagination">
  122. <el-pagination
  123. v-model:current-page="currentPage"
  124. v-model:page-size="pageSize"
  125. :total="total"
  126. :page-sizes="[10, 20, 30, 50]"
  127. layout="total, sizes, prev, pager, next"
  128. @size-change="handleSizeChange"
  129. @current-change="handleCurrentChange"
  130. />
  131. </div>
  132. </div>
  133. </div>
  134. </template>
  135. <script setup>
  136. import { ref, reactive, onMounted } from 'vue'
  137. import { ElMessage } from 'element-plus'
  138. import { Search, View, Star, Download, Document, Files, Grid, PictureFilled, Folder } from '@element-plus/icons-vue'
  139. import { get, post,put } from '../utils/request'
  140. import { useUserStore } from '../stores/user'
  141. import { useRoute, useRouter } from 'vue-router'
  142. import { downloadFile } from '../utils'
  143. const userStore = useUserStore()
  144. const router = useRouter()
  145. import docTypeJs from '../assets/doc-type-js.png'
  146. import docTypeSw from '../assets/doc-type-sw.png'
  147. import docTypeXz from '../assets/doc-type-xz.png'
  148. // 搜索相关
  149. const searchQuery = ref('')
  150. const loading = ref(false)
  151. const searchResults = ref([])
  152. const currentPage = ref(1)
  153. const pageSize = ref(10)
  154. const total = ref(0)
  155. const activeType = ref('')
  156. const handleTypeSelect = (type) => {
  157. activeType.value = type
  158. // handleSearch()
  159. }
  160. // 选项配置
  161. const typeOptions = ref([])
  162. // 添加获取知识库列表的方法
  163. const getKnowInfoList = async () => {
  164. try {
  165. // const response = await get('/admin/knowInfo/getList',{pageNum:1,pageSize:100})
  166. const userId = userStore.userInfo.id
  167. const response = await get('/admin/user/chat/knowInfoList?userId='+userId)
  168. console.log('response', response);
  169. typeOptions.value = response.data.map(item => ({
  170. value: item.id,
  171. label: item.name,
  172. icon: getIconByType(item.type) // 根据类型获取对应图标
  173. }))
  174. } catch (error) {
  175. ElMessage.error('获取知识库列表失败')
  176. }
  177. }
  178. // 添加根据类型获取图标的方法
  179. const getIconByType = (type) => {
  180. const iconMap = {
  181. 'technical': docTypeJs,
  182. 'business': docTypeSw,
  183. 'administrative': docTypeXz
  184. }
  185. return iconMap[type] || docTypeJs
  186. }
  187. // 在组件挂载时获取知识库列表
  188. onMounted(() => {
  189. getKnowInfoList()
  190. })
  191. // 搜索处理
  192. const handleSearch = async () => {
  193. // if (!searchQuery.value.trim() && !activeType.value) return
  194. loading.value = true
  195. try {
  196. const params = {
  197. searchText: searchQuery.value,
  198. konwInfoId: activeType.value,
  199. pageNum: currentPage.value,
  200. pageSize: pageSize.value
  201. }
  202. const response = await get('/admin/knowFile/search', params)
  203. searchResults.value = response.data.rows
  204. total.value = Number(response.data.total)
  205. } catch (error) {
  206. // 模拟数据
  207. // searchResults.value = [
  208. // {
  209. // id: 1,
  210. // title: '文档1',
  211. // summary: '文档1摘要',
  212. // type: 'doc',
  213. // date: '2024-01-01',
  214. // isFavorite: false,
  215. // favorites: 0,
  216. // url: 'https://www.baidu.com'
  217. // },
  218. // {
  219. // id: 2,
  220. // title: '文档2',
  221. // summary: '文档2摘要',
  222. // type: 'doc',
  223. // date: '2024-01-01',
  224. // }
  225. // ] ;
  226. // total.value = 2;
  227. ElMessage.error('搜索失败,请重试')
  228. } finally {
  229. loading.value = false
  230. }
  231. }
  232. // 分页处理
  233. const handleSizeChange = (val) => {
  234. pageSize.value = val
  235. handleSearch()
  236. }
  237. const handleCurrentChange = (val) => {
  238. currentPage.value = val
  239. handleSearch()
  240. }
  241. // 文档操作
  242. const viewDocument = (doc) => {
  243. window.open(doc.fileUrl, '_blank')
  244. }
  245. const downloadDocument = async (doc) => {
  246. try {
  247. await downloadFile(doc.fileUrl, doc.fileName)
  248. // 添加文件下载记录
  249. await post('/admin/userHistoryLog/saveFileDownloadLog', {
  250. relatedId: doc.id
  251. })
  252. } catch (error) {
  253. console.log('error', error)
  254. ElMessage.error('下载失败,请重试文件地址:'+doc.fileUrl)
  255. }
  256. }
  257. const toggleFavorite = async (doc) => {
  258. console.log('doc', doc);
  259. try {
  260. if (doc.isCollect == 0) {
  261. await post('admin/userCollect/save', {
  262. id: doc.collectId,
  263. relatedType: 'file.search',
  264. relatedId: doc.id,
  265. })
  266. } else {
  267. await put('admin/userCollect/cancel', {
  268. id: doc.collectId,
  269. relatedType: 'file.search',//related_type : AI问答 : ai.answer 方案:scheme 文件检索: file.search
  270. relatedId: doc.id,
  271. })
  272. }
  273. doc.isCollect = doc.isCollect == 0 ? 1 : 0
  274. ElMessage.success(doc.isCollect == 1 ? '收藏成功' : '已取消收藏')
  275. handleSearch()
  276. } catch (error) {
  277. ElMessage.error('操作失败,请重试')
  278. }
  279. }
  280. // 工具函数
  281. const getTagType = (type) => {
  282. const types = {
  283. doc: '',
  284. pdf: 'success',
  285. excel: 'warning',
  286. ppt: 'danger'
  287. }
  288. return types[type] || 'info'
  289. }
  290. const formatDate = (date) => {
  291. return new Date(date).toLocaleDateString()
  292. }
  293. const highlightText = (text) => {
  294. if (!searchQuery.value) return text
  295. const regex = new RegExp(searchQuery.value, 'gi')
  296. return text.replace(regex, match => `<span class="highlight">${match}</span>`)
  297. }
  298. </script>
  299. <style lang="scss" scoped>
  300. .doc-search-container {
  301. height: 100%;
  302. display: flex;
  303. // gap: $spacing-base;
  304. background-color: #fff;
  305. // padding: $spacing-base;
  306. }
  307. .sidebar {
  308. width: 350px;
  309. background-color: $background-gray;
  310. border-radius: 0px 30px 30px 0px;
  311. box-shadow: 2px 0px 10px 0px rgba(172,165,165,0.5);
  312. .sidebar-title {
  313. padding: $spacing-base;
  314. margin-bottom: 23px;
  315. font-size: 24px;
  316. font-weight: bold;
  317. border-bottom: 1px solid $border-lighter;
  318. text-align: center;
  319. color: #191A1E;
  320. cursor: pointer;
  321. }
  322. .type-list {
  323. list-style: none;
  324. padding: 0;
  325. margin: 0;
  326. .type-item {
  327. padding: 0 50px 0 50px;
  328. cursor: pointer;
  329. transition: all 0.3s ease;
  330. // &:hover {
  331. // .type-content{
  332. // border-image: linear-gradient(226deg, rgba(237, 38, 38, 1), rgba(246, 181, 181, 1)) 1 1;
  333. // }
  334. // }
  335. &.active {
  336. .type-content{
  337. border: 1px solid #ED2626;
  338. // border-image: linear-gradient(226deg, rgba(237, 38, 38, 1), rgba(246, 181, 181, 1)) 1 1;
  339. }
  340. }
  341. .type-content {
  342. display: flex;
  343. align-items: center;
  344. justify-content: center;
  345. gap: 12px;
  346. height: 80px;
  347. background: linear-gradient( 131deg, #FFFFFF 0%, #FFFFFF 100%);
  348. box-shadow: 0px 1px 3px 0px rgba(192,202,209,0.5);
  349. border-radius: 15px;
  350. margin-bottom: 20px;
  351. .type-icon {
  352. width: 40px;
  353. height: 46px;
  354. object-fit: contain;
  355. }
  356. span {
  357. font-weight: 500;
  358. font-size: 20px;
  359. color: #31333C;
  360. }
  361. }
  362. }
  363. }
  364. }
  365. .main-content {
  366. flex: 1;
  367. display: flex;
  368. flex-direction: column;
  369. gap: $spacing-base;
  370. min-width: 0; // 防止内容溢出
  371. padding: 0 20px;
  372. }
  373. .search-section {
  374. padding-top: 76px;
  375. // padding: $spacing-base;
  376. background-color: $background-white;
  377. border-radius: $border-radius-base;
  378. // box-shadow: $box-shadow-light;
  379. .search-input{
  380. height: 60px;
  381. border-radius: 15px;
  382. }
  383. :deep(.el-input__wrapper) {
  384. border-top-left-radius: 15px;
  385. border-bottom-left-radius: 15px;
  386. }
  387. :deep(.el-input-group__append) {
  388. border-top-right-radius: 15px;
  389. border-bottom-right-radius: 15px;
  390. width: 78px;
  391. }
  392. }
  393. .search-tips{
  394. font-weight: 500;
  395. font-size: 16px;
  396. color: #31333C;
  397. line-height: 30px;
  398. }
  399. .result-section {
  400. flex: 1;
  401. overflow-y: auto;
  402. padding: $spacing-base;
  403. background-color: #F9FAFB;
  404. border-radius: 12px;
  405. // box-shadow: $box-shadow-light;
  406. }
  407. .result-item {
  408. padding: $spacing-base;
  409. margin-bottom: 14px;
  410. background-color: #fff;
  411. &:last-child {
  412. border-bottom: none;
  413. }
  414. .result-content {
  415. .file-icon {
  416. flex-shrink: 0;
  417. width: 40px;
  418. height: 40px;
  419. display: flex;
  420. align-items: center;
  421. justify-content: center;
  422. background-color: $background-lighter;
  423. border-radius: $border-radius-base;
  424. .el-icon {
  425. font-size: 24px;
  426. color: $primary-color;
  427. }
  428. }
  429. .result-summary {
  430. margin: $spacing-mini 0;
  431. font-weight: 400;
  432. font-size: 21px;
  433. color: #191A1E;
  434. line-height: 32px;
  435. margin-bottom: 24px;
  436. :deep(.highlight) {
  437. color: $primary-color;
  438. font-weight: bold;
  439. background-color: rgba($primary-color, 0.1);
  440. padding: 0 2px;
  441. border-radius: 2px;
  442. }
  443. }
  444. .result-info {
  445. flex: 1;
  446. min-width: 0;
  447. .result-title {
  448. margin: 0 0 $spacing-mini;
  449. font-size: 16px;
  450. color: #6C6E72;
  451. cursor: pointer;
  452. margin-left: 12px;
  453. }
  454. }
  455. .result-content-bottom{
  456. display: flex;
  457. align-items: center;
  458. justify-content: space-between;
  459. background: #FFF6F6;
  460. border-radius: 12px;
  461. padding: 8px 20px;
  462. }
  463. .result-actions {
  464. flex-shrink: 0;
  465. display: flex;
  466. gap: $spacing-mini;
  467. .el-icon {
  468. cursor: pointer;
  469. margin-left: 10px;
  470. }
  471. .el-button {
  472. &.is-favorite {
  473. background-color: $warning-color;
  474. border-color: $warning-color;
  475. &:hover {
  476. background-color: $primary-color-hover;
  477. border-color: $primary-color-hover;
  478. }
  479. }
  480. }
  481. }
  482. }
  483. }
  484. .pagination {
  485. margin-top: $spacing-large;
  486. margin-bottom: 20px;
  487. display: flex;
  488. justify-content: center;
  489. }
  490. </style>