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

95 lines
2.5 KiB
JavaScript

const dateUtil = require('../../utils/date')
const employeesApi = require('../../api/employees')
function shortDate(value) {
return String(value || '').slice(0, 10) || '-'
}
function timeRange(item) {
const start = String(item.startTime || '').slice(11, 16)
const end = String(item.endTime || '').slice(11, 16)
if (!start && !end) return '-'
return `${start || '-'}-${end || '-'}`
}
function orderAmount(item) {
const value = item.amount !== undefined && item.amount !== null ? item.amount : item.manualProductPrice
if (value === undefined || value === null || value === '') return '0'
const amount = Number(value)
if (Number.isNaN(amount)) return String(value)
return amount.toFixed(2).replace(/\.00$/, '')
}
function toOrderRow(item, index) {
return {
id: item.orderItemId || index,
detailId: item.orderItemId || item.id || '',
date: item.date || shortDate(item.startTime),
project: item.productTitle || '预约作品',
customer: item.customerName || '到店客户',
artist: item.artistName || '未分配',
time: timeRange(item),
status: item.statusText || '-',
amount: orderAmount(item)
}
}
Page({
data: {
id: '',
startDate: dateUtil.formatDate(dateUtil.startOfMonth(new Date())),
endDate: dateUtil.formatDate(new Date()),
orders: [],
loading: false,
loadError: ''
},
onLoad(options) {
this.setData({ id: options.id || '' })
this.loadOrders()
},
onStartDate(event) {
this.setData({ startDate: event.detail.value }, () => this.loadOrders())
},
onEndDate(event) {
this.setData({ endDate: event.detail.value }, () => this.loadOrders())
},
async loadOrders() {
if (!this.data.id) {
this.setData({ orders: [], loadError: '员工ID不能为空' })
return
}
this.setData({ loading: true, loadError: '' })
try {
const list = await employeesApi.orders(this.data.id, {
startDate: this.data.startDate,
endDate: this.data.endDate
})
this.setData({
orders: (Array.isArray(list) ? list : []).map(toOrderRow)
})
} catch (error) {
this.setData({
orders: [],
loadError: error.message || '员工订单加载失败'
})
} finally {
this.setData({ loading: false })
}
},
openOrder(event) {
const id = event.currentTarget.dataset.id
if (!id) {
wx.showToast({ title: '订单项ID不能为空', icon: 'none' })
return
}
wx.navigateTo({ url: `/pages/appointment-detail/index?id=${id}` })
},
back() { wx.navigateBack() }
})