-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathSearchSettingsModal.vue
More file actions
651 lines (564 loc) · 15.9 KB
/
SearchSettingsModal.vue
File metadata and controls
651 lines (564 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
<template>
<a-modal
v-model:visible="modalVisible"
title="搜索设置"
:width="800"
:mask-closable="false"
@ok="handleConfirm"
@cancel="handleCancel"
>
<div class="search-settings">
<div class="settings-header">
<div class="header-left">
<h4>选择搜索源</h4>
<p class="settings-desc">选择要参与聚合搜索的数据源</p>
</div>
<div class="header-right">
<div class="search-tip">
<icon-info-circle class="tip-icon"/>
<span class="tip-text">只有 searchable 属性不为 0 的源才支持搜索功能</span>
</div>
</div>
</div>
<div class="sources-section">
<div class="section-header">
<div class="select-all-container">
<a-checkbox
v-model="selectAll"
:indeterminate="indeterminate"
@change="handleSelectAll"
>
全选
</a-checkbox>
<span class="selected-count">
已选择 {{ selectedSources.length }} / {{ filteredSources.length }} 个源
</span>
</div>
<div class="header-actions">
<a-button size="small" @click="resetToDefault">
<template #icon>
<icon-undo/>
</template>
重置
</a-button>
<a-button size="small" @click="refreshSources">
<template #icon>
<icon-refresh/>
</template>
刷新
</a-button>
</div>
</div>
<!-- 搜索框 -->
<div class="search-filter-container">
<a-input
v-model="searchFilter"
placeholder="搜索源名称、API或分组..."
allow-clear
@input="onSearchFilterChange"
>
<template #prefix>
<icon-search/>
</template>
</a-input>
</div>
<div class="sources-list">
<div
v-for="source in filteredSources"
:key="source.key"
class="source-item"
>
<a-checkbox
v-model="selectedSources"
:value="source.key"
@change="handleSourceChange"
>
<div class="source-info">
<div class="source-main">
<span class="source-name">{{ source.name }}</span>
<div class="source-tags">
<a-tag
v-if="source.quickSearch"
color="green"
size="small"
>
快搜
</a-tag>
<a-tag
v-if="source.searchable === 1"
color="blue"
size="small"
>
标准搜索
</a-tag>
<a-tag
v-if="source.searchable === 2"
color="orange"
size="small"
>
高级搜索
</a-tag>
</div>
</div>
<div class="source-meta">
<span v-if="source.api" class="meta-item">{{ source.api }}</span>
<span v-if="source.group" class="meta-item">{{ source.group }}</span>
</div>
</div>
</a-checkbox>
</div>
</div>
<div v-if="filteredSources.length === 0" class="empty-sources">
<icon-empty class="empty-icon"/>
<p v-if="availableSources.length === 0">暂无可用的搜索源</p>
<p v-else>未找到匹配的搜索源</p>
<p class="empty-desc" v-if="availableSources.length === 0">请确保已配置支持搜索的数据源</p>
<p class="empty-desc" v-else>请尝试其他关键词或清空搜索条件</p>
</div>
</div>
</div>
<template #footer>
<div class="modal-footer">
<a-button @click="handleCancel">取消</a-button>
<a-button
type="primary"
@click="handleConfirm"
:disabled="selectedSources.length === 0"
>
确定 ({{ selectedSources.length }})
</a-button>
</div>
</template>
</a-modal>
</template>
<script>
import { defineComponent, ref, computed, watch, onMounted } from 'vue';
import { Message } from '@arco-design/web-vue';
import siteService from '@/api/services/site';
export default defineComponent({
name: 'SearchSettingsModal',
props: {
visible: {
type: Boolean,
default: false
}
},
emits: ['update:visible', 'confirm'],
setup(props, { emit }) {
const modalVisible = ref(false);
const availableSources = ref([]);
const selectedSources = ref([]);
const selectAll = ref(false);
const searchFilter = ref('');
// 计算属性
const indeterminate = computed(() => {
return selectedSources.value.length > 0 &&
selectedSources.value.length < filteredSources.value.length;
});
// 筛选后的搜索源
const filteredSources = computed(() => {
if (!searchFilter.value.trim()) {
return availableSources.value;
}
const keyword = searchFilter.value.toLowerCase().trim();
return availableSources.value.filter(source => {
return (
source.name.toLowerCase().includes(keyword) ||
(source.api && source.api.toLowerCase().includes(keyword)) ||
(source.group && source.group.toLowerCase().includes(keyword)) ||
source.key.toLowerCase().includes(keyword)
);
});
});
// 方法
const loadAvailableSources = () => {
try {
const allSites = siteService.getAllSites();
// 过滤出支持搜索的源
availableSources.value = allSites.filter(site =>
site.searchable && site.searchable !== 0
).map(site => ({
key: site.key,
name: site.name || site.key,
api: site.api,
group: site.group,
searchable: site.searchable,
quickSearch: site.quickSearch
}));
console.log('可用搜索源:', availableSources.value);
} catch (error) {
console.error('加载搜索源失败:', error);
Message.error('加载搜索源失败');
availableSources.value = [];
}
};
const loadCurrentSettings = () => {
try {
const settings = localStorage.getItem('searchAggregationSettings');
if (settings) {
const parsed = JSON.parse(settings);
// 验证保存的设置是否有效
if (parsed && Array.isArray(parsed.selectedSources)) {
// 过滤掉不存在的源
const validSources = parsed.selectedSources.filter(sourceKey =>
availableSources.value.some(source => source.key === sourceKey)
);
selectedSources.value = validSources;
console.log('已加载搜索设置:', { selectedSources: validSources });
} else {
// 设置格式无效,使用默认值
selectedSources.value = availableSources.value.map(source => source.key);
console.log('设置格式无效,使用默认配置');
}
} else {
// 没有保存的设置,默认选择所有可用源
selectedSources.value = availableSources.value.map(source => source.key);
console.log('未找到保存的设置,使用默认配置');
}
// 如果没有选中任何源,至少选择第一个可用源
if (selectedSources.value.length === 0 && availableSources.value.length > 0) {
selectedSources.value = [availableSources.value[0].key];
console.log('没有有效的选中源,选择第一个可用源');
}
// 更新全选状态
updateSelectAllState();
} catch (error) {
console.error('加载搜索设置失败:', error);
// 发生错误时使用默认配置
selectedSources.value = availableSources.value.map(source => source.key);
updateSelectAllState();
}
};
const updateSelectAllState = () => {
const filteredKeys = filteredSources.value.map(source => source.key);
const selectedFilteredCount = selectedSources.value.filter(key =>
filteredKeys.includes(key)
).length;
selectAll.value = selectedFilteredCount === filteredSources.value.length && filteredSources.value.length > 0;
};
const handleSelectAll = (checked) => {
const filteredKeys = filteredSources.value.map(source => source.key);
if (checked) {
// 添加当前筛选结果中未选中的源
const newSelected = [...selectedSources.value];
filteredKeys.forEach(key => {
if (!newSelected.includes(key)) {
newSelected.push(key);
}
});
selectedSources.value = newSelected;
} else {
// 移除当前筛选结果中已选中的源
selectedSources.value = selectedSources.value.filter(key =>
!filteredKeys.includes(key)
);
}
};
const handleSourceChange = () => {
updateSelectAllState();
};
const refreshSources = () => {
loadAvailableSources();
Message.success('已刷新搜索源列表');
};
const resetToDefault = () => {
// 重置为默认配置(选择所有可用源)
selectedSources.value = availableSources.value.map(source => source.key);
updateSelectAllState();
// 不进行持久化,需点击“确定”才保存
Message.info('已恢复默认选择,点击“确定”后保存');
};
const onSearchFilterChange = () => {
// 当搜索条件改变时,更新全选状态
updateSelectAllState();
};
const handleConfirm = () => {
if (selectedSources.value.length === 0) {
Message.warning('请至少选择一个搜索源');
return;
}
const settings = {
selectedSources: selectedSources.value,
updatedAt: Date.now()
};
// 立即保存到localStorage
try {
localStorage.setItem('searchAggregationSettings', JSON.stringify(settings));
console.log('搜索设置已保存:', settings);
} catch (error) {
console.error('保存搜索设置失败:', error);
Message.error('保存设置失败');
return;
}
emit('confirm', settings);
modalVisible.value = false;
};
const handleCancel = () => {
modalVisible.value = false;
// 恢复之前的设置
loadCurrentSettings();
};
// 监听器
watch(() => props.visible, (newVal) => {
modalVisible.value = newVal;
if (newVal) {
loadAvailableSources();
loadCurrentSettings();
}
});
watch(modalVisible, (newVal) => {
emit('update:visible', newVal);
});
watch(selectedSources, () => {
updateSelectAllState();
}, { deep: true });
watch(filteredSources, () => {
updateSelectAllState();
}, { deep: true });
// 组件挂载时初始化
onMounted(() => {
loadAvailableSources();
});
return {
modalVisible,
availableSources,
filteredSources,
selectedSources,
selectAll,
indeterminate,
searchFilter,
handleSelectAll,
handleSourceChange,
refreshSources,
resetToDefault,
onSearchFilterChange,
handleConfirm,
handleCancel
};
}
});
</script>
<style scoped>
.search-settings {
padding: 8px 0;
}
.settings-header {
margin-bottom: 16px;
display: flex;
align-items: flex-start;
justify-content: space-between;
}
.header-left {
flex: 1;
}
.header-left h4 {
margin: 0 0 8px 0;
font-size: 16px;
font-weight: 600;
color: var(--color-text-1);
}
.settings-desc {
margin: 0;
font-size: 14px;
color: var(--color-text-3);
line-height: 1.5;
}
.header-right {
flex-shrink: 0;
margin-left: 24px;
}
.search-tip {
display: flex;
align-items: center;
gap: 6px;
padding: 8px 12px;
background: var(--color-fill-1);
border-radius: 6px;
border: 1px solid var(--color-border-2);
}
.tip-icon {
font-size: 14px;
color: var(--color-primary-6);
flex-shrink: 0;
}
.tip-text {
font-size: 12px;
color: var(--color-text-2);
line-height: 1.4;
white-space: nowrap;
}
.sources-section {
margin-bottom: 12px;
}
.section-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 16px;
padding-bottom: 12px;
border-bottom: 1px solid var(--color-border-2);
}
.select-all-container {
display: flex;
align-items: center;
gap: 12px;
}
.selected-count {
font-size: 13px;
color: var(--color-text-3);
}
.header-actions {
display: flex;
gap: 8px;
}
.search-filter-container {
margin-bottom: 16px;
}
.search-filter-container :deep(.arco-input) {
border-radius: 8px;
}
.search-filter-container :deep(.arco-input-prefix) {
color: var(--color-text-3);
}
.sources-list {
max-height: 480px;
overflow-y: auto;
border: 1px solid var(--color-border-2);
border-radius: 6px;
background: var(--color-bg-1);
display: grid;
grid-template-columns: 1fr 1fr;
gap: 0;
}
.source-item {
padding: 12px;
border-bottom: 1px solid var(--color-border-2);
border-right: 1px solid var(--color-border-2);
transition: background-color 0.2s ease;
}
.source-item:nth-child(odd) {
border-right: 1px solid var(--color-border-2);
}
.source-item:nth-child(even) {
border-right: none;
}
.source-item:nth-last-child(-n+2) {
border-bottom: none;
}
.source-item:hover {
background: var(--color-fill-1);
}
.source-item :deep(.arco-checkbox) {
width: 100%;
}
.source-item :deep(.arco-checkbox-label) {
width: 100%;
padding-left: 8px;
}
.source-info {
width: 100%;
}
.source-main {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 6px;
}
.source-name {
font-size: 13px;
font-weight: 500;
color: var(--color-text-1);
flex: 1;
margin-right: 8px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.source-tags {
display: flex;
gap: 4px;
flex-shrink: 0;
}
.source-meta {
display: flex;
gap: 8px;
flex-wrap: wrap;
}
.meta-item {
font-size: 11px;
color: var(--color-text-3);
background: var(--color-fill-2);
padding: 1px 6px;
border-radius: 3px;
}
.empty-sources {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 40px 20px;
text-align: center;
color: var(--color-text-3);
}
.empty-icon {
font-size: 48px;
color: var(--color-text-4);
margin-bottom: 16px;
}
.empty-sources p {
margin: 0 0 8px 0;
font-size: 14px;
}
.empty-desc {
font-size: 12px;
color: var(--color-text-4);
}
.modal-footer {
display: flex;
justify-content: flex-end;
gap: 12px;
}
/* 滚动条样式 */
.sources-list::-webkit-scrollbar {
width: 6px;
}
.sources-list::-webkit-scrollbar-track {
background: var(--color-fill-2);
border-radius: 3px;
}
.sources-list::-webkit-scrollbar-thumb {
background: var(--color-fill-4);
border-radius: 3px;
}
.sources-list::-webkit-scrollbar-thumb:hover {
background: var(--color-fill-5);
}
/* 响应式设计 */
@media (max-width: 768px) {
.section-header {
flex-direction: column;
align-items: flex-start;
gap: 12px;
}
.select-all-container {
width: 100%;
}
.search-filter-container {
margin-bottom: 12px;
}
.source-main {
flex-direction: column;
align-items: flex-start;
gap: 8px;
}
.source-tags {
align-self: flex-end;
}
.modal-footer {
flex-direction: column;
}
.modal-footer .arco-btn {
width: 100%;
}
}
</style>