2026-03-25 18:17:54 +08:00

953 lines
26 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<div class="scan-box-container" >
<!-- Tab 切换 -->
<div class="scan-tabs">
<el-tabs v-model="activeTab" stretch @tab-click="handleTabChange">
<el-tab-pane label="筛选" name="custom">
<template #label>
<span class="tab-label">
<el-icon><Operation /></el-icon>
筛选
</span>
</template>
</el-tab-pane>
<el-tab-pane label="过滤" name="preset">
<template #label>
<span class="tab-label">
<el-icon><DataAnalysis /></el-icon>
过滤
</span>
</template>
</el-tab-pane>
</el-tabs>
</div>
<!-- 内容区域 -->
<div class="scan-content">
<!-- 自定义筛选 -->
<div v-show="activeTab === 'custom'" class="custom-filter">
<div class="filter-scroll-area" ref="filterScroll">
<!-- 所有字段配置的表单 -->
<div class="field-forms">
<template v-for="field in availableFields" :key="field.value">
<!-- 文本类型 -->
<div v-if="field.type === 'text'" class="field-form-item">
<div class="field-label">
<span>{{ field.label }}</span>
<el-tag v-if="field.required" size="small" type="danger">必填</el-tag>
</div>
<div class="field-control">
<el-select
v-model="customConditions[field.value].operator"
placeholder="条件类型"
size="small"
class="operator-select"
@change="handleTextOperatorChange(field)"
>
<el-option
v-for="op in getOperators(field)"
:key="op.value"
:label="op.label"
:value="op.value"
/>
</el-select>
<el-input
v-model="customConditions[field.value].value"
:placeholder="getTextPlaceholder(field)"
size="small"
clearable
class="value-input"
@change="handleCustomChange"
@blur="normalizeTextValue(field)"
/>
</div>
</div>
<!-- 数字类型 -->
<div v-else-if="field.type === 'number'" class="field-form-item">
<div class="field-label">
<span>{{ field.label }}</span>
<el-tag v-if="field.required" size="small" type="danger">必填</el-tag>
</div>
<div class="field-control">
<el-select
v-model="customConditions[field.value].operator"
placeholder="请选择"
size="small"
class="operator-select"
>
<el-option
v-for="op in getOperators(field)"
:key="op.value"
:label="op.label"
:value="op.value"
/>
</el-select>
<el-input-number
v-model="customConditions[field.value].value"
:controls="false"
size="small"
:placeholder="field.placeholder || `请输入${field.label}`"
class="value-input"
@change="handleCustomChange"
/>
</div>
</div>
<!-- 日期类型 -->
<div v-else-if="field.type === 'date'" class="field-form-item">
<div class="field-label">
<span>{{ field.label }}</span>
<el-tag v-if="field.required" size="small" type="danger">必填</el-tag>
</div>
<div class="field-control">
<el-select
v-model="customConditions[field.value].operator"
placeholder="请选择"
size="small"
class="operator-select"
>
<el-option
v-for="op in getOperators(field)"
:key="op.value"
:label="op.label"
:value="op.value"
/>
</el-select>
<el-date-picker
v-model="customConditions[field.value].value"
type="date"
:placeholder="field.placeholder || `请选择${field.label}`"
size="small"
class="value-input"
format="YYYY-MM-DD"
value-format="YYYY-MM-DD"
@change="handleCustomChange"
/>
</div>
</div>
<!-- 日期范围类型 -->
<div v-else-if="field.type === 'daterange'" class="field-form-item">
<div class="field-label">
<span>{{ field.label }}</span>
<el-tag v-if="field.required" size="small" type="danger">必填</el-tag>
</div>
<div class="field-control">
<el-date-picker
v-model="customConditions[field.value].value"
type="daterange"
range-separator="至"
:start-placeholder="field.startPlaceholder || '开始日期'"
:end-placeholder="field.endPlaceholder || '结束日期'"
size="small"
style="width: 100%;"
format="YYYY-MM-DD"
value-format="YYYY-MM-DD"
@change="handleCustomChange"
/>
</div>
</div>
<!-- 下拉选择类型 -->
<div v-else-if="field.type === 'select'" class="field-form-item">
<div class="field-label">
<span>{{ field.label }}</span>
<el-tag v-if="field.required" size="small" type="danger">必填</el-tag>
</div>
<div class="field-control">
<el-select
v-model="customConditions[field.value].value"
:placeholder="field.placeholder || `请选择${field.label}`"
size="small"
filterable
clearable
style="width: 100%;"
@change="handleCustomChange"
>
<el-option
v-for="option in field.options"
:key="option.value"
:label="option.label"
:value="option.value"
/>
</el-select>
</div>
</div>
<!-- 多选类型 -->
<div v-else-if="field.type === 'multiple'" class="field-form-item">
<div class="field-label">
<span>{{ field.label }}</span>
<el-tag v-if="field.required" size="small" type="danger">必填</el-tag>
</div>
<div class="field-control">
<el-select
v-model="customConditions[field.value].value"
multiple
:placeholder="field.placeholder || `请选择${field.label}`"
size="small"
:filterable = "field.filterable"
style="width: 100%;"
@change="handleCustomChange"
>
<el-option
v-for="option in field.options"
:key="option.value"
:label="option.label"
:value="option.value"
/>
</el-select>
</div>
</div>
<!-- 布尔类型 -->
<div v-else-if="field.type === 'boolean'" class="field-form-item">
<div class="field-label">
<span>{{ field.label }}</span>
<el-tag v-if="field.required" size="small" type="danger">必填</el-tag>
</div>
<div class="field-control">
<el-select
v-model="customConditions[field.value].value"
:placeholder="field.placeholder || `请选择${field.label}`"
size="small"
style="width: 100%;"
@change="handleCustomChange"
>
<el-option label="是" :value="true" />
<el-option label="否" :value="false" />
<el-option label="全部" :value="null" />
</el-select>
</div>
</div>
<!-- 自定义组件类型 -->
<div v-else-if="field.type === 'custom'" class="field-form-item">
<div class="field-label">
<span>{{ field.label }}</span>
<el-tag v-if="field.required" size="small" type="danger">必填</el-tag>
</div>
<div class="field-control">
<component
:is="field.component"
v-model="customConditions[field.value].value"
v-bind="field.props || {}"
@change="handleCustomChange"
/>
</div>
</div>
</template>
</div>
</div>
<!-- 固定底部操作栏 -->
<div class="fixed-bottom-actions" :class="{ 'has-scroll': hasScroll }">
<div class="actions-content">
<el-button
size="small"
@click="handleReset"
class="reset-btn"
>
重置
</el-button>
<el-button
type="primary"
size="small"
@click="handleSubmit"
:loading="submitting"
>
确定
</el-button>
</div>
</div>
</div>
<!-- 预设过滤 -->
<div v-show="activeTab === 'preset'" class="preset-filter">
<div class="filter-scroll-area">
<!-- 搜索框 -->
<div class="preset-search" v-if="presetTree && presetTree.length > 0">
<el-input
v-model="presetSearchText"
placeholder="搜索过滤条件"
size="small"
clearable
@input="filterPresetTree"
>
<template #prefix>
<el-icon><Search /></el-icon>
</template>
</el-input>
</div>
<!-- 预设树 -->
<div class="preset-tree" v-if="filteredPresetTree && filteredPresetTree.length > 0">
<el-tree
ref="presetTreeRef"
:data="filteredPresetTree"
:props="treeProps"
node-key="id"
accordion
highlight-current
:expand-on-click-node="false"
:filter-node-method="filterNode"
@node-click="handlePresetClick"
>
<template #default="{ node, data }">
<span class="custom-tree-node">
<span class="tree-node-content">
<i v-if="data.icon" :class="data.icon" class="tree-icon"></i>
<span class="node-label">{{ node.label }}</span>
<span v-if="data.count" class="count-badge">{{ data.count }}</span>
</span>
</span>
</template>
</el-tree>
</div>
<!-- 空状态 -->
<div v-else class="empty-preset">
<div class="empty-content">
<el-icon size="40"><Files /></el-icon>
<p>暂无预设过滤条件</p>
</div>
</div>
</div>
</div>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, reactive, watch, computed, onMounted, onBeforeUnmount, nextTick } from 'vue'
import { Operation, DataAnalysis, Search, Files } from '@element-plus/icons-vue'
defineOptions({
name: 'ScanBox'
})
const props = defineProps({
// 组件宽度
width: {
type: String,
default: '300px'
},
// 预设树状条件
presetTree: {
type: Array,
default: () => []
},
// 树形结构配置
treeProps: {
type: Object,
default: () => ({
children: 'children',
label: 'label'
})
},
// 可用字段配置
availableFields: {
type: Array as any,
default: () => []
},
// 默认操作符配置
defaultOperators: {
type: Object,
default: () => ({
text: [
{ value: 'eq', label: '等于' },
{ value: 'ne', label: '不等于' },
{ value: 'contains', label: '包含' },
{ value: 'startswith', label: '开头为' },
{ value: 'endswith', label: '结尾为' }
],
number: [
{ value: 'eq', label: '等于' },
{ value: 'ne', label: '不等于' },
{ value: 'gt', label: '大于' },
{ value: 'lt', label: '小于' },
{ value: 'ge', label: '大于等于' },
{ value: 'le', label: '小于等于' },
{ value: 'between', label: '包含' }
],
date: [
{ value: 'eq', label: '等于' },
{ value: 'ne', label: '不等于' },
{ value: 'gt', label: '晚于' },
{ value: 'lt', label: '早于' }
]
})
},
// 初始条件
initialConditions: {
type: Object,
default: null
},
// 是否自动提交
autoSubmit: {
type: Boolean,
default: false
}
})
const emit = defineEmits(['condition-change', 'reset', 'condition-clear', 'submit'])
// Data
const activeTab = ref('custom')
const customConditions = reactive<any>({})
const presetSearchText = ref('')
const filteredPresetTree = ref<any[]>([])
const activeCondition = ref<any>(null)
const submitting = ref(false)
const hasScroll = ref(false)
const filterScroll = ref<HTMLElement | null>(null)
const presetTreeRef = ref<any>(null)
let debounceTimer: any = null
// Watchers
watch(() => props.initialConditions, (val) => {
if (val) {
loadInitialConditions(val)
}
}, { immediate: true })
watch(() => props.presetTree, (val) => {
if (val && Array.isArray(val)) {
filteredPresetTree.value = JSON.parse(JSON.stringify(val))
} else {
filteredPresetTree.value = []
}
}, { immediate: true })
watch(() => props.availableFields, (fields) => {
initCustomConditions(fields)
}, { immediate: true })
// Computed
const fieldValues = computed(() => {
return props.availableFields.map((field: any) => field.value)
})
// Lifecycle
onMounted(() => {
initCustomConditions(props.availableFields)
if (filterScroll.value) {
filterScroll.value.addEventListener('scroll', handleScroll)
}
checkScroll()
})
onBeforeUnmount(() => {
if (filterScroll.value) {
filterScroll.value.removeEventListener('scroll', handleScroll)
}
})
// Methods
function getTextPlaceholder(field: any) {
const operator = customConditions[field.value].operator
const basePlaceholder = field.placeholder || `请输入${field.label}`
if (operator === 'contains') {
return `${basePlaceholder}(多个值用逗号分隔)`
}
return basePlaceholder
}
function handleTextOperatorChange(field: any) {
const operator = customConditions[field.value].operator
if (operator !== 'contains') {
const currentValue = customConditions[field.value].value
if (Array.isArray(currentValue)) {
customConditions[field.value].value = currentValue.join(', ')
}
}
handleCustomChange()
}
function normalizeTextValue(field: any) {
const operator = customConditions[field.value].operator
const value = customConditions[field.value].value
if (operator !== 'contains' || !value) {
return
}
let normalizedValue = value
normalizedValue = normalizedValue
.replace(/[,;;、]/g, ',')
.replace(/\s+/g, '')
.replace(/,+/g, ',')
.replace(/^,|,$/g, '')
if (normalizedValue !== value) {
customConditions[field.value].value = normalizedValue
handleCustomChange()
}
}
function initCustomConditions(fields: any[]) {
fields.forEach(field => {
let defaultValue = undefined
let defaultOperator = ''
switch (field.type) {
case 'select':
case 'multiple':
defaultValue = field.multiple ? [] : undefined
break
case 'boolean':
defaultValue = null
break
case 'daterange':
defaultValue = []
break
default:
defaultValue = undefined
}
if (field.operators && Array.isArray(field.operators) && field.operators.length > 0) {
defaultOperator = field.defaultOperator || field.operators[0]
} else {
const defaultOps = props.defaultOperators[field.type as keyof typeof props.defaultOperators]
if (defaultOps && defaultOps.length > 0) {
defaultOperator = field.defaultOperator || defaultOps[0].value || ''
}
}
// Assign to reactive object
// Using Object.assign or direct assignment for existing properties to maintain reactivity if needed,
// but here we are initializing/resetting so direct assignment is fine, but we need to ensure reactivity.
// Since customConditions is reactive({}), adding new properties works.
if (!customConditions[field.value]) {
customConditions[field.value] = { value: defaultValue, operator: defaultOperator }
} else {
customConditions[field.value].value = defaultValue
customConditions[field.value].operator = defaultOperator
}
})
}
function loadInitialConditions(conditions: any) {
if (conditions.type === 'preset') {
activeCondition.value = conditions
activeTab.value = 'preset'
} else if (conditions.type === 'custom') {
activeCondition.value = conditions
activeTab.value = 'custom'
if (conditions.conditions) {
Object.keys(conditions.conditions).forEach(field => {
if (customConditions[field]) {
Object.assign(customConditions[field], conditions.conditions[field])
}
})
}
}
}
function checkScroll() {
if (!filterScroll.value) return
hasScroll.value = filterScroll.value.scrollHeight > filterScroll.value.clientHeight
}
function handleScroll() {
checkScroll()
}
function handleTabChange(pane: any) {
// Element Plus tab-click returns pane context. pane.paneName is the name.
// Vue 2 was tab component instance.
if (pane.paneName === 'custom') {
nextTick(() => {
checkScroll()
})
}
}
function getOperators(field: any) {
if (field.operators && field.operators.length > 0) {
const operatorDefinitions = props.defaultOperators[field.type as keyof typeof props.defaultOperators] || []
return operatorDefinitions.filter((op: any) => field.operators.includes(op.value))
}
return props.defaultOperators[field.type as keyof typeof props.defaultOperators] || (props.defaultOperators as any).text || []
}
function handlePresetClick(data: any, node: any) {
if (!node.isLeaf) return
activeCondition.value = data.value
emit('condition-change', activeCondition.value)
}
function handleCustomChange() {
if (props.autoSubmit) {
clearTimeout(debounceTimer)
debounceTimer = setTimeout(() => {
buildAndEmitCustomCondition()
}, 300)
}
}
function buildAndEmitCustomCondition() {
const conditions: any = {}
let hasCondition = false
props.availableFields.forEach((field: any) => {
const condition = customConditions[field.value]
let conditionValue = condition.value
if (field.type === 'text' && condition.operator === 'contains' && conditionValue) {
const values = conditionValue.split(',').filter((v: string) => v.trim())
if (values.length > 0) {
conditionValue = values
}
}
if (conditionValue !== undefined &&
conditionValue !== null &&
conditionValue !== '' &&
!(Array.isArray(conditionValue) && conditionValue.length === 0)) {
conditions[field.value] = {
value: conditionValue,
operator: condition.operator,
field: field.value,
type: field.type,
isMultiple: Array.isArray(conditionValue)
}
hasCondition = true
}
})
if (hasCondition) {
activeCondition.value = conditions
emit('condition-change', activeCondition.value)
} else if (activeCondition.value && activeCondition.value.type === 'custom') {
clearActiveCondition()
}
}
function filterPresetTree(val: string) {
if (presetTreeRef.value) {
presetTreeRef.value.filter(val)
}
}
function filterNode(value: string, data: any) {
if (!value) return true
return data.label && data.label.toLowerCase().includes(value.toLowerCase())
}
function handleReset() {
initCustomConditions(props.availableFields)
activeCondition.value = null
presetSearchText.value = ''
filterPresetTree('')
emit('reset')
if (props.autoSubmit) {
buildAndEmitCustomCondition()
}
}
function handleSubmit() {
submitting.value = true
buildAndEmitCustomCondition()
emit('submit', activeCondition.value)
setTimeout(() => {
submitting.value = false
}, 300)
}
function clearActiveCondition() {
activeCondition.value = null
emit('condition-clear')
}
// 对外暴露的方法,供父组件调用
function getConditions() {
// 如果当前是自定义筛选,且没有已应用的 activeCondition,或者想要获取最新的输入值
// 我们可以根据 activeTab 来决定返回什么
if (activeTab.value === 'custom') {
const currentConditions = buildConditionsObject()
return currentConditions
}
if (!activeCondition.value) return {}
if (typeof activeCondition.value === 'string' || typeof activeCondition.value === 'number') {
return { preset: activeCondition.value }
}
if (activeCondition.value && (activeCondition.value as any).conditions) {
const where: any = {}
Object.keys((activeCondition.value as any).conditions).forEach((k) => {
const c = (activeCondition.value as any).conditions[k]
where[k] = c && c.value !== undefined ? c.value : c
})
return where
}
if (typeof activeCondition.value === 'object') {
const where: any = {}
Object.keys(activeCondition.value).forEach((k) => {
const c = (activeCondition.value as any)[k]
where[k] = c && c.value !== undefined ? c.value : c
})
return where
}
return {}
}
function buildConditionsObject() {
const conditions: any = {}
props.availableFields.forEach((field: any) => {
const condition = customConditions[field.value]
let conditionValue = condition.value
if (field.type === 'text' && condition.operator === 'contains' && conditionValue) {
const values = conditionValue.split(',').filter((v: string) => v.trim())
if (values.length > 0) {
conditionValue = values
}
}
if (conditionValue !== undefined &&
conditionValue !== null &&
conditionValue !== '' &&
!(Array.isArray(conditionValue) && conditionValue.length === 0)) {
conditions[field.value] = conditionValue
}
})
return conditions
}
function reset() {
handleReset()
}
function clear() {
clearActiveCondition()
}
defineExpose({ getConditions, reset, clear })
</script>
<style scoped>
.scan-box-container {
height: 100%;
display: flex;
flex-direction: column;
background: #fff;
border-right: 1px solid #e8e8e8;
box-shadow: 1px 0 2px rgba(0, 0, 0, 0.05);
width: 250px;
min-width: 160px;
}
.scan-tabs {
flex-shrink: 0;
border-bottom: 1px solid #e8e8e8;
background: #fafafa;
}
.scan-tabs :deep(.el-tabs__header) {
margin: 0;
}
.scan-tabs :deep(.el-tabs__nav-wrap::after) {
height: 1px;
}
.scan-tabs :deep(.el-tabs__item) {
height: 40px;
line-height: 40px;
font-size: 13px;
}
.tab-label {
display: flex;
align-items: center;
justify-content: center;
gap: 4px;
}
.scan-content {
flex: 1;
display: flex;
flex-direction: column;
overflow: hidden;
}
.custom-filter,
.preset-filter {
flex: 1;
display: flex;
flex-direction: column;
overflow: hidden;
}
.filter-scroll-area {
flex: 1;
overflow-y: auto;
padding: 16px;
/* 隐藏滚动条 */
&::-webkit-scrollbar {
display: none;
}
-ms-overflow-style: none; /* IE and Edge */
scrollbar-width: none; /* Firefox */
}
/* 自定义筛选样式 */
.field-forms {
display: flex;
flex-direction: column;
gap: 16px;
}
.field-form-item {
display: flex;
flex-direction: column;
gap: 8px;
}
.field-label {
display: flex;
justify-content: space-between;
align-items: center;
font-size: 13px;
font-weight: 500;
color: #333;
}
.field-label .el-tag {
margin-left: 4px;
}
.field-control {
display: flex;
flex-direction: row;
gap: 4px;
}
.operator-select {
width: 85px;
flex-shrink: 0;
}
.value-input {
flex: 1;
min-width: 0;
}
.field-control :deep(.el-input__wrapper),
.field-control :deep(.el-select__wrapper) {
border-radius: 4px;
}
/* 预设筛选样式 */
.preset-search {
margin-bottom: 12px;
}
.preset-tree {
padding: 8px 0;
}
.custom-tree-node {
flex: 1;
display: flex;
align-items: center;
justify-content: space-between;
font-size: 13px;
padding: 2px 0;
}
.tree-node-content {
display: flex;
align-items: center;
gap: 6px;
}
.tree-icon {
color: #409eff;
font-size: 14px;
}
.node-label {
flex: 1;
}
.count-badge {
background: #f0f0f0;
padding: 2px 6px;
border-radius: 10px;
font-size: 12px;
color: #999;
}
.empty-preset {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
color: #999;
}
.empty-content {
text-align: center;
}
.empty-content i {
font-size: 40px;
margin-bottom: 12px;
color: #e0e0e0;
}
.empty-content p {
margin: 0;
font-size: 13px;
}
/* 底部固定操作栏 */
.fixed-bottom-actions {
flex-shrink: 0;
padding: 12px 16px;
background: #fff;
border-top: 1px solid #e8e8e8;
display: flex;
justify-content: flex-end;
box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.05);
z-index: 10;
transition: all 0.3s;
}
.fixed-bottom-actions.has-scroll {
box-shadow: 0 -4px 12px rgba(0, 0, 0, 0.1);
}
.actions-content {
display: flex;
gap: 12px;
width: 100%;
}
.actions-content button {
flex: 1;
}
.reset-btn {
color: #666;
}
</style>