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

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
+7
View File
@@ -14,6 +14,13 @@ module.exports = {
})
},
matchIdentity(nickname, phone) {
return request({
url: '/manager/customers/matches',
params: { nickname, phone }
})
},
create(data) {
return request({
url: '/manager/customers',
@@ -7,13 +7,17 @@ global.wx = {
setStorageSync: () => {},
removeStorageSync: () => {},
showToast: () => {},
showModal: options => options.success({ confirm: modalConfirm }),
navigateTo: () => {}
}
global.getApp = () => ({ globalData: { userInfo: { type: 2, id: 1, shopId: 2 } } })
let modalConfirm = true
let definition
global.Page = value => { definition = value }
require('./index')
const customersApi = require('../../api/customers')
function createPage() {
return {
@@ -39,11 +43,30 @@ async function run() {
assert.strictEqual(page.data.customerSheetVisible, false)
page.data.form.customerUserId = 18
page.data.manualCustomerForm = { name: '新客户', phone: '13900139000' }
page.useManualCustomer()
page.onCustomerInput({ currentTarget: { dataset: { field: 'customerName' } }, detail: { value: '新客户' } })
assert.strictEqual(page.data.form.customerUserId, '')
assert.strictEqual(page.data.form.customerName, '新客户')
assert.strictEqual(page.data.form.phone, '13900139000')
assert.strictEqual(page.data.form.phone, '')
customersApi.matchIdentity = async () => []
customersApi.create = async payload => {
assert.strictEqual(payload.nickname, '新客户')
assert.strictEqual(payload.phone, undefined)
return '42'
}
assert.strictEqual(await page.ensureCustomerBeforeSubmit(), true)
assert.strictEqual(page.data.form.customerUserId, '42')
page.data.form.customerUserId = ''
page.data.form.customerName = '小周'
page.data.form.phone = '13800138000'
customersApi.matchIdentity = async (name, phone) => {
assert.strictEqual(name, '小周')
assert.strictEqual(phone, '13800138000')
return [{ id: '18', nickname: '小周', phone: '13800138000' }]
}
assert.strictEqual(await page.ensureCustomerBeforeSubmit(), true)
assert.strictEqual(page.data.form.customerUserId, '18')
page.data.artists = [{ id: 1, name: '小周' }, { id: 2, name: '小李' }]
page.onArtistSearch({ detail: { value: '李' } })
+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 = {
@@ -1,15 +1,13 @@
<view class="page no-tab">
<view class="card form-card">
<view class="field customer-field" bindtap="openCustomerSheet">
<text class="field-label required">客户</text>
<view class="reference-card {{form.customerName ? 'filled' : ''}}">
<view class="reference-copy">
<text class="reference-title">{{form.customerName || '请选择客户'}}</text>
<text class="reference-subtitle">{{form.phone || '搜索已有客户或手动录入'}}</text>
</view>
<text wx:if="{{form.customerUserId}}" class="reference-badge">已有</text>
<text class="reference-arrow">›</text>
<view class="field customer-field">
<text class="field-label required">客户姓名</text>
<view class="customer-entry">
<input class="customer-name-input" value="{{form.customerName}}" data-field="customerName" bindinput="onCustomerInput" placeholder="请输入客户姓名" />
<button class="customer-select-btn" catchtap="openCustomerSheet">选择已有客户</button>
</view>
<input class="customer-phone-input" value="{{form.phone}}" data-field="phone" bindinput="onCustomerInput" type="number" maxlength="11" placeholder="手机号(可选)" />
<text wx:if="{{form.customerUserId}}" class="customer-selected-tip">已关联系统客户</text>
</view>
<view class="field">
<text class="field-label required">预约日期</text>
@@ -76,12 +74,10 @@
selected-id="{{form.customerUserId}}"
loading="{{customerLoading}}"
error-text="{{customerError}}"
empty-text="暂无匹配客户,可手动录入"
action-text="手动录入客户"
empty-text="暂无匹配客户"
bind:search="onCustomerSearch"
bind:select="selectCustomer"
bind:retry="retryCustomerSearch"
bind:action="openManualCustomer"
bind:close="closeCustomerSheet"
/>
@@ -103,29 +99,6 @@
bind:close="closeArtistSheet"
/>
<view wx:if="{{manualCustomerVisible}}" class="modal-mask" bindtap="closeManualCustomer" catchtouchmove="stopTouchMove">
<view class="manual-customer-sheet" catchtap="stopTap">
<view class="modal-head">
<view>
<text class="modal-title">手动录入客户</text>
<text class="modal-subtitle">录入后将直接带回本次预约</text>
</view>
<view class="modal-close" bindtap="closeManualCustomer">×</view>
</view>
<view class="manual-customer-form">
<view class="manual-customer-field">
<text class="required-label">客户姓名</text>
<input value="{{manualCustomerForm.name}}" data-field="name" bindinput="onManualCustomerInput" placeholder="请输入客户姓名" />
</view>
<view class="manual-customer-field">
<text class="required-label">手机号</text>
<input value="{{manualCustomerForm.phone}}" data-field="phone" bindinput="onManualCustomerInput" type="number" maxlength="11" placeholder="请输入手机号" />
</view>
</view>
<button class="primary-btn manual-customer-submit" bindtap="useManualCustomer">使用该客户</button>
</view>
</view>
<product-selector-sheet visible="{{productModalVisible}}" mode="{{productSelectorMode}}" title="选择预约作品" keyword="{{productKeyword}}" placeholder="搜索作品、标签、美甲师" items="{{filteredProducts}}" selected-id="{{form.productId}}" loading="{{loadingProducts}}" error-text="{{productError}}" empty-text="暂无匹配作品" manual-form="{{manualProductDraft}}" manual-more-expanded="{{manualProductMoreExpanded}}" manual-image-url="{{manualProductDraft.imageUrl}}" manual-image-uploading="{{manualImageUploading}}" bind:search="onProductSearch" bind:select="selectProduct" bind:openmanual="openManualProduct" bind:backtolist="backToProductList" bind:manualinput="onManualProductInput" bind:togglemore="toggleManualProductMore" bind:chooseimage="chooseManualProductImage" bind:removeimage="removeManualProductImage" bind:confirmmanual="useManualProduct" bind:retry="loadOptions" bind:close="closeProductModal" />
<view wx:if="{{timeSheetVisible}}" class="modal-mask" bindtap="closeTimeSheet">
@@ -4,6 +4,7 @@
}
.customer-field {
display: block;
align-items: stretch;
padding: 18rpx 0;
}
@@ -13,6 +14,58 @@
align-items: center;
}
.customer-entry {
display: flex;
align-items: center;
gap: 14rpx;
width: 100%;
}
.customer-name-input {
min-width: 0;
flex: 1;
height: 78rpx;
padding: 0 18rpx;
border: 1rpx solid #e7e0d7;
border-radius: 18rpx;
background: #f8f4ee;
color: #29241f;
font-size: 27rpx;
}
.customer-select-btn {
flex: 0 0 auto;
min-width: 176rpx;
height: 78rpx;
margin: 0;
padding: 0 18rpx;
border: 1rpx solid #d7b38e;
border-radius: 18rpx;
background: #fff7ed;
color: #9f5f2d;
font-size: 23rpx;
line-height: 78rpx;
}
.customer-phone-input {
width: 100%;
height: 66rpx;
margin-top: 14rpx;
padding: 0 18rpx;
border: 1rpx solid #e7e0d7;
border-radius: 16rpx;
background: #fff;
color: #29241f;
font-size: 25rpx;
}
.customer-selected-tip {
display: block;
margin-top: 10rpx;
color: #55786d;
font-size: 21rpx;
}
.reference-card,
.reference-value {
display: flex;
@@ -215,49 +268,6 @@
background: rgba(18, 15, 12, 0.48);
}
.manual-customer-sheet {
display: flex;
width: 100%;
height: 88vh;
max-height: 88vh;
flex-direction: column;
padding: 28rpx 28rpx calc(28rpx + env(safe-area-inset-bottom));
border-radius: 34rpx 34rpx 0 0;
background: #fffaf4;
box-shadow: 0 -18rpx 40rpx rgba(41, 31, 23, 0.16);
}
.manual-customer-form {
flex: 1;
padding-top: 12rpx;
}
.manual-customer-field {
margin-bottom: 20rpx;
padding: 20rpx 22rpx;
border: 1rpx solid #e7e0d7;
border-radius: 20rpx;
background: #fff;
}
.manual-customer-field text {
display: block;
margin-bottom: 10rpx;
color: #625c55;
font-size: 24rpx;
font-weight: 800;
}
.manual-customer-field input {
height: 64rpx;
color: #29241f;
font-size: 28rpx;
}
.manual-customer-submit {
flex: 0 0 auto;
}
.time-modal {
width: 100%;
max-height: 82vh;