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

171 lines
4.2 KiB
JavaScript

const customersApi = require('../../api/customers')
const DEFAULT_CUSTOMER_AVATARS = [
'/assets/images/default-avatar-1.png',
'/assets/images/default-avatar-2.png',
'/assets/images/default-avatar-3.png',
'/assets/images/default-avatar-4.png'
]
function formatCustomerAmount(value) {
const amount = Number(value || 0)
return Number.isFinite(amount) ? amount.toFixed(2) : '0.00'
}
function formatCustomerDate(value) {
if (!value) {
return '-'
}
return String(value).replace('T', ' ').slice(0, 10)
}
function resolveCustomerAmount(item) {
return item.lastOrderAmount
|| item.lastConsumeAmount
|| item.totalAmount
|| item.amount
|| 0
}
function resolveCustomerMonthAmount(item) {
return item.monthAmount
|| item.monthConsumeAmount
|| item.monthConsumption
|| item.totalAmount
|| 0
}
function resolveCustomerTag(item) {
return item.memberLevelName
|| item.levelName
|| item.tagName
|| (item.member ? '会员' : '普通客户')
}
function isToday(value) {
if (!value) {
return false
}
return formatCustomerDate(value) === new Date().toISOString().slice(0, 10)
}
function mapCustomer(item, index) {
const name = item.nickname || item.name || item.account || '客户'
return {
id: item.id,
name,
avatar: item.avatar || item.avatarUrl || item.customerAvatar || DEFAULT_CUSTOMER_AVATARS[index % DEFAULT_CUSTOMER_AVATARS.length],
tagText: resolveCustomerTag(item),
amountText: formatCustomerAmount(resolveCustomerAmount(item)),
lastDateText: formatCustomerDate(item.lastOrderTime || item.lastConsumeTime),
arrivedToday: isToday(item.lastOrderTime || item.lastConsumeTime)
}
}
Page({
data: {
keyword: '',
customers: [],
customerStats: {
todayArrival: 0,
monthAmount: '0.00',
memberCount: 0
},
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 || []
const customers = list.map(mapCustomer)
const monthAmount = list.reduce((total, item) => total + Number(resolveCustomerMonthAmount(item) || 0), 0)
this.setData({
loadError: '',
customers,
customerStats: {
todayArrival: customers.filter(item => item.arrivedToday).length,
monthAmount: formatCustomerAmount(monthAmount),
memberCount: list.filter(item => item.member || item.memberLevelName || item.levelName).length
}
})
} 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) {
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 })
}
}
})