const customersApi = require('../../api/customers') Page({ data: { keyword: '', customers: [], loadError: '', createModalVisible: false, duplicateModalVisible: false, duplicateCustomers: [], creating: false, createForm: { nickname: '', wechatId: '', phone: '' } }, 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({ loadError: '', 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) { this.setData({ customers: [], loadError: error.message || '客户列表加载失败' }) } }, openDetail(event) { wx.navigateTo({ url: `/pages/customer-detail/index?id=${event.currentTarget.dataset.id}` }) }, 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 } } }) }, stopTap() { }, onCreateInput(event) { this.setData({ [`createForm.${event.currentTarget.dataset.field}`]: event.detail.value }) }, resetCreateForm() { this.setData({ createForm: { nickname: '', wechatId: '', phone: '' } }) }, async submitCustomer() { const form = this.data.createForm const nickname = String(form.nickname || '').trim() const wechatId = String(form.wechatId || '').trim() const phone = String(form.phone || '').trim() if (!nickname) { wx.showToast({ title: '请填写客户姓名', icon: 'none' }) return } if (phone && !/^1[3-9]\d{9}$/.test(phone)) { wx.showToast({ title: '请输入正确手机号', icon: 'none' }) return } 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 }) 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 } } wx.showToast({ title: error.message || '新增失败', icon: 'none' }) } finally { this.setData({ creating: false }) } } })