MyComponent.vue
17.6 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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
<!--
* @Date: 2026-05-25 17:10:00
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2026-06-05 21:52:12
* @FilePath: /data-table/src/components/PersonPickerField/MyComponent.vue
* @Description: 人员筛选控件内部面板
-->
<template>
<div class="person-picker-field-inner">
<div
:class="['select-person-box', { 'select-person-box--readonly': props.component_props.readonly }]"
@click="!props.component_props.readonly && openPopup()"
>
<template v-if="confirmedPersons.length">
<div
v-for="person in confirmedPersons"
:key="person.id"
class="select-person-item"
>
<van-icon name="contact-o" />
<div class="person-display-text">
<span>{{ formatPersonTitle(person) }}</span>
<span v-if="isPersonBlacklisted(person)" class="blacklist-badge blacklist-badge--chip">黑名单</span>
</div>
</div>
</template>
<div v-else class="select-person-placeholder">
{{ props.component_props.readonly ? '暂无已选人员' : '点击搜索人员' }}
</div>
</div>
<van-popup
v-if="!props.component_props.readonly"
v-model:show="showPopup"
position="bottom"
:close-on-click-overlay="false"
:style="{ height: '100vh' }"
>
<div class="search-bar">
<div class="search-bar-title">{{ getPickerTitle() }}</div>
<div class="search-input-row">
<van-field
ref="searchInputRef"
v-model="searchKeyword"
class="search-input-field"
placeholder="请输入姓名、法名、手机号或证件号"
:border="false"
@keydown.enter.prevent.stop="onSearchEnter"
>
<template #button>
<div class="search-bar-actions">
<van-button size="small" type="primary" :loading="searching" @click="onSearch">搜索</van-button>
<van-button size="small" plain type="default" @click="onClearSearch">清空</van-button>
</div>
</template>
</van-field>
</div>
</div>
<div class="selected-box" style="margin: 1rem;">
<div v-for="person in draftPersons" :key="person.id" class="selected-item">
<div class="selected-item__text">
<span>{{ formatPersonTitle(person) }}</span>
<span v-if="isPersonBlacklisted(person)" class="blacklist-badge blacklist-badge--chip">黑名单</span>
</div>
<van-icon name="close" @click="removeDraftPerson(person)" />
</div>
</div>
<div v-if="hasSearched" class="search-result-container">
<van-list
v-model:loading="loading"
:finished="finished"
:finished-text="finishedTextStatus ? '没有更多了' : ''"
@load="onLoad"
>
<van-checkbox-group v-model="searchResultChecked" @change="onSearchResultChange">
<div v-for="person in searchResults" :key="person.id" class="search-result-item">
<van-checkbox
:name="person.id"
shape="square"
icon-size="14px"
:checked-color="styleColor.baseColor"
>
<div class="search-result-item__content">
<div class="search-result-item__title">
<span>{{ formatPersonDisplayName(person) }}</span>
<span v-if="isPersonBlacklisted(person)" class="blacklist-badge">黑名单</span>
</div>
<div class="search-result-item__meta">
<span v-if="person.phone">{{ person.phone }}</span>
</div>
</div>
</van-checkbox>
</div>
</van-checkbox-group>
</van-list>
<div v-if="emptyStatus" class="search-result-empty">暂无搜索结果</div>
</div>
<div class="popup-footer">
<van-row gutter="10" style="padding: 0 10px 1rem;">
<van-col span="12">
<van-button block round type="default" @click="onCancelClick">取消</van-button>
</van-col>
<van-col span="12">
<van-button block round type="primary" @click="onConfirmClick">确定</van-button>
</van-col>
</van-row>
</div>
</van-popup>
</div>
</template>
<script setup>
import { inject, onMounted, ref } from 'vue';
import { useRoute } from 'vue-router';
import { useCustomFieldValue } from '@vant/use';
import Cookies from 'js-cookie';
import _ from 'lodash';
import { showToast } from 'vant';
import { styleColor } from '@/constant.js';
import { searchPersonPickerAPI } from '@/api/component.js';
const props = inject('props');
const $route = useRoute();
const showPopup = ref(false);
const searching = ref(false);
const hasSearched = ref(false);
const loading = ref(false);
const finished = ref(false);
const finishedTextStatus = ref(true);
const emptyStatus = ref(false);
const searchInputRef = ref(null);
const searchKeyword = ref('');
const searchResults = ref([]);
const searchResultChecked = ref([]);
const confirmedPersons = ref([]);
const draftPersons = ref([]);
const fieldValue = ref([]);
const searchPage = ref(0);
const searchLimit = ref(Number(props.component_props.limit) || 20);
const searchTotal = ref(0);
/**
* 当前字段配置的人员类型编码,直接传给搜索接口的 type 参数。
*
* @returns {string}
*/
const getConfiguredPersonType = () => {
return String(props.component_props.person_type || '').trim();
};
/**
* 当前字段配置的人员类型中文名,用于已选项和结果列表展示。
*
* @returns {string}
*/
const getConfiguredPersonTypeTitle = () => {
return String(props.component_props.person_type_title || '').trim();
};
/**
* 将人员接口记录整理成组件内部统一结构。
* 这里直接按后端约定字段读取;如果历史数据里没带 type,就回退到当前字段配置的 person_type。
*
* @param {Object} person 人员记录
* @returns {{ id: number, name: string, nickname: string, phone: string, idcard: string, type: string, is_blacklist: boolean }}
*/
const mapPersonRecord = (person) => {
return {
id: Number(person.id),
name: person.name || '',
nickname: person.nickname || '',
phone: person.phone || '',
idcard: person.idcard || '',
type: String(person.type || getConfiguredPersonType()).trim(),
is_blacklist: normalizeBlacklistFlag(person.is_blacklist),
};
};
/**
* 兼容接口和历史缓存里可能出现的布尔、数字、字符串黑名单标识。
*
* @param {boolean|number|string} value 黑名单标识
* @returns {boolean}
*/
const normalizeBlacklistFlag = (value) => {
if (typeof value === 'string') {
const normalizedValue = value.trim().toLowerCase();
return normalizedValue === 'true' || normalizedValue === '1';
}
return value === true || value === 1;
};
/**
* 组件默认值可能来自历史数据或 cookie,这里保留最小限度的字符串反序列化。
*
* @param {Array|String} value 已选人员列表
* @returns {Array}
*/
const normalizeStoredPersonList = (value) => {
const source = typeof value === 'string' ? JSON.parse(value || '[]') : (value || []);
return source.map(mapPersonRecord);
};
/**
* 将已确认人员同步到自定义字段值,保证表单提交拿到的是最终确认结果。
*/
const syncFieldValue = () => {
fieldValue.value = _.cloneDeep(confirmedPersons.value);
props.value = fieldValue.value;
};
const formatPersonType = (type) => {
const configuredType = getConfiguredPersonType();
const configuredTypeTitle = getConfiguredPersonTypeTitle();
if (configuredTypeTitle && (!type || type === configuredType)) {
return configuredTypeTitle;
}
return type || configuredTypeTitle || '人员';
};
const getPickerTitle = () => {
return `选择${formatPersonType(getConfiguredPersonType())}`;
};
const formatPersonDisplayName = (person) => {
if (!person.nickname) {
return person.name;
}
return `${person.name}(${person.nickname})`;
};
const formatPersonTitle = (person) => {
return formatPersonDisplayName(person);
};
const isPersonBlacklisted = (person) => {
return Boolean(person?.is_blacklist);
};
/**
* 根据当前草稿已选项回填搜索结果勾选状态,保证翻页后勾选表现一致。
*/
const syncSearchChecked = () => {
const selectedIdSet = new Set(draftPersons.value.map((person) => person.id));
searchResultChecked.value = searchResults.value
.filter((person) => selectedIdSet.has(person.id))
.map((person) => person.id);
};
/**
* 重置搜索面板状态;打开弹层、取消和清空搜索时都会复用这套逻辑。
*/
const resetSearchState = () => {
hasSearched.value = false;
searching.value = false;
loading.value = false;
finished.value = false;
finishedTextStatus.value = true;
emptyStatus.value = false;
searchKeyword.value = '';
searchResults.value = [];
searchResultChecked.value = [];
searchPage.value = 0;
searchTotal.value = 0;
};
const openPopup = async () => {
if (props.component_props.readonly) {
return false;
}
draftPersons.value = _.cloneDeep(confirmedPersons.value);
resetSearchState();
showPopup.value = true;
await runSearch({
keyword: '',
});
};
const onCancelClick = () => {
showPopup.value = false;
draftPersons.value = _.cloneDeep(confirmedPersons.value);
resetSearchState();
};
const writeDraftCookie = () => {
const currentValue = _.cloneDeep(fieldValue.value);
const existingCookie = Cookies.get($route.query.code);
if (existingCookie) {
const obj = JSON.parse(existingCookie);
obj[props.key] = currentValue;
Cookies.set($route.query.code, JSON.stringify(obj), { expires: 1 });
} else {
Cookies.set($route.query.code, JSON.stringify({ [props.key]: currentValue }), { expires: 1 });
}
};
const onConfirmClick = () => {
confirmedPersons.value = _.cloneDeep(draftPersons.value);
syncFieldValue();
writeDraftCookie();
showPopup.value = false;
resetSearchState();
};
const removeDraftPerson = (person) => {
draftPersons.value = draftPersons.value.filter((item) => item.id !== person.id);
syncSearchChecked();
};
/**
* 勾选变化时只处理当前搜索结果页,避免跨页已选项被误删。
*
* @param {number[]} checkedIds 当前结果页勾选的人员 id
*/
const onSearchResultChange = (checkedIds) => {
const checkedIdSet = new Set(checkedIds);
const currentSearchIdSet = new Set(searchResults.value.map((person) => person.id));
// 先移除当前搜索页中被取消勾选的人员,避免跨页已选项被误删。
draftPersons.value = draftPersons.value.filter((person) => {
if (!currentSearchIdSet.has(person.id)) {
return true;
}
return checkedIdSet.has(person.id);
});
const selectedFromCurrentSearch = searchResults.value.filter((person) => checkedIdSet.has(person.id));
draftPersons.value = _.uniqBy([...draftPersons.value, ...selectedFromCurrentSearch], 'id');
};
const onClearSearch = () => {
resetSearchState();
runSearch({
keyword: '',
});
};
/**
* 组装人员搜索接口参数。
*
* @param {number} page 页码,从 0 开始
* @returns {{ form_code: string, field_name: string, keyword: string, page: number, limit: number, type?: string, force_back?: string }}
*/
const buildSearchParams = (page) => {
const params = {
form_code: $route.query.code,
field_name: props.key,
keyword: searchKeyword.value.trim(),
page,
limit: searchLimit.value,
type: getConfiguredPersonType(),
};
if ($route.query.force_back) {
params.force_back = $route.query.force_back;
}
return params;
};
/**
* 拉取单页搜索结果,并转换成组件内部统一结构。
* 组件接口封装在报错时会返回 false,这里需要兜底,避免搜索面板直接抛运行时异常。
*
* @param {number} page 页码,从 0 开始
* @returns {Promise<{ count: number, data: Array }>}
*/
const fetchSearchPage = async (page) => {
const result = await searchPersonPickerAPI(buildSearchParams(page));
if (!result || !Array.isArray(result.data)) {
return {
count: 0,
data: [],
};
}
// 搜索结果以接口返回为准,当前字段只会请求配置里的单一 person_type。
const remoteResults = result.data.map(mapPersonRecord);
return {
count: Number(result.count) || remoteResults.length,
data: remoteResults,
};
};
/**
* 合并分页结果并去重,避免上拉加载时重复插入同一人员。
*
* @param {Array} data 当前页结果
*/
const mergeSearchResults = (data) => {
searchResults.value = _.uniqBy([...searchResults.value, ...data], 'id');
emptyStatus.value = Object.is(searchResults.value.length, 0);
finishedTextStatus.value = !emptyStatus.value;
syncSearchChecked();
};
/**
* 根据接口总数和当前页结果更新无限滚动结束状态。
*
* @param {Array} pageData 当前页结果
* @param {number} total 接口返回总数
*/
const updateFinishedStatus = (pageData, total) => {
searchTotal.value = total;
if (!pageData.length) {
finished.value = true;
return;
}
if (searchResults.value.length >= total) {
finished.value = true;
return;
}
if (pageData.length < searchLimit.value) {
finished.value = true;
}
};
/**
* 执行一次新的搜索,并重置分页状态。
*/
const runSearch = async ({ keyword }) => {
searchKeyword.value = keyword;
searching.value = true;
hasSearched.value = true;
loading.value = true;
finished.value = false;
finishedTextStatus.value = true;
emptyStatus.value = false;
searchResults.value = [];
searchResultChecked.value = [];
searchPage.value = 0;
searchTotal.value = 0;
try {
const firstPageResult = await fetchSearchPage(0);
mergeSearchResults(firstPageResult.data);
updateFinishedStatus(firstPageResult.data, firstPageResult.count);
} finally {
loading.value = false;
searching.value = false;
}
};
const onSearch = async () => {
const keyword = searchKeyword.value.trim();
await runSearch({
keyword,
});
};
const onSearchEnter = async () => {
await onSearch();
};
/**
* 加载下一页搜索结果。
*/
const onLoad = async () => {
if (!hasSearched.value || finished.value) {
loading.value = false;
return;
}
const nextPage = searchPage.value + 1;
try {
const nextPageResult = await fetchSearchPage(nextPage);
mergeSearchResults(nextPageResult.data);
updateFinishedStatus(nextPageResult.data, nextPageResult.count);
searchPage.value = nextPage;
} finally {
loading.value = false;
}
};
useCustomFieldValue(() => fieldValue.value);
onMounted(() => {
confirmedPersons.value = normalizeStoredPersonList(props.component_props.default);
draftPersons.value = _.cloneDeep(confirmedPersons.value);
syncFieldValue();
});
</script>
<style lang="less" scoped>
.person-picker-field-inner {
width: 100%;
}
.select-person-box,
.selected-box {
min-height: 4rem;
border: 1px dashed #dfdfdf;
margin: 1rem 0;
overflow: auto;
border-radius: 5px;
padding: 0.5rem;
display: flex;
flex-wrap: wrap;
align-items: flex-start;
}
.select-person-item,
.selected-item {
margin-right: 5px;
margin-bottom: 5px;
font-size: 0.85rem;
padding: 5px 8px;
background-color: #c2915f;
color: #fff;
min-height: 1.2rem;
display: flex;
align-items: center;
border-radius: 4px;
}
.selected-item {
max-width: 100%;
}
.selected-item__text {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 0.35rem;
margin-right: 0.25rem;
}
.person-display-text {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 0.35rem;
}
.select-person-placeholder {
color: #999;
font-size: 0.9rem;
line-height: 1.8rem;
}
.select-person-box--readonly {
min-height: auto;
padding: 0.2rem 0;
border: none;
margin: 0;
border-radius: 0;
}
.search-bar {
margin: 1rem 0;
padding: 0;
}
.search-bar-title {
padding: 0 0.75rem 0.5rem;
font-size: 1.1rem;
font-weight: 600;
color: #c2915f;
line-height: 1.5;
}
.search-input-row {
display: flex;
align-items: center;
}
.search-input-field {
width: 100%;
}
.search-bar-actions {
display: flex;
gap: 0.5rem;
}
.type-filter-row {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
margin-top: 0.35rem;
padding-left: 0.75rem;
}
.type-filter-chip {
padding: 0.15rem 0.1rem;
color: #888;
font-size: 0.82rem;
line-height: 1.2rem;
}
.type-filter-chip--active {
color: #c2915f;
font-weight: 600;
}
.search-result-container {
height: calc(90vh - 17rem);
overflow: auto;
padding: 0 1rem 6rem;
}
.search-result-item {
padding: 0.75rem 0;
border-bottom: 1px solid #f0f0f0;
}
.search-result-item__content {
padding-left: 0.5rem;
white-space: normal;
}
.search-result-item__title {
font-size: 0.9rem;
color: #333;
line-height: 1.4;
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 0.35rem;
}
.search-result-item__meta {
margin-top: 0.25rem;
font-size: 0.78rem;
color: #888;
word-break: break-all;
}
.search-result-empty {
padding: 1rem 0;
text-align: center;
color: #999;
font-size: 0.9rem;
}
.blacklist-badge {
display: inline-flex;
align-items: center;
justify-content: center;
padding: 0 0.35rem;
border-radius: 999px;
font-size: 0.7rem;
line-height: 1.35rem;
color: #fff;
background-color: #d84d4d;
}
.blacklist-badge--light {
background-color: rgba(216, 77, 77, 0.16);
color: #d84d4d;
}
.blacklist-badge--chip {
background-color: rgba(255, 244, 225, 0.92);
color: #a46943;
}
.popup-footer {
position: fixed;
bottom: 0;
left: 0;
width: 100%;
background: #fff;
}
:deep(.van-field__body) {
border: var(--border-style);
border-radius: 0.25rem;
padding: 0.25rem 0.5rem;
}
</style>