diff --git a/JY-Salon-Front/src/utils/customer-selection.test.ts b/JY-Salon-Front/src/utils/customer-selection.test.ts new file mode 100644 index 00000000..bc27931b --- /dev/null +++ b/JY-Salon-Front/src/utils/customer-selection.test.ts @@ -0,0 +1,44 @@ +import { describe, expect, it } from 'vitest' +import type { UserInfo } from '@/types/common' +import { + buildCustomerCreateDraft, + resolveCustomerDisplay, + validateAppointmentCustomerSelection +} from '@/utils/customer-selection' + +describe('customer selection helpers', () => { + it('requires a selected customer id before appointment submit', () => { + expect(validateAppointmentCustomerSelection('', '王女士', '13800138000')).toBe('请先选择客户或新增客户') + expect(validateAppointmentCustomerSelection('42', '', '13800138000')).toBe('请补全客户姓名和手机号') + expect(validateAppointmentCustomerSelection('42', '王女士', '13800138000')).toBeNull() + }) + + it('resolves selected customer display data from existing user', () => { + const customer: UserInfo = { + id: 42, + nickname: '王女士', + phone: '13800138000', + avatar: 'https://img.test/avatar.jpg' + } + + expect(resolveCustomerDisplay(customer)).toEqual({ + id: '42', + name: '王女士', + phone: '13800138000', + avatar: 'https://img.test/avatar.jpg', + initial: '王', + searchText: '王女士 13800138000' + }) + }) + + it('prefills quick create form from search keyword', () => { + expect(buildCustomerCreateDraft('王女士 13800138000')).toEqual({ + nickname: '王女士', + phone: '13800138000' + }) + expect(buildCustomerCreateDraft('13800138000')).toEqual({ + nickname: '', + phone: '13800138000' + }) + }) +}) diff --git a/JY-Salon-Front/src/utils/customer-selection.ts b/JY-Salon-Front/src/utils/customer-selection.ts new file mode 100644 index 00000000..9ccbdfb5 --- /dev/null +++ b/JY-Salon-Front/src/utils/customer-selection.ts @@ -0,0 +1,52 @@ +import type { UserInfo } from '@/types/common' + +export interface CustomerCreateDraft { + nickname: string + phone: string +} + +export interface CustomerDisplay { + id: string + name: string + phone: string + avatar: string + initial: string + searchText: string +} + +const PHONE_PATTERN = /1[3-9]\d{9}/ + +export const buildCustomerSearchText = (name: string, phone: string): string => { + return [name, phone].map(value => value.trim()).filter(Boolean).join(' ') +} + +export const buildCustomerCreateDraft = (keyword: string): CustomerCreateDraft => { + const normalizedKeyword = keyword.trim() + const phone = normalizedKeyword.match(PHONE_PATTERN)?.[0] || '' + const nickname = phone ? normalizedKeyword.replace(phone, '').trim() : normalizedKeyword + return { nickname, phone } +} + +export const resolveCustomerDisplay = (customer: UserInfo): CustomerDisplay => { + const name = String(customer.nickname || customer.account || customer.phone || customer.contactPhone || '未命名客户') + const phone = String(customer.phone || customer.contactPhone || '') + const avatar = String(customer.avatar || '') + return { + id: String(customer.id), + name, + phone, + avatar, + initial: name.slice(0, 1) || '客', + searchText: buildCustomerSearchText(name, phone) + } +} + +export const validateAppointmentCustomerSelection = (customerUserId: string, customerName: string, phone: string): string | null => { + if (!customerUserId.trim()) { + return '请先选择客户或新增客户' + } + if (!customerName.trim() || !phone.trim()) { + return '请补全客户姓名和手机号' + } + return null +} diff --git a/JY-Salon-Front/src/views/AppointmentFormView.vue b/JY-Salon-Front/src/views/AppointmentFormView.vue index 4e9c1587..dbd99c1c 100644 --- a/JY-Salon-Front/src/views/AppointmentFormView.vue +++ b/JY-Salon-Front/src/views/AppointmentFormView.vue @@ -10,8 +10,14 @@
正在搜索客户...
-
- 新客户 - - - +
+ 没有找到客户? +
@@ -129,6 +139,28 @@ + + +
+
+ + +
+
+ + +
+ +
+
@@ -151,6 +183,13 @@ import { useSessionStore } from '@/stores/session' import { useToastStore } from '@/stores/toast' import { formatDate, minutesOfDay, timeFromMinutes } from '@/utils/date' import { buildAvailableTimeGroups, type AvailableTimeGroup } from '@/utils/available-times' +import { + buildCustomerCreateDraft, + buildCustomerSearchText, + resolveCustomerDisplay, + validateAppointmentCustomerSelection, + type CustomerDisplay +} from '@/utils/customer-selection' import { buildClientRequestId, normalizeDuration } from '@/utils/planning' import { findArtistByUserId, normalizeArtistSelectionId, resolveArtistUserId } from '@/utils/artist' import { readDraftForm, removeDraftForm, writeDraftForm } from '@/utils/appointment-draft' @@ -170,7 +209,14 @@ interface AppointmentModalState { timeSheetVisible: boolean } +interface CustomerCreateForm { + nickname: string + phone: string +} + const APPOINTMENT_MODAL_STATE_DRAFT_NAME = 'appointment-modal-state' +const CUSTOMER_CREATE_DRAFT_NAME = 'appointment-customer-create' +const CUSTOMER_PHONE_PATTERN = /^1[3-9]\d{9}$/ const route = useRoute() const router = useRouter() @@ -178,8 +224,9 @@ const session = useSessionStore() const toast = useToastStore() const baseForm = readDraftForm(session.userInfo) const modalStateDraft = readFormModalDraft(session.userInfo, APPOINTMENT_MODAL_STATE_DRAFT_NAME) +const createCustomerDraft = readFormModalDraft(session.userInfo, CUSTOMER_CREATE_DRAFT_NAME) const form = reactive({ - customerUserId: '', + customerUserId: String(route.query.customerUserId || baseForm?.customerUserId || ''), customerName: String(route.query.customerName || baseForm?.customerName || ''), phone: String(route.query.phone || baseForm?.phone || ''), appointmentDate: String(route.query.date || baseForm?.appointmentDate || formatDate(new Date())), @@ -203,8 +250,13 @@ const customerTimer = ref(null) const customerSearchText = ref([form.customerName, form.phone].filter(Boolean).join(' ')) const customerDropdownOpen = ref(false) const customerSearching = ref(false) -const newCustomerName = ref(form.customerName) -const newCustomerPhone = ref(form.phone) +const selectedCustomer = ref(null) +const createCustomerModalVisible = ref(Boolean(createCustomerDraft?.open)) +const creatingCustomer = ref(false) +const createCustomerForm = reactive({ + nickname: createCustomerDraft?.form.nickname || '', + phone: createCustomerDraft?.form.phone || '' +}) const products = ref([]) const filteredProducts = ref([]) const productKeyword = ref('') @@ -220,7 +272,28 @@ const conflictLevel = ref<'free' | 'danger'>('free') const loading = ref(false) const manualImageUploading = ref(false) const selectedCustomerReady = computed(() => { - return Boolean(form.customerName || form.phone) + return Boolean(form.customerUserId) +}) +const customerOptionItems = computed(() => { + return customerOptions.value.map(customer => ({ customer, display: resolveCustomerDisplay(customer) })) +}) +const selectedCustomerDisplay = computed(() => { + if (selectedCustomer.value) { + return resolveCustomerDisplay(selectedCustomer.value) + } + const name = form.customerName || '未填写姓名' + const phone = form.phone || '' + return { + id: String(form.customerUserId || ''), + name, + phone, + avatar: '', + initial: name.slice(0, 1) || '客', + searchText: buildCustomerSearchText(name, phone) + } +}) +const createCustomerChanged = computed(() => { + return Boolean(createCustomerForm.nickname || createCustomerForm.phone) }) const manualProductChanged = computed(() => { return Boolean( @@ -286,6 +359,39 @@ const updateTimeSheetVisible = (value: boolean): void => { syncAppointmentModalState() } +const snapshotCreateCustomerForm = (): CustomerCreateForm => ({ + nickname: createCustomerForm.nickname, + phone: createCustomerForm.phone +}) + +const applyCreateCustomerForm = (value: CustomerCreateForm): void => { + createCustomerForm.nickname = value.nickname || '' + createCustomerForm.phone = value.phone || '' +} + +const resetCreateCustomerForm = (): void => { + applyCreateCustomerForm({ nickname: '', phone: '' }) +} + +const syncCreateCustomerDraft = (): void => { + if (!createCustomerModalVisible.value) { + removeFormModalDraft(session.userInfo, CUSTOMER_CREATE_DRAFT_NAME) + return + } + if (!createCustomerChanged.value) { + return + } + writeFormModalDraft(session.userInfo, CUSTOMER_CREATE_DRAFT_NAME, snapshotCreateCustomerForm(), true) +} + +const updateCreateCustomerModalVisible = (value: boolean): void => { + createCustomerModalVisible.value = value + if (!value) { + removeFormModalDraft(session.userInfo, CUSTOMER_CREATE_DRAFT_NAME) + resetCreateCustomerForm() + } +} + const selectedProductTitle = computed(() => { if (selectedProduct.value) { return selectedProduct.value.title @@ -395,7 +501,7 @@ const onCustomerSearchInput = (): void => { form.customerUserId = '' form.customerName = '' form.phone = '' - newCustomerName.value = customerSearchText.value + selectedCustomer.value = null customerDropdownOpen.value = true scheduleCustomerSearch() } @@ -405,36 +511,50 @@ const clearCustomerSelection = (): void => { form.customerName = '' form.phone = '' customerSearchText.value = '' - newCustomerName.value = '' - newCustomerPhone.value = '' + selectedCustomer.value = null customerOptions.value = [] customerDropdownOpen.value = true } const selectCustomer = (customer: UserInfo): void => { - form.customerUserId = String(customer.id) - form.customerName = customer.nickname || customer.account || '' - form.phone = customer.phone || customer.contactPhone || '' - customerSearchText.value = [form.customerName, form.phone].filter(Boolean).join(' ') - newCustomerName.value = form.customerName - newCustomerPhone.value = form.phone + const display = resolveCustomerDisplay(customer) + selectedCustomer.value = customer + form.customerUserId = display.id + form.customerName = display.name + form.phone = display.phone + customerSearchText.value = display.searchText customerOptions.value = [] customerDropdownOpen.value = false } -const useNewCustomer = (): void => { - const name = newCustomerName.value.trim() - const phone = newCustomerPhone.value.trim() - if (!name || !phone) { - toast.error('请填写新客户姓名和手机号') +const openCreateCustomerModal = (): void => { + applyCreateCustomerForm(buildCustomerCreateDraft(customerSearchText.value)) + customerDropdownOpen.value = false + createCustomerModalVisible.value = true + syncCreateCustomerDraft() +} + +const createCustomer = async (): Promise => { + const nickname = createCustomerForm.nickname.trim() + const phone = createCustomerForm.phone.trim() + if (!nickname || !phone) { + toast.error('请填写客户姓名和手机号') return } - form.customerUserId = '' - form.customerName = name - form.phone = phone - customerSearchText.value = [name, phone].filter(Boolean).join(' ') - customerOptions.value = [] - customerDropdownOpen.value = false + if (!CUSTOMER_PHONE_PATTERN.test(phone)) { + toast.error('手机号格式不正确') + return + } + creatingCustomer.value = true + try { + const customerId = await managerCustomersApi.create({ nickname, phone }) + const customer = await managerCustomersApi.detail(customerId) + toast.success('客户已新增') + updateCreateCustomerModalVisible(false) + selectCustomer(customer) + } finally { + creatingCustomer.value = false + } } const onDateChange = async (): Promise => { @@ -552,6 +672,11 @@ const checkAvailability = async (): Promise<{ ok: boolean; tip?: string }> => { const submit = async (): Promise => { const hasManualProduct = form.manualProductTitle.trim() && form.manualProductPrice.trim() + const customerSelectionError = validateAppointmentCustomerSelection(form.customerUserId, form.customerName, form.phone) + if (customerSelectionError) { + toast.error(customerSelectionError) + return + } if (!form.customerName || !form.phone || (!form.productId && !hasManualProduct) || !form.artistId || !form.appointmentTime) { toast.error('请补全预约信息') return @@ -603,6 +728,10 @@ const clearDraft = (): void => { onMounted(async () => { await loadOptions() + if (form.customerUserId) { + const customer = await managerCustomersApi.detail(form.customerUserId) + selectCustomer(customer) + } await searchCustomers() await refreshAvailableTimes() }) @@ -610,6 +739,8 @@ onMounted(async () => { watch(form, syncAppointmentDraft, { deep: true }) watch(productModalVisible, syncAppointmentModalState) watch(timeSheetVisible, syncAppointmentModalState) +watch(createCustomerForm, syncCreateCustomerDraft, { deep: true }) +watch(createCustomerModalVisible, syncCreateCustomerDraft)