af-mobile-client-vue3 1.2.18 → 1.2.20

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.
@@ -53,9 +53,9 @@ const props = withDefaults(defineProps<{
53
53
  submitButton: true,
54
54
  isHandleFormKey: true,
55
55
  })
56
- const emits = defineEmits(['onSubmit'])
56
+ const emits = defineEmits(['onSubmit', 'xFormItemEmitFunc'])
57
57
  const userStore = useUserStore()
58
- const formRef = ref<FormInstance>()
58
+ const xFormRef = ref<FormInstance>()
59
59
  const myFormItems = ref<FormItem[]>([])
60
60
  const rules = reactive({})
61
61
  const form = ref({})
@@ -203,21 +203,6 @@ function init(params) {
203
203
  }
204
204
 
205
205
  function setFormProps(form, item) {
206
- // 只有在字段未定义时,按类型赋有意义的默认值
207
- if (form.value[item.model] === undefined) {
208
- switch (item.rule?.type) {
209
- case 'number':
210
- case 'integer':
211
- case 'float':
212
- form.value[item.model] = 0
213
- break
214
- case 'string':
215
- form.value[item.model] = ''
216
- break
217
- default:
218
- form.value[item.model] = ''
219
- }
220
- }
221
206
  if (item.rule) {
222
207
  rules[item.model] = []
223
208
  let defaultValue
@@ -273,9 +258,9 @@ function getRealKey(key, mustHandleKey = false) {
273
258
  async function asyncSubmit() {
274
259
  return new Promise((resolve, reject) => {
275
260
  validate().then(async () => {
276
- const requestForm = prepareForm()
277
- await appendSilenceAddFields(requestForm)
278
- const realForm = handleFormKeys(requestForm)
261
+ const cleanedForm = prepareForm()
262
+ await appendSilenceAddFields(cleanedForm)
263
+ const realForm = handleFormKeys(cleanedForm)
279
264
  resolve({
280
265
  realForm,
281
266
  mode: props.mode,
@@ -364,21 +349,74 @@ async function appendSilenceAddFields(form) {
364
349
 
365
350
  function prepareForm() {
366
351
  const formObj = { ...form.value }
352
+ const cleanedForm = {}
353
+
367
354
  for (const key of Object.keys(formObj)) {
368
355
  const value = formObj[key]
369
- if (value === null || (typeof value === 'object' && Object.keys(value).length === 0)) {
370
- formObj[key] = undefined
356
+
357
+ // 跳过无效值
358
+ if (value === null || value === undefined || value === '') {
359
+ continue
360
+ }
361
+
362
+ // 处理数组
363
+ if (Array.isArray(value)) {
364
+ // 过滤掉空字符串、null、undefined
365
+ const filteredArray = value.filter(item =>
366
+ item !== null && item !== undefined && item !== '',
367
+ )
368
+ // 只有当数组不为空时才添加
369
+ if (filteredArray.length > 0) {
370
+ cleanedForm[key] = filteredArray
371
+ }
372
+ continue
373
+ }
374
+
375
+ // 处理对象
376
+ if (typeof value === 'object' && value !== null) {
377
+ // 检查对象是否为空
378
+ const objectKeys = Object.keys(value)
379
+ if (objectKeys.length === 0) {
380
+ continue
381
+ }
382
+
383
+ // 递归清理对象内部
384
+ const cleanedObject = {}
385
+ let hasValidValue = false
386
+
387
+ for (const objKey of objectKeys) {
388
+ const objValue = value[objKey]
389
+ if (objValue !== null && objValue !== undefined && objValue !== '') {
390
+ cleanedObject[objKey] = objValue
391
+ hasValidValue = true
392
+ }
393
+ }
394
+
395
+ // 只有当对象有有效值时才添加
396
+ if (hasValidValue) {
397
+ cleanedForm[key] = cleanedObject
398
+ }
399
+ continue
400
+ }
401
+
402
+ // 处理基本类型
403
+ if (value !== null && value !== undefined && value !== '') {
404
+ cleanedForm[key] = value
371
405
  }
372
406
  }
373
- return formObj
407
+
408
+ return cleanedForm
374
409
  }
375
410
 
376
411
  async function onSubmit() {
412
+ await validate()
413
+ // 清理表单数据
414
+ const cleanedForm = prepareForm()
377
415
  if (!props.configName && props.groupFormItems) {
378
416
  // 只有单表才可以成功,多表关联或者自定义sql不行
379
- const requestForm = prepareForm()
380
- await appendSilenceAddFields(requestForm)
381
- const realForm = handleFormKeys(requestForm)
417
+ await appendSilenceAddFields(cleanedForm)
418
+ const realForm = handleFormKeys(cleanedForm)
419
+
382
420
  try {
383
421
  addOrModifyEntity(realForm, tableName.value, props.serviceName || import.meta.env.VITE_APP_SYSTEM_NAME).then(() => {
384
422
  showSuccessToast('提交成功!!')
@@ -394,20 +432,26 @@ async function onSubmit() {
394
432
  }
395
433
  }
396
434
  else {
397
- emits('onSubmit', form.value)
435
+ // 使用清理后的数据
436
+ emits('onSubmit', cleanedForm)
398
437
  }
399
438
  }
400
439
  async function validate() {
401
- await formRef.value?.validate()
440
+ await xFormRef.value?.validate()
441
+ }
442
+ function emitFunc(func, data, value) {
443
+ emits(func, data, value)
444
+ emits('xFormItemEmitFunc', func, data, value)
402
445
  }
446
+
403
447
  watch(() => props.formData, (_val) => {
404
448
  form.value = _val
405
449
  })
406
- defineExpose({ init, form, formGroupName, validate, asyncSubmit })
450
+ defineExpose({ init, form, formGroupName, validate, asyncSubmit, setForm })
407
451
  </script>
408
452
 
409
453
  <template>
410
- <VanForm ref="formRef" class="x-form-container" @submit="onSubmit">
454
+ <VanForm ref="xFormRef" class="x-form-container" @submit="onSubmit">
411
455
  <div class="form-fields-scrollable">
412
456
  <VanCellGroup inset>
413
457
  <XFormItem
@@ -421,6 +465,7 @@ defineExpose({ init, form, formGroupName, validate, asyncSubmit })
421
465
  :service-name="myServiceName"
422
466
  :get-data-params="myGetDataParams"
423
467
  @set-form="setForm"
468
+ @x-form-item-emit-func="emitFunc"
424
469
  />
425
470
  <slot name="extraFormItem" />
426
471
  </VanCellGroup>