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

141 lines
3.6 KiB
JavaScript

const customersApi = require('../../api/customers')
function canCreateAppointment() {
const app = getApp()
const userInfo = app.globalData.userInfo || {}
return Number(userInfo.type) !== 3
}
Page({
data: {
id: '',
canCreateAppointment: true,
avatarText: '客',
customer: {},
stats: { orderCount: 0, amount: 0, lastTime: '-' },
orders: [],
loadError: '',
editModalVisible: false,
saving: false,
editForm: {
contactPhone: '',
remark: ''
}
},
onLoad(options) {
this.setData({
id: options.id || '',
canCreateAppointment: canCreateAppointment()
})
this.loadCustomer()
},
async loadCustomer() {
if (!this.data.id) {
this.setData({
customer: {},
stats: { orderCount: 0, amount: 0, lastTime: '-' },
orders: [],
loadError: '缺少客户 ID'
})
return
}
try {
const detail = await customersApi.detail(this.data.id)
if (!detail) {
throw new Error('客户详情不存在')
}
const customer = {
id: detail.id,
name: detail.nickname || detail.name || detail.account || '客户',
phone: detail.contactPhone || detail.phone || '未填写',
contactPhone: detail.contactPhone || '',
remark: detail.signature || ''
}
this.setData({
customer,
editForm: {
contactPhone: customer.contactPhone || detail.phone || '',
remark: customer.remark
},
avatarText: String(customer.name || '客').slice(-1),
stats: {
orderCount: detail.orderCount || detail.totalOrderCount || 0,
amount: detail.totalAmount || detail.consumeAmount || 0,
lastTime: detail.lastOrderTime || detail.lastConsumeTime || '-'
},
orders: Array.isArray(detail.orders) ? detail.orders : [],
loadError: ''
})
} catch (error) {
this.setData({
customer: {},
stats: { orderCount: 0, amount: 0, lastTime: '-' },
orders: [],
loadError: error.message || '客户详情加载失败'
})
}
},
openEditModal() {
const customer = this.data.customer || {}
this.setData({
editModalVisible: true,
editForm: {
contactPhone: customer.contactPhone || customer.phone || '',
remark: customer.remark || ''
}
})
},
closeEditModal() {
if (this.data.saving) {
return
}
this.setData({ editModalVisible: false })
},
stopTap() {
},
onEditInput(event) {
this.setData({
[`editForm.${event.currentTarget.dataset.field}`]: event.detail.value
})
},
async saveCustomer() {
const form = this.data.editForm || {}
this.setData({ saving: true })
try {
await customersApi.update(this.data.id, {
contactPhone: String(form.contactPhone || '').trim(),
remark: String(form.remark || '').trim()
})
wx.showToast({ title: '客户信息已保存', icon: 'success' })
this.setData({ editModalVisible: false })
await this.loadCustomer()
} catch (error) {
wx.showToast({ title: error.message || '保存失败', icon: 'none' })
} finally {
this.setData({ saving: false })
}
},
createAppointment() {
if (!this.data.canCreateAppointment) {
wx.showToast({ title: '美甲师账号只能查看预约', icon: 'none' })
return
}
const customer = this.data.customer || {}
wx.navigateTo({
url: `/pages/appointment-form/index?customerName=${encodeURIComponent(customer.name || '')}&phone=${encodeURIComponent(customer.phone || '')}`
})
},
back() {
wx.navigateBack()
}
})