liu
23 小时以前 5a322d24b59c4b70c08e792f21162be0f05d0199
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<script lang="ts" setup>
import type { ProblemFormType } from '../data';
 
import type { AftersalesProblemApi } from '#/api/aftersales/problem';
 
import { computed, ref } from 'vue';
 
import { useVbenModal } from '@vben/common-ui';
import { Button, message } from 'ant-design-vue';
 
import { useVbenForm } from '#/adapter/form';
import {
  createProblem,
  getProblem,
  updateProblem,
} from '#/api/aftersales/problem';
 
import { useProblemFormSchema } from '../data';
 
defineOptions({ name: 'AftersalesProblemForm' });
 
const emit = defineEmits(['success']);
 
const formType = ref<ProblemFormType>('create');
const id = ref<number>();
const submitting = ref(false);
 
const isEdit = computed(() => formType.value !== 'detail');
const title = computed(() => {
  if (formType.value === 'detail') return '查看售后问题';
  if (id.value) return '编辑售后问题';
  return '新增售后问题';
});
 
// 主表单
const [Form, formApi] = useVbenForm({
  commonConfig: { componentProps: { class: 'w-full' }, formItemClass: 'col-span-1', labelWidth: 100 },
  layout: 'horizontal',
  schema: [],
  showDefaultActions: false,
  wrapperClass: 'grid-cols-2',
});
 
/** 把问题对象映射成表单值(附件转为 FileUpload 结构) */
function problemToForm(p: AftersalesProblemApi.Problem): Record<string, unknown> {
  const form: Record<string, unknown> = { ...p };
  if (p.attachmentList?.length) {
    form.blobIds = p.attachmentList.map((item) => ({
      uid: String(item.id),
      name: item.name || '',
      url: item.url || '',
      status: 'done',
      id: item.id,
    }));
  }
  return form;
}
 
/** 把表单里的附件值规整成 blobId 数组 */
function normalizeBlobIds(value: unknown): number[] {
  const arr = Array.isArray(value) ? value : [];
  return arr
    .map((x) =>
      typeof x === 'number'
        ? x
        : x && typeof x === 'object' && 'id' in (x as Record<string, unknown>)
          ? Number((x as Record<string, unknown>).id)
          : Number(x),
    )
    .filter((n) => Number.isFinite(n));
}
 
// Modal
const [Modal, modalApi] = useVbenModal({
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      id.value = undefined;
      return;
    }
    const data = modalApi.getData<{ formType: ProblemFormType; id?: number }>();
    formType.value = data.formType;
    id.value = data.id;
    formApi.setState({ schema: useProblemFormSchema(formType.value) });
    formApi.setDisabled(formType.value === 'detail');
    if (data.id) {
      modalApi.lock();
      try {
        const detail = await getProblem(data.id);
        await formApi.setValues(problemToForm(detail));
      } finally {
        modalApi.unlock();
      }
    } else {
      await formApi.resetForm();
    }
  },
});
 
async function handleSubmit() {
  const { valid } = await formApi.validate();
  if (!valid) {
    return;
  }
  submitting.value = true;
  try {
    const data = (await formApi.getValues()) as Record<string, unknown>;
    const blobIds = normalizeBlobIds(data.blobIds);
    const payload = { ...data, blobIds } as AftersalesProblemApi.Problem;
    if (id.value) {
      await updateProblem({ ...payload, id: id.value });
    } else {
      await createProblem(payload);
    }
    message.success('操作成功');
    await modalApi.close();
    emit('success');
  } catch (e: unknown) {
    message.error((e as Error).message || '提交失败');
  } finally {
    submitting.value = false;
  }
}
</script>
 
<template>
  <Modal :title="title" class="w-2/3">
    <Form class="mx-4" />
 
    <template #footer>
      <div class="flex justify-end gap-2">
        <Button @click="modalApi.close()">取消</Button>
        <Button v-if="isEdit" type="primary" :loading="submitting" @click="handleSubmit">
          确认
        </Button>
        <Button v-else type="primary" @click="modalApi.close()">关闭</Button>
      </div>
    </template>
  </Modal>
</template>