Files
JayuCloud/JY-Salon-Front/src/utils/customer-selection.ts
T

53 lines
1.6 KiB
TypeScript

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
}