Files
JayuCloud/JC-Salon-WeChat/pages/customers/index.js
T

147 lines
4.2 KiB
JavaScript
Raw Normal View History

2026-06-04 16:39:06 +08:00
const customersApi = require('../../api/customers')
Page({
data: {
keyword: '',
2026-06-26 11:07:54 +08:00
customers: [],
loadError: '',
createModalVisible: false,
duplicateModalVisible: false,
duplicateCustomers: [],
2026-06-26 11:07:54 +08:00
creating: false,
createForm: {
nickname: '',
wechatId: '',
2026-06-26 11:07:54 +08:00
phone: ''
}
2026-06-04 16:39:06 +08:00
},
onLoad() {
this.loadCustomers()
},
onSearchInput(event) {
this.setData({ keyword: event.detail.value })
clearTimeout(this._timer)
this._timer = setTimeout(() => this.loadCustomers(), 300)
},
async loadCustomers() {
try {
const data = await customersApi.list({ keyword: this.data.keyword, page: 1, size: 30 })
const list = data.list || data || []
this.setData({
2026-06-26 11:07:54 +08:00
loadError: '',
2026-06-04 16:39:06 +08:00
customers: list.map(item => ({
id: item.id,
name: item.nickname || item.name || item.account || '客户',
phone: item.contactPhone || item.phone || '未填写',
lastTime: item.lastOrderTime || item.lastConsumeTime || '-'
}))
})
} catch (error) {
2026-06-26 11:07:54 +08:00
this.setData({
customers: [],
loadError: error.message || '客户列表加载失败'
})
2026-06-04 16:39:06 +08:00
}
},
openDetail(event) {
wx.navigateTo({ url: `/pages/customer-detail/index?id=${event.currentTarget.dataset.id}` })
2026-06-26 11:07:54 +08:00
},
addCustomer() {
this.setData({ createModalVisible: true })
},
closeCreateModal() {
if (this.data.creating) {
return
}
this.setData({ createModalVisible: false })
},
closeDuplicateModal() {
this.setData({ duplicateModalVisible: false })
},
selectDuplicateCustomer(event) {
const customerId = event.currentTarget.dataset.id
if (!customerId) return
this.setData({ duplicateModalVisible: false })
this.openDetail({ currentTarget: { dataset: { id: customerId } } })
},
2026-06-26 11:07:54 +08:00
stopTap() {
},
onCreateInput(event) {
this.setData({
[`createForm.${event.currentTarget.dataset.field}`]: event.detail.value
})
},
resetCreateForm() {
this.setData({
createForm: {
nickname: '',
wechatId: '',
2026-06-26 11:07:54 +08:00
phone: ''
}
})
},
async submitCustomer() {
const form = this.data.createForm
const nickname = String(form.nickname || '').trim()
const wechatId = String(form.wechatId || '').trim()
2026-06-26 11:07:54 +08:00
const phone = String(form.phone || '').trim()
if (!nickname) {
wx.showToast({ title: '请填写客户姓名', icon: 'none' })
2026-06-26 11:07:54 +08:00
return
}
if (phone && !/^1[3-9]\d{9}$/.test(phone)) {
wx.showToast({ title: '请输入正确手机号', icon: 'none' })
return
}
2026-06-26 11:07:54 +08:00
this.setData({ creating: true })
try {
const matches = await customersApi.matchExactIdentity(nickname, wechatId || undefined, phone || undefined)
if (Array.isArray(matches) && matches.length) {
this.setData({
createModalVisible: false,
duplicateCustomers: matches.map(item => ({
id: item.id,
name: item.name || item.nickname || nickname,
phone: item.phone || '未填写手机号'
})),
duplicateModalVisible: true
})
return
}
await customersApi.create({ nickname, wechatId: wechatId || undefined, phone: phone || undefined })
2026-06-26 11:07:54 +08:00
wx.showToast({ title: '客户已新增', icon: 'success' })
this.resetCreateForm()
this.setData({ createModalVisible: false })
await this.loadCustomers()
} catch (error) {
if (/客户已存在|重复|相同/.test(String(error.message || ''))) {
try {
const matches = await customersApi.matchExactIdentity(nickname, wechatId || undefined, phone || undefined)
if (Array.isArray(matches) && matches.length) {
this.setData({ createModalVisible: false, duplicateCustomers: matches.map(item => ({ id: item.id, name: item.name || item.nickname || nickname, phone: item.phone || '未填写手机号' })), duplicateModalVisible: true })
return
}
} catch (lookupError) {
wx.showToast({ title: lookupError.message || '客户重复,请重新查询已有客户', icon: 'none' })
return
}
}
2026-06-26 11:07:54 +08:00
wx.showToast({ title: error.message || '新增失败', icon: 'none' })
} finally {
this.setData({ creating: false })
}
2026-06-04 16:39:06 +08:00
}
})