优化:统一小程序业务引用选择交互
This commit is contained in:
@@ -231,6 +231,25 @@ function normalizeCustomer(customer) {
|
||||
}
|
||||
}
|
||||
|
||||
function buildCustomerSelectorItem(customer) {
|
||||
return {
|
||||
...customer,
|
||||
title: customer.displayName,
|
||||
subtitle: customer.displayPhone,
|
||||
badge: '已有客户'
|
||||
}
|
||||
}
|
||||
|
||||
function buildArtistSelectorItem(artist) {
|
||||
return {
|
||||
...artist,
|
||||
title: artist.name || '未命名员工',
|
||||
subtitle: artist.statusText || '可用于指定预约',
|
||||
meta: artist.workStartTime && artist.workEndTime ? `${artist.workStartTime.slice(0, 5)}-${artist.workEndTime.slice(0, 5)}` : '',
|
||||
avatar: artist.avatar || artist.avatarUrl || '/assets/images/default-user-avatar.png'
|
||||
}
|
||||
}
|
||||
|
||||
Page({
|
||||
data: {
|
||||
form: {
|
||||
@@ -253,12 +272,19 @@ Page({
|
||||
remark: ''
|
||||
},
|
||||
customerOptions: [],
|
||||
customerDropdownVisible: false,
|
||||
customerSheetVisible: false,
|
||||
customerKeyword: '',
|
||||
customerLoading: false,
|
||||
customerError: '',
|
||||
manualCustomerVisible: false,
|
||||
manualCustomerForm: { name: '', phone: '' },
|
||||
products: [],
|
||||
filteredProducts: [],
|
||||
artists: [],
|
||||
artistSelectorItems: [],
|
||||
artistSheetVisible: false,
|
||||
artistKeyword: '',
|
||||
artistError: '',
|
||||
selectedProduct: {},
|
||||
selectedArtist: {},
|
||||
productModalVisible: false,
|
||||
@@ -298,7 +324,6 @@ Page({
|
||||
return
|
||||
}
|
||||
this.loadOptions()
|
||||
this.searchCustomers()
|
||||
},
|
||||
|
||||
async loadOptions() {
|
||||
@@ -319,21 +344,37 @@ Page({
|
||||
this.checkAvailability()
|
||||
},
|
||||
|
||||
async loadArtists() {
|
||||
async loadArtists(preferredArtistId, fallbackEmployee) {
|
||||
try {
|
||||
const calendar = await planningApi.calendar({
|
||||
startDate: this.data.form.appointmentDate,
|
||||
endDate: this.data.form.appointmentDate
|
||||
})
|
||||
const artists = getCalendarArtists(calendar)
|
||||
const selectedArtist = this.data.form.artistId ? findArtist(artists, this.data.form.artistId) : {}
|
||||
this.setData({ artists, selectedArtist })
|
||||
let artists = getCalendarArtists(calendar)
|
||||
const requestedId = preferredArtistId || this.data.form.artistId
|
||||
if (fallbackEmployee && requestedId && !findArtist(artists, requestedId).id) {
|
||||
artists = artists.concat({
|
||||
id: requestedId,
|
||||
name: fallbackEmployee.name || fallbackEmployee.title,
|
||||
avatar: fallbackEmployee.avatar || ''
|
||||
})
|
||||
}
|
||||
const selectedArtist = requestedId ? findArtist(artists, requestedId) : {}
|
||||
this.setData({
|
||||
artists,
|
||||
artistSelectorItems: this.filterArtistItems(artists, this.data.artistKeyword),
|
||||
selectedArtist,
|
||||
artistError: '',
|
||||
'form.artistId': selectedArtist.id || ''
|
||||
})
|
||||
this.refreshProductCards()
|
||||
} catch (error) {
|
||||
this.setData({
|
||||
artists: [],
|
||||
artistSelectorItems: [],
|
||||
selectedArtist: {},
|
||||
'form.artistId': '',
|
||||
artistError: error.message || '员工列表加载失败',
|
||||
conflictTip: error.message || '美甲师列表加载失败',
|
||||
conflictLevel: 'danger'
|
||||
})
|
||||
@@ -345,44 +386,40 @@ Page({
|
||||
const patch = {
|
||||
[`form.${field}`]: event.detail.value
|
||||
}
|
||||
if (field === 'customerName' || field === 'phone') {
|
||||
patch['form.customerUserId'] = ''
|
||||
patch.customerDropdownVisible = true
|
||||
}
|
||||
this.setData(patch)
|
||||
if (field === 'customerName' || field === 'phone') {
|
||||
this.scheduleCustomerSearch(field)
|
||||
}
|
||||
if (field === 'durationMinutes') {
|
||||
this.refreshAvailableTimes()
|
||||
this.checkAvailability()
|
||||
}
|
||||
},
|
||||
|
||||
onCustomerFocus(event) {
|
||||
const field = event.currentTarget.dataset.field
|
||||
this.setData({ customerDropdownVisible: true })
|
||||
this.searchCustomers(field)
|
||||
openCustomerSheet() {
|
||||
this.setData({ customerSheetVisible: true })
|
||||
this.searchCustomers(this.data.customerKeyword)
|
||||
},
|
||||
|
||||
scheduleCustomerSearch(field) {
|
||||
closeCustomerSheet() {
|
||||
this.setData({ customerSheetVisible: false })
|
||||
},
|
||||
|
||||
onCustomerSearch(event) {
|
||||
const keyword = event.detail.value
|
||||
this.setData({ customerKeyword: keyword })
|
||||
clearTimeout(this._customerTimer)
|
||||
this._customerTimer = setTimeout(() => this.searchCustomers(field), 250)
|
||||
this._customerTimer = setTimeout(() => this.searchCustomers(keyword), 250)
|
||||
},
|
||||
|
||||
async searchCustomers(preferredField) {
|
||||
async searchCustomers(keywordValue) {
|
||||
if (!this.data.canCreateAppointment) {
|
||||
return
|
||||
}
|
||||
const form = this.data.form
|
||||
const sourceValue = preferredField === 'phone' ? form.phone : form.customerName || form.phone
|
||||
const keyword = String(sourceValue || '').trim()
|
||||
const keyword = String(keywordValue === undefined ? this.data.customerKeyword : keywordValue || '').trim()
|
||||
this.setData({ customerLoading: true })
|
||||
try {
|
||||
const data = await customersApi.list({ keyword, page: 1, size: 20 })
|
||||
const list = data.list || data || []
|
||||
this.setData({
|
||||
customerOptions: list.map(normalizeCustomer),
|
||||
customerOptions: list.map(normalizeCustomer).map(buildCustomerSelectorItem),
|
||||
customerError: ''
|
||||
})
|
||||
} catch (error) {
|
||||
@@ -395,8 +432,12 @@ Page({
|
||||
}
|
||||
},
|
||||
|
||||
retryCustomerSearch() {
|
||||
this.searchCustomers(this.data.customerKeyword)
|
||||
},
|
||||
|
||||
selectCustomer(event) {
|
||||
const customer = this.data.customerOptions.find(item => String(item.id) === String(event.currentTarget.dataset.id))
|
||||
const customer = event.detail.item
|
||||
if (!customer) {
|
||||
return
|
||||
}
|
||||
@@ -404,14 +445,48 @@ Page({
|
||||
'form.customerUserId': customer.id,
|
||||
'form.customerName': customer.name,
|
||||
'form.phone': customer.phone,
|
||||
customerDropdownVisible: false
|
||||
customerSheetVisible: false,
|
||||
customerKeyword: ''
|
||||
})
|
||||
},
|
||||
|
||||
hideCustomerDropdown() {
|
||||
setTimeout(() => {
|
||||
this.setData({ customerDropdownVisible: false })
|
||||
}, 180)
|
||||
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() {
|
||||
@@ -534,17 +609,65 @@ Page({
|
||||
})
|
||||
},
|
||||
|
||||
onArtistChange(event) {
|
||||
const selectedArtist = this.data.artists[Number(event.detail.value)] || {}
|
||||
filterArtistItems(artists, keywordValue) {
|
||||
const keyword = String(keywordValue || '').trim().toLowerCase()
|
||||
return (artists || [])
|
||||
.filter(item => !keyword || String(item.name || '').toLowerCase().includes(keyword))
|
||||
.map(buildArtistSelectorItem)
|
||||
},
|
||||
|
||||
openArtistSheet() {
|
||||
this.setData({
|
||||
artistSheetVisible: true,
|
||||
artistSelectorItems: this.filterArtistItems(this.data.artists, this.data.artistKeyword)
|
||||
})
|
||||
},
|
||||
|
||||
closeArtistSheet() {
|
||||
this.setData({ artistSheetVisible: false })
|
||||
},
|
||||
|
||||
retryArtists() {
|
||||
this.loadArtists()
|
||||
},
|
||||
|
||||
onArtistSearch(event) {
|
||||
const artistKeyword = event.detail.value
|
||||
this.setData({
|
||||
artistKeyword,
|
||||
artistSelectorItems: this.filterArtistItems(this.data.artists, artistKeyword)
|
||||
})
|
||||
},
|
||||
|
||||
selectArtist(event) {
|
||||
const selectedArtist = event.detail.item || {}
|
||||
this.setData({
|
||||
selectedArtist,
|
||||
'form.artistId': selectedArtist.id,
|
||||
'form.appointmentTime': ''
|
||||
'form.appointmentTime': '',
|
||||
artistSheetVisible: false
|
||||
})
|
||||
this.refreshAvailableTimes()
|
||||
this.checkAvailability()
|
||||
},
|
||||
|
||||
addEmployeeFromSelector() {
|
||||
this.setData({ artistSheetVisible: false })
|
||||
wx.navigateTo({
|
||||
url: '/pages/employee-create/index?picker=1',
|
||||
success: result => {
|
||||
result.eventChannel.on('employeeCreated', employee => this.handleEmployeeCreated(employee))
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
async handleEmployeeCreated(employee) {
|
||||
await this.loadArtists(employee.id, employee)
|
||||
this.setData({ 'form.appointmentTime': '' })
|
||||
await this.refreshAvailableTimes()
|
||||
this.checkAvailability()
|
||||
},
|
||||
|
||||
canOpenTimeSheet() {
|
||||
const form = this.data.form
|
||||
return Boolean(form.appointmentDate && form.artistId && normalizeDuration(form.durationMinutes))
|
||||
|
||||
Reference in New Issue
Block a user