Files
agt-web/apps/web-antd/src/views/license/customer/data.ts

284 lines
6.6 KiB
TypeScript

import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn, VxeTableGridOptions } from '#/adapter/vxe-table';
import type { CustomerApi } from '#/api/license/customer';
import { ref } from 'vue';
import { useAccess } from '@vben/access';
import { z } from '#/adapter/form';
import {
isCustomerCodeUnique,
isCustomerNameUnique,
} from '#/api/license/customer';
import { getAreaTree } from '#/api/system/area';
import { $t } from '#/locales';
import { DICT_TYPE, getDictOptions, getRangePickerDefaultProps } from '#/utils';
const { hasAccessByCodes } = useAccess();
export const formData = ref<CustomerApi.Customer>();
/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
return [
{
fieldName: 'id',
component: 'Input',
dependencies: {
triggerFields: [''],
show: () => false,
},
},
{
fieldName: 'name',
label: $t('customer.customerName'),
component: 'Input',
rules: z
.string()
.min(1, $t('ui.formRules.required', [$t('customer.customerName')]))
.max(
60,
$t('ui.formRules.maxLength', [$t('customer.customerName'), 60]),
)
.refine(
async (value: string) => {
return await isCustomerNameUnique(value, formData.value?.id);
},
(value) => ({
message: $t('ui.formRules.alreadyExists', [
$t('customer.customerName'),
value,
]),
}),
),
},
{
fieldName: 'code',
label: $t('customer.customerSn'),
component: 'InputNumber',
rules: z
.number({
message: $t('ui.formRules.required', [$t('customer.customerSn')]),
})
.min(
2000,
$t('ui.formRules.range', [$t('customer.customerSn'), 2000, 9999]),
)
.max(
9999,
$t('ui.formRules.range', [$t('customer.customerSn'), 2000, 9999]),
)
.refine(
async (value: number) => {
return await isCustomerCodeUnique(value, formData.value?.id);
},
(value) => ({
message: $t('ui.formRules.alreadyExists', [
$t('customer.customerSn'),
value,
]),
}),
)
// .nullable()
.refine(
(value: null | number) => {
return value;
},
{ message: $t('ui.formRules.required', [$t('customer.customerSn')]) },
),
},
{
fieldName: 'type',
label: $t('customer.customerType'),
rules: 'required',
component: 'Select',
componentProps: {
options: getDictOptions(DICT_TYPE.LIC_CUSTOMER_TYPE, 'number'),
},
},
{
fieldName: 'contacts',
label: $t('customer.contacts'),
rules: 'required',
component: 'Input',
},
{
fieldName: 'areaIds',
label: $t('customer.area'),
rules: 'required',
component: 'ApiCascader',
componentProps: {
api: getAreaTree,
expandTrigger: 'hover',
changeOnSelect: true,
fieldNames: {
label: 'name',
value: 'id',
children: 'children',
},
showSearch: true,
},
},
{
fieldName: 'role',
label: $t('customer.role'),
component: 'Input',
},
{
fieldName: 'phone',
label: $t('customer.phone'),
component: 'Input',
},
{
fieldName: 'email',
label: $t('customer.email'),
component: 'Input',
rules: z.string().email().or(z.literal('')).optional(),
},
{
fieldName: 'remark',
label: $t('customer.remarks'),
component: 'Textarea',
formItemClass: 'col-span-2',
},
];
}
/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
return [
{
fieldName: 'name',
label: $t('customer.customerName'),
component: 'Input',
componentProps: {
allowClear: true,
},
},
{
fieldName: 'type',
label: $t('customer.customerType'),
component: 'Select',
componentProps: {
allowClear: true,
options: getDictOptions(DICT_TYPE.LIC_CUSTOMER_TYPE, 'number'),
},
},
{
fieldName: 'code',
label: $t('customer.customerSn'),
component: 'Input',
componentProps: {
allowClear: true,
},
},
{
fieldName: 'contacts',
label: $t('customer.contacts'),
component: 'Input',
componentProps: {
allowClear: true,
},
},
{
fieldName: 'createTime',
label: $t('customer.creationTime'),
component: 'RangePicker',
componentProps: {
...getRangePickerDefaultProps(),
allowClear: true,
},
},
];
}
/** 列表的字段 */
export function useGridColumns(
onActionClick?: OnActionClickFn<CustomerApi.Customer>,
): VxeTableGridOptions<CustomerApi.Customer>['columns'] {
return [
{
field: 'name',
title: $t('customer.customerName'),
minWidth: 120,
},
{
field: 'code',
title: $t('customer.customerSn'),
minWidth: 120,
},
{
field: 'type',
title: $t('customer.customerType'),
minWidth: 120,
cellRender: {
name: 'CellDict',
props: { type: DICT_TYPE.LIC_CUSTOMER_TYPE },
},
},
{
field: 'contacts',
title: $t('customer.contacts'),
minWidth: 120,
},
{
field: 'areaNames',
title: $t('customer.area'),
minWidth: 120,
},
{
field: 'role',
title: $t('customer.role'),
minWidth: 120,
},
{
field: 'phone',
title: $t('customer.phone'),
minWidth: 120,
},
{
field: 'email',
title: $t('customer.email'),
minWidth: 120,
},
{
field: 'remark',
title: $t('customer.remarks'),
minWidth: 120,
},
{
field: 'createTime',
title: $t('customer.creationTime'),
minWidth: 120,
formatter: 'formatDateTime',
},
{
field: 'operation',
title: $t('customer.operation'),
minWidth: 200,
align: 'center',
fixed: 'right',
headerAlign: 'center',
showOverflow: false,
cellRender: {
attrs: {
nameField: 'name',
nameTitle: $t('customer.customer'),
onClick: onActionClick,
},
name: 'CellOperation',
options: [
{
code: 'edit',
show: hasAccessByCodes(['license:customer:update']),
},
{
code: 'delete',
show: hasAccessByCodes(['license:customer:delete']),
},
],
},
},
];
}