优化预约客户输入与唯一性校验

This commit is contained in:
2026-10-16 17:33:58 +08:00
parent 3d18a1972b
commit 4a7bd90088
12 changed files with 318 additions and 164 deletions
+95 -43
View File
@@ -295,8 +295,6 @@ Page({
customerKeyword: '',
customerLoading: false,
customerError: '',
manualCustomerVisible: false,
manualCustomerForm: { name: '', phone: '' },
products: [],
filteredProducts: [],
artists: [],
@@ -434,6 +432,20 @@ Page({
}
},
onCustomerInput(event) {
const field = event.currentTarget.dataset.field
const patch = {
[`form.${field}`]: event.detail.value,
'form.customerUserId': ''
}
if (field === 'customerName' && this.data.form.customerUserId) {
patch['form.phone'] = ''
}
this.setData({
...patch
})
},
openCustomerSheet() {
this.setData({ customerSheetVisible: true })
this.searchCustomers(this.data.customerKeyword)
@@ -491,45 +503,6 @@ Page({
})
},
openManualCustomer() {
this.setData({
customerSheetVisible: false,
manualCustomerVisible: true,
manualCustomerForm: {
name: this.data.form.customerName || '',
phone: this.data.form.phone || ''
}
})
},
closeManualCustomer() {
this.setData({ manualCustomerVisible: false })
},
onManualCustomerInput(event) {
this.setData({ [`manualCustomerForm.${event.currentTarget.dataset.field}`]: event.detail.value })
},
useManualCustomer() {
const name = String(this.data.manualCustomerForm.name || '').trim()
const phone = String(this.data.manualCustomerForm.phone || '').trim()
if (!name) {
wx.showToast({ title: '请输入客户姓名', icon: 'none' })
return
}
if (!/^1[3-9]\d{9}$/.test(phone)) {
wx.showToast({ title: '请输入正确手机号', icon: 'none' })
return
}
this.setData({
'form.customerUserId': '',
'form.customerName': name,
'form.phone': phone,
manualCustomerVisible: false,
customerKeyword: ''
})
},
onUnload() {
if (this._customerTimer) {
clearTimeout(this._customerTimer)
@@ -949,7 +922,83 @@ Page({
}
},
async ensureCustomerBeforeSubmit() {
const name = String(this.data.form.customerName || '').trim()
const phone = String(this.data.form.phone || '').trim()
if (!name) {
wx.showToast({ title: '请输入客户姓名', icon: 'none' })
return false
}
if (phone && !/^1[3-9]\d{9}$/.test(phone)) {
wx.showToast({ title: '请输入正确手机号', icon: 'none' })
return false
}
if (this.data.form.customerUserId) {
return true
}
this.setData({ loading: true })
try {
const matches = await customersApi.matchIdentity(name, phone || undefined)
if (matches.length > 1) {
this.setData({
customerOptions: matches.map(normalizeCustomer).map(buildCustomerSelectorItem),
customerKeyword: name,
customerSheetVisible: true
})
return false
}
if (matches.length > 0) {
const customer = normalizeCustomer(matches[0])
const choice = await new Promise(resolve => {
wx.showModal({
title: '客户已存在',
content: `系统中已有“${name}”${phone ? `(${phone})` : ''},是否使用该客户?`,
cancelText: '取消',
confirmText: '使用已有',
success: resolve,
fail: () => resolve({ confirm: false })
})
})
if (!choice.confirm) {
return false
}
this.setData({
'form.customerUserId': customer.id,
'form.customerName': customer.name,
'form.phone': customer.phone
})
return true
}
const createChoice = await new Promise(resolve => {
wx.showModal({
title: '新建客户',
content: '系统中没有匹配客户,是否新建到客户档案?',
cancelText: '不新建',
confirmText: '确认',
success: resolve,
fail: () => resolve({ confirm: false })
})
})
if (!createChoice.confirm) {
return false
}
const customerId = await customersApi.create({ nickname: name, phone: phone || undefined })
this.setData({ 'form.customerUserId': customerId })
return true
} catch (error) {
wx.showToast({ title: error.message || '客户校验失败,请稍后重试', icon: 'none' })
return false
} finally {
this.setData({ loading: false })
}
},
async submit() {
if (this.data.loading) {
return
}
if (!this.data.canCreateAppointment) {
wx.showToast({ title: '当前账号仅支持查看预约', icon: 'none' })
return
@@ -957,11 +1006,11 @@ Page({
const form = this.data.form
const hasManualProduct = String(form.manualProductTitle || '').trim() && String(form.manualProductPrice || '').trim()
const durationMinutes = normalizeDuration(form.durationMinutes)
if (!form.customerName || !form.phone || (!form.productId && !hasManualProduct) || !form.appointmentDate || !form.appointmentTime) {
if (!form.customerName || (!form.productId && !hasManualProduct) || !form.appointmentDate || !form.appointmentTime) {
wx.showToast({ title: '请补全预约信息', icon: 'none' })
return
}
if (!/^1[3-9]\d{9}$/.test(String(form.phone).trim())) {
if (form.phone && !/^1[3-9]\d{9}$/.test(String(form.phone).trim())) {
wx.showToast({ title: '请输入正确手机号', icon: 'none' })
return
}
@@ -987,6 +1036,9 @@ Page({
})
if (!result.confirm) return
}
if (!(await this.ensureCustomerBeforeSubmit())) {
return
}
this.setData({ loading: true })
try {
const payload = {