gaoluyang
2 小时以前 e449a5408265e4bd1f6c66f5be28a42efac444ee
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
<!--
  多条件查询弹层(schema 驱动)
 
  <FilterPanel v-model:show="showFilter" :model-value="query" :fields="filterFields"
               @search="onSearch" @reset="onReset" />
 
  注意:query 用 reactive 定义时不要写 v-model="query"。
  组件回吐的是整个筛选对象,reactive 常量不可整体重新赋值
  (会编译成 `query = $event`,运行时抛 Assignment to constant variable)。
  用 :model-value 传入 + 在 @search/@reset 里 Object.assign(query, values) 写回:
 
    const query = reactive({ projectName: '', bidResult: '' });
    const onSearch = values => { Object.assign(query, values); getList(); };
 
  页面若另有搜索输入框绑到 query 的某个字段,两边会天然同步(同一个对象)。
 
  fields 支持的类型:
    { prop: 'projectName', label: '项目名称', type: 'input' }
    { prop: 'bidResult',   label: '招投标结果', type: 'select', options: [...] }
    { prop: 'filingDate',  label: '立案时间',   type: 'date' }
    { prop: 'bidDateRange', label: '投标日期',  type: 'daterange' }
 
  daterange 类型的值是 [开始, 结束] 两元素数组,页面在提交前自行拆成后端要的参数名
  (各模块的区间参数名不统一,如 bidDateStart/bidDateEnd、beginShippingDate/endShippingDate)。
-->
<template>
  <up-popup :show="show"
            mode="bottom"
            :round="16"
            @close="close">
    <view class="filter-panel">
      <view class="filter-panel__header">
        <text class="filter-panel__title">{{ title }}</text>
        <u-icon name="close"
                size="20"
                color="#909399"
                @click="close"></u-icon>
      </view>
 
      <scroll-view scroll-y
                   class="filter-panel__body">
        <view v-for="field in fields"
              :key="field.prop"
              class="filter-panel__field">
          <text class="filter-panel__label">{{ field.label }}</text>
          <view class="filter-panel__control">
            <u-input v-if="field.type === 'input'"
                     v-model="local[field.prop]"
                     :placeholder="field.placeholder || '请输入'"
                     clearable />
            <FormPicker v-else-if="field.type === 'select'"
                        v-model="local[field.prop]"
                        type="select"
                        :options="field.options || []"
                        :title="`选择${field.label}`"
                        :placeholder="field.placeholder || '请选择'" />
            <FormPicker v-else-if="field.type === 'date'"
                        v-model="local[field.prop]"
                        type="date"
                        :placeholder="field.placeholder || '请选择'" />
            <view v-else-if="field.type === 'daterange'"
                  class="filter-panel__range">
              <FormPicker v-model="local[field.prop][0]"
                          type="date"
                          placeholder="开始日期" />
              <text class="filter-panel__range-sep">至</text>
              <FormPicker v-model="local[field.prop][1]"
                          type="date"
                          placeholder="结束日期" />
            </view>
          </view>
        </view>
      </scroll-view>
 
      <view class="filter-panel__footer">
        <u-button class="filter-panel__btn"
                  @click="reset">重置</u-button>
        <u-button class="filter-panel__btn"
                  type="primary"
                  @click="search">查询</u-button>
      </view>
    </view>
  </up-popup>
</template>
 
<script setup>
  import { ref, watch } from "vue";
  import FormPicker from "@/components/FormPicker.vue";
 
  const props = defineProps({
    show: {
      type: Boolean,
      default: false,
    },
    modelValue: {
      type: Object,
      default: () => ({}),
    },
    fields: {
      type: Array,
      default: () => [],
    },
    title: {
      type: String,
      default: "筛选",
    },
  });
 
  const emit = defineEmits(["update:show", "update:modelValue", "search", "reset"]);
 
  const buildEmpty = () => {
    const empty = {};
    props.fields.forEach(field => {
      empty[field.prop] = field.type === "daterange" ? [] : "";
    });
    return empty;
  };
 
  // 必须先用 buildEmpty() 铺满字段:弹层即使隐藏时也会渲染子节点,
  // daterange 的 v-model 会对 local[prop][0] 取值,local[prop] 为 undefined 会直接报错
  const local = ref(buildEmpty());
 
  const syncFromParent = () => {
    const source = props.modelValue || {};
    const next = buildEmpty();
    props.fields.forEach(field => {
      const value = source[field.prop];
      if (field.type === "daterange") {
        next[field.prop] = Array.isArray(value) ? [...value] : [];
      } else if (value !== undefined && value !== null) {
        next[field.prop] = value;
      }
    });
    local.value = next;
  };
 
  watch(
    () => props.show,
    show => {
      if (show) syncFromParent();
    },
    { immediate: true }
  );
 
  // fields 是异步拿到的场景:补一次铺底,避免字段缺失
  watch(
    () => props.fields,
    () => {
      if (props.show) {
        syncFromParent();
      } else {
        local.value = buildEmpty();
      }
    },
    { deep: true }
  );
 
  const close = () => {
    emit("update:show", false);
  };
 
  const search = () => {
    emit("update:modelValue", { ...local.value });
    emit("search", { ...local.value });
    close();
  };
 
  const reset = () => {
    local.value = buildEmpty();
    emit("update:modelValue", { ...local.value });
    emit("reset", { ...local.value });
  };
</script>
 
<style scoped lang="scss">
  .filter-panel {
    background: #ffffff;
    padding: 16px;
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
  }
 
  .filter-panel__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-bottom: 12px;
    border-bottom: 1px solid #f0f0f0;
  }
 
  .filter-panel__title {
    font-size: 16px;
    font-weight: 600;
    color: #303133;
  }
 
  .filter-panel__body {
    max-height: 55vh;
    padding: 8px 0;
  }
 
  .filter-panel__field {
    display: flex;
    align-items: center;
    padding: 10px 0;
    border-bottom: 1px solid #f8f8f8;
  }
 
  .filter-panel__label {
    width: 90px;
    flex-shrink: 0;
    font-size: 14px;
    color: #606266;
  }
 
  .filter-panel__control {
    flex: 1;
    min-width: 0;
  }
 
  .filter-panel__range {
    display: flex;
    align-items: center;
    gap: 8px;
  }
 
  .filter-panel__range-sep {
    flex-shrink: 0;
    font-size: 13px;
    color: #909399;
  }
 
  .filter-panel__footer {
    display: flex;
    gap: 12px;
    padding-top: 16px;
  }
 
  .filter-panel__btn {
    flex: 1;
  }
</style>