FilterPanel.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <template>
  2. <div class="filter-panel">
  3. <div class="filter-row">
  4. <span class="filter-label">文档类型</span>
  5. <div class="filter-tags">
  6. <span
  7. v-for="t in docTypes"
  8. :key="t"
  9. class="filter-tag"
  10. :class="{ active: docType === t }"
  11. @click="selectDocType(t)"
  12. >{{ t }}</span>
  13. </div>
  14. </div>
  15. <!-- 发布日期(暂时注释,后面可能用到)
  16. <div class="filter-row">
  17. <span class="filter-label">发布日期</span>
  18. <div class="filter-content">
  19. <el-date-picker
  20. :model-value="dateRange"
  21. type="daterange"
  22. range-separator="至"
  23. start-placeholder="年 /月 /日"
  24. end-placeholder="年 /月 /日"
  25. size="default"
  26. value-format="YYYY-MM-DD"
  27. @update:model-value="onDateRangeChange"
  28. class="date-picker"
  29. />
  30. </div>
  31. </div>
  32. -->
  33. <div class="filter-row">
  34. <span class="filter-label">发布单位</span>
  35. <!-- 按钮模式 -->
  36. <div v-if="!showSearch" class="filter-tags">
  37. <span
  38. class="filter-tag"
  39. :class="{ active: issuingAuthority === '全部' }"
  40. @click="selectAuthority({ shortName: '全部', fullName: '全部', count: 0 })"
  41. >全部</span>
  42. <span
  43. v-for="d in topAuthorities"
  44. :key="d.fullName"
  45. class="filter-tag"
  46. :class="{ active: issuingAuthority === d.fullName }"
  47. :title="d.fullName"
  48. @click="selectAuthority(d)"
  49. >{{ d.shortName }}</span>
  50. <span
  51. v-if="!showSearch"
  52. class="filter-tag more-tag"
  53. @click="toggleSearch"
  54. >更多</span>
  55. </div>
  56. <!-- 搜索模式 -->
  57. <div v-else class="filter-content">
  58. <div class="search-inline">
  59. <el-input
  60. v-model="searchInput"
  61. placeholder="输入发布单位名称,如发改委"
  62. size="default"
  63. clearable
  64. prefix-icon="Search"
  65. class="authority-search"
  66. @input="onSearchInput"
  67. />
  68. <span class="collapse-btn" @click="toggleSearch">收起</span>
  69. </div>
  70. <div v-if="searchResults.length > 0" class="search-results">
  71. <div
  72. v-for="item in searchResults"
  73. :key="item.fullName"
  74. class="search-result-item"
  75. :class="{ active: issuingAuthority === item.fullName }"
  76. @click="selectAuthority(item)"
  77. >
  78. <span class="result-short">{{ item.shortName }}</span>
  79. <span class="result-full">{{ item.fullName }}</span>
  80. </div>
  81. </div>
  82. <div v-if="hasSearched && searchInput && searchResults.length === 0" class="search-empty">
  83. 未找到匹配的发布单位
  84. </div>
  85. </div>
  86. </div>
  87. </div>
  88. </template>
  89. <script setup lang="ts">
  90. import { ref, onMounted, computed, watch } from 'vue'
  91. import { getIssuingAuthorities } from '@/api/retrieval'
  92. interface Authority {
  93. shortName: string
  94. fullName: string
  95. count: number
  96. }
  97. const docTypes = ['全部', '法规制度', '技术标准', '操作指南', '通知公告', '工作报告']
  98. // 所有发布单位(从后端 API 获取)
  99. const allAuthorities = ref<Authority[]>([])
  100. const authoritiesLoading = ref(false)
  101. // 加载全部发布单位(无关键词,取前50个)
  102. async function loadAuthorities(keyword = '') {
  103. authoritiesLoading.value = true
  104. try {
  105. const res = await getIssuingAuthorities(keyword)
  106. const items = (res.data?.authorities || []) as Array<{ full_name: string; count: number }>
  107. allAuthorities.value = items.slice(0, 50).map((a) => ({
  108. shortName: a.full_name.length > 12 ? a.full_name.substring(0, 12) + '…' : a.full_name,
  109. fullName: a.full_name,
  110. count: a.count,
  111. }))
  112. } catch (e) {
  113. console.warn('[FilterPanel] 加载发布单位失败:', e)
  114. allAuthorities.value = []
  115. } finally {
  116. authoritiesLoading.value = false
  117. }
  118. }
  119. // 前5发布单位(用于按钮展示)
  120. const topAuthorities = computed(() => allAuthorities.value.slice(0, 5))
  121. // 页面加载时获取发布单位列表
  122. onMounted(() => {
  123. loadAuthorities('')
  124. })
  125. const props = defineProps<{
  126. docType: string
  127. department: string
  128. dateRange: [string, string] | null
  129. }>()
  130. const emit = defineEmits<{
  131. 'update:docType': [value: string]
  132. 'update:department': [value: string]
  133. 'update:dateRange': [value: [string, string] | null]
  134. change: [params: {
  135. docType: string
  136. department: string
  137. publishDateFrom: string
  138. publishDateTo: string
  139. }]
  140. }>()
  141. // 发布单位状态
  142. const issuingAuthority = ref('全部')
  143. const showSearch = ref(false)
  144. const searchInput = ref('')
  145. const searchResults = ref<Authority[]>([])
  146. const hasSearched = ref(false)
  147. // 同步父组件传入的 department(如从详情页返回时恢复筛选条件)
  148. watch(() => props.department, (newVal) => {
  149. if (newVal && newVal !== issuingAuthority.value) {
  150. issuingAuthority.value = newVal
  151. searchInput.value = newVal
  152. }
  153. }, { immediate: true })
  154. let debounceTimer: ReturnType<typeof setTimeout> | null = null
  155. let isSelectingFromList = false
  156. function selectDocType(type: string) {
  157. emit('update:docType', type)
  158. emitChange(type)
  159. }
  160. function selectAuthority(item: Authority) {
  161. isSelectingFromList = true
  162. hasSearched.value = false
  163. if (debounceTimer) clearTimeout(debounceTimer)
  164. issuingAuthority.value = item.fullName
  165. searchInput.value = item.fullName
  166. searchResults.value = []
  167. emit('update:department', item.fullName)
  168. emitChange(undefined, item.fullName)
  169. setTimeout(() => {
  170. isSelectingFromList = false
  171. }, 50)
  172. }
  173. function toggleSearch() {
  174. showSearch.value = !showSearch.value
  175. if (!showSearch.value) {
  176. searchInput.value = ''
  177. searchResults.value = []
  178. hasSearched.value = false
  179. }
  180. }
  181. function onSearchInput(val: string) {
  182. if (isSelectingFromList) return
  183. hasSearched.value = val.trim() !== ''
  184. if (debounceTimer) clearTimeout(debounceTimer)
  185. debounceTimer = setTimeout(() => {
  186. const kw = val.trim()
  187. if (!kw) {
  188. searchResults.value = []
  189. return
  190. }
  191. // 调用后端 API 搜索发布单位
  192. loadSearchResults(kw)
  193. }, 300)
  194. }
  195. async function loadSearchResults(keyword: string) {
  196. try {
  197. const res = await getIssuingAuthorities(keyword)
  198. const items = (res.data?.authorities || []) as Array<{ full_name: string; count: number }>
  199. searchResults.value = items.slice(0, 50).map((a) => ({
  200. shortName: a.full_name.length > 12 ? a.full_name.substring(0, 12) + '…' : a.full_name,
  201. fullName: a.full_name,
  202. count: a.count,
  203. }))
  204. } catch (e) {
  205. console.warn('[FilterPanel] 搜索发布单位失败:', e)
  206. searchResults.value = []
  207. }
  208. }
  209. function onDateRangeChange(val: [string, string] | null) {
  210. emit('update:dateRange', val)
  211. emitChange(undefined, undefined, val)
  212. }
  213. function emitChange(
  214. docType?: string,
  215. department?: string,
  216. dateRange?: [string, string] | null
  217. ) {
  218. emit('change', {
  219. docType: docType ?? props.docType,
  220. department: department ?? props.department,
  221. publishDateFrom: (dateRange ?? props.dateRange)?.[0] || '',
  222. publishDateTo: (dateRange ?? props.dateRange)?.[1] || '',
  223. })
  224. }
  225. </script>
  226. <style scoped>
  227. .filter-panel {
  228. background: #fff;
  229. border-radius: 12px;
  230. padding: 24px 32px;
  231. box-shadow: 0 1px 4px rgba(0, 0, 0, 0.04);
  232. margin-bottom: 8px;
  233. }
  234. .filter-row {
  235. display: flex;
  236. align-items: flex-start;
  237. padding: 10px 0;
  238. border-bottom: 1px dashed #f0f0f0;
  239. }
  240. .filter-row:last-child {
  241. border-bottom: none;
  242. }
  243. .filter-label {
  244. width: 80px;
  245. flex-shrink: 0;
  246. font-size: 14px;
  247. color: #909399;
  248. font-weight: 400;
  249. margin-top: 6px;
  250. }
  251. .filter-content {
  252. flex: 1;
  253. }
  254. .filter-tags {
  255. flex: 1;
  256. display: flex;
  257. flex-wrap: wrap;
  258. gap: 8px;
  259. }
  260. .filter-tag {
  261. padding: 6px 18px;
  262. border-radius: 4px;
  263. font-size: 13px;
  264. color: #606266;
  265. background: #f5f7fa;
  266. cursor: pointer;
  267. transition: all 0.2s;
  268. user-select: none;
  269. border: 1px solid transparent;
  270. min-width: 60px;
  271. text-align: center;
  272. }
  273. .filter-tag:hover {
  274. color: #f5a623;
  275. background: #fff8f0;
  276. border-color: #f5d9b0;
  277. }
  278. .filter-tag.active {
  279. color: #fff;
  280. background: #f5a623;
  281. border-color: #f5a623;
  282. box-shadow: 0 2px 8px rgba(245, 166, 35, 0.3);
  283. font-weight: 500;
  284. }
  285. .more-tag {
  286. color: #f5a623;
  287. background: #fff8f0;
  288. border-color: #f5d9b0;
  289. }
  290. .more-tag:hover {
  291. color: #fff;
  292. background: #f5a623;
  293. border-color: #f5a623;
  294. }
  295. /* 搜索模式 */
  296. .search-inline {
  297. display: flex;
  298. align-items: center;
  299. gap: 8px;
  300. }
  301. .authority-search {
  302. flex: 1;
  303. max-width: 360px;
  304. }
  305. .collapse-btn {
  306. font-size: 13px;
  307. color: #f5a623;
  308. cursor: pointer;
  309. white-space: nowrap;
  310. padding: 6px 14px;
  311. border-radius: 4px;
  312. border: 1px solid #f5d9b0;
  313. background: #fff8f0;
  314. transition: all 0.2s;
  315. }
  316. .collapse-btn:hover {
  317. color: #fff;
  318. background: #f5a623;
  319. border-color: #f5a623;
  320. }
  321. .search-results {
  322. margin-top: 4px;
  323. max-height: 220px;
  324. overflow-y: auto;
  325. background: #fff;
  326. border: 1px solid #e4e7ed;
  327. border-radius: 6px;
  328. box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  329. }
  330. .search-result-item {
  331. padding: 8px 16px;
  332. cursor: pointer;
  333. transition: background 0.15s;
  334. display: flex;
  335. align-items: center;
  336. gap: 16px;
  337. }
  338. .search-result-item:hover {
  339. background: #f5f7fa;
  340. }
  341. .search-result-item.active {
  342. background: #fff8f0;
  343. }
  344. .result-short {
  345. font-size: 13px;
  346. font-weight: 500;
  347. min-width: 100px;
  348. color: #303133;
  349. }
  350. .search-result-item.active .result-short {
  351. color: #f5a623;
  352. }
  353. .result-full {
  354. font-size: 12px;
  355. color: #909399;
  356. overflow: hidden;
  357. text-overflow: ellipsis;
  358. white-space: nowrap;
  359. }
  360. .search-empty {
  361. padding: 16px;
  362. text-align: center;
  363. color: #c0c4cc;
  364. font-size: 13px;
  365. }
  366. .date-picker {
  367. width: 100%;
  368. max-width: 460px;
  369. }
  370. .date-picker :deep(.el-input__wrapper) {
  371. border-radius: 4px;
  372. box-shadow: 0 0 0 1px #dcdfe6 inset;
  373. }
  374. </style>