af-mobile-client-vue3 1.4.1 → 1.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,1581 +1,1601 @@
1
- <script setup lang="ts">
2
- import type { FieldType } from 'vant'
3
- import type { Numeric } from 'vant/es/utils'
4
- import ImageUploader from '@af-mobile-client-vue3/components/core/ImageUploader/index.vue'
5
- import XGridDropOption from '@af-mobile-client-vue3/components/core/XGridDropOption/index.vue'
6
- import XMultiSelect from '@af-mobile-client-vue3/components/core/XMultiSelect/index.vue'
7
- import XSelect from '@af-mobile-client-vue3/components/core/XSelect/index.vue'
8
- import XLocationPicker from '@af-mobile-client-vue3/components/data/XOlMap/XLocationPicker/index.vue'
9
- import { getConfigByNameAsync, runLogic } from '@af-mobile-client-vue3/services/api/common'
10
- import { post } from '@af-mobile-client-vue3/services/restTools'
11
- import { searchToListOption, searchToOption } from '@af-mobile-client-vue3/services/v3Api'
12
- import { useUserStore } from '@af-mobile-client-vue3/stores/modules/user'
13
- import { getDict } from '@af-mobile-client-vue3/utils/dictUtil'
14
- import { mobileUtil } from '@af-mobile-client-vue3/utils/mobileUtil'
15
- import { executeStrFunctionByContext } from '@af-mobile-client-vue3/utils/runEvalFunction'
16
- import { areaList } from '@vant/area-data'
17
- import dayjs from 'dayjs/esm/index'
18
- import { debounce } from 'lodash-es'
19
- import {
20
- showToast,
21
- Area as VanArea,
22
- Button as VanButton,
23
- Calendar as VanCalendar,
24
- Cascader as VanCascader,
25
- Checkbox as VanCheckbox,
26
- CheckboxGroup as vanCheckboxGroup,
27
- DatePicker as VanDatePicker,
28
- Field as VanField,
29
- Picker as VanPicker,
30
- PickerGroup as VanPickerGroup,
31
- Popup as VanPopup,
32
- Radio as VanRadio,
33
- RadioGroup as VanRadioGroup,
34
- Rate as VanRate,
35
- Slider as VanSlider,
36
- Stepper as VanStepper,
37
- Switch as VanSwitch,
38
- TimePicker as VanTimePicker,
39
- } from 'vant'
40
- import { computed, defineEmits, defineModel, defineProps, getCurrentInstance, onBeforeMount, ref, watch } from 'vue'
41
-
42
- const props = defineProps({
43
- attr: {
44
- type: Object,
45
- },
46
- form: {
47
- type: Object,
48
- },
49
- // 整表只读:来自 XForm,统一控制交互
50
- formReadonly: {
51
- type: Boolean,
52
- default: false,
53
- },
54
- datePickerFilter: {
55
- type: Function,
56
- default: () => true,
57
- },
58
- datePickerFormatter: {
59
- type: Function,
60
- default: (type, val) => val,
61
- },
62
- mode: {
63
- type: String,
64
- default: '查询',
65
- },
66
- serviceName: {
67
- type: String,
68
- default: undefined,
69
- },
70
- // 调用logic获取数据源的追加参数
71
- getDataParams: {
72
- type: Object,
73
- default: undefined,
74
- },
75
- disabled: {
76
- type: Boolean,
77
- default: false,
78
- },
79
- rules: {
80
- type: Object,
81
- default: () => {},
82
- },
83
- // 用 defineModel 替代 modelValue/emit
84
- modelValue: {
85
- type: [String, Number, Boolean, Array, Object],
86
- default: undefined,
87
- },
88
- showLabel: {
89
- type: Boolean,
90
- default: true,
91
- },
92
- // radio/checkbox/select/mul-select 选项数据结构
93
- columnsField: {
94
- type: Object,
95
- default: () => {
96
- return { text: 'label', value: 'value' }
97
- },
98
- },
99
- isAsyncUpload: {
100
- type: Boolean,
101
- default: false,
102
- },
103
-
104
- })
105
-
106
- const emits = defineEmits(['setForm', 'xFormItemEmitFunc'])
107
-
108
- // 用 defineModel 替代 modelValue/emit
109
- const modelData = defineModel<string | number | boolean | any[] | Record<string, any>>()
110
-
111
- // 获取字典
112
- interface OptionItem {
113
- label: string
114
- value: any
115
- children?: OptionItem[]
116
- }
117
-
118
- // 判断并初始化防抖函数
119
- let debouncedUserLinkFunc: (() => void) | null = null
120
- let debouncedDepLinkFunc: (() => void) | null = null
121
- let debouncedUpdateOptions: (() => void) | null = null
122
-
123
- const { attr, form, mode, serviceName, getDataParams, columnsField } = props
124
- // 配置的表单值格式(仅针对 datePicker 生效)
125
- // 作用:统一控制日期值的格式化输入/输出与选择器展示粒度
126
- // 可选:'YYYY' | 'YYYY-MM' | 'YYYY-MM-DD' | 'YYYY-MM-DD HH' | 'YYYY-MM-DD HH:mm' | 'YYYY-MM-DD HH:mm:ss'
127
- const formValueFormat = computed(() => {
128
- // 默认全格式
129
- return (attr && (attr as any).formValueFormat) || 'YYYY-MM-DD HH:mm:ss'
130
- })
131
-
132
- // 根据 formValueFormat 动态计算日期列类型(决定 VanDatePicker 展示年/月/日)
133
- // 例如:YYYY 只显示年;YYYY-MM 显示年、月;YYYY-MM-DD 显示年、月、日
134
- const dateColumnsType = computed(() => {
135
- const format = formValueFormat.value
136
- const columns: string[] = ['year']
137
- if (format.includes('MM'))
138
- columns.push('month')
139
- if (format.includes('DD'))
140
- columns.push('day')
141
- return columns as any
142
- })
143
-
144
- // 是否包含时间(决定是否展示时间页签与 VanTimePicker)
145
- const hasTime = computed(() => formValueFormat.value.includes('HH'))
146
-
147
- // 根据 formValueFormat 动态计算时间列类型(决定时/分/秒的展示)
148
- // 例如:包含 HH 显示小时;包含 mm 显示分钟;包含 ss 显示秒
149
- const timeColumnsType = computed(() => {
150
- const format = formValueFormat.value
151
- const columns: string[] = []
152
- if (format.includes('HH'))
153
- columns.push('hour')
154
- if (format.includes('mm'))
155
- columns.push('minute')
156
- if (format.includes('ss'))
157
- columns.push('second')
158
- return columns as any
159
- })
160
- const calendarShow = ref(false)
161
- const option = ref([])
162
- const pickerValue = ref(undefined)
163
- const timePickerValue = ref(undefined)
164
- const area = ref<any>(undefined)
165
- const showPicker = ref(false)
166
- const showDatePicker = ref(false)
167
- const showTimePicker = ref(false)
168
- const showArea = ref(false)
169
- const errorMessage = ref('')
170
- const showTreeSelect = ref(false)
171
- const treeValue = ref('')
172
- // 懒加载 最后检索版本
173
- const lastFetchId = ref(0)
174
-
175
- // 登录信息 (可以在配置的动态函数中使用 this.setupState 获取到当前组件内的全部函数和变量 例:this.setupState.userState)
176
- const userState = useUserStore().getLogin()
177
- const currUser = computed(() => userState.f.resources.id)
178
- const userInfo = computed(() => ({
179
- orgId: userState.f.resources.orgid,
180
- userId: userState.f.resources.id,
181
- }))
182
-
183
- // 是否展示当前项
184
- const showItem = ref(true)
185
-
186
- // 当前组件实例(不推荐使用,可能会在后续的版本更迭中调整,暂时用来绑定函数的上下文)
187
- const currInst = getCurrentInstance()
188
-
189
- // 配置中心->表单项变更触发函数
190
- async function dataChangeFunc() {
191
- if (attr.dataChangeFunc) {
192
- await executeStrFunctionByContext(currInst, attr.dataChangeFunc, [props.form, (formData: any) => emits('setForm', formData), attr, null, mode, runLogic, getConfigByNameAsync])
193
- }
194
- }
195
- const dataChangeFuncdebounce = debounce(dataChangeFunc, 300)
196
- // 配置中心->表单项展示函数
197
- async function showFormItemFunc() {
198
- if (attr.showFormItemFunc) {
199
- const obj = await executeStrFunctionByContext(currInst, attr.showFormItemFunc, [form, attr, null, mode])
200
- // 判断是 bool 还是 obj 兼容
201
- if (typeof obj === 'boolean') {
202
- showItem.value = obj
203
- }
204
- else if (obj && typeof obj === 'object') {
205
- // obj 是一个对象,并且不是数组
206
- showItem.value = obj?.show
207
- }
208
- }
209
- }
210
- const showFormItemFuncdebounce = debounce(showFormItemFunc, 300)
211
- /**
212
- * 检测是否传入了有效的值
213
- * @returns any
214
- */
215
- function checkModel(val = props.modelValue) {
216
- if (val === null || val === undefined || val === '')
217
- return false
218
- if (Array.isArray(val))
219
- return val.length > 0
220
- return true
221
- }
222
- /**
223
- * 获取表单项的默认值
224
- * @returns any
225
- */
226
- function getDefaultValue() {
227
- const val = props.modelValue
228
- console.warn('>>>>>props', props)
229
- console.warn('>>>> attr', attr)
230
- // 如果有有效值,直接返回(datePicker 初始值需按 formValueFormat 归一)
231
- // 目的:外部通过 formData 传入的初始值在组件挂载即与配置格式一致
232
- if (checkModel(val)) {
233
- if (attr.type === 'datePicker' && typeof val === 'string') {
234
- const parsed = dayjs(val)
235
- if (parsed.isValid())
236
- return parsed.format(formValueFormat.value)
237
- }
238
- return val
239
- }
240
-
241
- // 根据类型获取默认值
242
- const getDefaultByType = () => {
243
- const def = mode !== '查询' ? attr.formDefault : attr.queryFormDefault
244
-
245
- if (['treeSelect', 'select', 'checkbox'].includes(attr.type) && ['curOrgId', 'curDepId', 'curUserId'].includes(def)) {
246
- if (def === 'curOrgId') {
247
- if (attr.type === 'treeSelect') {
248
- treeValue.value = userState.f.resources.orgs
249
- }
250
- return attr.type === 'select' ? userState.f.resources.orgid : [userState.f.resources.orgid]
251
- }
252
- if (def === 'curDepId') {
253
- if (attr.type === 'treeSelect') {
254
- treeValue.value = userState.f.resources.deps
255
- }
256
- return attr.type === 'select' ? userState.f.resources.depids : [userState.f.resources.depids]
257
- }
258
- if (def === 'curUserId') {
259
- if (attr.type === 'treeSelect') {
260
- treeValue.value = userState.f.resources.name
261
- }
262
- return attr.type === 'select' ? userState.f.resources.id : [userState.f.resources.id]
263
- }
264
- }
265
-
266
- // 数组类型默认值
267
- const arrayTypes = ['uploader', 'checkbox', 'file', 'area', 'image', 'treeSelect']
268
- if (arrayTypes.includes(attr.type)) {
269
- return def !== undefined ? def : []
270
- }
271
-
272
- // 特殊类型默认值
273
- const specialDefaults = {
274
- switch: false,
275
- stepper: 1,
276
- addressSearch: val,
277
- }
278
-
279
- if (specialDefaults[attr.type] !== undefined) {
280
- return specialDefaults[attr.type]
281
- }
282
-
283
- // 日期时间类型:调用 getDateRange,并传入 formValueFormat
284
- // 说明:让初始化/查询默认值同样遵循配置的格式显示
285
- const dateTypes = ['rangePicker', 'yearPicker', 'monthPicker', 'yearRangePicker', 'monthRangePicker', 'datePicker', 'timePicker']
286
- if (dateTypes.includes(attr.type)) {
287
- return getDateRange({
288
- type: attr.type,
289
- formDefault: attr.formDefault ?? '',
290
- queryFormDefault: attr.queryFormDefault ?? '',
291
- queryType: attr.queryType,
292
- queryValueFormat: attr.queryValueFormat,
293
- name: attr.name ?? '',
294
- formValueFormat: formValueFormat.value,
295
- }) ?? []
296
- }
297
-
298
- // 其他类型(字符串、数字等)
299
- return def ?? ''
300
- }
301
-
302
- return getDefaultByType()
303
- }
304
-
305
- // 初始化日期组件初始值,组件自定义格式显示值和实际值(日期相关初始化都在此函数中操作)
306
- function getDateRange({
307
- type,
308
- formDefault: defaultValue,
309
- queryFormDefault,
310
- queryType,
311
- queryValueFormat: defaultFormat,
312
- name,
313
- formValueFormat: formFormat,
314
- }: {
315
- type: string
316
- formDefault: string
317
- queryFormDefault: string
318
- queryType?: string
319
- queryValueFormat?: string
320
- name: string
321
- // 新增:用于优先覆盖 datePicker 的显示/存储格式
322
- formValueFormat?: string
323
- }): string | [string, string] | undefined {
324
- const formatMap: Record<string, string> = {
325
- yearPicker: 'YYYY',
326
- yearRangePicker: 'YYYY',
327
- monthPicker: 'YYYY-MM',
328
- monthRangePicker: 'YYYY-MM',
329
- datePicker: 'YYYY-MM-DD HH:mm:ss',
330
- rangePicker: 'YYYY-MM-DD HH:mm:ss',
331
- }
332
- if (mode) {
333
- // datePicker 优先使用 formValueFormat(否则退回 queryValueFormat 或默认映射)
334
- const preferFormat = type === 'datePicker' ? (formFormat || defaultFormat) : defaultFormat
335
- const format = preferFormat || formatMap[type]
336
- const val = mode === '查询' ? queryFormDefault : defaultValue
337
- let start: string, end: string
338
- switch (val) {
339
- case 'curYear':
340
- start = dayjs().startOf('year').format(format)
341
- end = dayjs().endOf('year').format(format)
342
- break
343
- case 'curMonth':
344
- start = dayjs().startOf('month').format(format)
345
- end = dayjs().endOf('month').format(format)
346
- break
347
- case 'curDay':
348
- start = dayjs().startOf('day').format(format)
349
- end = dayjs().endOf('day').format(format)
350
- break
351
- case 'curTime':
352
- start = dayjs().format(format)
353
- end = dayjs().format(format)
354
- break
355
- default:
356
- return undefined
357
- }
358
- if (['monthPicker', 'yearPicker', 'datePicker'].includes(type)) {
359
- if (mode !== '查询') {
360
- if (queryType === 'BETWEEN') {
361
- return [start, end]
362
- }
363
- if (name.includes('开始') || name.includes('起始')) {
364
- return start
365
- }
366
- else {
367
- return end
368
- }
369
- }
370
- else {
371
- return start
372
- }
373
- }
374
- // rangePicker组件表单显示的值
375
- if (mode === '查询' && type === 'rangePicker') {
376
- pickerValue.value = `${start} ~ ${end}`
377
- }
378
- return mode !== '查询' ? start : [start, end]
379
- }
380
- else {
381
- return undefined
382
- }
383
- }
384
-
385
- // 监听 props.form 的变化
386
- watch(
387
- () => props.form,
388
- (newVal, oldVal) => {
389
- // 如果是从函数获取 options
390
- if (props.attr.keyName && (props.attr.keyName.toString().includes('async ') || props.attr.keyName.toString().includes('function'))) {
391
- debouncedUpdateOptions()
392
- }
393
- if (props.attr.showFormItemFunc) {
394
- showFormItemFuncdebounce()
395
- }
396
- },
397
- { deep: true },
398
- )
399
-
400
- // 监听 modelData 的变化,调用 dataChangeFunc
401
- watch(
402
- () => modelData.value,
403
- (newVal, oldVal) => {
404
- // 避免初始化时的调用
405
- if (newVal !== oldVal) {
406
- dataChangeFuncdebounce()
407
- }
408
- },
409
- { deep: true },
410
- )
411
-
412
- // 监听 option.value 的变化
413
- watch(
414
- () => option.value,
415
- (newOption) => {
416
- if (attr.type === 'treeSelect' && newOption && newOption.length > 0) {
417
- // 你可以在这里调用 findOptionInTree 函数来查找默认值对应的 label
418
- const result = findOptionInTree(option.value, modelData.value)
419
- if (attr.type === 'treeSelect' && result)
420
- treeValue.value = result.label
421
- }
422
- },
423
- )
424
-
425
- function updateFile(files, _index) {
426
- modelData.value = files
427
- }
428
-
429
- // 表单校验的类型校验
430
- function formTypeCheck(attr, value) {
431
- if (mode === '查询' || mode === '预览')
432
- return
433
- // if (!attr.rule || !attr.rule.required || attr.rule.required === 'false')
434
- // return
435
- switch (attr.rule.type) {
436
- case 'string':
437
- if (value.length === 0) {
438
- errorMessage.value = `请输入${attr.name}`
439
- return
440
- }
441
- break
442
- case 'number':
443
- if (!/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)$/.test(value)) {
444
- errorMessage.value = `${attr.name}必须为数字`
445
- return
446
- }
447
- break
448
- case 'boolean':
449
- case 'array':
450
- case 'regexp':
451
- if (value) {
452
- errorMessage.value = ''
453
- return
454
- }
455
- break
456
- case 'integer':
457
- if (!/^-?\d+$/.test(value)) {
458
- errorMessage.value = `${attr.name}必须为整数`
459
- return
460
- }
461
- break
462
- case 'float':
463
- if (!/^-?\d+\.\d+$/.test(value)) {
464
- errorMessage.value = `${attr.name}必须为小数`
465
- return
466
- }
467
- break
468
- case 'email':
469
- if (!/^[\w.%+-]+@[a-z0-9.-]+\.[a-z]{2,}$/i.test(value)) {
470
- errorMessage.value = `请输入正确的邮箱地址`
471
- return
472
- }
473
- break
474
- case 'idNumber':
475
- if (!/^(?:\d{15}|\d{17}[0-9X])$/.test(value)) {
476
- errorMessage.value = `请输入正确的身份证号码`
477
- return
478
- }
479
- break
480
- case 'userPhone':
481
- if (!/^1[3-9]\d{9}$/.test(value)) {
482
- errorMessage.value = `请输入正确的手机号码`
483
- return
484
- }
485
- break
486
- case 'landlineNumber':
487
- if (!/^0\d{2,3}[-\s]?\d{7,8}$/.test(value)) {
488
- errorMessage.value = `请输入正确的座机号码`
489
- return
490
- }
491
- break
492
- case 'greaterThanZero':
493
- if (!/^(?:[1-9]\d*(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i.test(value)) {
494
- errorMessage.value = `请输入一个大于0的数字`
495
- return
496
- }
497
- break
498
- case 'greaterThanOrEqualZero':
499
- if (!/^(?:\d+|\d*\.\d+)$/.test(value)) {
500
- errorMessage.value = `请输入一个大于等于0的数字`
501
- return
502
- }
503
- break
504
- case 'stringLength':
505
- if (!(value.length >= attr.rule.minLen && value.length < attr.rule.maxLen)) {
506
- errorMessage.value = `长度必须在${attr.rule.minLen}~${attr.rule.maxLen}之间`
507
- return
508
- }
509
- break
510
- case 'customJs':
511
- // eslint-disable-next-line no-case-declarations
512
- const funcStr = attr.rule.customValidatorFunc
513
- // 输入的数据是否符合条件
514
- // eslint-disable-next-line no-case-declarations
515
- const status = ref(true)
516
- // 提取函数体部分
517
- // eslint-disable-next-line no-case-declarations
518
- const funcBodyMatch = funcStr.match(/function\s*\(.*?\)\s*\{([\s\S]*)\}/)
519
- if (!funcBodyMatch)
520
- throw new Error('自定义校验函数不合法')
521
- // eslint-disable-next-line no-case-declarations
522
- const funcBody = funcBodyMatch[1].trim() // 提取函数体
523
- // 使用 new Function 创建函数
524
- // eslint-disable-next-line no-new-func,no-case-declarations
525
- const customValidatorFunc = new Function('rule', 'value', 'callback', 'form', 'attr', 'util', funcBody)
526
- // 定义 callback 函数
527
- // eslint-disable-next-line no-case-declarations
528
- const callback = (error) => {
529
- if (error) {
530
- errorMessage.value = `${error}`
531
- status.value = false // 表示有错误发生
532
- }
533
- }
534
- // 调用自定义校验函数
535
- customValidatorFunc(
536
- attr.rule,
537
- value,
538
- callback,
539
- form,
540
- attr,
541
- {}, // util 对象(可以根据需要传递)
542
- )
543
- if (!status.value)
544
- return
545
- break
546
- default:
547
- errorMessage.value = ''
548
- break
549
- }
550
- errorMessage.value = ''
551
- }
552
-
553
- onBeforeMount(() => {
554
- init()
555
- modelData.value = getDefaultValue()
556
- showFormItemFunc()
557
- dataChangeFunc()
558
- if (attr?.keyName?.toString()?.startsWith('search@根据表单项[') && attr?.keyName?.toString().endsWith(']联动人员'))
559
- debouncedUserLinkFunc = debounce(() => updateResOptions('人员'), 200)
560
-
561
- if (attr?.keyName?.toString()?.startsWith('search@根据表单项[') && attr?.keyName?.toString().endsWith(']联动部门'))
562
- debouncedDepLinkFunc = debounce(() => updateResOptions('部门'), 200)
563
-
564
- if (attr.keyName && (attr?.keyName?.toString().indexOf('async ') !== -1 || attr?.keyName?.toString()?.indexOf('function') !== -1)) {
565
- debouncedUpdateOptions = debounce(updateOptions, 200)
566
- }
567
- })
568
- // 是否展示表单左侧label文字
569
- const labelData = computed(() => {
570
- return props.showLabel ? attr.name : null
571
- })
572
- // 是否展示表单左侧label文字
573
- const labelAlign = computed(() => {
574
- return attr.labelAlign ? attr.labelAlign : 'left'
575
- })
576
- // 是否只读
577
- const readonly = computed(() => {
578
- return props.formReadonly || attr.addOrEdit === 'readonly' || mode === '预览'
579
- })
580
- // 提示内容
581
- const placeholder = computed(() => {
582
- if (attr.addOrEdit === 'readonly' || mode === '预览') {
583
- return '暂无内容 ~ '
584
- }
585
- else
586
- if (attr.placeholder) {
587
- return attr.placeholder
588
- }
589
- else {
590
- switch (attr.type) {
591
- case 'datePicker':
592
- case 'timePicker':
593
- case 'rangePicker':
594
- case 'radio':
595
- case 'select':
596
- case 'treeSelect':
597
- case 'area':
598
- case 'citySelect':
599
- case 'picker':
600
- return `请选择${attr.name}`
601
- case 'addressSearch':
602
- case 'input':
603
- case 'textarea':
604
- case 'intervalPicker':
605
- return `请输入${attr.name}`
606
- default:
607
- return `请选择${attr.name}`
608
- }
609
- }
610
- })
611
-
612
- const formatDate = date => `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`
613
-
614
- function onCalendarConfirm(values) {
615
- modelData.value = [formatDate(values[0]), formatDate(values[1])]
616
- pickerValue.value = `${formatDate(values[0])} ~ ${formatDate(values[1])}`
617
- calendarShow.value = false
618
- }
619
-
620
- // js 函数作为数据源
621
- async function updateOptions() {
622
- if (attr.keyName && (attr.keyName.toString().includes('async ') || attr.keyName.toString().includes('function '))) {
623
- option.value = await executeStrFunctionByContext(this, attr.keyName, [props.form, runLogic, props.mode, getConfigByNameAsync, post])
624
- }
625
- }
626
-
627
- function init() {
628
- if (attr.keyName && typeof attr.keyName === 'string') {
629
- if (attr.keyName && attr.keyName.includes('logic@')) {
630
- getData({}, (res) => {
631
- option.value = res
632
- initRadioValue()
633
- })
634
- }
635
- else if (attr.keyName && attr.keyName.includes('config@')) {
636
- const configName = attr.keyName.substring(7)
637
- getDict(configName, (result) => {
638
- if (result)
639
- option.value = result
640
- }, serviceName)
641
- }
642
- else if (attr.keyName && attr.keyName.includes('search@')) {
643
- let source = attr.keyName.substring(7)
644
- const userid = currUser.value
645
- let roleName = 'roleName'
646
- if (source.startsWith('根据角色[') && source.endsWith(']获取人员')) {
647
- const startIndex = source.indexOf('[') + 1
648
- const endIndex = source.indexOf(']', startIndex)
649
- roleName = source.substring(startIndex, endIndex)
650
- source = '根据角色获取人员'
651
- }
652
- const searchData = { source, userid, roleName }
653
- if (source.startsWith('根据表单项[') && source.endsWith(']联动人员'))
654
- updateResOptions('人员')
655
- else if (source.startsWith('根据表单项[') && source.endsWith(']联动部门'))
656
- updateResOptions('部门')
657
- else if (attr.type === 'select' || attr.type === 'checkbox')
658
- searchToListOption(searchData, res => getDataCallback(res))
659
- else
660
- searchToOption(searchData, res => getDataCallback(res))
661
- }
662
- else if (attr.keyName.toString().includes('async ') || attr.keyName.toString().includes('function ')) {
663
- updateOptions()
664
- }
665
- else {
666
- initRadioValue()
667
- }
668
- }
669
- }
670
-
671
- function getDataCallback(res) {
672
- option.value = res
673
- if (attr.type === 'radio')
674
- initRadioValue()
675
- }
676
-
677
- async function updateResOptions(type) {
678
- if (attr?.keyName?.toString()?.startsWith('search@根据表单项[') && attr?.keyName?.toString()?.endsWith(`]联动${type}`)) {
679
- const searchData = { source: `获取${type}`, userid: currUser.value }
680
- const startIndex = attr.keyName.indexOf('[') + 1
681
- const endIndex = attr.keyName.indexOf(']', startIndex)
682
- const formModel = attr.keyName.substring(startIndex, endIndex).replace('.', '_')
683
- const formModelData = form[formModel]
684
- if (formModel?.length && formModelData?.length) {
685
- await searchToListOption(searchData, (res) => {
686
- getDataCallback(res.filter((h) => {
687
- return formModelData['0'] === h.f_organization_id || formModelData['0'] === h.f_department_id || formModelData['0'] === h.parentid
688
- // if (formModel.indexOf('org') > -1) {
689
- // return formModelData?.includes(h.orgid || h.f_organization_id || h.parentid)
690
- // } else {
691
- // return formModelData?.includes(h?.parentid)
692
- // }
693
- }))
694
- })
695
- }
696
- }
697
- }
698
-
699
- function initRadioValue() {
700
- if ((mode === '新增' || mode === '修改') && attr.type === 'radio' && !props.modelValue) {
701
- if (attr.keys && attr.keys.length > 0)
702
- modelData.value = attr.keys[0].value
703
- else if (option.value && option.value.length > 0)
704
- modelData.value = option.value[0].value
705
- }
706
- }
707
-
708
- function getData(value, callback) {
709
- if (value !== '') {
710
- const logicName = attr.keyName
711
- const logic = logicName.substring(6)
712
- // 调用logic前设置参数
713
- if (getDataParams && getDataParams[attr.model])
714
- Object.assign(value, getDataParams[attr.model])
715
- Object.assign(value, userInfo.value)
716
- runLogic(logic, value, serviceName).then((res) => {
717
- callback(res)
718
- })
719
- }
720
- }
721
-
722
- // 已废弃 不进行维护
723
- function onPickerConfirm({ selectedOptions }) {
724
- showPicker.value = false
725
- modelData.value = selectedOptions[0].text
726
- }
727
-
728
- // 日期时间选择数据
729
- const dateTimePickerValue = ref<any>({})
730
- // 展示日期时间选择器:根据 formValueFormat 初始化 VanDatePicker/VanTimePicker 的当前值
731
- // 规则:
732
- // - 若已有字符串值,按 formValueFormat 解析并拆分为日期/时间数组
733
- // - 若无值,按当前时间生成匹配格式的默认数组
734
- function showDataTimePicker() {
735
- if (props.modelValue && typeof props.modelValue === 'string') {
736
- const base = dayjs(props.modelValue as string, formValueFormat.value)
737
- const dateArr: string[] = []
738
- dateArr.push(base.format('YYYY'))
739
- if (dateColumnsType.value.includes('month'))
740
- dateArr.push(base.format('MM'))
741
- if (dateColumnsType.value.includes('day'))
742
- dateArr.push(base.format('DD'))
743
- const timeArr: string[] = []
744
- if (hasTime.value) {
745
- if (timeColumnsType.value.includes('hour'))
746
- timeArr.push(base.format('HH'))
747
- if (timeColumnsType.value.includes('minute'))
748
- timeArr.push(base.format('mm'))
749
- if (timeColumnsType.value.includes('second'))
750
- timeArr.push(base.format('ss'))
751
- }
752
- dateTimePickerValue.value = {
753
- date: dateArr,
754
- time: timeArr.length ? timeArr : ['00', '00', '00'].slice(0, timeColumnsType.value.length),
755
- }
756
- }
757
- else {
758
- const now = dayjs()
759
- const dateArr: string[] = []
760
- dateArr.push(now.format('YYYY'))
761
- if (dateColumnsType.value.includes('month'))
762
- dateArr.push(now.format('MM'))
763
- if (dateColumnsType.value.includes('day'))
764
- dateArr.push(now.format('DD'))
765
- const timeArr: string[] = []
766
- if (hasTime.value) {
767
- if (timeColumnsType.value.includes('hour'))
768
- timeArr.push(now.format('HH'))
769
- if (timeColumnsType.value.includes('minute'))
770
- timeArr.push(now.format('mm'))
771
- if (timeColumnsType.value.includes('second'))
772
- timeArr.push(now.format('ss'))
773
- }
774
- dateTimePickerValue.value = {
775
- date: dateArr,
776
- time: timeArr.length ? timeArr : ['00', '00', '00'].slice(0, timeColumnsType.value.length),
777
- }
778
- }
779
- showDatePicker.value = true
780
- }
781
-
782
- function onDatePickerConfirm({ selectedValues }) {
783
- showDatePicker.value = false
784
- modelData.value = selectedValues.join('-')
785
- }
786
-
787
- // 已废弃 不进行维护
788
- function onTimePickerConfirm({ selectedValues }) {
789
- showTimePicker.value = false
790
- timePickerValue.value = selectedValues.join(':')
791
- modelData.value = timePickerValue.value
792
- }
793
-
794
- // 没人用到本次先不动。后续需要看这个组件需要怎么使用
795
- function onAreaConfirm({ selectedOptions }) {
796
- area.value = `${selectedOptions[0].text}-${selectedOptions[1].text}-${selectedOptions[2].text}`
797
- showArea.value = false
798
- modelData.value = [{
799
- province: selectedOptions[0].text,
800
- city: selectedOptions[1].text,
801
- district: selectedOptions[2].text,
802
- }]
803
- }
804
-
805
- // 日期时间选择确认:将选择的年月日(+时分秒)拼装后,最终使用 formValueFormat 输出到 v-model
806
- function onDateTimePickerConfirm() {
807
- showDatePicker.value = false
808
- const dateParts = dateTimePickerValue.value.date as string[]
809
- const timeParts = (dateTimePickerValue.value.time as string[]) || []
810
- const year = dateParts[0]
811
- const month = dateParts[1] || '01'
812
- const day = dateParts[2] || '01'
813
- const hour = hasTime.value && timeParts[0] ? timeParts[0] : '00'
814
- const minute = hasTime.value && timeParts[1] ? timeParts[1] : '00'
815
- const second = hasTime.value && timeParts[2] ? timeParts[2] : '00'
816
- const full = `${year}-${month}-${day} ${hour}:${minute}:${second}`
817
- modelData.value = dayjs(full, 'YYYY-MM-DD HH:mm:ss').format(formValueFormat.value)
818
- }
819
-
820
- function onPickerCancel() {
821
- showDatePicker.value = false
822
- }
823
-
824
- const showAddressPicker = ref(false)
825
- const addressValue = ref('')
826
-
827
- // XLocationPicker 默认加载的中心点
828
- const defaultMapCenter = computed(() => {
829
- const lonLat = form[`${attr.model}_lon_lat`]
830
- if (lonLat && typeof lonLat === 'string' && lonLat.includes(',')) {
831
- const [lon, lat] = lonLat.split(',').map(Number)
832
- if (!Number.isNaN(lon) && !Number.isNaN(lat)) {
833
- return [lon, lat] as [number, number]
834
- }
835
- }
836
- return undefined
837
- })
838
-
839
- // 处理地址选择器确认
840
- function handleAddressConfirm(location) {
841
- // 构造新的数据格式
842
- const formData = {
843
- [`${attr.model}_lon_lat`]: `${location.longitude},${location.latitude}`,
844
- [attr.model]: location.address,
845
- }
846
- // 更新表单数据
847
- emits('setForm', formData)
848
- showAddressPicker.value = false
849
- }
850
-
851
- // 重置方法,供父组件调用
852
- function reset() {
853
- modelData.value = getDefaultValue()
854
- errorMessage.value = ''
855
- treeValue.value = null
856
- area.value = null
857
- pickerValue.value = null
858
- }
859
-
860
- defineExpose({
861
- reset,
862
- })
863
-
864
- // 数据处理
865
- function cleanEmptyChildren(options, fieldNames = { text: 'label', value: 'value', children: 'children' }) {
866
- if (!Array.isArray(options))
867
- return options
868
-
869
- const childrenKey = fieldNames.children || 'children'
870
-
871
- return options.map((option) => {
872
- // 深拷贝选项,避免修改原始数据
873
- const newOption = { ...option }
874
-
875
- // 如果存在children属性且是空数组
876
- if (newOption[childrenKey] && Array.isArray(newOption[childrenKey]) && newOption[childrenKey].length === 0) {
877
- delete newOption[childrenKey]
878
- }
879
- // 如果存在children属性且非空,则递归处理
880
- else if (newOption[childrenKey] && Array.isArray(newOption[childrenKey])) {
881
- newOption[childrenKey] = cleanEmptyChildren(newOption[childrenKey], fieldNames)
882
- }
883
- return newOption
884
- })
885
- }
886
-
887
- // 级联选择完成事件
888
- function onTreeSelectFinish({ selectedOptions }) {
889
- const index = selectedOptions.length - 1
890
- treeValue.value = selectedOptions[index].label
891
- if (mode === '查询') {
892
- modelData.value = [selectedOptions[index].value]
893
- }
894
- else {
895
- modelData.value = selectedOptions[index].value
896
- }
897
- showTreeSelect.value = false
898
- }
899
-
900
- function emitFunc(func, data) {
901
- emits('xFormItemEmitFunc', func, data, data?.model ? form[data.model] : form)
902
- }
903
-
904
- function findOptionInTree(options, value) {
905
- // 在当前层级查找
906
- const foundItem = options.find(item => item[columnsField.value] === value)
907
- if (foundItem) {
908
- return foundItem
909
- }
910
- // 递归查找子级
911
- for (const item of options) {
912
- if (item.children?.length) {
913
- const foundInChildren = findOptionInTree(item.children, value)
914
- if (foundInChildren)
915
- return foundInChildren
916
- }
917
- }
918
-
919
- return null
920
- }
921
-
922
- // 懒加载搜索函数
923
- async function handleLazySearch(searchText: string) {
924
- if (!attr.keyName || typeof attr.keyName !== 'string') {
925
- return []
926
- }
927
-
928
- try {
929
- // 如果是 search@ 类型的数据源
930
- if (attr.keyName.includes('search@')) {
931
- let source = attr.keyName.substring(7)
932
- const userid = currUser.value
933
- let roleName = 'roleName'
934
-
935
- if (source.startsWith('根据角色[') && source.endsWith(']获取人员')) {
936
- const startIndex = source.indexOf('[') + 1
937
- const endIndex = source.indexOf(']', startIndex)
938
- roleName = source.substring(startIndex, endIndex)
939
- source = '根据角色获取人员'
940
- }
941
-
942
- const searchData = {
943
- source,
944
- userid,
945
- roleName,
946
- searchText, // 添加搜索关键词
947
- }
948
-
949
- return new Promise((resolve) => {
950
- if (attr.type === 'select' || attr.type === 'checkbox') {
951
- searchToListOption(searchData, (res) => {
952
- // 根据搜索关键词过滤结果
953
- const filtered = res.filter((item) => {
954
- const text = item[columnsField.value.text] || item.label || ''
955
- return text.toString().toLowerCase().includes(searchText.toLowerCase())
956
- })
957
- resolve(filtered)
958
- })
959
- }
960
- else {
961
- searchToOption(searchData, (res) => {
962
- const filtered = res.filter((item) => {
963
- const text = item[columnsField.value.text] || item.label || ''
964
- return text.toString().toLowerCase().includes(searchText.toLowerCase())
965
- })
966
- resolve(filtered)
967
- })
968
- }
969
- })
970
- }
971
-
972
- // logic 数据源
973
- if (attr.keyName.includes('logic@')) {
974
- const logicName = attr.keyName.substring(6)
975
- const value = { word: searchText, ...userInfo.value }
976
-
977
- // 调用logic前设置参数
978
- if (getDataParams && getDataParams[attr.model]) {
979
- Object.assign(value, getDataParams[attr.model])
980
- }
981
-
982
- lastFetchId.value++
983
- const fetchId = lastFetchId.value
984
- // 根据搜索关键词过滤结果
985
- const results = await runLogic(logicName, value, serviceName) as any
986
- if (fetchId !== lastFetchId.value) {
987
- return
988
- }
989
- if (searchText) {
990
- return results.filter((item) => {
991
- const text = item[columnsField.value.text] || item.label || ''
992
- return text.toString().toLowerCase().includes(searchText.toLowerCase())
993
- })
994
- }
995
- return results
996
- }
997
-
998
- // 自定义 js 函数
999
- if (attr.keyName.includes('async ') || attr.keyName.includes('function ')) {
1000
- const results = await executeStrFunctionByContext(currInst, attr.keyName, [
1001
- { ...props.form, searchText },
1002
- runLogic,
1003
- props.mode,
1004
- getConfigByNameAsync,
1005
- post,
1006
- ])
1007
- // 根据搜索关键词过滤结果
1008
- return results.filter((item) => {
1009
- const text = item[columnsField.value.text] || item.label || ''
1010
- return text.toString().toLowerCase().includes(searchText.toLowerCase())
1011
- })
1012
- }
1013
-
1014
- return []
1015
- }
1016
- catch (error) {
1017
- console.error('懒加载搜索失败:', error)
1018
- return []
1019
- }
1020
- }
1021
-
1022
- // 扫码/NFC
1023
- function scanCodeOrNfc(attr) {
1024
- if (attr.type === 'scanCode') {
1025
- // 扫码逻辑
1026
- mobileUtil.execute({
1027
- funcName: 'scanBarcode',
1028
- param: {},
1029
- callbackFunc: (res: any) => {
1030
- console.log('让我看看你扫码结果:--', res)
1031
- if (res && res.status === 'success') {
1032
- if (res.data.status && res.data.status === 'cancelled') {
1033
- showToast(res.data?.message || '扫码已取消')
1034
- return
1035
- }
1036
- modelData.value = res.data?.rawValue
1037
- showToast('扫码成功')
1038
- }
1039
- else {
1040
- showToast('扫码失败')
1041
- }
1042
- },
1043
- })
1044
- }
1045
- else if (attr.type === 'nfc') {
1046
- // NFC逻辑
1047
- console.log('--------NFC功能即将开发--------')
1048
- showToast('NFC功能即将开发')
1049
- }
1050
- }
1051
- </script>
1052
-
1053
- <template>
1054
- <div>
1055
- <!-- switch开关 -->
1056
- <VanField
1057
- v-if="attr.type === 'switch' && showItem"
1058
- name="switch"
1059
- :label="labelData"
1060
- :label-align="labelAlign"
1061
- :input-align="attr.inputAlign ? attr.inputAlign : 'right'"
1062
- :rules="[{ required: attr.rule.required === 'true', message: `请选择${attr.name}` }]"
1063
- :required="attr.rule.required === 'true'"
1064
- >
1065
- <template #input>
1066
- <VanSwitch v-model="modelData" />
1067
- </template>
1068
- </VanField>
1069
-
1070
- <!-- 复选框 -->
1071
- <!-- <VanField
1072
- v-if="attr.type === 'checkbox'"
1073
- name="checkbox"
1074
- :label="labelData"
1075
- >
1076
- <template #input>
1077
- <VanCheckbox v-model="modelData" shape="square" />
1078
- </template>
1079
- </VanField> -->
1080
-
1081
- <!-- 多选框-checkbox-复选框组 -->
1082
- <template v-if="attr.type === 'checkbox' && showItem">
1083
- <!-- 勾选 -->
1084
- <VanField
1085
- v-if="attr.showMode === 'checkbox' && mode !== '查询'"
1086
- name="checkboxGroup"
1087
- :label="labelData"
1088
- :label-align="labelAlign"
1089
- :input-align="attr.inputAlign ? attr.inputAlign : 'left'"
1090
- :rules="[{ required: attr.rule.required === 'true', message: `请选择${attr.name}` }]"
1091
- :required="attr.rule.required === 'true'"
1092
- >
1093
- <template #input>
1094
- <van-checkbox-group v-model="modelData as any[]" direction="horizontal" shape="square" :disabled="readonly">
1095
- <VanCheckbox v-for="(item, index) in option" :key="index" style="padding: 2px" :name="item[columnsField.value]" :shape="rules?.[attr.model].shape" :value="item[columnsField.value]">
1096
- {{ item[columnsField.text] }}
1097
- </VanCheckbox>
1098
- </van-checkbox-group>
1099
- </template>
1100
- </VanField>
1101
- <VanField
1102
- v-if="attr.showMode === 'checkbox' && mode === '查询'"
1103
- name="checkboxGroup"
1104
- :label="labelData"
1105
- :label-align="labelAlign"
1106
- :input-align="attr.inputAlign ? attr.inputAlign : 'left'"
1107
- :rules="[{ required: attr.rule.required === 'true', message: `请选择${attr.name}` }]"
1108
- >
1109
- <template #input>
1110
- <XGridDropOption
1111
- v-model="(modelData as string[])"
1112
- :column-num="labelData ? 3 : 4"
1113
- :multiple="true"
1114
- :columns="option"
1115
- />
1116
- </template>
1117
- </VanField>
1118
- <!-- 下拉 -->
1119
- <XMultiSelect
1120
- v-if="(!attr.showMode || attr.showMode === 'select') && mode === '查询'"
1121
- v-model="modelData"
1122
- :label="labelData"
1123
- :readonly="readonly"
1124
- :placeholder="placeholder"
1125
- :columns="option"
1126
- :select-value="Array.isArray(modelData) ? modelData : []"
1127
- :option="attr.option ? attr.option : columnsField"
1128
- :rules="[{ required: attr.rule.required === 'true', message: `请选择${attr.name}` }]"
1129
- :required="attr.rule.required === 'true'"
1130
- :lazy-load="attr.lazyLoad"
1131
- :on-search="attr.lazyLoad ? handleLazySearch : null"
1132
- />
1133
- </template>
1134
-
1135
- <!-- 单选框 -->
1136
- <VanField
1137
- v-if="attr.type === 'radio' && mode !== '查询' && showItem"
1138
- name="radio"
1139
- center
1140
- :placeholder="attr.placeholder ? attr.placeholder : `请选择${attr.name}`"
1141
- :label="labelData"
1142
- :label-align="labelAlign"
1143
- :input-align="attr.inputAlign ? attr.inputAlign : 'left'"
1144
- :rules="[{ required: attr.rule.required === 'true', message: `请选择${attr.name}` }]"
1145
- :required="attr.rule.required === 'true'"
1146
- >
1147
- <template #input>
1148
- <VanRadioGroup v-model="modelData" direction="horizontal" :disabled="readonly">
1149
- <VanRadio v-for="(item, index) in option" :key="index" style="padding: 2px" :name="item[columnsField.value]" :value="item[columnsField.value]">
1150
- {{ item[columnsField.text] }}
1151
- </VanRadio>
1152
- </VanRadioGroup>
1153
- </template>
1154
- </VanField>
1155
-
1156
- <!-- 单选框-查询 -->
1157
- <VanField
1158
- v-if="attr.type === 'radio' && mode === '查询' && showItem"
1159
- name="radio"
1160
- :placeholder="attr.placeholder ? attr.placeholder : `请选择${attr.name}`"
1161
- :label="labelData"
1162
- :label-align="labelAlign"
1163
- :input-align="attr.inputAlign ? attr.inputAlign : 'left'"
1164
- :rules="[{ required: attr.rule.required === 'true', message: `请选择${attr.name}` }]"
1165
- >
1166
- <template #input>
1167
- <XGridDropOption
1168
- v-model="(modelData as string)"
1169
- :column-num="labelData ? 3 : 4"
1170
- :columns="option"
1171
- />
1172
- </template>
1173
- </VanField>
1174
-
1175
- <!-- 步进器 -->
1176
- <VanField
1177
- v-if="attr.type === 'stepper' && showItem"
1178
- name="stepper"
1179
- :label="labelData"
1180
- :label-align="labelAlign"
1181
- :input-align="attr.inputAlign ? attr.inputAlign : 'center'"
1182
- :rules="[{ required: attr.rule.required === 'true', message: `请选择${attr.name}` }]"
1183
- :required="attr.rule.required === 'true'"
1184
- >
1185
- <template #input>
1186
- <VanStepper v-model="modelData as any" :disabled="readonly" />
1187
- </template>
1188
- </VanField>
1189
-
1190
- <!-- 评分 -->
1191
- <VanField
1192
- v-if="attr.type === 'rate' && showItem"
1193
- name="rate"
1194
- :label="labelData"
1195
- :label-align="labelAlign"
1196
- :input-align="attr.inputAlign ? attr.inputAlign : 'center'"
1197
- :rules="[{ required: attr.rule.required === 'true', message: `请选择${attr.name}` }]"
1198
- :required="attr.rule.required === 'true'"
1199
- >
1200
- <template #input>
1201
- <VanRate v-model="modelData as number" :size="25" :readonly="readonly" void-color="#eee" void-icon="star" color="#ffd21e" />
1202
- </template>
1203
- </VanField>
1204
-
1205
- <!-- 滑块 -->
1206
- <VanField
1207
- v-if="attr.type === 'slider' && showItem"
1208
- name="slider"
1209
- :label="labelData"
1210
- :label-align="labelAlign"
1211
- :input-align="attr.inputAlign ? attr.inputAlign : 'left'"
1212
- :rules="[{ required: attr.rule.required === 'true', message: `请选择${attr.name}` }]"
1213
- :required="attr.rule.required === 'true'"
1214
- >
1215
- <template #input>
1216
- <VanSlider v-model="modelData as number" :readonly="readonly" />
1217
- </template>
1218
- </VanField>
1219
-
1220
- <!-- 文件上传 -->
1221
- <!-- 图片上传, 手机端拍照 -->
1222
- <VanField
1223
- v-if="(attr.type === 'image' || attr.type === 'file') && showItem"
1224
- name="image"
1225
- :label="labelData"
1226
- :label-align="labelAlign"
1227
- :input-align="attr.inputAlign ? attr.inputAlign : 'left'"
1228
- :rules="[{ required: attr.rule.required === 'true', message: `请选择${attr.name}` }]"
1229
- :required="attr.rule.required === 'true'"
1230
- >
1231
- <template #input>
1232
- <ImageUploader
1233
- upload-mode="server"
1234
- :image-list="(modelData as any[])"
1235
- authority="admin"
1236
- :attr="attr"
1237
- :mode="props.mode"
1238
- :readonly="readonly"
1239
- :is-async-upload="isAsyncUpload"
1240
- @update-file-list="updateFile"
1241
- />
1242
- </template>
1243
- </VanField>
1244
-
1245
- <!-- 选择器 琉璃中不存在,不进行维护后续将删除 -->
1246
- <VanField
1247
- v-if="attr.type === 'picker' && showItem"
1248
- v-model="pickerValue"
1249
- name="picker"
1250
- :placeholder="attr.placeholder ? attr.placeholder : `请选择${attr.name}`"
1251
- :label="labelData"
1252
- :label-align="labelAlign"
1253
- :input-align="attr.inputAlign ? attr.inputAlign : 'left'"
1254
- readonly
1255
- is-link
1256
- :rules="[{ required: attr.rule.required === 'true', message: `请选择${attr.name}` }]"
1257
- @click="readonly ? null : showPicker = true"
1258
- />
1259
- <VanPopup v-model:show="showPicker" round position="bottom" teleport="body" overlay-class="date-picker-overlay">
1260
- <VanPicker
1261
- v-model="(modelData as Numeric[])"
1262
- :title="attr.name"
1263
- :columns="attr.selectKey"
1264
- :readonly="readonly"
1265
- :columns-field-names="attr.customFieldName ? attr.customFieldName : { text: 'text', value: 'value', children: 'children' }"
1266
- :confirm-button-text="attr.confirmButtonText || attr.confirmButtonText === '' ? attr.confirmButtonText : '确认'"
1267
- :cancel-button-text="attr.cancelButtonText || attr.cancelButtonText === '' ? attr.cancelButtonText : '取消'"
1268
- @cancel="showPicker = false"
1269
- @confirm="onPickerConfirm"
1270
- />
1271
- </VanPopup>
1272
-
1273
- <!-- 日历选择-查询 -->
1274
- <VanField
1275
- v-if="attr.type === 'rangePicker' && mode === '查询' && showItem"
1276
- v-model="(pickerValue as string | number)"
1277
- is-link
1278
- readonly
1279
- name="rangePicker"
1280
- :label="labelData"
1281
- :label-align="labelAlign"
1282
- :input-align="attr.inputAlign ? attr.inputAlign : 'left'"
1283
- :placeholder="attr.placeholder ? attr.placeholder : `请选择${attr.name}`"
1284
- :rules="[{ required: attr.rule.required === 'true', message: `请选择${attr.name}` }]"
1285
- @click="calendarShow = true"
1286
- />
1287
- <VanCalendar
1288
- v-model:show="calendarShow"
1289
- switch-mode="year-month"
1290
- type="range"
1291
- teleport="body"
1292
- overlay-class="date-picker-overlay"
1293
- :show-confirm="attr.showConfirm"
1294
- @confirm="onCalendarConfirm"
1295
- />
1296
-
1297
- <!-- 日期选择-非查询 -->
1298
- <VanField
1299
- v-if="(attr.type === 'datePicker' || attr.type === 'rangePicker') && mode !== '查询' && showItem"
1300
- v-model="(modelData as string | number)"
1301
- name="datePicker"
1302
- :label="labelData"
1303
- :label-align="labelAlign"
1304
- :input-align="attr.inputAlign ? attr.inputAlign : 'left'"
1305
- readonly
1306
- :is-link="true"
1307
- :placeholder="placeholder"
1308
- :rules="[{ required: attr.rule.required === 'true', message: `请选择${attr.name}` }]"
1309
- :required="attr.rule.required === 'true'"
1310
- @click="readonly ? null : showDataTimePicker()"
1311
- />
1312
- <VanPopup v-model:show="showDatePicker" position="bottom" teleport="body" overlay-class="date-picker-overlay">
1313
- <VanPickerGroup
1314
- :title="attr.name"
1315
- :tabs="hasTime ? ['选择日期', '选择时间'] : ['选择日期']"
1316
- next-step-text="下一步"
1317
- :confirm-button-text="attr.confirmButtonText ? attr.confirmButtonText : '确认'"
1318
- :cancel-button-text="attr.cancelButtonText ? attr.cancelButtonText : '取消'"
1319
- @confirm="onDateTimePickerConfirm"
1320
- @cancel="onPickerCancel"
1321
- >
1322
- <VanDatePicker
1323
- v-model="dateTimePickerValue.date"
1324
- :columns-type="attr.dateColumnsType || dateColumnsType"
1325
- />
1326
- <VanTimePicker
1327
- v-if="hasTime"
1328
- v-model="dateTimePickerValue.time"
1329
- :columns-type="attr.timeColumnsType || timeColumnsType"
1330
- :min-time="attr.minTime ? attr.minTime : '00:00:00'"
1331
- :max-time="attr.maxTime ? attr.maxTime : '23:59:59'"
1332
- />
1333
- </VanPickerGroup>
1334
- </VanPopup>
1335
-
1336
- <!-- 日期选择-查询 -->
1337
- <VanField
1338
- v-if="attr.type === 'datePicker' && mode === '查询' && showItem"
1339
- v-model="(modelData as string | number)"
1340
- name="datePicker"
1341
- :label="labelData"
1342
- :label-align="labelAlign"
1343
- :input-align="attr.inputAlign ? attr.inputAlign : 'left'"
1344
- readonly
1345
- :is-link="true"
1346
- :placeholder="attr.placeholder ? attr.placeholder : `请选择${attr.name}`"
1347
- :rules="[{ required: attr.rule.required === 'true', message: `请选择${attr.name}` }]"
1348
- @click="readonly ? null : showDataTimePicker()"
1349
- />
1350
- <VanPopup v-model:show="showDatePicker" position="bottom" teleport="body" overlay-class="date-picker-overlay">
1351
- <VanPickerGroup
1352
- :title="attr.name"
1353
- :tabs="hasTime ? ['选择日期', '选择时间'] : ['选择日期']"
1354
- next-step-text="下一步"
1355
- :confirm-button-text="attr.confirmButtonText ? attr.confirmButtonText : '确认'"
1356
- :cancel-button-text="attr.cancelButtonText ? attr.cancelButtonText : '取消'"
1357
- @confirm="onDateTimePickerConfirm"
1358
- @cancel="onPickerCancel"
1359
- >
1360
- <VanDatePicker
1361
- v-model="dateTimePickerValue.date"
1362
- :columns-type="attr.dateColumnsType || dateColumnsType"
1363
- />
1364
- <VanTimePicker
1365
- v-if="hasTime"
1366
- v-model="dateTimePickerValue.time"
1367
- :columns-type="attr.timeColumnsType || timeColumnsType"
1368
- :min-time="attr.minTime ? attr.minTime : '00:00:00'"
1369
- :max-time="attr.maxTime ? attr.maxTime : '23:59:59'"
1370
- />
1371
- </VanPickerGroup>
1372
- </VanPopup>
1373
-
1374
- <!-- 时间选择 --该配置未在pc找到不进行维护 后续将删除 -->
1375
- <VanField
1376
- v-if="attr.type === 'timePicker' && showItem"
1377
- v-model="timePickerValue"
1378
- name="timePicker"
1379
- is-link
1380
- readonly
1381
- :placeholder="attr.placeholder"
1382
- :label="labelData"
1383
- :label-align="labelAlign"
1384
- :input-align="attr.inputAlign ? attr.inputAlign : 'left'"
1385
- :rules="[{ required: attr.rule.required === 'true', message: '请选择' }]"
1386
- @click="readonly ? null : (showTimePicker = true)"
1387
- />
1388
- <VanPopup v-model:show="showTimePicker" position="bottom" teleport="body" overlay-class="date-picker-overlay">
1389
- <VanTimePicker
1390
- v-model="modelData as string[]"
1391
- :title="attr.name"
1392
- :columns-type="attr.columnsType ? attr.columnsType : ['hour', 'minute', 'second']"
1393
- :min-time="attr.minTime ? attr.minTime : '00:00:00'"
1394
- :max-time="attr.maxTime ? attr.maxTime : '23:59:59'"
1395
- :readonly="readonly"
1396
- @cancel="showTimePicker = false"
1397
- @confirm="onTimePickerConfirm"
1398
- />
1399
- </VanPopup>
1400
-
1401
- <!-- 省市区选择 -->
1402
- <VanField
1403
- v-if="(attr.type === 'area' || attr.type === 'citySelect') && showItem"
1404
- v-model="area"
1405
- name="area"
1406
- :placeholder="attr.placeholder ? attr.placeholder : `请选择${attr.name}`"
1407
- is-link
1408
- readonly
1409
- :label="labelData"
1410
- :label-align="labelAlign"
1411
- :input-align="attr.inputAlign ? attr.inputAlign : 'left'"
1412
- :rules="[{ required: attr.rule.required === 'true', message: '请选择' }]"
1413
- :required="attr.rule.required === 'true'"
1414
- @click="readonly ? null : (showArea = true)"
1415
- />
1416
- <VanPopup v-model:show="showArea" position="bottom" teleport="body" overlay-class="date-picker-overlay">
1417
- <VanArea
1418
- v-model="modelData as string" :title="attr.name" :area-list="areaList"
1419
- @confirm="onAreaConfirm"
1420
- @cancel="showArea = false"
1421
- />
1422
- </VanPopup>
1423
-
1424
- <!-- 单选下拉列表 -->
1425
- <XSelect
1426
- v-if="attr.type === 'select' && showItem"
1427
- v-model="modelData"
1428
- :label="labelData"
1429
- :readonly="readonly"
1430
- clearable
1431
- :placeholder="placeholder"
1432
- :columns="option"
1433
- :option="attr.option ? attr.option : columnsField"
1434
- :rules="[{ required: attr.rule.required === 'true', message: `请选择${attr.name}` }]"
1435
- :required="attr.rule.required === 'true'"
1436
- :lazy-load="attr.lazyLoad"
1437
- :on-search="attr.lazyLoad ? handleLazySearch : null"
1438
- />
1439
-
1440
- <!-- 文本区域 -->
1441
- <VanField
1442
- v-if="attr.type === 'textarea' && showItem"
1443
- v-model="(modelData as string)"
1444
- rows="3"
1445
- autosize
1446
- :label="labelData"
1447
- :label-align="labelAlign"
1448
- :input-align="attr.inputAlign ? attr.inputAlign : 'left'"
1449
- type="textarea"
1450
- :readonly="readonly"
1451
- :maxlength="attr.maxlength ? attr.maxlength : 200"
1452
- :placeholder="attr.placeholder ? attr.placeholder : `请输入${attr.name}`"
1453
- show-word-limit
1454
- :rules="[{ required: attr.rule.required === 'true', message: `请填写${attr.name}` }]"
1455
- :required="attr.rule.required === 'true'"
1456
- />
1457
-
1458
- <!-- 文本输入框 -->
1459
- <VanField
1460
- v-if="(attr.type === 'input' || attr.type === 'intervalPicker' || attr.type === 'scanCode' || attr.type === 'nfc') && showItem"
1461
- v-model="(modelData as string)"
1462
- style="align-items: center"
1463
- :label="labelData"
1464
- :label-align="labelAlign"
1465
- :input-align="attr.inputAlign ? attr.inputAlign : 'left'"
1466
- :type="attr.type as FieldType"
1467
- :readonly="readonly"
1468
- :disabled="attr.disabled"
1469
- :placeholder="placeholder"
1470
- :error-message="errorMessage"
1471
- :clearable="attr.clearable"
1472
- :rules="[{ required: attr.rule.required === 'true', message: `请填写${attr.name}` }]"
1473
- :required="attr.rule.required === 'true'"
1474
- @blur="() => formTypeCheck(attr, modelData as string)"
1475
- >
1476
- <template #input>
1477
- <input
1478
- :value="modelData"
1479
- :readonly="readonly"
1480
- class="van-field__control"
1481
- :placeholder="placeholder"
1482
- style="flex: 1; min-width: 0;"
1483
- @input="e => modelData = (e.target as HTMLInputElement).value"
1484
- @blur="() => formTypeCheck(attr, modelData as string)"
1485
- >
1486
- <VanButton
1487
- v-if="(attr.type === 'input' || attr.type === 'intervalPicker') && !props.formReadonly && attr.inputOnAfterName && attr.inputOnAfterFunc && !attr.inputOnAfterName.includes('|')"
1488
- class="action-btn"
1489
- round
1490
- type="primary"
1491
- size="small"
1492
- @click="emitFunc(attr.inputOnAfterFunc, attr)"
1493
- >
1494
- {{ attr.inputOnAfterName }}
1495
- </VanButton>
1496
- <VanButton
1497
- v-if="(attr.type === 'scanCode' || attr.type === 'nfc') && !props.formReadonly"
1498
- class="action-btn"
1499
- round
1500
- type="primary"
1501
- size="small"
1502
- @click="scanCodeOrNfc(attr)"
1503
- >
1504
- {{ attr.type === 'scanCode' ? '扫码' : 'NFC' }}
1505
- </VanButton>
1506
- </template>
1507
- </VanField>
1508
-
1509
- <!-- 地址选择器 -->
1510
- <VanField
1511
- v-if="attr.type === 'addressSearch' && showItem"
1512
- v-model="modelData as string"
1513
- name="addressSearch"
1514
- :label="labelData"
1515
- :label-align="labelAlign"
1516
- :input-align="attr.inputAlign ? attr.inputAlign : 'left'"
1517
- readonly
1518
- is-link
1519
- :placeholder="placeholder"
1520
- :rules="[{ required: attr.rule.required === 'true', message: '请选择地址' }]"
1521
- :required="attr.rule.required === 'true'"
1522
- @click="readonly ? null : (showAddressPicker = true)"
1523
- />
1524
- <VanPopup
1525
- v-model:show="showAddressPicker"
1526
- position="bottom"
1527
- :style="{ height: '80vh' }"
1528
- teleport="body"
1529
- overlay-class="date-picker-overlay"
1530
- >
1531
- <XLocationPicker
1532
- :default-center="defaultMapCenter"
1533
- :service-name="serviceName"
1534
- @confirm="handleAddressConfirm"
1535
- />
1536
- </VanPopup>
1537
-
1538
- <!-- pc的树形选择框————》 手机端采用 Cascader 级联选择 -->
1539
- <VanField
1540
- v-if="attr.type === 'treeSelect' && showItem"
1541
- v-model="treeValue"
1542
- name="treeSelect"
1543
- :label="labelData"
1544
- :label-align="labelAlign"
1545
- :input-align="attr.inputAlign ? attr.inputAlign : 'left'"
1546
- readonly
1547
- is-link
1548
- :placeholder="attr.placeholder ? attr.placeholder : `请选择${attr.name}`"
1549
- :rules="[{ required: attr.rule.required === 'true', message: `请选择${attr.name}` }]"
1550
- :required="attr.rule.required === 'true'"
1551
- @click="readonly ? null : (showTreeSelect = true)"
1552
- />
1553
- <VanPopup
1554
- v-model:show="showTreeSelect"
1555
- position="bottom"
1556
- teleport="body"
1557
- overlay-class="date-picker-overlay"
1558
- >
1559
- <VanCascader
1560
- :options="cleanEmptyChildren(option, attr.customFieldName ? attr.customFieldName : { text: 'label', value: 'value', children: 'children' })"
1561
- :field-names="attr.customFieldName ? attr.customFieldName : { text: 'label', value: 'value', children: 'children' }"
1562
- :title="attr.name"
1563
- :closeable="true"
1564
- @close="showTreeSelect = false"
1565
- @finish="onTreeSelectFinish"
1566
- />
1567
- </VanPopup>
1568
- </div>
1569
- </template>
1570
-
1571
- <style scoped>
1572
- .date-picker-overlay {
1573
- background-color: rgba(0, 0, 0, 0.2); /* 设置为半透明的黑色 */
1574
- }
1575
- .action-btn {
1576
- border-radius: 10px;
1577
- margin-left: 8px;
1578
- min-width: 4rem;
1579
- max-width: 6rem;
1580
- }
1581
- </style>
1
+ <script setup lang="ts">
2
+ import type { FieldType } from 'vant'
3
+ import type { Numeric } from 'vant/es/utils'
4
+ import ImageUploader from '@af-mobile-client-vue3/components/core/ImageUploader/index.vue'
5
+ import XGridDropOption from '@af-mobile-client-vue3/components/core/XGridDropOption/index.vue'
6
+ import XMultiSelect from '@af-mobile-client-vue3/components/core/XMultiSelect/index.vue'
7
+ import XSelect from '@af-mobile-client-vue3/components/core/XSelect/index.vue'
8
+ import XLocationPicker from '@af-mobile-client-vue3/components/data/XOlMap/XLocationPicker/index.vue'
9
+ import { getConfigByNameAsync, runLogic } from '@af-mobile-client-vue3/services/api/common'
10
+ import { post } from '@af-mobile-client-vue3/services/restTools'
11
+ import { searchToListOption, searchToOption } from '@af-mobile-client-vue3/services/v3Api'
12
+ import { useUserStore } from '@af-mobile-client-vue3/stores/modules/user'
13
+ import { getDict } from '@af-mobile-client-vue3/utils/dictUtil'
14
+ import { mobileUtil } from '@af-mobile-client-vue3/utils/mobileUtil'
15
+ import { executeStrFunctionByContext } from '@af-mobile-client-vue3/utils/runEvalFunction'
16
+ import { areaList } from '@vant/area-data'
17
+ import dayjs from 'dayjs/esm/index'
18
+ import { debounce } from 'lodash-es'
19
+ import {
20
+ showToast,
21
+ Area as VanArea,
22
+ Button as VanButton,
23
+ Calendar as VanCalendar,
24
+ Cascader as VanCascader,
25
+ Checkbox as VanCheckbox,
26
+ CheckboxGroup as vanCheckboxGroup,
27
+ DatePicker as VanDatePicker,
28
+ Field as VanField,
29
+ Picker as VanPicker,
30
+ PickerGroup as VanPickerGroup,
31
+ Popup as VanPopup,
32
+ Radio as VanRadio,
33
+ RadioGroup as VanRadioGroup,
34
+ Rate as VanRate,
35
+ Slider as VanSlider,
36
+ Stepper as VanStepper,
37
+ Switch as VanSwitch,
38
+ TimePicker as VanTimePicker,
39
+ } from 'vant'
40
+ import { computed, defineEmits, defineModel, defineProps, getCurrentInstance, onBeforeMount, ref, watch } from 'vue'
41
+
42
+ const props = defineProps({
43
+ attr: {
44
+ type: Object,
45
+ },
46
+ form: {
47
+ type: Object,
48
+ },
49
+ // 整表只读:来自 XForm,统一控制交互
50
+ formReadonly: {
51
+ type: Boolean,
52
+ default: false,
53
+ },
54
+ datePickerFilter: {
55
+ type: Function,
56
+ default: () => true,
57
+ },
58
+ datePickerFormatter: {
59
+ type: Function,
60
+ default: (type, val) => val,
61
+ },
62
+ mode: {
63
+ type: String,
64
+ default: '查询',
65
+ },
66
+ serviceName: {
67
+ type: String,
68
+ default: undefined,
69
+ },
70
+ // 调用logic获取数据源的追加参数
71
+ getDataParams: {
72
+ type: Object,
73
+ default: undefined,
74
+ },
75
+ disabled: {
76
+ type: Boolean,
77
+ default: false,
78
+ },
79
+ rules: {
80
+ type: Object,
81
+ default: () => {},
82
+ },
83
+ // 用 defineModel 替代 modelValue/emit
84
+ modelValue: {
85
+ type: [String, Number, Boolean, Array, Object],
86
+ default: undefined,
87
+ },
88
+ showLabel: {
89
+ type: Boolean,
90
+ default: true,
91
+ },
92
+ // radio/checkbox/select/mul-select 选项数据结构
93
+ columnsField: {
94
+ type: Object,
95
+ default: () => {
96
+ return { text: 'label', value: 'value' }
97
+ },
98
+ },
99
+ isAsyncUpload: {
100
+ type: Boolean,
101
+ default: false,
102
+ },
103
+
104
+ })
105
+
106
+ const emits = defineEmits(['setForm', 'xFormItemEmitFunc', 'scanCodeOrNfc'])
107
+
108
+ // 用 defineModel 替代 modelValue/emit
109
+ const modelData = defineModel<string | number | boolean | any[] | Record<string, any>>()
110
+
111
+ // 获取字典
112
+ interface OptionItem {
113
+ label: string
114
+ value: any
115
+ children?: OptionItem[]
116
+ }
117
+
118
+ // 判断并初始化防抖函数
119
+ let debouncedUserLinkFunc: (() => void) | null = null
120
+ let debouncedDepLinkFunc: (() => void) | null = null
121
+ let debouncedUpdateOptions: (() => void) | null = null
122
+
123
+ const { attr, form, mode, serviceName, getDataParams, columnsField } = props
124
+ // 配置的表单值格式(仅针对 datePicker 生效)
125
+ // 作用:统一控制日期值的格式化输入/输出与选择器展示粒度
126
+ // 可选:'YYYY' | 'YYYY-MM' | 'YYYY-MM-DD' | 'YYYY-MM-DD HH' | 'YYYY-MM-DD HH:mm' | 'YYYY-MM-DD HH:mm:ss'
127
+ const formValueFormat = computed(() => {
128
+ // 默认全格式
129
+ return (attr && (attr as any).formValueFormat) || 'YYYY-MM-DD HH:mm:ss'
130
+ })
131
+
132
+ // 根据 formValueFormat 动态计算日期列类型(决定 VanDatePicker 展示年/月/日)
133
+ // 例如:YYYY 只显示年;YYYY-MM 显示年、月;YYYY-MM-DD 显示年、月、日
134
+ const dateColumnsType = computed(() => {
135
+ const format = formValueFormat.value
136
+ const columns: string[] = ['year']
137
+ if (format.includes('MM'))
138
+ columns.push('month')
139
+ if (format.includes('DD'))
140
+ columns.push('day')
141
+ return columns as any
142
+ })
143
+
144
+ // 是否包含时间(决定是否展示时间页签与 VanTimePicker)
145
+ const hasTime = computed(() => formValueFormat.value.includes('HH'))
146
+
147
+ // 根据 formValueFormat 动态计算时间列类型(决定时/分/秒的展示)
148
+ // 例如:包含 HH 显示小时;包含 mm 显示分钟;包含 ss 显示秒
149
+ const timeColumnsType = computed(() => {
150
+ const format = formValueFormat.value
151
+ const columns: string[] = []
152
+ if (format.includes('HH'))
153
+ columns.push('hour')
154
+ if (format.includes('mm'))
155
+ columns.push('minute')
156
+ if (format.includes('ss'))
157
+ columns.push('second')
158
+ return columns as any
159
+ })
160
+ const calendarShow = ref(false)
161
+ const option = ref([])
162
+ const pickerValue = ref(undefined)
163
+ const timePickerValue = ref(undefined)
164
+ const area = ref<any>(undefined)
165
+ const showPicker = ref(false)
166
+ const showDatePicker = ref(false)
167
+ const showTimePicker = ref(false)
168
+ const showArea = ref(false)
169
+ const errorMessage = ref('')
170
+ const showTreeSelect = ref(false)
171
+ const treeValue = ref('')
172
+ // 懒加载 最后检索版本
173
+ const lastFetchId = ref(0)
174
+
175
+ // 登录信息 (可以在配置的动态函数中使用 this.setupState 获取到当前组件内的全部函数和变量 例:this.setupState.userState)
176
+ const userState = useUserStore().getLogin()
177
+ const currUser = computed(() => userState.f.resources.id)
178
+ const userInfo = computed(() => ({
179
+ orgId: userState.f.resources.orgid,
180
+ userId: userState.f.resources.id,
181
+ }))
182
+
183
+ // 是否展示当前项
184
+ const showItem = ref(true)
185
+
186
+ // 当前组件实例(不推荐使用,可能会在后续的版本更迭中调整,暂时用来绑定函数的上下文)
187
+ const currInst = getCurrentInstance()
188
+
189
+ // 配置中心->表单项变更触发函数
190
+ async function dataChangeFunc() {
191
+ if (attr.dataChangeFunc) {
192
+ await executeStrFunctionByContext(currInst, attr.dataChangeFunc, [props.form, (formData: any) => emits('setForm', formData), attr, null, mode, runLogic, getConfigByNameAsync])
193
+ }
194
+ }
195
+ const dataChangeFuncdebounce = debounce(dataChangeFunc, 300)
196
+ // 配置中心->表单项展示函数
197
+ async function showFormItemFunc() {
198
+ if (attr.showFormItemFunc) {
199
+ const obj = await executeStrFunctionByContext(currInst, attr.showFormItemFunc, [form, attr, null, mode])
200
+ // 判断是 bool 还是 obj 兼容
201
+ if (typeof obj === 'boolean') {
202
+ showItem.value = obj
203
+ }
204
+ else if (obj && typeof obj === 'object') {
205
+ // obj 是一个对象,并且不是数组
206
+ showItem.value = obj?.show
207
+ }
208
+ }
209
+ }
210
+ const showFormItemFuncdebounce = debounce(showFormItemFunc, 300)
211
+ /**
212
+ * 检测是否传入了有效的值
213
+ * @returns any
214
+ */
215
+ function checkModel(val = props.modelValue) {
216
+ if (val === null || val === undefined || val === '')
217
+ return false
218
+ if (Array.isArray(val))
219
+ return val.length > 0
220
+ return true
221
+ }
222
+ /**
223
+ * 获取表单项的默认值
224
+ * @returns any
225
+ */
226
+ function getDefaultValue() {
227
+ const val = props.modelValue
228
+ console.warn('>>>>>props', props)
229
+ console.warn('>>>> attr', attr)
230
+ // 如果有有效值,直接返回(datePicker 初始值需按 formValueFormat 归一)
231
+ // 目的:外部通过 formData 传入的初始值在组件挂载即与配置格式一致
232
+ if (checkModel(val)) {
233
+ if (attr.type === 'datePicker' && typeof val === 'string') {
234
+ const parsed = dayjs(val)
235
+ if (parsed.isValid())
236
+ return parsed.format(formValueFormat.value)
237
+ }
238
+ return val
239
+ }
240
+
241
+ // 根据类型获取默认值
242
+ const getDefaultByType = () => {
243
+ const def = mode !== '查询' ? attr.formDefault : attr.queryFormDefault
244
+
245
+ if (['treeSelect', 'select', 'checkbox'].includes(attr.type) && ['curOrgId', 'curDepId', 'curUserId'].includes(def)) {
246
+ if (def === 'curOrgId') {
247
+ if (attr.type === 'treeSelect') {
248
+ treeValue.value = userState.f.resources.orgs
249
+ }
250
+ return attr.type === 'select' ? userState.f.resources.orgid : [userState.f.resources.orgid]
251
+ }
252
+ if (def === 'curDepId') {
253
+ if (attr.type === 'treeSelect') {
254
+ treeValue.value = userState.f.resources.deps
255
+ }
256
+ return attr.type === 'select' ? userState.f.resources.depids : [userState.f.resources.depids]
257
+ }
258
+ if (def === 'curUserId') {
259
+ if (attr.type === 'treeSelect') {
260
+ treeValue.value = userState.f.resources.name
261
+ }
262
+ return attr.type === 'select' ? userState.f.resources.id : [userState.f.resources.id]
263
+ }
264
+ }
265
+
266
+ // 数组类型默认值
267
+ const arrayTypes = ['uploader', 'checkbox', 'file', 'area', 'image', 'treeSelect']
268
+ if (arrayTypes.includes(attr.type)) {
269
+ return def !== undefined ? def : []
270
+ }
271
+
272
+ // 特殊类型默认值
273
+ const specialDefaults = {
274
+ switch: false,
275
+ stepper: 1,
276
+ addressSearch: val,
277
+ }
278
+
279
+ if (specialDefaults[attr.type] !== undefined) {
280
+ return specialDefaults[attr.type]
281
+ }
282
+
283
+ // 日期时间类型:调用 getDateRange,并传入 formValueFormat
284
+ // 说明:让初始化/查询默认值同样遵循配置的格式显示
285
+ const dateTypes = ['rangePicker', 'yearPicker', 'monthPicker', 'yearRangePicker', 'monthRangePicker', 'datePicker', 'timePicker']
286
+ if (dateTypes.includes(attr.type)) {
287
+ return getDateRange({
288
+ type: attr.type,
289
+ formDefault: attr.formDefault ?? '',
290
+ queryFormDefault: attr.queryFormDefault ?? '',
291
+ queryType: attr.queryType,
292
+ queryValueFormat: attr.queryValueFormat,
293
+ name: attr.name ?? '',
294
+ formValueFormat: formValueFormat.value,
295
+ }) ?? []
296
+ }
297
+
298
+ // 其他类型(字符串、数字等)
299
+ return def ?? ''
300
+ }
301
+
302
+ return getDefaultByType()
303
+ }
304
+
305
+ // 初始化日期组件初始值,组件自定义格式显示值和实际值(日期相关初始化都在此函数中操作)
306
+ function getDateRange({
307
+ type,
308
+ formDefault: defaultValue,
309
+ queryFormDefault,
310
+ queryType,
311
+ queryValueFormat: defaultFormat,
312
+ name,
313
+ formValueFormat: formFormat,
314
+ }: {
315
+ type: string
316
+ formDefault: string
317
+ queryFormDefault: string
318
+ queryType?: string
319
+ queryValueFormat?: string
320
+ name: string
321
+ // 新增:用于优先覆盖 datePicker 的显示/存储格式
322
+ formValueFormat?: string
323
+ }): string | [string, string] | undefined {
324
+ const formatMap: Record<string, string> = {
325
+ yearPicker: 'YYYY',
326
+ yearRangePicker: 'YYYY',
327
+ monthPicker: 'YYYY-MM',
328
+ monthRangePicker: 'YYYY-MM',
329
+ datePicker: 'YYYY-MM-DD HH:mm:ss',
330
+ rangePicker: 'YYYY-MM-DD HH:mm:ss',
331
+ }
332
+ if (mode) {
333
+ // datePicker 优先使用 formValueFormat(否则退回 queryValueFormat 或默认映射)
334
+ const preferFormat = type === 'datePicker' ? (formFormat || defaultFormat) : defaultFormat
335
+ const format = preferFormat || formatMap[type]
336
+ const val = mode === '查询' ? queryFormDefault : defaultValue
337
+ let start: string, end: string
338
+ switch (val) {
339
+ case 'curYear':
340
+ start = dayjs().startOf('year').format(format)
341
+ end = dayjs().endOf('year').format(format)
342
+ break
343
+ case 'curMonth':
344
+ start = dayjs().startOf('month').format(format)
345
+ end = dayjs().endOf('month').format(format)
346
+ break
347
+ case 'curDay':
348
+ start = dayjs().startOf('day').format(format)
349
+ end = dayjs().endOf('day').format(format)
350
+ break
351
+ case 'curTime':
352
+ start = dayjs().format(format)
353
+ end = dayjs().format(format)
354
+ break
355
+ default:
356
+ return undefined
357
+ }
358
+ if (['monthPicker', 'yearPicker', 'datePicker'].includes(type)) {
359
+ if (mode !== '查询') {
360
+ if (queryType === 'BETWEEN') {
361
+ return [start, end]
362
+ }
363
+ if (name.includes('开始') || name.includes('起始')) {
364
+ return start
365
+ }
366
+ else {
367
+ return end
368
+ }
369
+ }
370
+ else {
371
+ return start
372
+ }
373
+ }
374
+ // rangePicker组件表单显示的值
375
+ if (mode === '查询' && type === 'rangePicker') {
376
+ pickerValue.value = `${start} ~ ${end}`
377
+ }
378
+ return mode !== '查询' ? start : [start, end]
379
+ }
380
+ else {
381
+ return undefined
382
+ }
383
+ }
384
+
385
+ // 监听 props.form 的变化
386
+ watch(
387
+ () => props.form,
388
+ (newVal, oldVal) => {
389
+ // 如果是从函数获取 options
390
+ if (props.attr.keyName && (props.attr.keyName.toString().includes('async ') || props.attr.keyName.toString().includes('function'))) {
391
+ debouncedUpdateOptions()
392
+ }
393
+ if (props.attr.showFormItemFunc) {
394
+ showFormItemFuncdebounce()
395
+ }
396
+ },
397
+ { deep: true },
398
+ )
399
+
400
+ // 监听 modelData 的变化,调用 dataChangeFunc
401
+ watch(
402
+ () => modelData.value,
403
+ (newVal, oldVal) => {
404
+ // 避免初始化时的调用
405
+ if (newVal !== oldVal) {
406
+ dataChangeFuncdebounce()
407
+ }
408
+ },
409
+ { deep: true },
410
+ )
411
+
412
+ // 监听 option.value 的变化
413
+ watch(
414
+ () => option.value,
415
+ (newOption) => {
416
+ if (attr.type === 'treeSelect' && newOption && newOption.length > 0) {
417
+ // 你可以在这里调用 findOptionInTree 函数来查找默认值对应的 label
418
+ const result = findOptionInTree(option.value, modelData.value)
419
+ if (attr.type === 'treeSelect' && result)
420
+ treeValue.value = result.label
421
+ }
422
+ },
423
+ )
424
+
425
+ function updateFile(files, _index) {
426
+ modelData.value = files
427
+ }
428
+
429
+ // 表单校验的类型校验
430
+ function formTypeCheck(attr, value) {
431
+ if (mode === '查询' || mode === '预览')
432
+ return
433
+ // if (!attr.rule || !attr.rule.required || attr.rule.required === 'false')
434
+ // return
435
+ switch (attr.rule.type) {
436
+ case 'string':
437
+ if (value.length === 0) {
438
+ errorMessage.value = `请输入${attr.name}`
439
+ return
440
+ }
441
+ break
442
+ case 'number':
443
+ if (!/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)$/.test(value)) {
444
+ errorMessage.value = `${attr.name}必须为数字`
445
+ return
446
+ }
447
+ break
448
+ case 'boolean':
449
+ case 'array':
450
+ case 'regexp':
451
+ if (value) {
452
+ errorMessage.value = ''
453
+ return
454
+ }
455
+ break
456
+ case 'integer':
457
+ if (!/^-?\d+$/.test(value)) {
458
+ errorMessage.value = `${attr.name}必须为整数`
459
+ return
460
+ }
461
+ break
462
+ case 'float':
463
+ if (!/^-?\d+\.\d+$/.test(value)) {
464
+ errorMessage.value = `${attr.name}必须为小数`
465
+ return
466
+ }
467
+ break
468
+ case 'email':
469
+ if (!/^[\w.%+-]+@[a-z0-9.-]+\.[a-z]{2,}$/i.test(value)) {
470
+ errorMessage.value = `请输入正确的邮箱地址`
471
+ return
472
+ }
473
+ break
474
+ case 'idNumber':
475
+ if (!/^(?:\d{15}|\d{17}[0-9X])$/.test(value)) {
476
+ errorMessage.value = `请输入正确的身份证号码`
477
+ return
478
+ }
479
+ break
480
+ case 'userPhone':
481
+ if (!/^1[3-9]\d{9}$/.test(value)) {
482
+ errorMessage.value = `请输入正确的手机号码`
483
+ return
484
+ }
485
+ break
486
+ case 'landlineNumber':
487
+ if (!/^0\d{2,3}[-\s]?\d{7,8}$/.test(value)) {
488
+ errorMessage.value = `请输入正确的座机号码`
489
+ return
490
+ }
491
+ break
492
+ case 'greaterThanZero':
493
+ if (!/^(?:[1-9]\d*(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i.test(value)) {
494
+ errorMessage.value = `请输入一个大于0的数字`
495
+ return
496
+ }
497
+ break
498
+ case 'greaterThanOrEqualZero':
499
+ if (!/^(?:\d+|\d*\.\d+)$/.test(value)) {
500
+ errorMessage.value = `请输入一个大于等于0的数字`
501
+ return
502
+ }
503
+ break
504
+ case 'stringLength':
505
+ if (!(value.length >= attr.rule.minLen && value.length < attr.rule.maxLen)) {
506
+ errorMessage.value = `长度必须在${attr.rule.minLen}~${attr.rule.maxLen}之间`
507
+ return
508
+ }
509
+ break
510
+ case 'customJs':
511
+ // eslint-disable-next-line no-case-declarations
512
+ const funcStr = attr.rule.customValidatorFunc
513
+ // 输入的数据是否符合条件
514
+ // eslint-disable-next-line no-case-declarations
515
+ const status = ref(true)
516
+ // 提取函数体部分
517
+ // eslint-disable-next-line no-case-declarations
518
+ const funcBodyMatch = funcStr.match(/function\s*\(.*?\)\s*\{([\s\S]*)\}/)
519
+ if (!funcBodyMatch)
520
+ throw new Error('自定义校验函数不合法')
521
+ // eslint-disable-next-line no-case-declarations
522
+ const funcBody = funcBodyMatch[1].trim() // 提取函数体
523
+ // 使用 new Function 创建函数
524
+ // eslint-disable-next-line no-new-func,no-case-declarations
525
+ const customValidatorFunc = new Function('rule', 'value', 'callback', 'form', 'attr', 'util', funcBody)
526
+ // 定义 callback 函数
527
+ // eslint-disable-next-line no-case-declarations
528
+ const callback = (error) => {
529
+ if (error) {
530
+ errorMessage.value = `${error}`
531
+ status.value = false // 表示有错误发生
532
+ }
533
+ }
534
+ // 调用自定义校验函数
535
+ customValidatorFunc(
536
+ attr.rule,
537
+ value,
538
+ callback,
539
+ form,
540
+ attr,
541
+ {}, // util 对象(可以根据需要传递)
542
+ )
543
+ if (!status.value)
544
+ return
545
+ break
546
+ default:
547
+ errorMessage.value = ''
548
+ break
549
+ }
550
+ errorMessage.value = ''
551
+ }
552
+
553
+ onBeforeMount(() => {
554
+ init()
555
+ modelData.value = getDefaultValue()
556
+ showFormItemFunc()
557
+ dataChangeFunc()
558
+ if (attr?.keyName?.toString()?.startsWith('search@根据表单项[') && attr?.keyName?.toString().endsWith(']联动人员'))
559
+ debouncedUserLinkFunc = debounce(() => updateResOptions('人员'), 200)
560
+
561
+ if (attr?.keyName?.toString()?.startsWith('search@根据表单项[') && attr?.keyName?.toString().endsWith(']联动部门'))
562
+ debouncedDepLinkFunc = debounce(() => updateResOptions('部门'), 200)
563
+
564
+ if (attr.keyName && (attr?.keyName?.toString().indexOf('async ') !== -1 || attr?.keyName?.toString()?.indexOf('function') !== -1)) {
565
+ debouncedUpdateOptions = debounce(updateOptions, 200)
566
+ }
567
+ })
568
+ // 是否展示表单左侧label文字
569
+ const labelData = computed(() => {
570
+ return props.showLabel ? attr.name : null
571
+ })
572
+ // 是否展示表单左侧label文字
573
+ const labelAlign = computed(() => {
574
+ return attr.labelAlign ? attr.labelAlign : 'left'
575
+ })
576
+ // 是否只读
577
+ const readonly = computed(() => {
578
+ return props.formReadonly || attr.addOrEdit === 'readonly' || mode === '预览'
579
+ })
580
+ // 提示内容
581
+ const placeholder = computed(() => {
582
+ if (attr.addOrEdit === 'readonly' || mode === '预览') {
583
+ return '暂无内容 ~ '
584
+ }
585
+ else
586
+ if (attr.placeholder) {
587
+ return attr.placeholder
588
+ }
589
+ else {
590
+ switch (attr.type) {
591
+ case 'datePicker':
592
+ case 'timePicker':
593
+ case 'rangePicker':
594
+ case 'radio':
595
+ case 'select':
596
+ case 'treeSelect':
597
+ case 'area':
598
+ case 'citySelect':
599
+ case 'picker':
600
+ return `请选择${attr.name}`
601
+ case 'addressSearch':
602
+ case 'input':
603
+ case 'textarea':
604
+ case 'intervalPicker':
605
+ return `请输入${attr.name}`
606
+ default:
607
+ return `请选择${attr.name}`
608
+ }
609
+ }
610
+ })
611
+
612
+ const formatDate = date => `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`
613
+
614
+ function onCalendarConfirm(values) {
615
+ modelData.value = [formatDate(values[0]), formatDate(values[1])]
616
+ pickerValue.value = `${formatDate(values[0])} ~ ${formatDate(values[1])}`
617
+ calendarShow.value = false
618
+ }
619
+
620
+ // js 函数作为数据源
621
+ async function updateOptions() {
622
+ if (attr.keyName && (attr.keyName.toString().includes('async ') || attr.keyName.toString().includes('function '))) {
623
+ option.value = await executeStrFunctionByContext(this, attr.keyName, [props.form, runLogic, props.mode, getConfigByNameAsync, post])
624
+ }
625
+ }
626
+
627
+ function init() {
628
+ if (attr.keyName && typeof attr.keyName === 'string') {
629
+ if (attr.keyName && attr.keyName.includes('logic@')) {
630
+ getData({}, (res) => {
631
+ option.value = res
632
+ initRadioValue()
633
+ })
634
+ }
635
+ else if (attr.keyName && attr.keyName.includes('config@')) {
636
+ const configName = attr.keyName.substring(7)
637
+ getDict(configName, (result) => {
638
+ if (result)
639
+ option.value = result
640
+ }, serviceName)
641
+ }
642
+ else if (attr.keyName && attr.keyName.includes('search@')) {
643
+ let source = attr.keyName.substring(7)
644
+ const userid = currUser.value
645
+ let roleName = 'roleName'
646
+ if (source.startsWith('根据角色[') && source.endsWith(']获取人员')) {
647
+ const startIndex = source.indexOf('[') + 1
648
+ const endIndex = source.indexOf(']', startIndex)
649
+ roleName = source.substring(startIndex, endIndex)
650
+ source = '根据角色获取人员'
651
+ }
652
+ const searchData = { source, userid, roleName }
653
+ if (source.startsWith('根据表单项[') && source.endsWith(']联动人员'))
654
+ updateResOptions('人员')
655
+ else if (source.startsWith('根据表单项[') && source.endsWith(']联动部门'))
656
+ updateResOptions('部门')
657
+ else if (attr.type === 'select' || attr.type === 'checkbox')
658
+ searchToListOption(searchData, res => getDataCallback(res))
659
+ else
660
+ searchToOption(searchData, res => getDataCallback(res))
661
+ }
662
+ else if (attr.keyName.toString().includes('async ') || attr.keyName.toString().includes('function ')) {
663
+ updateOptions()
664
+ }
665
+ else {
666
+ initRadioValue()
667
+ }
668
+ }
669
+ }
670
+
671
+ function getDataCallback(res) {
672
+ option.value = res
673
+ if (attr.type === 'radio')
674
+ initRadioValue()
675
+ }
676
+
677
+ async function updateResOptions(type) {
678
+ if (attr?.keyName?.toString()?.startsWith('search@根据表单项[') && attr?.keyName?.toString()?.endsWith(`]联动${type}`)) {
679
+ const searchData = { source: `获取${type}`, userid: currUser.value }
680
+ const startIndex = attr.keyName.indexOf('[') + 1
681
+ const endIndex = attr.keyName.indexOf(']', startIndex)
682
+ const formModel = attr.keyName.substring(startIndex, endIndex).replace('.', '_')
683
+ const formModelData = form[formModel]
684
+ if (formModel?.length && formModelData?.length) {
685
+ await searchToListOption(searchData, (res) => {
686
+ getDataCallback(res.filter((h) => {
687
+ return formModelData['0'] === h.f_organization_id || formModelData['0'] === h.f_department_id || formModelData['0'] === h.parentid
688
+ // if (formModel.indexOf('org') > -1) {
689
+ // return formModelData?.includes(h.orgid || h.f_organization_id || h.parentid)
690
+ // } else {
691
+ // return formModelData?.includes(h?.parentid)
692
+ // }
693
+ }))
694
+ })
695
+ }
696
+ }
697
+ }
698
+
699
+ function initRadioValue() {
700
+ if ((mode === '新增' || mode === '修改') && attr.type === 'radio' && !props.modelValue) {
701
+ if (attr.keys && attr.keys.length > 0)
702
+ modelData.value = attr.keys[0].value
703
+ else if (option.value && option.value.length > 0)
704
+ modelData.value = option.value[0].value
705
+ }
706
+ }
707
+
708
+ function getData(value, callback) {
709
+ if (value !== '') {
710
+ const logicName = attr.keyName
711
+ const logic = logicName.substring(6)
712
+ // 调用logic前设置参数
713
+ if (getDataParams && getDataParams[attr.model])
714
+ Object.assign(value, getDataParams[attr.model])
715
+ Object.assign(value, userInfo.value)
716
+ runLogic(logic, value, serviceName).then((res) => {
717
+ callback(res)
718
+ })
719
+ }
720
+ }
721
+
722
+ // 已废弃 不进行维护
723
+ function onPickerConfirm({ selectedOptions }) {
724
+ showPicker.value = false
725
+ modelData.value = selectedOptions[0].text
726
+ }
727
+
728
+ // 日期时间选择数据
729
+ const dateTimePickerValue = ref<any>({})
730
+ // 展示日期时间选择器:根据 formValueFormat 初始化 VanDatePicker/VanTimePicker 的当前值
731
+ // 规则:
732
+ // - 若已有字符串值,按 formValueFormat 解析并拆分为日期/时间数组
733
+ // - 若无值,按当前时间生成匹配格式的默认数组
734
+ function showDataTimePicker() {
735
+ if (props.modelValue && typeof props.modelValue === 'string') {
736
+ const base = dayjs(props.modelValue as string, formValueFormat.value)
737
+ const dateArr: string[] = []
738
+ dateArr.push(base.format('YYYY'))
739
+ if (dateColumnsType.value.includes('month'))
740
+ dateArr.push(base.format('MM'))
741
+ if (dateColumnsType.value.includes('day'))
742
+ dateArr.push(base.format('DD'))
743
+ const timeArr: string[] = []
744
+ if (hasTime.value) {
745
+ if (timeColumnsType.value.includes('hour'))
746
+ timeArr.push(base.format('HH'))
747
+ if (timeColumnsType.value.includes('minute'))
748
+ timeArr.push(base.format('mm'))
749
+ if (timeColumnsType.value.includes('second'))
750
+ timeArr.push(base.format('ss'))
751
+ }
752
+ dateTimePickerValue.value = {
753
+ date: dateArr,
754
+ time: timeArr.length ? timeArr : ['00', '00', '00'].slice(0, timeColumnsType.value.length),
755
+ }
756
+ }
757
+ else {
758
+ const now = dayjs()
759
+ const dateArr: string[] = []
760
+ dateArr.push(now.format('YYYY'))
761
+ if (dateColumnsType.value.includes('month'))
762
+ dateArr.push(now.format('MM'))
763
+ if (dateColumnsType.value.includes('day'))
764
+ dateArr.push(now.format('DD'))
765
+ const timeArr: string[] = []
766
+ if (hasTime.value) {
767
+ if (timeColumnsType.value.includes('hour'))
768
+ timeArr.push(now.format('HH'))
769
+ if (timeColumnsType.value.includes('minute'))
770
+ timeArr.push(now.format('mm'))
771
+ if (timeColumnsType.value.includes('second'))
772
+ timeArr.push(now.format('ss'))
773
+ }
774
+ dateTimePickerValue.value = {
775
+ date: dateArr,
776
+ time: timeArr.length ? timeArr : ['00', '00', '00'].slice(0, timeColumnsType.value.length),
777
+ }
778
+ }
779
+ showDatePicker.value = true
780
+ }
781
+
782
+ function onDatePickerConfirm({ selectedValues }) {
783
+ showDatePicker.value = false
784
+ modelData.value = selectedValues.join('-')
785
+ }
786
+
787
+ // 已废弃 不进行维护
788
+ function onTimePickerConfirm({ selectedValues }) {
789
+ showTimePicker.value = false
790
+ timePickerValue.value = selectedValues.join(':')
791
+ modelData.value = timePickerValue.value
792
+ }
793
+
794
+ // 没人用到本次先不动。后续需要看这个组件需要怎么使用
795
+ function onAreaConfirm({ selectedOptions }) {
796
+ area.value = `${selectedOptions[0].text}-${selectedOptions[1].text}-${selectedOptions[2].text}`
797
+ showArea.value = false
798
+ modelData.value = [{
799
+ province: selectedOptions[0].text,
800
+ city: selectedOptions[1].text,
801
+ district: selectedOptions[2].text,
802
+ }]
803
+ }
804
+
805
+ // 日期时间选择确认:将选择的年月日(+时分秒)拼装后,最终使用 formValueFormat 输出到 v-model
806
+ function onDateTimePickerConfirm() {
807
+ showDatePicker.value = false
808
+ const dateParts = dateTimePickerValue.value.date as string[]
809
+ const timeParts = (dateTimePickerValue.value.time as string[]) || []
810
+ const year = dateParts[0]
811
+ const month = dateParts[1] || '01'
812
+ const day = dateParts[2] || '01'
813
+ const hour = hasTime.value && timeParts[0] ? timeParts[0] : '00'
814
+ const minute = hasTime.value && timeParts[1] ? timeParts[1] : '00'
815
+ const second = hasTime.value && timeParts[2] ? timeParts[2] : '00'
816
+ const full = `${year}-${month}-${day} ${hour}:${minute}:${second}`
817
+ modelData.value = dayjs(full, 'YYYY-MM-DD HH:mm:ss').format(formValueFormat.value)
818
+ }
819
+
820
+ function onPickerCancel() {
821
+ showDatePicker.value = false
822
+ }
823
+
824
+ const showAddressPicker = ref(false)
825
+ const addressValue = ref('')
826
+
827
+ // XLocationPicker 默认加载的中心点
828
+ const defaultMapCenter = computed(() => {
829
+ const lonLat = form[`${attr.model}_lon_lat`]
830
+ if (lonLat && typeof lonLat === 'string' && lonLat.includes(',')) {
831
+ const [lon, lat] = lonLat.split(',').map(Number)
832
+ if (!Number.isNaN(lon) && !Number.isNaN(lat)) {
833
+ return [lon, lat] as [number, number]
834
+ }
835
+ }
836
+ return undefined
837
+ })
838
+
839
+ // 处理地址选择器确认
840
+ function handleAddressConfirm(location) {
841
+ // 构造新的数据格式
842
+ const formData = {
843
+ [`${attr.model}_lon_lat`]: `${location.longitude},${location.latitude}`,
844
+ [attr.model]: location.address,
845
+ }
846
+ // 更新表单数据
847
+ emits('setForm', formData)
848
+ showAddressPicker.value = false
849
+ }
850
+
851
+ // 重置方法,供父组件调用
852
+ function reset() {
853
+ modelData.value = getDefaultValue()
854
+ errorMessage.value = ''
855
+ treeValue.value = null
856
+ area.value = null
857
+ pickerValue.value = null
858
+ }
859
+
860
+ defineExpose({
861
+ reset,
862
+ })
863
+
864
+ // 数据处理
865
+ function cleanEmptyChildren(options, fieldNames = { text: 'label', value: 'value', children: 'children' }) {
866
+ if (!Array.isArray(options))
867
+ return options
868
+
869
+ const childrenKey = fieldNames.children || 'children'
870
+
871
+ return options.map((option) => {
872
+ // 深拷贝选项,避免修改原始数据
873
+ const newOption = { ...option }
874
+
875
+ // 如果存在children属性且是空数组
876
+ if (newOption[childrenKey] && Array.isArray(newOption[childrenKey]) && newOption[childrenKey].length === 0) {
877
+ delete newOption[childrenKey]
878
+ }
879
+ // 如果存在children属性且非空,则递归处理
880
+ else if (newOption[childrenKey] && Array.isArray(newOption[childrenKey])) {
881
+ newOption[childrenKey] = cleanEmptyChildren(newOption[childrenKey], fieldNames)
882
+ }
883
+ return newOption
884
+ })
885
+ }
886
+
887
+ // 级联选择完成事件
888
+ function onTreeSelectFinish({ selectedOptions }) {
889
+ const index = selectedOptions.length - 1
890
+ treeValue.value = selectedOptions[index].label
891
+ if (mode === '查询') {
892
+ modelData.value = [selectedOptions[index].value]
893
+ }
894
+ else {
895
+ modelData.value = selectedOptions[index].value
896
+ }
897
+ showTreeSelect.value = false
898
+ }
899
+
900
+ function emitFunc(func, data) {
901
+ emits('xFormItemEmitFunc', func, data, data?.model ? form[data.model] : form)
902
+ }
903
+
904
+ function findOptionInTree(options, value) {
905
+ // 在当前层级查找
906
+ const foundItem = options.find(item => item[columnsField.value] === value)
907
+ if (foundItem) {
908
+ return foundItem
909
+ }
910
+ // 递归查找子级
911
+ for (const item of options) {
912
+ if (item.children?.length) {
913
+ const foundInChildren = findOptionInTree(item.children, value)
914
+ if (foundInChildren)
915
+ return foundInChildren
916
+ }
917
+ }
918
+
919
+ return null
920
+ }
921
+
922
+ // 懒加载搜索函数
923
+ async function handleLazySearch(searchText: string) {
924
+ if (!attr.keyName || typeof attr.keyName !== 'string') {
925
+ return []
926
+ }
927
+
928
+ try {
929
+ // 如果是 search@ 类型的数据源
930
+ if (attr.keyName.includes('search@')) {
931
+ let source = attr.keyName.substring(7)
932
+ const userid = currUser.value
933
+ let roleName = 'roleName'
934
+
935
+ if (source.startsWith('根据角色[') && source.endsWith(']获取人员')) {
936
+ const startIndex = source.indexOf('[') + 1
937
+ const endIndex = source.indexOf(']', startIndex)
938
+ roleName = source.substring(startIndex, endIndex)
939
+ source = '根据角色获取人员'
940
+ }
941
+
942
+ const searchData = {
943
+ source,
944
+ userid,
945
+ roleName,
946
+ searchText, // 添加搜索关键词
947
+ }
948
+
949
+ return new Promise((resolve) => {
950
+ if (attr.type === 'select' || attr.type === 'checkbox') {
951
+ searchToListOption(searchData, (res) => {
952
+ // 根据搜索关键词过滤结果
953
+ const filtered = res.filter((item) => {
954
+ const text = item[columnsField.value.text] || item.label || ''
955
+ return text.toString().toLowerCase().includes(searchText.toLowerCase())
956
+ })
957
+ resolve(filtered)
958
+ })
959
+ }
960
+ else {
961
+ searchToOption(searchData, (res) => {
962
+ const filtered = res.filter((item) => {
963
+ const text = item[columnsField.value.text] || item.label || ''
964
+ return text.toString().toLowerCase().includes(searchText.toLowerCase())
965
+ })
966
+ resolve(filtered)
967
+ })
968
+ }
969
+ })
970
+ }
971
+
972
+ // logic 数据源
973
+ if (attr.keyName.includes('logic@')) {
974
+ const logicName = attr.keyName.substring(6)
975
+ const value = { word: searchText, ...userInfo.value }
976
+
977
+ // 调用logic前设置参数
978
+ if (getDataParams && getDataParams[attr.model]) {
979
+ Object.assign(value, getDataParams[attr.model])
980
+ }
981
+
982
+ lastFetchId.value++
983
+ const fetchId = lastFetchId.value
984
+ // 根据搜索关键词过滤结果
985
+ const results = await runLogic(logicName, value, serviceName) as any
986
+ if (fetchId !== lastFetchId.value) {
987
+ return
988
+ }
989
+ if (searchText) {
990
+ return results.filter((item) => {
991
+ const text = item[columnsField.value.text] || item.label || ''
992
+ return text.toString().toLowerCase().includes(searchText.toLowerCase())
993
+ })
994
+ }
995
+ return results
996
+ }
997
+
998
+ // 自定义 js 函数
999
+ if (attr.keyName.includes('async ') || attr.keyName.includes('function ')) {
1000
+ const results = await executeStrFunctionByContext(currInst, attr.keyName, [
1001
+ { ...props.form, searchText },
1002
+ runLogic,
1003
+ props.mode,
1004
+ getConfigByNameAsync,
1005
+ post,
1006
+ ])
1007
+ // 根据搜索关键词过滤结果
1008
+ return results.filter((item) => {
1009
+ const text = item[columnsField.value.text] || item.label || ''
1010
+ return text.toString().toLowerCase().includes(searchText.toLowerCase())
1011
+ })
1012
+ }
1013
+
1014
+ return []
1015
+ }
1016
+ catch (error) {
1017
+ console.error('懒加载搜索失败:', error)
1018
+ return []
1019
+ }
1020
+ }
1021
+
1022
+ // 扫码/NFC
1023
+ function scanCodeOrNfc(attr) {
1024
+ if (attr.type === 'scanCode') {
1025
+ // 扫码逻辑
1026
+ mobileUtil.execute({
1027
+ funcName: 'scanBarcode',
1028
+ param: {},
1029
+ callbackFunc: (res: any) => {
1030
+ console.log('扫码结果:------', res)
1031
+ if (res && res.status === 'success') {
1032
+ if (res.data.status && res.data.status === 'cancelled') {
1033
+ showToast(res.data?.message || '扫码已取消')
1034
+ return
1035
+ }
1036
+ modelData.value = res.data?.rawValue
1037
+ showToast('扫码成功')
1038
+ emitFunc('xFormItemEmitFunc', res)
1039
+ }
1040
+ else {
1041
+ showToast('扫码失败')
1042
+ }
1043
+ },
1044
+ })
1045
+ }
1046
+ else if (attr.type === 'nfc') {
1047
+ // NFC逻辑
1048
+ mobileUtil.execute({
1049
+ funcName: 'startNfcScan',
1050
+ param: {},
1051
+ callbackFunc: (res: any) => {
1052
+ console.log('nfc结果:------', res)
1053
+ if (res && res.status === 'success') {
1054
+ modelData.value = res.data?.id
1055
+ showToast('NFC 检测成功')
1056
+ emitFunc('xFormItemEmitFunc', res)
1057
+ }
1058
+ else {
1059
+ showToast(res?.message || 'NFC 功能不可用')
1060
+ }
1061
+ },
1062
+ })
1063
+ }
1064
+ }
1065
+ </script>
1066
+
1067
+ <template>
1068
+ <div>
1069
+ <!-- switch开关 -->
1070
+ <VanField
1071
+ v-if="attr.type === 'switch' && showItem"
1072
+ name="switch"
1073
+ :label="labelData"
1074
+ :label-align="labelAlign"
1075
+ :input-align="attr.inputAlign ? attr.inputAlign : 'right'"
1076
+ :rules="[{ required: attr.rule.required === 'true', message: `请选择${attr.name}` }]"
1077
+ :required="attr.rule.required === 'true'"
1078
+ >
1079
+ <template #input>
1080
+ <VanSwitch v-model="modelData" />
1081
+ </template>
1082
+ </VanField>
1083
+
1084
+ <!-- 复选框 -->
1085
+ <!-- <VanField
1086
+ v-if="attr.type === 'checkbox'"
1087
+ name="checkbox"
1088
+ :label="labelData"
1089
+ >
1090
+ <template #input>
1091
+ <VanCheckbox v-model="modelData" shape="square" />
1092
+ </template>
1093
+ </VanField> -->
1094
+
1095
+ <!-- 多选框-checkbox-复选框组 -->
1096
+ <template v-if="attr.type === 'checkbox' && showItem">
1097
+ <!-- 勾选 -->
1098
+ <VanField
1099
+ v-if="attr.showMode === 'checkbox' && mode !== '查询'"
1100
+ name="checkboxGroup"
1101
+ :label="labelData"
1102
+ :label-align="labelAlign"
1103
+ :input-align="attr.inputAlign ? attr.inputAlign : 'left'"
1104
+ :rules="[{ required: attr.rule.required === 'true', message: `请选择${attr.name}` }]"
1105
+ :required="attr.rule.required === 'true'"
1106
+ >
1107
+ <template #input>
1108
+ <van-checkbox-group v-model="modelData as any[]" direction="horizontal" shape="square" :disabled="readonly">
1109
+ <VanCheckbox v-for="(item, index) in option" :key="index" style="padding: 2px" :name="item[columnsField.value]" :shape="rules?.[attr.model].shape" :value="item[columnsField.value]">
1110
+ {{ item[columnsField.text] }}
1111
+ </VanCheckbox>
1112
+ </van-checkbox-group>
1113
+ </template>
1114
+ </VanField>
1115
+ <VanField
1116
+ v-if="attr.showMode === 'checkbox' && mode === '查询'"
1117
+ name="checkboxGroup"
1118
+ :label="labelData"
1119
+ :label-align="labelAlign"
1120
+ :input-align="attr.inputAlign ? attr.inputAlign : 'left'"
1121
+ :rules="[{ required: attr.rule.required === 'true', message: `请选择${attr.name}` }]"
1122
+ >
1123
+ <template #input>
1124
+ <XGridDropOption
1125
+ v-model="(modelData as string[])"
1126
+ :column-num="labelData ? 3 : 4"
1127
+ :multiple="true"
1128
+ :columns="option"
1129
+ />
1130
+ </template>
1131
+ </VanField>
1132
+ <!-- 下拉 -->
1133
+ <XMultiSelect
1134
+ v-if="(!attr.showMode || attr.showMode === 'select') && mode === '查询'"
1135
+ v-model="modelData"
1136
+ :label="labelData"
1137
+ :readonly="readonly"
1138
+ :placeholder="placeholder"
1139
+ :columns="option"
1140
+ :select-value="Array.isArray(modelData) ? modelData : []"
1141
+ :option="attr.option ? attr.option : columnsField"
1142
+ :rules="[{ required: attr.rule.required === 'true', message: `请选择${attr.name}` }]"
1143
+ :required="attr.rule.required === 'true'"
1144
+ :lazy-load="attr.lazyLoad"
1145
+ :on-search="attr.lazyLoad ? handleLazySearch : null"
1146
+ />
1147
+ </template>
1148
+
1149
+ <!-- 单选框 -->
1150
+ <VanField
1151
+ v-if="attr.type === 'radio' && mode !== '查询' && showItem"
1152
+ name="radio"
1153
+ center
1154
+ :placeholder="attr.placeholder ? attr.placeholder : `请选择${attr.name}`"
1155
+ :label="labelData"
1156
+ :label-align="labelAlign"
1157
+ :input-align="attr.inputAlign ? attr.inputAlign : 'left'"
1158
+ :rules="[{ required: attr.rule.required === 'true', message: `请选择${attr.name}` }]"
1159
+ :required="attr.rule.required === 'true'"
1160
+ >
1161
+ <template #input>
1162
+ <VanRadioGroup v-model="modelData" direction="horizontal" :disabled="readonly">
1163
+ <VanRadio v-for="(item, index) in option" :key="index" style="padding: 2px" :name="item[columnsField.value]" :value="item[columnsField.value]">
1164
+ {{ item[columnsField.text] }}
1165
+ </VanRadio>
1166
+ </VanRadioGroup>
1167
+ </template>
1168
+ </VanField>
1169
+
1170
+ <!-- 单选框-查询 -->
1171
+ <VanField
1172
+ v-if="attr.type === 'radio' && mode === '查询' && showItem"
1173
+ name="radio"
1174
+ :placeholder="attr.placeholder ? attr.placeholder : `请选择${attr.name}`"
1175
+ :label="labelData"
1176
+ :label-align="labelAlign"
1177
+ :input-align="attr.inputAlign ? attr.inputAlign : 'left'"
1178
+ :rules="[{ required: attr.rule.required === 'true', message: `请选择${attr.name}` }]"
1179
+ >
1180
+ <template #input>
1181
+ <XGridDropOption
1182
+ v-model="(modelData as string)"
1183
+ :column-num="labelData ? 3 : 4"
1184
+ :columns="option"
1185
+ />
1186
+ </template>
1187
+ </VanField>
1188
+
1189
+ <!-- 步进器 -->
1190
+ <VanField
1191
+ v-if="attr.type === 'stepper' && showItem"
1192
+ name="stepper"
1193
+ :label="labelData"
1194
+ :label-align="labelAlign"
1195
+ :input-align="attr.inputAlign ? attr.inputAlign : 'center'"
1196
+ :rules="[{ required: attr.rule.required === 'true', message: `请选择${attr.name}` }]"
1197
+ :required="attr.rule.required === 'true'"
1198
+ >
1199
+ <template #input>
1200
+ <VanStepper v-model="modelData as any" :disabled="readonly" />
1201
+ </template>
1202
+ </VanField>
1203
+
1204
+ <!-- 评分 -->
1205
+ <VanField
1206
+ v-if="attr.type === 'rate' && showItem"
1207
+ name="rate"
1208
+ :label="labelData"
1209
+ :label-align="labelAlign"
1210
+ :input-align="attr.inputAlign ? attr.inputAlign : 'center'"
1211
+ :rules="[{ required: attr.rule.required === 'true', message: `请选择${attr.name}` }]"
1212
+ :required="attr.rule.required === 'true'"
1213
+ >
1214
+ <template #input>
1215
+ <VanRate v-model="modelData as number" :size="25" :readonly="readonly" void-color="#eee" void-icon="star" color="#ffd21e" />
1216
+ </template>
1217
+ </VanField>
1218
+
1219
+ <!-- 滑块 -->
1220
+ <VanField
1221
+ v-if="attr.type === 'slider' && showItem"
1222
+ name="slider"
1223
+ :label="labelData"
1224
+ :label-align="labelAlign"
1225
+ :input-align="attr.inputAlign ? attr.inputAlign : 'left'"
1226
+ :rules="[{ required: attr.rule.required === 'true', message: `请选择${attr.name}` }]"
1227
+ :required="attr.rule.required === 'true'"
1228
+ >
1229
+ <template #input>
1230
+ <VanSlider v-model="modelData as number" :readonly="readonly" />
1231
+ </template>
1232
+ </VanField>
1233
+
1234
+ <!-- 文件上传 -->
1235
+ <!-- 图片上传, 手机端拍照 -->
1236
+ <VanField
1237
+ v-if="(attr.type === 'image' || attr.type === 'file') && showItem"
1238
+ name="image"
1239
+ :label="labelData"
1240
+ :label-align="labelAlign"
1241
+ :input-align="attr.inputAlign ? attr.inputAlign : 'left'"
1242
+ :rules="[{ required: attr.rule.required === 'true', message: `请选择${attr.name}` }]"
1243
+ :required="attr.rule.required === 'true'"
1244
+ >
1245
+ <template #input>
1246
+ <ImageUploader
1247
+ upload-mode="server"
1248
+ :image-list="(modelData as any[])"
1249
+ authority="admin"
1250
+ :attr="attr"
1251
+ :mode="props.mode"
1252
+ :readonly="readonly"
1253
+ :is-async-upload="isAsyncUpload"
1254
+ @update-file-list="updateFile"
1255
+ />
1256
+ </template>
1257
+ </VanField>
1258
+
1259
+ <!-- 选择器 琉璃中不存在,不进行维护后续将删除 -->
1260
+ <VanField
1261
+ v-if="attr.type === 'picker' && showItem"
1262
+ v-model="pickerValue"
1263
+ name="picker"
1264
+ :placeholder="attr.placeholder ? attr.placeholder : `请选择${attr.name}`"
1265
+ :label="labelData"
1266
+ :label-align="labelAlign"
1267
+ :input-align="attr.inputAlign ? attr.inputAlign : 'left'"
1268
+ readonly
1269
+ is-link
1270
+ :rules="[{ required: attr.rule.required === 'true', message: `请选择${attr.name}` }]"
1271
+ @click="readonly ? null : showPicker = true"
1272
+ />
1273
+ <VanPopup v-model:show="showPicker" round position="bottom" teleport="body" overlay-class="date-picker-overlay">
1274
+ <VanPicker
1275
+ v-model="(modelData as Numeric[])"
1276
+ :title="attr.name"
1277
+ :columns="attr.selectKey"
1278
+ :readonly="readonly"
1279
+ :columns-field-names="attr.customFieldName ? attr.customFieldName : { text: 'text', value: 'value', children: 'children' }"
1280
+ :confirm-button-text="attr.confirmButtonText || attr.confirmButtonText === '' ? attr.confirmButtonText : '确认'"
1281
+ :cancel-button-text="attr.cancelButtonText || attr.cancelButtonText === '' ? attr.cancelButtonText : '取消'"
1282
+ @cancel="showPicker = false"
1283
+ @confirm="onPickerConfirm"
1284
+ />
1285
+ </VanPopup>
1286
+
1287
+ <!-- 日历选择-查询 -->
1288
+ <VanField
1289
+ v-if="attr.type === 'rangePicker' && mode === '查询' && showItem"
1290
+ v-model="(pickerValue as string | number)"
1291
+ is-link
1292
+ readonly
1293
+ name="rangePicker"
1294
+ :label="labelData"
1295
+ :label-align="labelAlign"
1296
+ :input-align="attr.inputAlign ? attr.inputAlign : 'left'"
1297
+ :placeholder="attr.placeholder ? attr.placeholder : `请选择${attr.name}`"
1298
+ :rules="[{ required: attr.rule.required === 'true', message: `请选择${attr.name}` }]"
1299
+ @click="calendarShow = true"
1300
+ />
1301
+ <VanCalendar
1302
+ v-model:show="calendarShow"
1303
+ switch-mode="year-month"
1304
+ type="range"
1305
+ teleport="body"
1306
+ overlay-class="date-picker-overlay"
1307
+ :show-confirm="attr.showConfirm"
1308
+ @confirm="onCalendarConfirm"
1309
+ />
1310
+
1311
+ <!-- 日期选择-非查询 -->
1312
+ <VanField
1313
+ v-if="(attr.type === 'datePicker' || attr.type === 'rangePicker') && mode !== '查询' && showItem"
1314
+ v-model="(modelData as string | number)"
1315
+ name="datePicker"
1316
+ :label="labelData"
1317
+ :label-align="labelAlign"
1318
+ :input-align="attr.inputAlign ? attr.inputAlign : 'left'"
1319
+ readonly
1320
+ :is-link="true"
1321
+ :placeholder="placeholder"
1322
+ :rules="[{ required: attr.rule.required === 'true', message: `请选择${attr.name}` }]"
1323
+ :required="attr.rule.required === 'true'"
1324
+ @click="readonly ? null : showDataTimePicker()"
1325
+ />
1326
+ <VanPopup v-model:show="showDatePicker" position="bottom" teleport="body" overlay-class="date-picker-overlay">
1327
+ <VanPickerGroup
1328
+ :title="attr.name"
1329
+ :tabs="hasTime ? ['选择日期', '选择时间'] : ['选择日期']"
1330
+ next-step-text="下一步"
1331
+ :confirm-button-text="attr.confirmButtonText ? attr.confirmButtonText : '确认'"
1332
+ :cancel-button-text="attr.cancelButtonText ? attr.cancelButtonText : '取消'"
1333
+ @confirm="onDateTimePickerConfirm"
1334
+ @cancel="onPickerCancel"
1335
+ >
1336
+ <VanDatePicker
1337
+ v-model="dateTimePickerValue.date"
1338
+ :columns-type="attr.dateColumnsType || dateColumnsType"
1339
+ />
1340
+ <VanTimePicker
1341
+ v-if="hasTime"
1342
+ v-model="dateTimePickerValue.time"
1343
+ :columns-type="attr.timeColumnsType || timeColumnsType"
1344
+ :min-time="attr.minTime ? attr.minTime : '00:00:00'"
1345
+ :max-time="attr.maxTime ? attr.maxTime : '23:59:59'"
1346
+ />
1347
+ </VanPickerGroup>
1348
+ </VanPopup>
1349
+
1350
+ <!-- 日期选择-查询 -->
1351
+ <VanField
1352
+ v-if="attr.type === 'datePicker' && mode === '查询' && showItem"
1353
+ v-model="(modelData as string | number)"
1354
+ name="datePicker"
1355
+ :label="labelData"
1356
+ :label-align="labelAlign"
1357
+ :input-align="attr.inputAlign ? attr.inputAlign : 'left'"
1358
+ readonly
1359
+ :is-link="true"
1360
+ :placeholder="attr.placeholder ? attr.placeholder : `请选择${attr.name}`"
1361
+ :rules="[{ required: attr.rule.required === 'true', message: `请选择${attr.name}` }]"
1362
+ @click="readonly ? null : showDataTimePicker()"
1363
+ />
1364
+ <VanPopup v-model:show="showDatePicker" position="bottom" teleport="body" overlay-class="date-picker-overlay">
1365
+ <VanPickerGroup
1366
+ :title="attr.name"
1367
+ :tabs="hasTime ? ['选择日期', '选择时间'] : ['选择日期']"
1368
+ next-step-text="下一步"
1369
+ :confirm-button-text="attr.confirmButtonText ? attr.confirmButtonText : '确认'"
1370
+ :cancel-button-text="attr.cancelButtonText ? attr.cancelButtonText : '取消'"
1371
+ @confirm="onDateTimePickerConfirm"
1372
+ @cancel="onPickerCancel"
1373
+ >
1374
+ <VanDatePicker
1375
+ v-model="dateTimePickerValue.date"
1376
+ :columns-type="attr.dateColumnsType || dateColumnsType"
1377
+ />
1378
+ <VanTimePicker
1379
+ v-if="hasTime"
1380
+ v-model="dateTimePickerValue.time"
1381
+ :columns-type="attr.timeColumnsType || timeColumnsType"
1382
+ :min-time="attr.minTime ? attr.minTime : '00:00:00'"
1383
+ :max-time="attr.maxTime ? attr.maxTime : '23:59:59'"
1384
+ />
1385
+ </VanPickerGroup>
1386
+ </VanPopup>
1387
+
1388
+ <!-- 时间选择 --该配置未在pc找到不进行维护 后续将删除 -->
1389
+ <VanField
1390
+ v-if="attr.type === 'timePicker' && showItem"
1391
+ v-model="timePickerValue"
1392
+ name="timePicker"
1393
+ is-link
1394
+ readonly
1395
+ :placeholder="attr.placeholder"
1396
+ :label="labelData"
1397
+ :label-align="labelAlign"
1398
+ :input-align="attr.inputAlign ? attr.inputAlign : 'left'"
1399
+ :rules="[{ required: attr.rule.required === 'true', message: '请选择' }]"
1400
+ @click="readonly ? null : (showTimePicker = true)"
1401
+ />
1402
+ <VanPopup v-model:show="showTimePicker" position="bottom" teleport="body" overlay-class="date-picker-overlay">
1403
+ <VanTimePicker
1404
+ v-model="modelData as string[]"
1405
+ :title="attr.name"
1406
+ :columns-type="attr.columnsType ? attr.columnsType : ['hour', 'minute', 'second']"
1407
+ :min-time="attr.minTime ? attr.minTime : '00:00:00'"
1408
+ :max-time="attr.maxTime ? attr.maxTime : '23:59:59'"
1409
+ :readonly="readonly"
1410
+ @cancel="showTimePicker = false"
1411
+ @confirm="onTimePickerConfirm"
1412
+ />
1413
+ </VanPopup>
1414
+
1415
+ <!-- 省市区选择 -->
1416
+ <VanField
1417
+ v-if="(attr.type === 'area' || attr.type === 'citySelect') && showItem"
1418
+ v-model="area"
1419
+ name="area"
1420
+ :placeholder="attr.placeholder ? attr.placeholder : `请选择${attr.name}`"
1421
+ is-link
1422
+ readonly
1423
+ :label="labelData"
1424
+ :label-align="labelAlign"
1425
+ :input-align="attr.inputAlign ? attr.inputAlign : 'left'"
1426
+ :rules="[{ required: attr.rule.required === 'true', message: '请选择' }]"
1427
+ :required="attr.rule.required === 'true'"
1428
+ @click="readonly ? null : (showArea = true)"
1429
+ />
1430
+ <VanPopup v-model:show="showArea" position="bottom" teleport="body" overlay-class="date-picker-overlay">
1431
+ <VanArea
1432
+ v-model="modelData as string" :title="attr.name" :area-list="areaList"
1433
+ @confirm="onAreaConfirm"
1434
+ @cancel="showArea = false"
1435
+ />
1436
+ </VanPopup>
1437
+
1438
+ <!-- 单选下拉列表 -->
1439
+ <XSelect
1440
+ v-if="attr.type === 'select' && showItem"
1441
+ v-model="modelData"
1442
+ :label="labelData"
1443
+ :readonly="readonly"
1444
+ clearable
1445
+ :placeholder="placeholder"
1446
+ :columns="option"
1447
+ :option="attr.option ? attr.option : columnsField"
1448
+ :rules="[{ required: attr.rule.required === 'true', message: `请选择${attr.name}` }]"
1449
+ :required="attr.rule.required === 'true'"
1450
+ :lazy-load="attr.lazyLoad"
1451
+ :on-search="attr.lazyLoad ? handleLazySearch : null"
1452
+ />
1453
+
1454
+ <!-- 文本区域 -->
1455
+ <VanField
1456
+ v-if="attr.type === 'textarea' && showItem"
1457
+ v-model="(modelData as string)"
1458
+ rows="3"
1459
+ autosize
1460
+ :label="labelData"
1461
+ :label-align="labelAlign"
1462
+ :input-align="attr.inputAlign ? attr.inputAlign : 'left'"
1463
+ type="textarea"
1464
+ :readonly="readonly"
1465
+ :maxlength="attr.maxlength ? attr.maxlength : 200"
1466
+ :placeholder="attr.placeholder ? attr.placeholder : `请输入${attr.name}`"
1467
+ show-word-limit
1468
+ :rules="[{ required: attr.rule.required === 'true', message: `请填写${attr.name}` }]"
1469
+ :required="attr.rule.required === 'true'"
1470
+ />
1471
+
1472
+ <!-- 文本输入框 -->
1473
+ <VanField
1474
+ v-if="(attr.type === 'input' || attr.type === 'intervalPicker' || attr.type === 'scanCode' || attr.type === 'nfc') && showItem"
1475
+ v-model="(modelData as string)"
1476
+ style="align-items: center"
1477
+ :label="labelData"
1478
+ :label-align="labelAlign"
1479
+ :input-align="attr.inputAlign ? attr.inputAlign : 'left'"
1480
+ :type="attr.type as FieldType"
1481
+ :readonly="readonly"
1482
+ :disabled="attr.disabled"
1483
+ :placeholder="placeholder"
1484
+ :error-message="errorMessage"
1485
+ :clearable="attr.clearable"
1486
+ :rules="[{ required: attr.rule.required === 'true', message: `请填写${attr.name}` }]"
1487
+ :required="attr.rule.required === 'true'"
1488
+ @blur="() => formTypeCheck(attr, modelData as string)"
1489
+ >
1490
+ <template #input>
1491
+ <input
1492
+ :value="modelData"
1493
+ :readonly="readonly"
1494
+ class="van-field__control"
1495
+ :placeholder="placeholder"
1496
+ style="flex: 1; min-width: 0;"
1497
+ @input="e => modelData = (e.target as HTMLInputElement).value"
1498
+ @blur="() => formTypeCheck(attr, modelData as string)"
1499
+ >
1500
+ <VanButton
1501
+ v-if="(attr.type === 'input' || attr.type === 'intervalPicker') && !props.formReadonly && attr.inputOnAfterName && attr.inputOnAfterFunc && !attr.inputOnAfterName.includes('|')"
1502
+ class="action-btn"
1503
+ round
1504
+ type="primary"
1505
+ size="small"
1506
+ @click="emitFunc(attr.inputOnAfterFunc, attr)"
1507
+ >
1508
+ {{ attr.inputOnAfterName }}
1509
+ </VanButton>
1510
+ <VanButton
1511
+ v-if="(attr.type === 'scanCode' || attr.type === 'nfc') && !props.formReadonly"
1512
+ class="action-btn scan-nfc"
1513
+ round
1514
+ size="small"
1515
+ @click="scanCodeOrNfc(attr)"
1516
+ >
1517
+ {{ attr.type === 'scanCode' ? '扫码' : 'NFC' }}
1518
+ </VanButton>
1519
+ </template>
1520
+ </VanField>
1521
+
1522
+ <!-- 地址选择器 -->
1523
+ <VanField
1524
+ v-if="attr.type === 'addressSearch' && showItem"
1525
+ v-model="modelData as string"
1526
+ name="addressSearch"
1527
+ :label="labelData"
1528
+ :label-align="labelAlign"
1529
+ :input-align="attr.inputAlign ? attr.inputAlign : 'left'"
1530
+ readonly
1531
+ is-link
1532
+ :placeholder="placeholder"
1533
+ :rules="[{ required: attr.rule.required === 'true', message: '请选择地址' }]"
1534
+ :required="attr.rule.required === 'true'"
1535
+ @click="readonly ? null : (showAddressPicker = true)"
1536
+ />
1537
+ <VanPopup
1538
+ v-model:show="showAddressPicker"
1539
+ position="bottom"
1540
+ :style="{ height: '80vh' }"
1541
+ teleport="body"
1542
+ overlay-class="date-picker-overlay"
1543
+ >
1544
+ <XLocationPicker
1545
+ :default-center="defaultMapCenter"
1546
+ :service-name="serviceName"
1547
+ @confirm="handleAddressConfirm"
1548
+ />
1549
+ </VanPopup>
1550
+
1551
+ <!-- pc的树形选择框————》 手机端采用 Cascader 级联选择 -->
1552
+ <VanField
1553
+ v-if="attr.type === 'treeSelect' && showItem"
1554
+ v-model="treeValue"
1555
+ name="treeSelect"
1556
+ :label="labelData"
1557
+ :label-align="labelAlign"
1558
+ :input-align="attr.inputAlign ? attr.inputAlign : 'left'"
1559
+ readonly
1560
+ is-link
1561
+ :placeholder="attr.placeholder ? attr.placeholder : `请选择${attr.name}`"
1562
+ :rules="[{ required: attr.rule.required === 'true', message: `请选择${attr.name}` }]"
1563
+ :required="attr.rule.required === 'true'"
1564
+ @click="readonly ? null : (showTreeSelect = true)"
1565
+ />
1566
+ <VanPopup
1567
+ v-model:show="showTreeSelect"
1568
+ position="bottom"
1569
+ teleport="body"
1570
+ overlay-class="date-picker-overlay"
1571
+ >
1572
+ <VanCascader
1573
+ :options="cleanEmptyChildren(option, attr.customFieldName ? attr.customFieldName : { text: 'label', value: 'value', children: 'children' })"
1574
+ :field-names="attr.customFieldName ? attr.customFieldName : { text: 'label', value: 'value', children: 'children' }"
1575
+ :title="attr.name"
1576
+ :closeable="true"
1577
+ @close="showTreeSelect = false"
1578
+ @finish="onTreeSelectFinish"
1579
+ />
1580
+ </VanPopup>
1581
+ </div>
1582
+ </template>
1583
+
1584
+ <style scoped>
1585
+ .date-picker-overlay {
1586
+ background-color: rgba(0, 0, 0, 0.2); /* 设置为半透明的黑色 */
1587
+ }
1588
+ .action-btn {
1589
+ border-radius: 10px;
1590
+ margin-left: 8px;
1591
+ min-width: 4rem;
1592
+ max-width: 6rem;
1593
+ }
1594
+
1595
+ /* 扫码/NFC按钮特殊样式 */
1596
+ .action-btn.scan-nfc {
1597
+ background-color: transparent;
1598
+ border: 1px dashed #aca8a8;
1599
+ color: #1989fa;
1600
+ }
1601
+ </style>