调整预约客户选择和快捷新增

This commit is contained in:
2026-07-01 23:57:29 +08:00
parent 2d4af6feca
commit 339add72b2
3 changed files with 337 additions and 47 deletions
@@ -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'
})
})
})
@@ -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
}
+241 -47
View File
@@ -10,8 +10,14 @@
<label class="required">客户</label>
<div class="customer-control">
<button v-if="selectedCustomerReady" class="customer-selected-row" type="button" @click="openCustomerDropdown">
<span>{{ form.customerName || '未填写姓名' }}</span>
<strong>{{ form.phone || '未填写手机号' }}</strong>
<span class="customer-selected-name">
<span class="customer-avatar">
<img v-if="selectedCustomerDisplay.avatar" :src="selectedCustomerDisplay.avatar" alt="" />
<i v-else>{{ selectedCustomerDisplay.initial }}</i>
</span>
<b>{{ selectedCustomerDisplay.name }}</b>
</span>
<strong>{{ selectedCustomerDisplay.phone || '未填写手机号' }}</strong>
</button>
<input
v-else
@@ -27,18 +33,22 @@
<div v-if="customerSearching" class="customer-muted">正在搜索客户...</div>
<template v-else>
<div v-if="customerOptions.length > 0" class="customer-options-scroll">
<button v-for="customer in customerOptions" :key="String(customer.id)" class="customer-option" type="button" @click="selectCustomer(customer)">
<strong>{{ customer.nickname || customer.account || customer.phone || customer.contactPhone }}</strong>
<span>{{ customer.phone || customer.contactPhone || '未填写手机号' }}</span>
<button v-for="item in customerOptionItems" :key="item.display.id" class="customer-option" type="button" @click="selectCustomer(item.customer)">
<span class="customer-option-main">
<span class="customer-avatar customer-avatar--small">
<img v-if="item.display.avatar" :src="item.display.avatar" alt="" />
<i v-else>{{ item.display.initial }}</i>
</span>
<strong>{{ item.display.name }}</strong>
</span>
<span class="customer-option-phone">{{ item.display.phone || '未填写手机号' }}</span>
</button>
</div>
<div v-else class="customer-muted">暂无匹配客户,可继续创建新客户</div>
<div v-else class="customer-muted">暂无匹配客户</div>
</template>
<div class="customer-new">
<strong>新客户</strong>
<input v-model.trim="newCustomerName" placeholder="客户姓名" />
<input v-model.trim="newCustomerPhone" inputmode="numeric" placeholder="手机号" />
<button class="secondary-btn" type="button" @click="useNewCustomer">使用新客户</button>
<div class="customer-new-entry">
<span>没有找到客户?</span>
<button class="secondary-btn" type="button" @click="openCreateCustomerModal">新增客户</button>
</div>
</div>
</div>
@@ -129,6 +139,28 @@
</section>
</div>
</BaseModal>
<BaseModal
:model-value="createCustomerModalVisible"
title="新增客户"
:close-disabled="creatingCustomer"
:has-unsaved-changes="createCustomerChanged"
@update:model-value="updateCreateCustomerModalVisible"
>
<div class="customer-create-form">
<div class="field">
<label class="required">客户姓名</label>
<input v-model.trim="createCustomerForm.nickname" placeholder="请输入客户姓名" />
</div>
<div class="field">
<label class="required">手机号</label>
<input v-model.trim="createCustomerForm.phone" inputmode="numeric" placeholder="请输入手机号" />
</div>
<button class="primary-btn" type="button" :disabled="creatingCustomer" @click="createCustomer">
{{ creatingCustomer ? '新增中...' : '保存并选择' }}
</button>
</div>
</BaseModal>
</MobileShell>
</template>
@@ -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<AppointmentModalState>(session.userInfo, APPOINTMENT_MODAL_STATE_DRAFT_NAME)
const createCustomerDraft = readFormModalDraft<CustomerCreateForm>(session.userInfo, CUSTOMER_CREATE_DRAFT_NAME)
const form = reactive<AppointmentForm>({
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<number | null>(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<UserInfo | null>(null)
const createCustomerModalVisible = ref(Boolean(createCustomerDraft?.open))
const creatingCustomer = ref(false)
const createCustomerForm = reactive<CustomerCreateForm>({
nickname: createCustomerDraft?.form.nickname || '',
phone: createCustomerDraft?.form.phone || ''
})
const products = ref<ProductCard[]>([])
const filteredProducts = ref<ProductCard[]>([])
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<CustomerDisplay>(() => {
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<void> => {
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<void> => {
@@ -552,6 +672,11 @@ const checkAvailability = async (): Promise<{ ok: boolean; tip?: string }> => {
const submit = async (): Promise<void> => {
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)
</script>
<style scoped>
@@ -657,15 +788,25 @@ watch(timeSheetVisible, syncAppointmentModalState)
text-align: left;
}
.customer-selected-row span,
.customer-selected-name,
.customer-selected-row strong {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.customer-selected-row span {
.customer-selected-name {
display: flex;
flex: 1 1 auto;
align-items: center;
gap: 8px;
min-width: 0;
}
.customer-selected-name b {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
font-weight: 700;
}
@@ -675,6 +816,36 @@ watch(timeSheetVisible, syncAppointmentModalState)
font-weight: 700;
}
.customer-avatar {
display: grid;
flex: 0 0 auto;
width: 30px;
height: 30px;
place-items: center;
overflow: hidden;
border-radius: 50%;
background: var(--accent-soft);
color: var(--accent);
font-size: 13px;
font-weight: 800;
}
.customer-avatar--small {
width: 28px;
height: 28px;
font-size: 12px;
}
.customer-avatar img {
width: 100%;
height: 100%;
object-fit: cover;
}
.customer-avatar i {
font-style: normal;
}
.duration-control {
display: flex;
flex: 1;
@@ -731,7 +902,7 @@ watch(timeSheetVisible, syncAppointmentModalState)
.customer-options-scroll {
display: grid;
max-height: calc(44px * 5 + 8px * 4);
max-height: calc(48px * 5 + 8px * 4);
gap: 8px;
overflow-y: auto;
padding-right: 2px;
@@ -744,43 +915,66 @@ watch(timeSheetVisible, syncAppointmentModalState)
justify-content: space-between;
gap: 10px;
width: 100%;
min-height: 44px;
min-height: 48px;
padding: 9px 10px;
border-radius: 10px;
background: var(--subtle);
text-align: left;
}
.customer-option strong,
.customer-option span {
.customer-option-main,
.customer-option-phone {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.customer-option strong {
.customer-option-main {
display: flex;
flex: 1 1 auto;
align-items: center;
gap: 8px;
min-width: 0;
}
.customer-option span,
.customer-option-main strong {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.customer-option-phone,
.customer-muted {
color: var(--muted);
font-size: 12px;
}
.customer-option span {
.customer-option-phone {
flex: 0 0 auto;
max-width: 52%;
text-align: right;
}
.customer-new {
display: grid;
gap: 8px;
.customer-new-entry {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
padding-top: 8px;
border-top: 1px solid var(--border);
}
.customer-new-entry span {
color: var(--muted);
font-size: 12px;
}
.customer-create-form {
display: grid;
gap: 12px;
padding: 14px;
}
.picker-value {
justify-content: flex-start;
color: var(--ink);