| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- <script lang="ts" setup>
- import type { CustomerEntity } from '@vben/types';
- import { computed, ref } from 'vue';
- import { useVbenModal } from '@vben/common-ui';
- import { ElMessage } from 'element-plus';
- import { useVbenForm, z } from '#/adapter/form';
- import {
- addCustomerApi,
- editCustomerApi,
- getCustomerDetailApi,
- } from '#/api/user';
- const emit = defineEmits(['finish']);
- const data = ref();
- const formType = ref<'create' | 'detail' | 'edit'>('create');
- const titleMap = {
- create: '新增客户',
- detail: '客户详情',
- edit: '编辑客户',
- } as const;
- const getTitle = computed(() => titleMap[formType.value]);
- const [BaseForm, baseFormApi] = useVbenForm({
- showDefaultActions: false,
- // 所有表单项共用,可单独在表单内覆盖
- commonConfig: {
- labelWidth: 120,
- // 所有表单项
- componentProps: {
- class: 'w-full',
- },
- },
- wrapperClass: 'grid-cols-1 lg:grid-cols-2',
- schema: [
- // { title: '用户名称', field: 'usersname' },
- // { title: '用户性质', field: 'usersnature' },
- // { title: '用户类型', field: 'userstype' },
- // { title: '用户证件号码', field: 'usersidcardnumber' },
- // { title: '用户开户银行名称', field: 'usersbankname' },
- // { title: '用户开户银行账号', field: 'usersbanknumber' },
- // { title: '用户手机号', field: 'usersphone' },
- // { title: '用户邮箱', field: 'usersemail' },
- // { title: '用户地址', field: 'usersaddress' },
- // { title: '用户联系手机号', field: 'userscontactphone' },
- // { title: '用户联系邮箱', field: 'userscontactemail' },
- // { title: '用户联系地址', field: 'userscontactaddress' },
- {
- component: 'Input',
- fieldName: 'usersname',
- label: '用户名称',
- componentProps: {
- placeholder: '请输入用户名称',
- allowClear: true,
- },
- rules: z.string().min(1, { message: '请输入用户名称' }),
- },
- {
- component: 'Select',
- fieldName: 'usersnature',
- label: '用户性质',
- componentProps: {
- placeholder: '请输入用户性质',
- allowClear: true,
- options: [
- {
- label: '用户',
- value: '用户',
- },
- {
- label: '渠道',
- value: '渠道',
- },
- ],
- },
- rules: z.string().min(1, { message: '请输入用户性质' }),
- },
- {
- component: 'Select',
- fieldName: 'userstype',
- label: '用户类型',
- defaultValue: '个人',
- componentProps: {
- placeholder: '请输入用户类型',
- allowClear: true,
- options: [
- {
- label: '个人',
- value: '个人',
- },
- {
- label: '企业',
- value: '企业',
- },
- ],
- },
- rules: z.string().min(1, { message: '请输入用户类型' }),
- },
- {
- component: 'Input',
- fieldName: 'usersidcardnumber',
- label: '用户身份证号码',
- componentProps: {
- placeholder: '请输入用户身份证号码',
- allowClear: true,
- },
- rules: z.string().min(1, { message: '请输入用户身份证号码' }),
- dependencies: {
- if(values) {
- return values.userstype && values.userstype === '个人';
- },
- triggerFields: ['userstype'],
- },
- },
- {
- component: 'Input',
- fieldName: 'usersidcardnumber',
- label: '纳税人识别号',
- componentProps: {
- placeholder: '请输入纳税人识别号',
- allowClear: true,
- },
- rules: z.string().min(1, { message: '请输入纳税人识别号' }),
- dependencies: {
- if(values) {
- return values.userstype && values.userstype === '企业';
- },
- triggerFields: ['userstype'],
- },
- },
- {
- component: 'Input',
- fieldName: 'usersbankname',
- label: '用户开户银行名称',
- componentProps: {
- placeholder: '请输入用户开户银行名称',
- allowClear: true,
- },
- rules: z.string().min(1, { message: '请输入用户开户银行名称' }),
- },
- {
- component: 'Input',
- fieldName: 'usersbanknumber',
- label: '用户开户银行账号',
- componentProps: {
- placeholder: '请输入用户开户银行账号',
- allowClear: true,
- },
- rules: z.string().min(1, { message: '请输入用户开户银行账号' }),
- },
- {
- component: 'Input',
- fieldName: 'usersphone',
- label: '用户手机号',
- componentProps: {
- placeholder: '请输入用户手机号',
- allowClear: true,
- },
- rules: z.string().min(1, { message: '请输入用户手机号' }),
- },
- {
- component: 'Input',
- fieldName: 'usersemail',
- label: '用户邮箱',
- componentProps: {
- placeholder: '请输入用户邮箱',
- allowClear: true,
- },
- rules: z.string().min(1, { message: '请输入用户邮箱' }),
- },
- {
- component: 'Input',
- fieldName: 'usersaddress',
- label: '用户地址',
- componentProps: {
- placeholder: '请输入用户地址',
- allowClear: true,
- },
- rules: z.string().min(1, { message: '请输入用户地址' }),
- },
- {
- component: 'Input',
- fieldName: 'userscontactphone',
- label: '用户联系手机号',
- componentProps: {
- placeholder: '请输入用户联系手机号',
- allowClear: true,
- },
- rules: z.string().min(1, { message: '请输入用户联系手机号' }),
- },
- {
- component: 'Input',
- fieldName: 'userscontactemail',
- label: '用户联系邮箱',
- componentProps: {
- placeholder: '请输入用户联系邮箱',
- allowClear: true,
- },
- rules: z.string().min(1, { message: '请输入用户联系邮箱' }),
- },
- {
- component: 'Input',
- fieldName: 'userscontactaddress',
- label: '用户联系地址',
- componentProps: {
- placeholder: '请输入用户联系地址',
- allowClear: true,
- },
- rules: z.string().min(1, { message: '请输入用户联系地址' }),
- },
- ],
- });
- const [Modal, modalApi] = useVbenModal({
- class: 'w-7/12',
- onCancel() {
- modalApi.close();
- },
- async onConfirm() {
- // 校验输入的数据
- const validate = await baseFormApi.validate();
- if (!validate.valid) {
- return;
- }
- try {
- // 调用新增或编辑接口
- const apiMap = {
- create: () => addCustomerApi(validate.values as CustomerEntity),
- edit: () =>
- editCustomerApi({
- ...validate.values,
- 'usersid.value': data.value.row.usersid,
- } as any),
- };
- if (formType.value === 'detail') {
- modalApi.close();
- return;
- }
- await apiMap[formType.value]();
- ElMessage.success('操作成功');
- emit('finish');
- modalApi.close();
- } catch {}
- },
- async onOpenChange(isOpen) {
- if (isOpen) {
- data.value = modalApi.getData();
- formType.value = data.value.formType;
- baseFormApi.setState({
- commonConfig: { disabled: formType.value === 'detail' },
- });
- if (data.value.formType === 'create') {
- return;
- }
- try {
- modalApi.setState({ loading: true });
- const detailData = await getCustomerDetailApi({
- usersid: data.value.row.usersid,
- });
- baseFormApi.setValues(detailData);
- } catch {
- // console.log(error);
- }
- modalApi.setState({ loading: false });
- }
- },
- });
- </script>
- <template>
- <Modal :title="getTitle">
- <BaseForm />
- </Modal>
- </template>
|