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

102 lines
2.4 KiB
JavaScript

const customersApi = require('../../api/customers')
Page({
data: {
keyword: '',
customers: [],
loadError: '',
createModalVisible: false,
creating: false,
createForm: {
nickname: '',
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 })
},
stopTap() {
},
onCreateInput(event) {
this.setData({
[`createForm.${event.currentTarget.dataset.field}`]: event.detail.value
})
},
resetCreateForm() {
this.setData({
createForm: {
nickname: '',
phone: ''
}
})
},
async submitCustomer() {
const form = this.data.createForm
const nickname = String(form.nickname || '').trim()
const phone = String(form.phone || '').trim()
if (!nickname || !phone) {
wx.showToast({ title: '请填写客户姓名和手机号', icon: 'none' })
return
}
this.setData({ creating: true })
try {
await customersApi.create({ nickname, phone })
wx.showToast({ title: '客户已新增', icon: 'success' })
this.resetCreateForm()
this.setData({ createModalVisible: false })
await this.loadCustomers()
} catch (error) {
wx.showToast({ title: error.message || '新增失败', icon: 'none' })
} finally {
this.setData({ creating: false })
}
}
})