diff --git a/apps/web-antd/src/api/license/customer/index.ts b/apps/web-antd/src/api/license/customer/index.ts index fe8a042..fa3319c 100644 --- a/apps/web-antd/src/api/license/customer/index.ts +++ b/apps/web-antd/src/api/license/customer/index.ts @@ -52,3 +52,15 @@ export function deleteCustomer(id: number) { export function exportCustomer(params: any) { return requestClient.download('/license/customer/export-excel', params); } + +/** 客户名称是否存在 */ +async function isCustomerNameExists( + name: string, + id?: CustomerApi.Customer['id'], +) { + return requestClient.get('/license/customer/name-exists', { + params: { id, name }, + }); +} + +export { isCustomerNameExists }; diff --git a/apps/web-antd/src/views/license/customer/data.ts b/apps/web-antd/src/views/license/customer/data.ts index 93f1436..f9a61a0 100644 --- a/apps/web-antd/src/views/license/customer/data.ts +++ b/apps/web-antd/src/views/license/customer/data.ts @@ -2,14 +2,19 @@ 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 { isCustomerNameExists } 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(); /** 新增/修改的表单 */ export function useFormSchema(): VbenFormSchema[] { return [ @@ -24,8 +29,25 @@ export function useFormSchema(): VbenFormSchema[] { { fieldName: 'name', label: $t('customer.customerName'), - rules: 'required', 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 isCustomerNameExists(value, formData.value?.id)); + }, + (value) => ({ + message: $t('ui.formRules.alreadyExists', [ + $t('customer.customerName'), + value, + ]), + }), + ), }, { fieldName: 'code', diff --git a/apps/web-antd/src/views/license/customer/modules/form.vue b/apps/web-antd/src/views/license/customer/modules/form.vue index 7aad623..c94c51f 100644 --- a/apps/web-antd/src/views/license/customer/modules/form.vue +++ b/apps/web-antd/src/views/license/customer/modules/form.vue @@ -1,7 +1,7 @@