// pages/order/index.js const API = require('../../utils/api.js') // 状态映射配置 const STATUS_MAP = { pending: { label: '待确认', text: '待确认', color: '#f59f00' }, confirmed: { label: '已确认', text: '已确认', color: '#4c6ef5' }, in_progress: { label: '进行中', text: '进行中', color: '#20c997' }, completed: { label: '已完成', text: '已完成', color: '#51cf66' }, cancelled: { label: '已取消', text: '已取消', color: '#ff6b6b' } }; // 状态筛选选项 const STATUS_FILTERS = [ { label: '全部', value: '', count: 0 }, { label: '待确认', value: 'pending', count: 0 }, { label: '已确认', value: 'confirmed', count: 0 }, { label: '进行中', value: 'in_progress', count: 0 }, { label: '已完成', value: 'completed', count: 0 }, { label: '已取消', value: 'cancelled', count: 0 } ]; Page({ /** * 页面的初始数据 */ data: { // 统计 stats: { total: 0, pending: 0, confirmed: 0, inProgress: 0, completed: 0, cancelled: 0 }, // 状态筛选 statusFilters: STATUS_FILTERS, currentStatus: '', // 订单列表 orderList: [], // 分页 page: 1, pageSize: 10, hasMore: true, // 加载状态 isLoading: false, isLoadingMore: false, isRefreshing: false }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { this.loadOrderList(); }, /** * 生命周期函数--监听页面显示 */ onShow() { if (this.data.orderList.length > 0) { this.refreshData(); } }, /** * 计算统计数据 */ calculateStats(orders) { const stats = { total: orders.length, pending: 0, confirmed: 0, inProgress: 0, completed: 0, cancelled: 0 }; orders.forEach(order => { switch (order.status) { case 'pending': stats.pending++; break; case 'confirmed': stats.confirmed++; break; case 'in_progress': stats.inProgress++; break; case 'completed': stats.completed++; break; case 'cancelled': stats.cancelled++; break; } }); return stats; }, /** * 更新筛选标签计数 */ updateFilterCounts(stats) { const statusFilters = this.data.statusFilters.map(filter => { let count = 0; switch (filter.value) { case '': count = stats.total; break; case 'pending': count = stats.pending; break; case 'confirmed': count = stats.confirmed; break; case 'in_progress': count = stats.inProgress; break; case 'completed': count = stats.completed; break; case 'cancelled': count = stats.cancelled; break; } return { ...filter, count }; }); this.setData({ statusFilters }); }, /** * 格式化日期 */ formatDate(dateStr) { if (!dateStr) return ''; const date = new Date(dateStr); const month = (date.getMonth() + 1).toString().padStart(2, '0'); const day = date.getDate().toString().padStart(2, '0'); return `${month}月${day}日`; }, /** * 处理订单数据 */ processOrders(orders) { return orders.map(order => ({ ...order, statusText: STATUS_MAP[order.status]?.text || order.status, schedule: { ...order.schedule, dateText: this.formatDate(order.schedule?.date) } })); }, /** * 加载订单列表 */ loadOrderList(isRefresh = false) { if (this.data.isLoading || this.data.isLoadingMore) return; const { currentStatus, page, pageSize } = this.data; this.setData({ isLoading: !isRefresh, isLoadingMore: isRefresh && page > 1 }); const params = { page, pageSize, status: 'pending,confirmed,in_progress,completed,cancelled' }; API.escort.getMyRecords(params) .then(res => { if (res.code !== 0) { wx.showToast({ title: res.message || '获取订单失败', icon: 'none' }); this.setData({ isLoading: false, isLoadingMore: false, isRefreshing: false }); return; } const data = res.data || {}; const list = data.records || []; let total = list.length || 0; const processedOrders = this.processOrders(list); const allOrders = isRefresh && page > 1 ? [...this.data.orderList, ...processedOrders] : processedOrders; const stats = this.calculateStats(allOrders); this.updateFilterCounts(stats); const { currentStatus } = this.data; const orderList = currentStatus ? allOrders.filter(order => order.status === currentStatus) : allOrders; total = orderList.length; this.setData({ orderList, stats, isLoading: false, isLoadingMore: false, isRefreshing: false, hasMore: orderList.length < total }); }) .catch(err => { console.error('获取订单列表失败', err); wx.showToast({ title: '网络错误,请重试', icon: 'none' }); this.setData({ isLoading: false, isLoadingMore: false, isRefreshing: false }); }); }, /** * 刷新数据 */ refreshData() { this.setData({ page: 1, hasMore: true }, () => { this.loadOrderList(); }); }, /** * 状态筛选切换 */ onStatusChange(e) { const status = e.currentTarget.dataset.status; if (status === this.data.currentStatus) return; this.setData({ currentStatus: status, page: 1, hasMore: true, orderList: [] }, () => { this.loadOrderList(); }); }, /** * 订单详情 */ onOrderDetail(e) { const id = e.currentTarget.dataset.id; wx.navigateTo({ url: `/pages/order/detail?id=${id}` }); }, /** * 订单操作 */ onOrderAction(e) { const { id, action } = e.currentTarget.dataset; const order = this.data.orderList.find(item => item._id === id); if (!order) return; const actionMap = { confirm: { title: '确认订单', content: '确认接受此订单?', nextStatus: 'confirmed' }, cancel: { title: '取消订单', content: '确定要取消此订单?', nextStatus: 'cancelled' }, start: { title: '开始服务', content: '确认开始陪诊服务?', nextStatus: 'in_progress' }, complete: { title: '完成服务', content: '确认陪诊服务已完成?', nextStatus: 'completed' }, detail: { title: '', content: '', nextStatus: '' } }; const actionConfig = actionMap[action]; if (!actionConfig) return; if (action === 'detail') { wx.navigateTo({ url: `/pages/order/detail?id=${id}` }); return; } wx.showModal({ title: actionConfig.title, content: actionConfig.content, confirmColor: '#4c6ef5', success: (res) => { if (res.confirm) { this.updateOrderStatus(id, actionConfig.nextStatus); } } }); }, /** * 更新订单状态 */ updateOrderStatus(id, newStatus) { wx.showLoading({ title: '处理中...' }); API.escort.updateStatus(id, { status: newStatus }) .then(res => { if (res.code !== 0) { wx.showToast({ title: res.message || '操作失败', icon: 'none' }); wx.hideLoading(); return; } const orderList = this.data.orderList.map(order => { if (order._id === id) { return { ...order, status: newStatus, statusText: STATUS_MAP[newStatus]?.text || newStatus, 'meta.updatetime': new Date().toISOString() }; } return order; }); const stats = this.calculateStats(orderList); this.updateFilterCounts(stats); this.setData({ orderList, stats }); wx.hideLoading(); wx.showToast({ title: '操作成功', icon: 'success' }); }) .catch(err => { console.error('更新订单状态失败', err); wx.showToast({ title: '网络错误,请重试', icon: 'none' }); wx.hideLoading(); }); }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { this.setData({ isRefreshing: true, page: 1, hasMore: true }, () => { this.loadOrderList(); }); }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { if (!this.data.hasMore || this.data.isLoadingMore) return; this.setData({ page: this.data.page + 1 }, () => { this.loadOrderList(true); }); }, /** * 用户点击右上角分享 */ onShareAppMessage() { return { title: '订单管理', path: '/pages/order/index' }; } });